Thread: Windows game loop

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    Windows game loop

    A simple game loop might look like this:
    Code:
    while(!exit)
    {
       showIntro()
       while(game)
       {
       }
    }
    Writing a game loop in using the Windows API doesn't seem so straight forward. What is one method?
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I usually do this. It puts messages as first priority so they won't stack up.
    Code:
    msg Message;
    bool GameIsOn = true;
    
    while(GameIsOn)
    {
       if(PeekMessage(&Message, NULL, 0, 0))
       {
          TranslateMessage(&Message);
          DispatchMessage(&Message);
       }
       else
       {
          //Update/Render/Whatever
          //To quit, set GameIsOn to false
       }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Note the use of PeekMessage() rather than GetMessage().

    PeekMessage() will return if there is not a message, allowing the game to continue. This however maens that there will be no point where the game is doing nothing ie100% CPU use

    GetMessage() will not return until there is a message for the app. This means the app can sit dormant until it is needed. It will not be able to do background processing unless the app generates a timer msg (very inaccurate)



    To be 100% accurate should use the new message loop (which handles the -1 error return)

    Code:
    while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
    { 
        if (bRet == -1)
        {
            // handle the error and possibly exit
        }
        else
        {
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        }
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    But in a game loop you DO want to make calculations periodically wether a message has been sent or not. Using GetMessage, as you said, will halt execution until another message is sent.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a simple Windows game
    By ejohns85 in forum C Programming
    Replies: 1
    Last Post: 05-22-2009, 12:46 PM
  2. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  3. Windows Game Server
    By JLocke in forum Tech Board
    Replies: 7
    Last Post: 02-12-2003, 07:50 PM
  4. Replies: 1
    Last Post: 01-10-2003, 10:08 PM
  5. Windows message loop variations
    By Leeman_s in forum Windows Programming
    Replies: 1
    Last Post: 01-10-2003, 09:01 PM