Skip to content

Instantly share code, notes, and snippets.

@lynxluna
Created December 8, 2012 22:01
Show Gist options
  • Save lynxluna/4242170 to your computer and use it in GitHub Desktop.
Save lynxluna/4242170 to your computer and use it in GitHub Desktop.
Win32 Game Loop
#include <Windows.h>
#include <tchar.h>
#define CURRENT_WND_CLASS _T("GameWndClass_Didiet")
#define DEF_CX 800
#define DEF_CY 600
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
INT WINAPI _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
{
WNDCLASSEX wcex; /* Structure needed for creating Window */
HWND hWnd; /* Window Handle */
MSG msg;
BOOL bDone = FALSE;
SIZE screenSize;
LONG winX, winY;
ZeroMemory(&wcex, sizeof(WNDCLASSEX));
ZeroMemory(&msg, sizeof(MSG));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wcex.hCursor = LoadCursor(hInstance, IDC_ARROW);
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hInstance = hInstance;
wcex.lpfnWndProc = WndProc;
wcex.lpszClassName = CURRENT_WND_CLASS;
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.cbWndExtra = 0;
wcex.cbClsExtra = 0;
wcex.lpszMenuName = NULL;
if (!RegisterClassEx(&wcex)) {
return -1;
}
screenSize.cx = GetSystemMetrics(SM_CXSCREEN);
screenSize.cy = GetSystemMetrics(SM_CYSCREEN);
winX = ( screenSize.cx - (DEF_CX + GetSystemMetrics(SM_CXBORDER)*2) ) / 2;
winY = ( screenSize.cy - (DEF_CY + GetSystemMetrics(SM_CYBORDER) + GetSystemMetrics(SM_CYCAPTION)) ) / 2;
hWnd = CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW,
CURRENT_WND_CLASS,
_T("Game Window"),
WS_OVERLAPPEDWINDOW,
winX,winY,
DEF_CX,DEF_CY,
0,
0,
hInstance,
0);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
while (FALSE == bDone) {
if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT) {
bDone = TRUE;
}
}
else {
/* do rendering here */
}
}
DestroyWindow( hWnd );
UnregisterClass(CURRENT_WND_CLASS, hInstance);
return 0;
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_QUIT:
case WM_DESTROY:
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
@CodeDiseaseDev
Copy link

Thanks 👍

@SvenGarson
Copy link

Very useful for the non-blocking part, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment