Guest User

Untitled

a guest
Apr 16th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 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_LEFT (iCurrentOffsetX-sx/5)>sx
  9. #define CAR_IS_OUT_RIGHT iCurrentOffsetX < 0
  10.  
  11. const int INTERVAL = 50;
  12. const int COEFFICIENT = 600;
  13. static double r_angle = 0;
  14.  
  15. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  16. HRGN& CreateCarRgn(HDC hDC, int x, int y, int width, int height, double angle, int iSpeed, int iTime);
  17. HRGN& CreateCarBodyRgn(HDC hDC, int x, int y, int width, int height, double angle);
  18. HRGN& CreateCarWheelsRgn(HDC hDC, int x, int y, int width, int height, double angle, int iSpeed, int iTime);
  19. double RotateX(int x, int y, double angle);
  20. double RotateY(int x, int y, double angle);
  21.  
  22. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  23. {
  24. MSG msg;
  25. KWnd mainWnd("MyWin", hInstance, nCmdShow, WndProc);
  26. while (GetMessage(&msg, NULL, 0, 0))
  27. {
  28. TranslateMessage(&msg);
  29. DispatchMessage(&msg);
  30. }
  31. return (msg.wParam);
  32. }
  33.  
  34. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  35. {
  36. PAINTSTRUCT ps;
  37. HDC hDC;
  38. int userReply;
  39. //timer pause
  40. static bool paused = false;
  41. //sx,sy
  42. static int sx, sy;
  43. //timer t
  44. static int t = 0;
  45. //pen
  46. static HPEN hPenCommon;
  47. //brushed
  48. static HBRUSH hBrushCurrent;
  49. //region
  50. static HRGN hCar;
  51. //car
  52. static POINT* pCarCoord = new POINT[4];
  53. static int iCurrentOffsetX;
  54. static int iCurrentOffsetY;
  55. static int iCurrentSpeedX;
  56. static int iCurrentSpeedY;
  57. static double dCarAngle;
  58.  
  59. switch (message)
  60. {
  61. case WM_SIZE:
  62. sx = LOWORD(lParam);
  63. sy = HIWORD(lParam);
  64. break;
  65. case WM_CREATE:
  66. //pen
  67. hPenCommon = (HPEN)GetStockObject(1);
  68. //car
  69. iCurrentOffsetX = 0;
  70. iCurrentSpeedX = 1;
  71. iCurrentOffsetY = 0;
  72. iCurrentSpeedY = 0;
  73. dCarAngle = 0;
  74. //set timer
  75. SetTimer(hWnd, 1, INTERVAL, NULL);
  76. break;
  77. case WM_PAINT:
  78. //check for stop
  79. if (iCurrentSpeedX == 0)
  80. {
  81. iCurrentSpeedY = 0;
  82. }
  83. //begin paint
  84. hDC = BeginPaint(hWnd, &ps);
  85. //
  86. SelectObject(hDC,hPenCommon);
  87. //set modes
  88. SetMapMode(hDC, MM_ANISOTROPIC);
  89. SetWindowExtEx(hDC, sx, sy, NULL);
  90. SetViewportExtEx(hDC, sx, -sy, NULL);
  91. SetViewportOrgEx(hDC, 0, sy, NULL);
  92. //draw
  93. hCar = CreateCarRgn(hDC, 0, 0, sx / 5, sy / 6, dCarAngle, iCurrentSpeedX, t);
  94. if (CAR_IS_OUT_LEFT)
  95. {
  96. iCurrentOffsetX = -sx / 5;
  97. iCurrentOffsetY = sy / 6;
  98. }
  99. OffsetRgn(hCar, iCurrentOffsetX, iCurrentOffsetY - sy);
  100. //color
  101. if (CAR_IS_OUT_RIGHT)
  102. {
  103. hBrushCurrent = CreateSolidBrush(RGB(255, 0, 255));
  104. }
  105. else
  106. {
  107. hBrushCurrent = CreateSolidBrush(RGB(255, (255 * iCurrentOffsetX) / sx, 255));
  108. }
  109. FillRgn(hDC, hCar, hBrushCurrent);
  110. DeleteObject(hBrushCurrent);
  111. //detete region
  112. DeleteObject(hCar);
  113. //end paint
  114. EndPaint(hWnd, &ps);
  115. break;
  116. case WM_TIMER:
  117. //Offset
  118. iCurrentOffsetX += iCurrentSpeedX;
  119. iCurrentOffsetY += iCurrentSpeedY;
  120. t++;
  121. if (iCurrentSpeedX != 0)
  122. {
  123. dCarAngle = atan((0.1*iCurrentSpeedY) / (0.1*iCurrentSpeedX));
  124. }
  125. else
  126. {
  127. dCarAngle = 0;
  128. }
  129. InvalidateRect(hWnd, NULL, TRUE);
  130. break;
  131. case WM_KEYDOWN:
  132. {
  133. switch (wParam)
  134. {
  135. case VK_SPACE:
  136. if (!paused)
  137. {
  138. KillTimer(hWnd, 1);
  139. paused = true;
  140. }
  141. else if (paused)
  142. {
  143. SetTimer(hWnd, 1, INTERVAL, NULL);
  144. paused = false;
  145. }
  146. break;
  147. case VK_RIGHT:
  148. iCurrentSpeedX++;
  149. break;
  150. case VK_LEFT:
  151. if (iCurrentSpeedX != 0)
  152. {
  153. iCurrentSpeedX--;
  154. }
  155. break;
  156. case VK_UP:
  157. iCurrentSpeedY++;
  158. break;
  159. case VK_DOWN:
  160. iCurrentSpeedY--;
  161. break;
  162. }
  163. break;
  164. }
  165. case WM_CLOSE:
  166. userReply = MessageBox(hWnd, "Are you sure?", "", MB_YESNO | MB_ICONQUESTION);
  167. if (IDYES == userReply)
  168. {
  169. DestroyWindow(hWnd);
  170. }
  171. break;
  172. case WM_DESTROY:
  173. //pen
  174. DeleteObject(hPenCommon);
  175. //timer
  176. KillTimer(hWnd, 1);
  177. PostQuitMessage(0);
  178. break;
  179. default:
  180. return DefWindowProc(hWnd, message, wParam, lParam);
  181. }
  182. return 0;
  183. }
  184.  
  185. HRGN& CreateCarRgn(HDC hDC, int x, int y, int width, int height, double angle, int iSpeed, int iTime)
  186. {
  187. HRGN hRgn, hWheels;
  188. hRgn = CreateCarBodyRgn(hDC, x, y, width, height, angle);
  189. hWheels = CreateCarWheelsRgn(hDC, x, y, width, height, angle, iSpeed, iTime);
  190. //make finite region
  191. CombineRgn(hRgn, hRgn, hWheels, RGN_DIFF);
  192. return hRgn;
  193. }
  194. double RotateX(int x, int y, double angle)
  195. {
  196. return (x * cos(angle) + y * sin(angle));
  197. }
  198. double RotateY(int x, int y, double angle)
  199. {
  200. return (-x * sin(angle) + y * cos(angle));
  201. }
  202.  
  203. #define LEFT_DOWN x,y-0.4*height,angle
  204. #define LEFT_UP x,y-0.8*height,angle
  205. #define WINDOW_UP x+0.6*width,y-0.8*height,angle
  206. #define WINDOW_DOWN x+0.7*width,y-0.6*height,angle
  207. #define WINDOW_RIGHT x+width,y-0.6*height,angle
  208. #define RIGHT_DOWN x+width,y-0.4*height,angle
  209.  
  210. #define FIRST_BODY_WHEEL x+0.1*width,y-0.2*height,angle
  211. #define SECOND_BODY_WHEEL x+0.9*width,y-0.2*height,angle
  212.  
  213. HRGN& CreateCarBodyRgn(HDC hDC, int x, int y, int width, int height, double angle)
  214. {
  215. HRGN hBody;
  216. //begin
  217. BeginPath(hDC);
  218. //draw body
  219. MoveToEx(hDC, (int)RotateX(LEFT_DOWN), (int)RotateY(LEFT_DOWN), NULL);
  220. LineTo(hDC, (int)RotateX(LEFT_UP), (int)RotateY(LEFT_UP));
  221. LineTo(hDC, (int)RotateX(WINDOW_UP), (int)RotateY(WINDOW_UP));
  222. LineTo(hDC, (int)RotateX(WINDOW_DOWN), (int)RotateY(WINDOW_DOWN));
  223. LineTo(hDC, (int)RotateX(WINDOW_RIGHT), (int)RotateY(WINDOW_RIGHT));
  224. LineTo(hDC, (int)RotateX(RIGHT_DOWN), (int)RotateY(RIGHT_DOWN));
  225. LineTo(hDC, (int)RotateX(LEFT_DOWN), (int)RotateY(LEFT_DOWN));
  226. //draw wheels
  227. MoveToEx(hDC, (int)RotateX(FIRST_BODY_WHEEL), (int)RotateY(FIRST_BODY_WHEEL), NULL);
  228. AngleArc(hDC, (int)RotateX(FIRST_BODY_WHEEL), (int)RotateY(FIRST_BODY_WHEEL), 0.2*height, 0, 360);
  229. MoveToEx(hDC, (int)RotateX(SECOND_BODY_WHEEL), (int)RotateY(SECOND_BODY_WHEEL), NULL);
  230. AngleArc(hDC, (int)RotateX(SECOND_BODY_WHEEL), (int)RotateY(SECOND_BODY_WHEEL), 0.2*height, 0, 360);
  231. CloseFigure(hDC);
  232. //end
  233. EndPath(hDC);
  234. //make region
  235. hBody = PathToRegion(hDC);
  236. return hBody;
  237. }
  238. HRGN& CreateCarWheelsRgn(HDC hDC, int x, int y, int width, int height, double angle, int iSpeed, int iTime)
  239. {
  240. //init r_angle
  241. if (iSpeed != 0)
  242. {
  243. r_angle += COEFFICIENT * (iSpeed) / 360; //rotation angle
  244. }
  245. HRGN hWheels;
  246. //begin
  247. BeginPath(hDC);
  248. //draw wheels
  249. //first
  250. MoveToEx(hDC, (int)RotateX(FIRST_BODY_WHEEL), (int)RotateY(FIRST_BODY_WHEEL), NULL);
  251. AngleArc(hDC, (int)RotateX(FIRST_BODY_WHEEL), (int)RotateY(FIRST_BODY_WHEEL), 0.15*height, r_angle, 15);
  252. MoveToEx(hDC, (int)RotateX(SECOND_BODY_WHEEL), (int)RotateY(SECOND_BODY_WHEEL), NULL);
  253. AngleArc(hDC, (int)RotateX(SECOND_BODY_WHEEL), (int)RotateY(SECOND_BODY_WHEEL), 0.15*height, r_angle, (15));
  254. //second
  255. CloseFigure(hDC);
  256. //end
  257. EndPath(hDC);
  258. //make region
  259. hWheels = PathToRegion(hDC);
  260. return hWheels;
  261. }
Add Comment
Please, Sign In to add comment