Screen flickering while running
When the program is running the entire screen (not just the game screen) starts flickering and you can't close out of it directly. Here is my code
main.h
Code:
#include <windows.h>
#define IDB_BOX 1
typedef int BoxPos;
const BoxPos TopLeft = 0,
TopRight = 1,
BottomRight = 2,
BottomLeft = 3;
HWND g_hWindow;
HINSTANCE g_hInstance;
class Box
{
protected:
HBITMAP m_hBox;
BITMAPINFO* m_BitmapInfo;
BoxPos m_bpBoxPos;
RECT m_rcClickZone;
int m_iWidth;
int m_iHeight;
BOOL m_bMoving;
public:
Box();
~Box();
void UpdateBox();
void GamePaint(HDC hDC);
void LeftClick(int x, int y);
int m_iXPos;
int m_iYPos;
};
Box* g_pBox;
void GameEnd();
main.cpp
Code:
#include "main.h"
Box::Box()
{
m_hBox = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_BOX));
m_bpBoxPos = TopLeft;
m_iWidth = 32;//The box is a 32x32 pixel square
m_iHeight = 32;
m_rcClickZone.left = 0;
m_rcClickZone.top = 0;
m_rcClickZone.right = m_iWidth;
m_rcClickZone.bottom = m_iHeight;
m_iXPos = m_iYPos = 0;
}
Box::~Box()
{
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(g_hWindow);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd, &ps);
g_pBox->GamePaint(hDC);
EndPaint(hwnd, &ps);
break;
case WM_LBUTTONDOWN:
g_pBox->LeftClick(LOWORD(lParam), HIWORD(lParam));
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
break;
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
WNDCLASSEX WinClass;
MSG msg;
LPSTR szClassName = "BoxApp";
g_pBox = new Box();
if(g_pBox == NULL)
MessageBox(g_hWindow, "g_pBox was no initialized", "ERROR", MB_ICONEXCLAMATION);
//Register the window class
WinClass.cbSize = sizeof(WNDCLASSEX);
WinClass.style = 0;
WinClass.lpfnWndProc = WndProc;
WinClass.cbClsExtra = 0;
WinClass.cbWndExtra = 0;
WinClass.hInstance = g_hInstance;
WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WinClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WinClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WinClass.lpszClassName = szClassName;
WinClass.lpszMenuName = NULL;
if(!RegisterClassEx(&WinClass))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
CreateWindowEx(WS_EX_CLIENTEDGE, szClassName, "The Box App",
WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
NULL, NULL, g_hInstance, NULL);
if(iCmdShow > 0)
ShowWindow(g_hWindow, iCmdShow);
else
ShowWindow(g_hWindow, SW_SHOWNORMAL);
UpdateWindow(g_hWindow);
while(TRUE)
{
g_pBox->UpdateBox();
InvalidateRect(g_hWindow, NULL, FALSE);
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
GameEnd();
return msg.wParam;
}
void Box::UpdateBox()
{
switch(m_bpBoxPos)
{
case TopLeft:
m_rcClickZone.left = 0;
m_rcClickZone.top = 0;
m_rcClickZone.right = m_iWidth;
m_rcClickZone.bottom = m_iHeight;
break;
case TopRight:
m_rcClickZone.left = 640 - m_iWidth;
m_rcClickZone.top = 0;
m_rcClickZone.right = 640;
m_rcClickZone.bottom = m_iHeight;
break;
case BottomRight:
m_rcClickZone.left = 640 - m_iWidth;
m_rcClickZone.top = 480 - m_iHeight;
m_rcClickZone.right = 640;
m_rcClickZone.bottom = 480;
break;
case BottomLeft:
m_rcClickZone.left = 0;
m_rcClickZone.top = 480 - m_iHeight;
m_rcClickZone.right = m_iWidth;
m_rcClickZone.bottom = 480;
break;
}
}
void Box::GamePaint(HDC hDC)
{
HDC hMemDC = CreateCompatibleDC(hDC);
if(m_bMoving)
{
switch(m_bpBoxPos)
{
case TopLeft:
if(m_iXPos >= 640)
{
m_bpBoxPos = TopRight;
break;
}
m_iXPos++;
break;
case TopRight:
if(m_iYPos >= 480)
{
m_bpBoxPos = BottomRight;
break;
}
m_iYPos++;
break;
case BottomRight:
if(m_iXPos <= 0)
{
m_bpBoxPos = BottomLeft;
break;
}
m_iXPos--;
break;
case BottomLeft:
if(m_iYPos <= 0)
{
m_bpBoxPos = TopLeft;
break;
}
m_iYPos--;
break;
}
}
BitBlt(hDC, m_iXPos, m_iYPos, m_iWidth, m_iHeight, hMemDC, 0, 0, SRCCOPY);
}
void Box::LeftClick(int x, int y)
{
if(!m_bMoving)
{
if((x >= m_rcClickZone.left) && (x <= m_rcClickZone.right) && (y >= m_rcClickZone.bottom) && (y <= m_rcClickZone.top))
m_bMoving = TRUE;
}
}
void GameEnd()
{
delete g_pBox;
delete g_hWindow;
delete g_hInstance;
}
I ran the debugger, but it didn't pick anything up, so I'm posting the problem here.