I'm sorry in advance if I wasn't supposed to ask Windows-specific code in this forum, but here goes:
Since I've started my first serious Windows programming, I've never felt truly comfortable with my Windows code. I wish that there was a simple, "right" way to create a window and run a main loop, but there obviously isn't. I'm sure that every single create-your-first window tutorial I have ever looked at has been slightly different.
Because of all this uncertainty, I wanted to ask the people here (who apparently know a bit more than I do) a question about creating a MsgProc().
The one I currently use (with all the game logic taken out) is shown here:
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_KEYUP:
if(wParam==VK_ESCAPE)
{
PostQuitMessage(0);
}
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
and here is my WinMain():
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
WNDCLASS wc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hCursor=LoadCursor(0, IDC_ARROW);
wc.hIcon=LoadIcon(0, IDI_APPLICATION);
wc.hInstance=hInst;
wc.lpfnWndProc=WinProc;
wc.lpszClassName="synth";
wc.lpszMenuName=0;
wc.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
HWND hwnd=CreateWindow("synth", "the_synth", WS_VISIBLE | WS_OVERLAPPEDWINDOW, 100, 100,
800, 600, 0, 0, hInst, 0);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
if(quit_game)
{
PostQuitMessage(0);
break;
}
}
}
UnregisterClass("synth", hInst);
return 0;
}
My questions are:
-Is my MsgProc() redundant? Do I really need to have procedures for WM_CLOSE, WM_DESTROY, and WM_QUIT? Does MsgProc() even handle the WM_QUIT event?
-Is the basic structure of my main loop correct (in the sense that it is sound and will not generate strange bugs as soon as I test it on a friend's computer)?
-Is my UnregisterClass() call necessary? It's just that someone told me in another thread that I'm not supposed to make this call and that Windows handles this stuff without being told. Is that true? I'm afraid that if I delete that line then the program will cease to be sound (in the same sense as in the previous question.)
Thanks a lot!
Greg Philbrick, Game Developercoming soon . . . Overhauled CellZenith