My pong game stops painting
I wrote a pong game in windows, I am like half way done. I have encountered a problem i have been trying to solve for hours, so maybe you guys can help me.
Code:
#include <windows.h>
#include <time.h>
#include <cstdlib>
const char g_szClassName[] = "myWindowClass";
struct hball{ int xslope; int yslope; int xpos; int ypos; int upleftx;
int uplefty; int bottomrightx; int bottomrighty; int size; int dir; int diry; };
struct paddle{ int xpos; int length; int ypos;};
paddle paddle1;
paddle paddle2;
HDC OFFSCRN;
HDC SCREEN;
HPEN ppen = CreatePen(PS_SOLID, 3, RGB(255, 255, 255));
HPEN bpen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
HPEN ballpen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
HBRUSH bbrush = CreateSolidBrush(RGB(0, 0, 0));
HBRUSH wbrush = CreateSolidBrush(RGB(255, 255, 255));
RECT brect;
RECT urect;
HBITMAP offscreenbm;
HBITMAP screenbm;
PAINTSTRUCT ps;
int currtick;
int oldtick;
int newtick;
bool gamestart;
HFONT font = CreateFont( 0, 0, 0, 0, 700, FALSE, FALSE, FALSE, 0,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH,
NULL);
hball ball;
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(ball.ypos >= 400) { ball.diry = 2; }
if(ball.ypos <= 5) { ball.diry = 1; }
if(ball.xpos == paddle1.xpos)
{
if(ball.ypos == paddle1.ypos)
{
ball.dir == 2;
}
}
if(ball.xpos == paddle2.xpos)
{
if(ball.ypos == paddle2.ypos)
{
ball.dir == 1;
}
}
if(ball.dir == 1) { ball.xpos = ball.xpos - ball.xslope; }
if(ball.dir == 2) { ball.xpos = ball.xpos + ball.xslope; }
if(ball.diry == 1) { ball.ypos = ball.ypos + ball.yslope; }
if(ball.diry == 2) { ball.ypos = ball.ypos - ball.yslope; }
ball.upleftx = ball.xpos;
ball.uplefty = ball.ypos;
ball.bottomrightx = ball.xpos - ball.size;
ball.bottomrighty = ball.ypos - ball.size;
newtick = GetTickCount();
currtick = newtick - oldtick;
if(currtick >= 2)
{
oldtick = GetTickCount();
RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE);
}
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_UP:
paddle1.ypos = paddle1.ypos - 6;
break;
case VK_DOWN:
paddle1.ypos = paddle1.ypos + 6;
break;
case 13:
gamestart = true;
break;
}
break;
case WM_PAINT:
// Sizing hdcs and bitmaps to the screen so it will draw to the whole
// windows client area
GetClientRect(hwnd, &brect);
OFFSCRN = CreateCompatibleDC(GetDC(hwnd));
SCREEN = CreateCompatibleDC(GetDC(hwnd));
offscreenbm = CreateCompatibleBitmap(GetDC(hwnd), brect.right, brect.bottom);
screenbm = CreateCompatibleBitmap(GetDC(hwnd), brect.right, brect.bottom);
SCREEN = BeginPaint(hwnd, &ps);
SelectObject(OFFSCRN, offscreenbm);
SelectObject(SCREEN, screenbm);
//Prepare to draw the background
SelectObject(OFFSCRN, bpen);
SelectObject(OFFSCRN, bbrush);
//Draw the background
Rectangle(OFFSCRN, brect.left, brect.top, brect.right, brect.bottom);
//Prepare to draw the board
SelectObject(OFFSCRN, ballpen);
//Draw the board
Rectangle(OFFSCRN, 5, 5, 700, 400);
MoveToEx(OFFSCRN, 350, 5, NULL);
LineTo(OFFSCRN, 350, 400);
//Prepare to draw the paddles
SelectObject(OFFSCRN, ppen);
SelectObject(OFFSCRN, bbrush);
//Paddle 1
MoveToEx(OFFSCRN, paddle1.xpos, paddle1.ypos + paddle1.length / 2, NULL);
LineTo(OFFSCRN, paddle1.xpos, paddle1.ypos - paddle1.length);
//Paddle 2
MoveToEx(OFFSCRN, paddle2.xpos, paddle2.ypos + paddle2.length / 2, NULL);
LineTo(OFFSCRN, paddle2.xpos, paddle2.ypos - paddle2.length);
//Ball
SelectObject(OFFSCRN, wbrush);
SelectObject(OFFSCRN, ballpen);
Ellipse(OFFSCRN, ball.upleftx, ball.uplefty, ball.bottomrightx, ball.bottomrighty);
if(gamestart == false)
{
SelectObject(OFFSCRN, font);
TextOut(OFFSCRN, 290, 190, "Hit <Enter> to start!", 21);
}
BitBlt(SCREEN, 0, 0, brect.right, brect.bottom, OFFSCRN, 0, 0, SRCCOPY);
ReleaseDC(hwnd, OFFSCRN);
ReleaseDC(hwnd, SCREEN);
EndPaint(hwnd, &ps);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE);
break;
}
return 0;
}
MSG Msg;
WNDCLASSEX wc;
HWND hwnd;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
ball.yslope = rand() % (1 - 7);
ball.dir = 1;
ball.diry = 2;
ball.xslope = 5;
paddle1.xpos = 20;
paddle1.ypos = 197;
paddle1.length = 45;
paddle2.xpos = 680;
paddle2.ypos = 197;
paddle2.length = 46;
ball.xpos = 347;
ball.ypos = 197;
ball.size = 10;
ball.xslope = 1;
ball.upleftx = ball.xpos - (ball.size / 2);
ball.uplefty = ball.ypos - (ball.size / 2);
ball.bottomrightx = ball.xpos + ball.size;
ball.bottomrighty = ball.ypos + ball.size;
// Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Pong",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 750, 510,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
oldtick = GetTickCount();
// The Message Loop
while(GetMessage(&Msg, hwnd, 0, 0) > 0)
{
PeekMessage(&Msg, hwnd, 0, 0, 0);
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
I am running Dev-C++ 4.9.9.2. If you compile this code, you will see what i am talking about. It stops drawing, and even if you minimize it and reopen it, a WM_PAINT message isn't sent.
:confused: Any ideas?