Why?
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; static Car A; HBRUSH hbrush; switch (message) { /* handle the messages */ case WM_CREATE: break; case WM_PAINT: hdc = GetDC(hwnd); hbrush = CreateSolidBrush(RGB(0, 255, 0)); SelectObject(hdc, hbrush); A.Draw(hdc, hbrush); //Rectangle(hdc,0,0,100,200); DeleteObject(hbrush); ReleaseDC(hwnd, hdc); break; case WM_KEYDOWN: switch (wParam) { case VK_RIGHT: //A.Move(5,5);change degree there break; case VK_LEFT: //A.Move(-5,-5); change degree there break; case VK_UP: A.Move(5, 0); //MessageBoxA(hwnd,"Yo","Yo",MB_OK|MB_SYSTEMMODAL); break; case VK_DOWN: A.Move(-5, 0); break; } //InvalidateRgn(hwnd,NULL,true); InvalidateRect(hwnd, NULL, 1); break; case WM_KILLFOCUS: //Game.Pause(); Game class method is raised here //when window lasts focus break; case WM_DESTROY: //delete A; //DeleteObject(hbrush); PostQuitMessage(0); /* send a WM_QUIT to the message queue */ break; default: /* for messages that we don't deal with */ return DefWindowProc(hwnd, message, wParam, lParam); } return 0; } class Car { float degree; bool collision; int pos_x; int pos_y; double x0, y0, x1, y1; const int width, height; /*const */ POINT pt[4]; //={{0,0},{125,0},{125,50},{0,50}}; public: Car():degree(0.f), collision(0), pos_x(0), pos_y(0), width(200), height(100), x0(250), y0(250), x1(500), y1(250), pt( { {0, 0}, {125, 0}, {125, 50}, {0, 50}}) {} void Draw(HDC hdc, HBRUSH oldbrush, HPEN hOldPen) { HPEN hNewPen; HBRUSH newbrush = CreateSolidBrush(RGB(255, 0, 0)); SelectObject(hdc, newbrush); //Using degree val to correctly draw the car frame ///Transform coordinates there ///pt[1].x = pt[0].x-(pt[1].x-pt[0].x)*cos(degree); ///pt[1].y = pt[0].y-(pt[1].y-pt[0].y)*sin(degree); ///^^^^^^^^^^^^^^^^^^^^^^^^^^^ Polygon(hdc, &pt[0], 4); /*Rectangle(hdc,pos_x,pos_y,pos_x+width,pos_y+height); */ MoveToEx(hdc, ceil(x0), ceil(y0), NULL); /// y1++; x1 = x0 + (x1 - x0) * (cos(degree)); char buff[40]; //itoa(cos(degree),buff,10); sprintf(buff, "%5.2lf", cos(degree)); string tmp("cos is: "); tmp.append(buff); TextOutA(hdc, 1, 2, tmp.c_str(), (int) tmp.length()); // itoa(ceil(sin(degree)),buff,10); tmp.assign("sin is: "); tmp.append(buff); TextOutA(hdc, 1, 25, tmp.c_str(), (int) tmp.length()); y1 = y0 + (y1 - y0) * (sin(degree)); hNewPen = CreatePen(PS_SOLID, 4, RGB(0, 255, 0)); SelectObject(hdc, hNewPen); LineTo(hdc, ceil(x1), ceil(y1)); SelectObject(hdc, hOldPen); SelectObject(hdc, oldbrush); DeleteObject(hNewPen); DeleteObject(newbrush); } void Move(const int dx, const int dy) { for (int i = 0; i < 4; i++) { pt[i].x += dx; pt[i].y += dy; } } void Degree(const float dx) { degree += dx; } void CheckCollision( /*const GameClass& A */ ) { } };
It eats constant.
Car
code lay out. - Ildar