Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma warning(disable:4996)
- #undef UNICODE
- #define _USE_MATH_DEFINES
- #include <windows.h>
- #include <cmath>
- #include "KWnd.h"
- #define CAR_IS_OUT (iCurrentOffsetX-sx/5)>sx
- LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
- void CreteCarRgn(HDC hDC, HRGN& hRgn, int x, int y, int width, int height, double angle);
- double RotateX(int x, int y, double angle);
- double RotateY(int x, int y, double angle);
- const int INTERVAL = 50;
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- MSG msg;
- KWnd mainWnd("MyWin", hInstance, nCmdShow, WndProc);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (msg.wParam);
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- PAINTSTRUCT ps;
- HDC hDC;
- int userReply;
- //timer pause
- static bool paused = false;
- //sx,sy
- static int sx, sy;
- //timer t
- static int t = 0;
- //pen
- static HPEN hPenCommon;
- //brushed
- static HBRUSH hBrushBlack, hBrushWhite, hBrushRed, hBrushYellow, hBrushGreen;
- //region
- static HRGN hCar;
- //car
- static POINT* pCarCoord = new POINT[4];
- static int iCurrentOffsetX;
- static int iCurrentOffsetY;
- static int iCurrentSpeedX;
- static int iCurrentSpeedY;
- static double dCarAngle;
- switch (message)
- {
- case WM_SIZE:
- sx = LOWORD(lParam);
- sy = HIWORD(lParam);
- break;
- case WM_CREATE:
- //pen
- hPenCommon = (HPEN)GetStockObject(1);
- //brushes
- hBrushBlack = CreateSolidBrush(RGB(0, 0, 0));
- hBrushWhite = CreateSolidBrush(RGB(255, 255, 255));
- hBrushRed = CreateSolidBrush(RGB(255, 0, 0));
- hBrushYellow = CreateSolidBrush(RGB(255, 165, 0));
- hBrushGreen = CreateSolidBrush(RGB(0, 255, 0));
- //car
- iCurrentOffsetX = 0;
- iCurrentSpeedX = 1;
- iCurrentOffsetY = 0;
- iCurrentSpeedY = 0;
- dCarAngle = 0;
- //set timer
- SetTimer(hWnd, 1, INTERVAL, NULL);
- break;
- case WM_PAINT:
- //begin paint
- hDC = BeginPaint(hWnd, &ps);
- //
- SelectObject(hDC,hPenCommon);
- //set modes
- SetMapMode(hDC, MM_ANISOTROPIC);
- SetWindowExtEx(hDC, sx, sy, NULL);
- SetViewportExtEx(hDC, sx, -sy, NULL);
- SetViewportOrgEx(hDC, 0, sy, NULL);
- //draw
- CreteCarRgn(hDC, hCar, 0, 0, sx / 5, sy / 6, dCarAngle);
- if (CAR_IS_OUT)
- {
- iCurrentOffsetX = -sx / 5;
- iCurrentOffsetY = 0;
- }
- OffsetRgn(hCar, iCurrentOffsetX, iCurrentOffsetY - sy);
- FillRgn(hDC, hCar, hBrushGreen);
- //detete region
- DeleteObject(hCar);
- //end paint
- EndPaint(hWnd, &ps);
- break;
- case WM_TIMER:
- if (t >= 10 * INTERVAL)
- t = 0;
- //Offset
- iCurrentOffsetX += iCurrentSpeedX;
- iCurrentOffsetY += iCurrentSpeedY;
- t++;
- InvalidateRect(hWnd, NULL, TRUE);
- break;
- case WM_KEYDOWN:
- {
- switch (wParam)
- {
- case VK_SPACE:
- if (!paused)
- {
- KillTimer(hWnd, 1);
- paused = true;
- }
- else if (paused)
- {
- SetTimer(hWnd, 1, INTERVAL, NULL);
- paused = false;
- }
- break;
- case VK_RIGHT:
- iCurrentSpeedX++;
- break;
- case VK_LEFT:
- iCurrentSpeedX--;
- break;
- case VK_UP:
- iCurrentSpeedY = iCurrentSpeedX;
- dCarAngle = M_PI / 10;
- break;
- case VK_DOWN:
- iCurrentSpeedY = 0;
- dCarAngle = 0;
- break;
- }
- break;
- }
- case WM_CLOSE:
- userReply = MessageBox(hWnd, "Are you sure?", "", MB_YESNO | MB_ICONQUESTION);
- if (IDYES == userReply)
- {
- DestroyWindow(hWnd);
- }
- break;
- case WM_DESTROY:
- //pen
- DeleteObject(hPenCommon);
- //brushes
- DeleteObject(hBrushBlack);
- DeleteObject(hBrushWhite);
- DeleteObject(hBrushRed);
- DeleteObject(hBrushYellow);
- DeleteObject(hBrushGreen);
- //timer
- KillTimer(hWnd, 1);
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- #define LEFT_DOWN x,y -0.4*height,angle
- #define LEFT_UP x,y-0.8*height,angle
- #define RIGHT_UP x+width,y-0.8*height,angle
- #define RIGHT_DOWN x+width,y-0.4*height,angle
- void CreteCarRgn(HDC hDC, HRGN& hRgn, int x, int y, int width, int height, double angle)
- {
- //Ellipse(hDC, x, y, x + width, y + height);
- //begin
- BeginPath(hDC);
- //make region
- //draw body
- MoveToEx(hDC, (int)RotateX(LEFT_DOWN), (int)RotateY(LEFT_DOWN), NULL);
- LineTo(hDC, (int)RotateX(LEFT_UP), (int)RotateY(LEFT_UP));
- LineTo(hDC, (int)RotateX(RIGHT_UP), (int)RotateY(RIGHT_UP));
- LineTo(hDC, (int)RotateX(RIGHT_DOWN), (int)RotateY(RIGHT_DOWN));
- LineTo(hDC, (int)RotateX(LEFT_DOWN), (int)RotateY(LEFT_DOWN));
- //draw wheels
- AngleArc(hDC, (int)RotateX(x, -0.2*height, angle), (int)RotateY(x, y - 0.2*height, angle), 0.2*height, 0, 360);
- MoveToEx(hDC, (int)RotateX(x + width, -0.2*height, angle), (int)RotateY(x + width, y - 0.2*height, angle), NULL);
- AngleArc(hDC, (int)RotateX(x + width, -0.2*height, angle), (int)RotateY(x + width, y - 0.2*height, angle), 0.2*height, 0, 360);
- CloseFigure(hDC);
- //end
- EndPath(hDC);
- hRgn = PathToRegion(hDC);
- }
- double RotateX(int x, int y, double angle)
- {
- return (x * cos(angle) + y * sin(angle)); //(x * cos(angle) - y * sin(angle));//
- }
- double RotateY(int x, int y, double angle)
- {
- return (-x * sin(angle) + y * cos(angle)); //(x*sin(angle) + y * cos(angle));//
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement