XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   
My XNA Book
      
       Go to section on this site

Additional info


Latest Forum posts

 Account settings
  Posted by: Anonymous
  When: 07/05/2014 at 09:48:39

 forced subtitle
  Posted by: Applefly
  When: 07/05/2014 at 06:00:48

 convert DVD into PMS
  Posted by: Applefly
  When: 07/05/2014 at 05:55:25

 DVD to Digital Copy easily
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:52:29

 DVD on Xbox 360/Xbox One Console
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:51:47

 Extract .Srt Subtitles
  Posted by: Applefly
  When: 04/05/2014 at 03:54:38

 Encode Movie collection
  Posted by: Applefly
  When: 04/05/2014 at 03:52:41

 Convert DVD to WMV
  Posted by: Applefly
  When: 29/04/2014 at 05:53:50

 rip DVDs into digital files
  Posted by: Applefly
  When: 29/04/2014 at 05:51:20

 iTunes movies/music to Kindle Fire
  Posted by: ciciyu80
  When: 29/04/2014 at 05:10:20


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:
  • WM_DESTROY
          Most people would rather close the application by ...
  • 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 - 2011 Riemer Grootjans
  • Translations

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

    Microsoft MVP Award



    2007 - 2011 MVP Award
    DirectX - XNA

    Contents

    News
    Home
    Forum
    XNA 2.0 Recipes Book (8)
    XNA 3.0 Recipes Book (8)
    Downloads
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Community Projects (1)
    Tutorials (160)
    XNA 4.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!