|
|
|
|

Ending your game loop by a keypress |
Last chapter you learned how to open a simple window and how to keep it open for 5 seconds. Now we will replace the 5 seconds of pause with a while loop, that quits when the user presses a key.
To do this, we will need a global variable, int_AppRunning, that is 1 when the while loop needs to continue and 0 when the while loop needs to stop. Since this is a global variable, define it right under your define code and initialize it to 1:
int int_AppRunning = 1;
Now remove the ‘pause(5000)’ line, and replace it with the following code:
while(int_AppRunning) { if(PeekMessage(&msg_Message,han_Window,0,0,PM_REMOVE)) { if(!IsDialogMessage(han_Window,&msg_Message)) { DispatchMessage(&msg_Message); } } }
This will loop until int_AppRunning becomes 0. In fact, it is this loop that will become our game loop, as in later tutorials the actual drawing of our scene will happen in this loop.
Every loop, the operating system will be asked if there are any messages (=events) for our window. To do this, we have to call the PeekMessage method and pass it the handle of our window. You also need to specify where to store the message, a lower and upper bound of the message queue and what to do with the message when it is read. Just remove it please.
If there is a new message for our window, this is stored in the msg_Message variable, which is then passed on (by calling the DispatchMessage method) to the window procedure linked to our window, which is in our case OurWindowProcedure. Of course we still have to define this variable, so put this at the beginning of your WinMain method:
MSG msg_Message;
Now all we need to do is check when for keypresses, and if one occurs, set the int_AppRunning variable to 0. We will do this in the OurWindowProcedure, because this method is called everytime an event occurs, such as a keypress.
The method automatically gets some parameters, such as the kind of event (in our case, a keypress) and other parameters (such as the number of the key). Add this code to your OurWindowProcedure:
switch(uint_Message) { case WM_KEYDOWN: { int_AppRunning = 0; break; } break; }
This will simply check if the message concers a keypress, and if so, it will set the int_AppRunning to 0, which will terminate the while loop, which causes the window to close and the application to terminate.

Click here to go to the forum on this chapter!
Or click on one of the topics on this chapter to go there: Hour Glass This example works fine accept a slight annoyance ...I think this needs DoEvents. //My Program Freezes Without This Code
//First I ...
This is what your code should look like:
#include<windows.h>
int int_AppRunning = 1;
LRESULT CALLBACK OurWindowProcedure(HWND han_Wind,UINT uint_Message,WPARAM parameter1,LPARAM parameter2) {
switch(uint_Message) { case WM_KEYDOWN: { int_AppRunning = 0; break; } break; }
return DefWindowProc(han_Wind,uint_Message,parameter1,parameter2); } HWND NewWindow(LPCTSTR str_Title,int int_XPos, int int_YPos, int int_Width, int int_Height) { WNDCLASSEX wnd_Structure; wnd_Structure.cbSize = sizeof(WNDCLASSEX); wnd_Structure.style = CS_HREDRAW | CS_VREDRAW; wnd_Structure.lpfnWndProc = OurWindowProcedure; wnd_Structure.cbClsExtra = 0; wnd_Structure.cbWndExtra = 0; wnd_Structure.hInstance = GetModuleHandle(NULL); wnd_Structure.hIcon = NULL; wnd_Structure.hCursor = NULL; wnd_Structure.hbrBackground = GetSysColorBrush(COLOR_BTNFACE); wnd_Structure.lpszMenuName = NULL; wnd_Structure.lpszClassName = "WindowClassName"; wnd_Structure.hIconSm = LoadIcon(NULL,IDI_APPLICATION); RegisterClassEx(&wnd_Structure); return CreateWindowEx(WS_EX_CONTROLPARENT, "WindowClassName", str_Title, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, int_XPos, int_YPos, int_Width, int_Height, NULL, NULL, GetModuleHandle(NULL), NULL); } int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow) {
MSG msg_Message;
HWND han_Window = NewWindow("DirectX C++ Tutorial",100,100,500,500);
while(int_AppRunning) { if(PeekMessage(&msg_Message,han_Window,0,0,PM_REMOVE)) { if(!IsDialogMessage(han_Window,&msg_Message)) { DispatchMessage(&msg_Message); } } }
DestroyWindow(han_Window); return 0; }
- Website design & XNA + DirectX code : Riemer Grootjans - ©2003 - 2008 Riemer Grootjans
|
|
|
|
|