Define a callback
to process events.
LRESULT CALLBACK WinProc(HWND hWnd,
UINT msg,
WPARaM wParam,
LPARAM lParam);
where some
common msg are:
|
Value |
Meaning |
|
WM_ACTIVATE |
window activated/got
focus |
|
WM_CLOSE |
window was
closed |
|
WM_CREATE |
window just
created |
|
WM_DESTROY |
window will
be killed |
|
WM_MOVE |
window has
been moved |
|
WM_MOUSEMOVE |
mouse has
been moved |
|
WM_KEYUP |
key released |
|
WM_KEYDOWN |
key pressed |
|
WM_TIMER |
timer event
occurred |
|
WM_USER |
user generated msg |
|
WM_PAINT |
window need
repainting |
|
WM_QUIT |
application
terminating |
|
WM_SIZE |
resize event |
// bare bones
event handler
LRESULT CALLBACK WindowProc(HWND hwnd,
LPARAM lparam)
{ // this function will handle msgs sent
HDC hdc; // device context for graphics
PAINTSTRUCT ps;
// also for graphics
switch (msg) // process the message
{ case WM_CREATE:
{ // this msg sent on window creation
// do init stuff here
return(0);
} break;
case WM_PAINT:
{ // repaint the window
hdc = BeginPaint(hwnd.&ps); // get device context
// paint window here
EndPaint(hwnd, &ps); //tells Windows
we repainted
return(0);
} break;
case WM_DESTROY
{ // clean
up
PostQuitMessage(0);
// this will cause system to send
WM_QUIT msg
return(0);
} break;
default: break;
}
// make Windows handle any msg you didn’t
return(DefWindowProc(hwnd, msg, wparam, lparam));
}
Messages - description
typedef struct tagMSG
{ HWND hwnd; // id msg
sent to
UINT message; // msg id
WPARAM wParam; // more msg
info
LPARAM
lParam;
// more msg info
DWORD
time; // time of msg event
POINT
pt; // mouse state
of event
} MSG;
Need 3
functions to deal with msgs in the event loop.
1. To
retrieve a message from the queue call:
BOOL GetMessage(LPMSG lpMsg, // ptr
to message
HWND hWnd, // handle of window
UINT
wMsgFilterMin; // 1st msg
UINT wMsgFilterMax);
// last msg
If the queue
is empty the function waits for a msg.
2. Translate
accelerator commands (^A) into basic events, by calling:
BOOL TranslateMessage( CONST MSG *lpMsg);
3. Send the msg address to event handler, with:
LONG DispatchMessage(CONST MSG *lpmsg);
Most windows
programs then use these three functions to write an event loop like:
// even loop
while(GetMessage(&msg, NULL, 0, 0))
{ // translate the accelerator keys
TranslateMessage(&msg);
// send the msg to event handler
DispatchMessage(&msg);
}
The problem
is that if there are no messages, it waits. This won’t work for a game because
the display needs to be refreshed. We need to detect if a
msg is available.
BOOL PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
this
command examines the msg queue, removes it and
returns true or returns false if no msg is available.
Now you can
write a game event loop!
MSG msg; // holds msg
// game
event loop
while(1)
{ // check for a message & process
if (PeekMessage(&msg, NULL,0, 0,
PM_REMOVE)
{
// test for QUIT
if (msg.message == WM_QUIT)
break; // leave the loop
// translate
TranslateMessage(&msg);
// send to event handler – WinProc()
DispatchMessage(&msg);
}
// main game code goes here
}// end game
loop