XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   September 24: 2D-19: Particle engine
My Book: Out Now!
      
       Go to section on this site

Additional info


Latest Forum posts

 Next Chapter
  Posted by: jasonbarry
  When: 15/10/2008 at 15:55:47

 HLSL: compilation fails
  Posted by: aguacate
  When: 15/10/2008 at 12:03:19

 my collision sucks!
  Posted by: orgad
  When: 15/10/2008 at 11:25:49

 HLSL: compilation fails
  Posted by: aguacate
  When: 15/10/2008 at 11:16:55

 Flickering/glitches
  Posted by: Danzence
  When: 15/10/2008 at 09:53:00

 Next Chapter
  Posted by: samer
  When: 15/10/2008 at 06:40:32

 Flickering/glitches
  Posted by: samer
  When: 15/10/2008 at 06:39:17

 Texture terrian c++
  Posted by: ruudriem
  When: 15/10/2008 at 05:55:16

 1.7 and 2.8 problem.
  Posted by: riemer
  When: 15/10/2008 at 03:36:34

 Next Chapter
  Posted by: riemer
  When: 15/10/2008 at 03:34:29


Ads



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.




DirectX Tutorial 2 - Ending the game loop

If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



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;
     }
     
     


    Google
     
    Webwww.riemers.net


    If you appreciate the amount of time I spend creating and updating
    these pages, feel free to donate -- any amount is welcome !



    - Website design & XNA + DirectX code : Riemer Grootjans -
    ©2003 - 2008 Riemer Grootjans
  • Translations

    This site in English
    This site in Korean
    This site in Czech

    Microsoft MVP Award



    2007 - 2008 MVP Award
    DirectX - XNA

    Contents

    News
    Home
    Forum
    XNA 2.0 Recipes Book (8)
    Downloads
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Tutorials (160)
    XNA 2.0 using C# (89)
    DirectX using C# (54)
    DirectX using C++ (15)
    Series 1: Terrain (15)
    Opening a window
    Ending the game loop
    Linking to the Device
    Clearing your window
    Drawing a triangle
    Culling
    Camera
    Rotation - Translation
    Indices
    Terrain creation
    Terrain from file
    DirectInput
    Importing .bmp files
    Adding colors
    DirectX Light basics
    DirectX using VB (2)
    -- Expand all --


    Thank you!

    Support this site --
    any amount is welcome !

    Stay up-to-date

    I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!