detective1711

Graphics.h

Mar 30th, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.13 KB | None | 0 0
  1. #ifndef GRAPHICS_H
  2. #define GRAPHICS_H
  3.  
  4. #include <iostream>
  5. #include <math.h>
  6. #include <windows.h>
  7. #include <Windows.h>
  8. using namespace std;
  9.  
  10. HPEN  getPen(int iPEN_STYLE, int iPEN_SIZE, int iCOLORREF);
  11. BOOL  SetPoint(HDC hDC, HPEN hPen, COORD PNT);
  12. BOOL  PlotLine(HDC hDC, HPEN hPen, COORD BGN, COORD END);
  13. BOOL  PlotRect(HDC hDC, HPEN hPen, COORD BGN, COORD END);
  14. BOOL  PlotCirc(HDC hDC, HPEN hPen, COORD BGN, COORD END);
  15. COORD setCordScale(COORD POS, RECT pRECT);
  16.  
  17. HDC hDC;
  18. HPEN PEN;
  19. HANDLE StdOut;
  20. WORD COLOR;
  21. COORD BGN, POS;
  22.  
  23. short mov;
  24. short movx;
  25.  
  26. void initgraph(void)
  27. {
  28.     system("cls");
  29.     mov = 30;
  30.     movx = 0;
  31.     HWND hWnd = GetForegroundWindow();
  32.     RECT    pRECT = { 0 };
  33.     COORD   POS1 = { 0 };
  34.     COORD   BGN1 = setCordScale(POS1, pRECT);
  35.     GetWindowRect(hWnd, &pRECT);
  36.     hDC = GetWindowDC(hWnd);
  37.     PEN = getPen(PS_SOLID, 2, RGB(255, 255, 255));
  38.     StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  39. }
  40.  
  41. void changey(int value) { if (value>-1000 & value <1000) mov = (short)value; }
  42. void changex(int value) { if (value>-1000 & value <1000) movx = (short)value; }
  43. int getmaxx(void) { return 640; }
  44. int getmaxy(void) { return 300; }
  45.  
  46. void setcolorRGB(int r, int g, int b)
  47. {
  48.     PEN = getPen(PS_SOLID, 2, RGB(r, g, b));
  49. }
  50. void setcolor(int color)
  51. {
  52.     if (color < 0) color = -color;
  53.     if (color > 15) color %= 16;
  54.     switch (color)
  55.     {
  56.     case 0: PEN = getPen(PS_SOLID, 2, RGB(0, 0, 0)); break;
  57.     case 1: PEN = getPen(PS_SOLID, 2, RGB(0, 0, 128)); break;
  58.     case 2: PEN = getPen(PS_SOLID, 2, RGB(0, 128, 0)); break;
  59.     case 3: PEN = getPen(PS_SOLID, 2, RGB(128, 0, 0)); break;
  60.     case 4: PEN = getPen(PS_SOLID, 2, RGB(0, 128, 128)); break;
  61.     case 5: PEN = getPen(PS_SOLID, 2, RGB(128, 0, 128)); break;
  62.     case 6: PEN = getPen(PS_SOLID, 2, RGB(128, 128, 0)); break;
  63.     case 7: PEN = getPen(PS_SOLID, 2, RGB(0, 0, 255)); break;
  64.     case 8: PEN = getPen(PS_SOLID, 2, RGB(0, 255, 0)); break;
  65.     case 9: PEN = getPen(PS_SOLID, 2, RGB(255, 0, 0)); break;
  66.     case 10: PEN = getPen(PS_SOLID, 2, RGB(0, 255, 255)); break;
  67.     case 11: PEN = getPen(PS_SOLID, 2, RGB(255, 0, 255)); break;
  68.     case 12: PEN = getPen(PS_SOLID, 2, RGB(255, 255, 0)); break;
  69.     case 13: PEN = getPen(PS_SOLID, 2, RGB(192, 192, 192)); break;
  70.     case 14: PEN = getPen(PS_SOLID, 2, RGB(192, 192, 192)); break;
  71.     case 15: PEN = getPen(PS_SOLID, 2, RGB(255, 255, 255)); break;
  72.     }
  73. }
  74.  
  75. void setfontcolor(int color)
  76. {
  77.     if (color < 0) color = -color;
  78.     if (color > 15) color %= 16;
  79.     switch (color)
  80.     {
  81.     case 0: COLOR = (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); COLOR = COMMON_LVB_REVERSE_VIDEO; break;
  82.     case 1: COLOR = (FOREGROUND_BLUE); break;
  83.     case 2: COLOR = (FOREGROUND_GREEN); break;
  84.     case 3: COLOR = (FOREGROUND_RED); break;
  85.     case 4: COLOR = (FOREGROUND_BLUE | FOREGROUND_GREEN); break;
  86.     case 5: COLOR = (FOREGROUND_BLUE | FOREGROUND_RED); break;
  87.     case 6: COLOR = (FOREGROUND_GREEN | FOREGROUND_RED); break;
  88.     case 7: COLOR = (FOREGROUND_BLUE | FOREGROUND_INTENSITY); break;
  89.     case 8: COLOR = (FOREGROUND_GREEN | FOREGROUND_INTENSITY); break;
  90.     case 9: COLOR = (FOREGROUND_RED | FOREGROUND_INTENSITY); break;
  91.     case 10: COLOR = (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY); break;
  92.     case 11: COLOR = (FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY); break;
  93.     case 12: COLOR = (FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); break;
  94.     case 13: COLOR = (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); break;
  95.     case 14: COLOR = (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | COMMON_LVB_REVERSE_VIDEO); break;
  96.     case 15: COLOR = (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | COMMON_LVB_REVERSE_VIDEO | FOREGROUND_INTENSITY); break;
  97.     }
  98.     SetConsoleTextAttribute(StdOut, COLOR);
  99. }
  100.  
  101.  
  102.  
  103. void line(int x1, int y1, int x2, int y2)
  104. {
  105.     BGN.X = (short)x1 + movx;
  106.     BGN.Y = (short)y1 + mov;
  107.     POS.X = (short)x2 + movx;
  108.     POS.Y = (short)y2 + mov;
  109.     PlotLine(hDC, PEN, BGN, POS);
  110. }
  111.  
  112.  
  113. void putpixel(int x, int y)
  114. {
  115.     line(x, y, x, y);
  116. }
  117.  
  118. void circle(int x, int y, int radius)
  119. {
  120.     BGN.X = (short)x + movx - radius / 2;
  121.     BGN.Y = (short)y + mov - radius / 2;
  122.     POS.X = (short)x + movx + radius / 2;
  123.     POS.Y = (short)y + mov + radius / 2;
  124.     PlotCirc(hDC, PEN, BGN, POS);
  125. }
  126.  
  127. void rectangle(int x1, int y1, int x2, int y2)
  128. {
  129.     BGN.X = (short)x1 + movx;
  130.     BGN.Y = (short)y1 + mov;
  131.     POS.X = (short)x2 + movx;
  132.     POS.Y = (short)y2 + mov;
  133.     PlotRect(hDC, PEN, BGN, POS);
  134. }
  135.  
  136. void gotoxy(int x, int y)
  137. {
  138.     BGN.X = (short)x;
  139.     BGN.Y = (short)y;
  140.     SetConsoleCursorPosition(StdOut, BGN);
  141. }
  142.  
  143. void outtextxy(int x, int y, char * text)
  144. {
  145.     gotoxy(x / 8, y / 12);
  146.     std::cout << text;
  147. }
  148.  
  149. void clrscr()
  150. {
  151.     setcolor(0);
  152.     for (int i = 5; i <= 645; i++)
  153.         line(i - movx, 32 - mov, i - movx, 331 - mov);
  154. }
  155. void SetWindow(int Width, int Height)
  156. {
  157.     _COORD coord;
  158.     coord.X = Width;
  159.     coord.Y = Height;
  160.  
  161.     _SMALL_RECT Rect;
  162.     Rect.Top = 0;
  163.     Rect.Left = 0;
  164.     Rect.Bottom = Height - 1;
  165.     Rect.Right = Width - 1;
  166.  
  167.     HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);      // Get Handle
  168.     SetConsoleScreenBufferSize(Handle, coord);            // Set Buffer Size
  169.     SetConsoleWindowInfo(Handle, TRUE, &Rect);            // Set Window Size
  170.  
  171.     WINDOWPLACEMENT wd;
  172.     HWND hWnd = GetForegroundWindow();
  173.     if (GetWindowPlacement(hWnd, &wd))
  174.     {
  175.         ShowWindow(hWnd, SW_MAXIMIZE);
  176.     }
  177. }
  178.  
  179.  
  180. HPEN getPen(int iPEN_STYLE, int iPEN_SIZE, int iCOLORREF)
  181. {
  182.     return CreatePen(iPEN_STYLE, iPEN_SIZE, iCOLORREF);
  183. }
  184.  
  185. BOOL SetPoint(HDC hDC, HPEN hPen, COORD PNT)
  186. {
  187.     EXTLOGPEN pINFO;
  188.     GetObject(hPen, sizeof(EXTLOGPEN), &pINFO);
  189.     SelectObject(hDC, hPen);
  190.     return Ellipse
  191.         (
  192.         hDC,
  193.         PNT.X - pINFO.elpWidth,
  194.         PNT.Y + pINFO.elpWidth,
  195.         PNT.X + pINFO.elpWidth,
  196.         PNT.Y - pINFO.elpWidth
  197.         );
  198. }
  199.  
  200. BOOL PlotLine(HDC hDC, HPEN hPen, COORD BGN, COORD END)
  201. {
  202.     SelectObject(hDC, hPen);
  203.     MoveToEx(hDC, BGN.X, BGN.Y, NULL);
  204.     return LineTo(hDC, END.X, END.Y);
  205. }
  206.  
  207. BOOL PlotRect(HDC hDC, HPEN hPen, COORD BGN, COORD END)
  208. {
  209.     SelectObject(hDC, hPen);
  210.     return Rectangle(hDC, BGN.X, BGN.Y, END.X, END.Y);
  211. }
  212.  
  213. BOOL  PlotCirc(HDC hDC, HPEN hPen, COORD BGN, COORD END)
  214. {
  215.     SelectObject(hDC, hPen);
  216.     return Ellipse(hDC, BGN.X, BGN.Y, END.X, END.Y);
  217. }
  218.  
  219. COORD setCordScale(COORD POS, RECT pRECT)
  220. {
  221.     if (POS.X == 0)
  222.         POS.X = 1;
  223.     if (POS.Y == 0)
  224.         POS.Y = 1;
  225.  
  226.     int nROWS = 25;
  227.     int nCOLS = 80;
  228.  
  229.     double CX = (pRECT.right - pRECT.left) / nCOLS;
  230.     double CY = (pRECT.bottom - pRECT.top) / nROWS;
  231.  
  232.     //Ïðè òåêñòîâîì ðåæèìå(text mode) ýêðàí  äåëèòñÿ  íà  ÿ÷åéêè
  233.     //(80  èëè 40 êîëîíîê â øèðèíó è 25 ñòðîê â âûñîòó)
  234.     POS.X *= CX;
  235.     POS.Y *= CY;
  236.  
  237.     int xBORDER = GetSystemMetrics(SM_CXBORDER);//Øèðèíà ãðàíèöû îêíà
  238.     int yBORDER = GetSystemMetrics(SM_CYMENU);  //Âûñîòà çàãîëîâêà îêíà ~= âûñîòå ñòðîê ìåíþ
  239.     int xDRAG = GetSystemMetrics(SM_CXDRAG);  //×èñëî ïèêñåëåé íà ãîðèç äðîæåíèå ìûøè
  240.     int yDRAG = GetSystemMetrics(SM_CYDRAG);  //×èñëî ïèêñåëåé íà âåðòèê äðîæåíèå ìûøè
  241.  
  242.     POS.X += xBORDER + xDRAG;//Øèðèíà ãðàíèöû îêíà + ÷èñëî ïèêñåëåé íà äðîæåíèå ìûøè
  243.     POS.Y += yBORDER + yDRAG;
  244.     return POS;
  245. }
  246.  
  247. #endif
Advertisement
Add Comment
Please, Sign In to add comment