Advertisement
YauhenMardan

WinAPI_5_3_Car

Apr 4th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 KB | None | 0 0
  1. #pragma warning(disable:4996)
  2. #undef UNICODE
  3. #define _USE_MATH_DEFINES
  4. #include <windows.h>
  5. #include <cmath>
  6. #include "KWnd.h"
  7.  
  8. #define CAR_IS_OUT (iCurrentOffsetX-sx/5)>sx
  9.  
  10. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  11. void CreteCarRgn(HDC hDC, HRGN& hRgn, int x, int y, int width, int height, double angle);
  12. double RotateX(int x, int y, double angle);
  13. double RotateY(int x, int y, double angle);
  14.  
  15. const int INTERVAL = 50;
  16.  
  17. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  18. {
  19.     MSG msg;
  20.     KWnd mainWnd("MyWin", hInstance, nCmdShow, WndProc);
  21.     while (GetMessage(&msg, NULL, 0, 0))
  22.     {
  23.         TranslateMessage(&msg);
  24.         DispatchMessage(&msg);
  25.     }
  26.     return (msg.wParam);
  27. }
  28.  
  29. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  30. {
  31.     PAINTSTRUCT ps;
  32.     HDC hDC;
  33.     int userReply;
  34.     //timer pause
  35.     static bool paused = false;
  36.     //sx,sy
  37.     static int sx, sy;
  38.     //timer t
  39.     static int t = 0;
  40.     //pen
  41.     static HPEN hPenCommon;
  42.     //brushed
  43.     static HBRUSH hBrushBlack, hBrushWhite, hBrushRed, hBrushYellow, hBrushGreen;
  44.     //region
  45.     static HRGN hCar;
  46.     //car
  47.     static POINT* pCarCoord = new POINT[4];
  48.     static int iCurrentOffsetX;
  49.     static int iCurrentOffsetY;
  50.     static int iCurrentSpeedX;
  51.     static int iCurrentSpeedY;
  52.     static double dCarAngle;
  53.  
  54.     switch (message)
  55.     {
  56.     case WM_SIZE:
  57.         sx = LOWORD(lParam);
  58.         sy = HIWORD(lParam);
  59.         break;
  60.     case WM_CREATE:
  61.         //pen
  62.         hPenCommon = (HPEN)GetStockObject(1);
  63.         //brushes
  64.         hBrushBlack = CreateSolidBrush(RGB(0, 0, 0));
  65.         hBrushWhite = CreateSolidBrush(RGB(255, 255, 255));
  66.         hBrushRed = CreateSolidBrush(RGB(255, 0, 0));
  67.         hBrushYellow = CreateSolidBrush(RGB(255, 165, 0));
  68.         hBrushGreen = CreateSolidBrush(RGB(0, 255, 0));
  69.         //car
  70.         iCurrentOffsetX = 0;
  71.         iCurrentSpeedX = 1;
  72.         iCurrentOffsetY = 0;
  73.         iCurrentSpeedY = 0;
  74.         dCarAngle = 0;
  75.         //set timer
  76.         SetTimer(hWnd, 1, INTERVAL, NULL);
  77.         break;
  78.     case WM_PAINT:
  79.         //begin paint
  80.         hDC = BeginPaint(hWnd, &ps);
  81.         //
  82.         SelectObject(hDC,hPenCommon);
  83.         //set modes
  84.         SetMapMode(hDC, MM_ANISOTROPIC);
  85.         SetWindowExtEx(hDC, sx, sy, NULL);
  86.         SetViewportExtEx(hDC, sx, -sy, NULL);
  87.         SetViewportOrgEx(hDC, 0, sy, NULL);
  88.         //draw
  89.         CreteCarRgn(hDC, hCar, 0, 0, sx / 5, sy / 6, dCarAngle);
  90.         if (CAR_IS_OUT)
  91.         {
  92.             iCurrentOffsetX = -sx / 5;
  93.             iCurrentOffsetY = 0;
  94.         }
  95.         OffsetRgn(hCar, iCurrentOffsetX, iCurrentOffsetY - sy);
  96.         FillRgn(hDC, hCar, hBrushGreen);
  97.         //detete region
  98.         DeleteObject(hCar);
  99.         //end paint
  100.         EndPaint(hWnd, &ps);
  101.         break;
  102.     case WM_TIMER:
  103.         if (t >= 10 * INTERVAL)
  104.             t = 0;
  105.         //Offset
  106.         iCurrentOffsetX += iCurrentSpeedX;
  107.         iCurrentOffsetY += iCurrentSpeedY;
  108.         t++;
  109.         InvalidateRect(hWnd, NULL, TRUE);
  110.         break;
  111.     case WM_KEYDOWN:
  112.     {
  113.         switch (wParam)
  114.         {
  115.         case VK_SPACE:
  116.             if (!paused)
  117.             {
  118.                 KillTimer(hWnd, 1);
  119.                 paused = true;
  120.             }
  121.             else if (paused)
  122.             {
  123.                 SetTimer(hWnd, 1, INTERVAL, NULL);
  124.                 paused = false;
  125.             }
  126.             break;
  127.         case VK_RIGHT:
  128.             iCurrentSpeedX++;
  129.             break;
  130.         case VK_LEFT:
  131.             iCurrentSpeedX--;
  132.             break;
  133.         case VK_UP:
  134.             iCurrentSpeedY = iCurrentSpeedX;
  135.             dCarAngle = M_PI / 10;
  136.             break;
  137.         case VK_DOWN:
  138.             iCurrentSpeedY = 0;
  139.             dCarAngle = 0;
  140.             break;
  141.         }
  142.         break;
  143.     }
  144.     case WM_CLOSE:
  145.         userReply = MessageBox(hWnd, "Are you sure?", "", MB_YESNO | MB_ICONQUESTION);
  146.         if (IDYES == userReply)
  147.         {
  148.             DestroyWindow(hWnd);
  149.         }
  150.         break;
  151.     case WM_DESTROY:
  152.         //pen
  153.         DeleteObject(hPenCommon);
  154.         //brushes
  155.         DeleteObject(hBrushBlack);
  156.         DeleteObject(hBrushWhite);
  157.         DeleteObject(hBrushRed);
  158.         DeleteObject(hBrushYellow);
  159.         DeleteObject(hBrushGreen);
  160.         //timer
  161.         KillTimer(hWnd, 1);
  162.         PostQuitMessage(0);
  163.         break;
  164.     default:
  165.         return DefWindowProc(hWnd, message, wParam, lParam);
  166.     }
  167.     return 0;
  168. }
  169.  
  170. #define LEFT_DOWN x,y -0.4*height,angle
  171. #define LEFT_UP x,y-0.8*height,angle
  172. #define RIGHT_UP x+width,y-0.8*height,angle
  173. #define RIGHT_DOWN x+width,y-0.4*height,angle
  174.  
  175. void CreteCarRgn(HDC hDC, HRGN& hRgn, int x, int y, int width, int height, double angle)
  176. {
  177.     //Ellipse(hDC, x, y, x + width, y + height);
  178.     //begin
  179.     BeginPath(hDC);
  180.     //make region
  181.         //draw body
  182.     MoveToEx(hDC, (int)RotateX(LEFT_DOWN), (int)RotateY(LEFT_DOWN), NULL);
  183.     LineTo(hDC, (int)RotateX(LEFT_UP), (int)RotateY(LEFT_UP));
  184.     LineTo(hDC, (int)RotateX(RIGHT_UP), (int)RotateY(RIGHT_UP));
  185.     LineTo(hDC, (int)RotateX(RIGHT_DOWN), (int)RotateY(RIGHT_DOWN));
  186.     LineTo(hDC, (int)RotateX(LEFT_DOWN), (int)RotateY(LEFT_DOWN));
  187.         //draw wheels
  188.     AngleArc(hDC, (int)RotateX(x, -0.2*height, angle), (int)RotateY(x, y - 0.2*height, angle), 0.2*height, 0, 360);
  189.     MoveToEx(hDC, (int)RotateX(x + width, -0.2*height, angle), (int)RotateY(x + width, y - 0.2*height, angle), NULL);
  190.     AngleArc(hDC, (int)RotateX(x + width, -0.2*height, angle), (int)RotateY(x + width, y - 0.2*height, angle), 0.2*height, 0, 360);
  191.     CloseFigure(hDC);
  192.     //end
  193.     EndPath(hDC);
  194.     hRgn = PathToRegion(hDC);
  195. }
  196. double RotateX(int x, int y, double angle)
  197. {
  198.     return (x * cos(angle) + y * sin(angle)); //(x * cos(angle) - y * sin(angle));//
  199. }
  200. double RotateY(int x, int y, double angle)
  201. {
  202.     return (-x * sin(angle) + y * cos(angle)); //(x*sin(angle) + y * cos(angle));//
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement