Advertisement
Amorf

Untitled

Dec 14th, 2021
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include "DrawEngine.h"
  2.  
  3. DrawEngine::DrawEngine(HDC hdc, HWND hwnd, int pxPerBlock,
  4.                        int width, int height) :
  5. hdc(hdc), hwnd(hwnd), width(width), height(height)
  6. {
  7.     GetClientRect(hwnd, &rect);
  8.  
  9.     SaveDC(hdc);
  10.  
  11.     // Set up coordinate system
  12.     SetMapMode(hdc, MM_ISOTROPIC);
  13.     SetViewportExtEx(hdc, pxPerBlock, pxPerBlock, 0);
  14.     SetWindowExtEx(hdc, 1, -1, 0);
  15.     SetViewportOrgEx(hdc, 0, rect.bottom, 0);
  16.  
  17.     // Set default colors
  18.     SetTextColor(hdc, RGB(255,255,255));
  19.     SetBkColor(hdc, RGB(70,70,70));
  20.     SetBkMode(hdc, TRANSPARENT);
  21. }
  22.  
  23. DrawEngine::~DrawEngine()
  24. {
  25.     // Restore coordinate system
  26.     RestoreDC(hdc, -1);
  27. }
  28.  
  29. void DrawEngine::drawBlock(int x, int y, COLORREF color)
  30. {
  31.     HBRUSH hBrush = CreateSolidBrush(color);
  32.     rect.left = x;
  33.     rect.right = x + 1;
  34.     rect.top = y;
  35.     rect.bottom = y + 1;
  36.  
  37.     FillRect(hdc, &rect, hBrush);
  38.  
  39.     // Draw left and bottom black border
  40.     MoveToEx(hdc, x, y + 1, NULL);
  41.     LineTo(hdc, x, y);
  42.     LineTo(hdc, x + 1, y);
  43.     DeleteObject(hBrush);
  44. }
  45.  
  46. void DrawEngine::drawInterface()
  47. {
  48.     // Draw a gray area at the right
  49.     HBRUSH hBrush = CreateSolidBrush(RGB(70,70,70));
  50.     rect.top = height;
  51.     rect.left = width;
  52.     rect.bottom = 0;
  53.     rect.right = width + 8;
  54.     FillRect(hdc, &rect, hBrush);
  55.     DeleteObject(hBrush);
  56. }
  57.  
  58. void DrawEngine::drawText(TCHAR *szText, int x, int y) const
  59. {
  60.     TextOut(hdc, x, y, szText, lstrlen(szText));
  61. }
  62.  
  63. void DrawEngine::drawScore(int score, int x, int y) const
  64. {
  65.     TCHAR szBuffer[20];
  66.     int len = wsprintf(szBuffer, TEXT("Score: %6d"), score);
  67.     SetBkMode(hdc, OPAQUE);
  68.     TextOut(hdc, x, y, szBuffer, len);
  69.     SetBkMode(hdc, TRANSPARENT);
  70. }
  71.  
  72. void DrawEngine::drawSpeed(int speed, int x, int y) const
  73. {
  74.     TCHAR szBuffer[20];
  75.     int len = wsprintf(szBuffer, TEXT("Speed: %6d"), speed);
  76.     SetBkMode(hdc, OPAQUE);
  77.     TextOut(hdc, x, y, szBuffer, len);
  78.     SetBkMode(hdc, TRANSPARENT);
  79. }
  80.  
  81. void DrawEngine::drawNextPiece(Piece &piece, int x, int y)
  82. {
  83.     TCHAR szBuffer[] = TEXT("Next:");
  84.     TextOut(hdc, x, y + 5, szBuffer, lstrlen(szBuffer));
  85.     COLORREF color = piece.getColor();
  86.  
  87.     // Draw the piece in a 4x4 square area
  88.     for (int i = 0; i < 4; i++)
  89.     {
  90.         for (int j = 0; j < 4; j++)
  91.         {
  92.             if (piece.isPointExists(i, j))
  93.                 drawBlock(i + x, j + y, color);
  94.             else
  95.                 drawBlock(i + x, j + y, RGB(0,0,0));
  96.         }
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement