Understanding the Window WinMain Cycle

This is a discussion on Understanding the Window WinMain Cycle within the Windows Programming forums, part of the Platform Specific Boards category; I am looking at the code in my WinMain, I know what it does, I just don't know the exact ...

  1. #1
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107

    Understanding the Window WinMain Cycle

    I am looking at the code in my WinMain, I know what it does,
    I just don't know the exact particulars and what regulates it.
    Code:
      while(TRUE)
            {
                if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
                {
                    //Process the message
                    if(msg.message==WM_QUIT) break;
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
                else
                {
                    //Make sure the game engine isn't sleeping
                    if(!GameEngine::GetEngine()->GetSleep())
                    {
                        //Check the tick count to see if a game cycle has
    elapsed
                        iTickCount=GetTickCount();
                        if(iTickCount>iTickTrigger)
                        {
                            iTickTrigger=iTickCount + 
                                  GameEngine::GetEngine()->GetFrameDelay();
                            GameCycle();
                        }
                    }
                }
            }
    Question 1:
    In the line if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) it
    is passing the message to be Translated and Dispatched if it fails to Peek, but how would it know what the messag is if it could not peek at it?
    So I am assuming that it is trying to peek, if it can then great perform a GameCycle, otherwise check to see it is a quit message, then translate it.
    Question 2:
    How often is this cycle of peeking messages ran through? Is it based on CPU Cycles...?


    Just seeking to understand everything better...

    Thanks for any responses.
    Last edited by BillBoeBaggins; 11-26-2003 at 04:02 PM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    PeekMessage
    messages and message queues.

    >>it
    is passing the message to be Translated and Dispatched if it fails to Peek<<

    No.
    msdn PeekMessage:
    Return Value

    If a message is available, the return value is nonzero.

    If no messages are available, the return value is zero.
    >>but how would it know what the messag is if it could not peek at it?<<
    msdn PeekMessage:
    checks the thread message queue for a posted message
    >>So I am assuming that it is trying to peek, if it can then great perform a GameCycle, otherwise check to see it is a quit message, then translate it.<<

    Other way around ie if there is a message, deal with it (including check for a quit message which should result in -er- quitting; that's what the break is for) else gamecycle.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Ok Ken, that makes sense. But how many times is GameCycle triggered per second per se. Is it based on computer speed...?

    Thanks.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,854
    >>GameEngine::GetEngine()->GetFrameDelay();

    This will return the number of millseconds between cycles.

    PeekMessage() is called as fast as the PC can, using 100% of CPU time. If there is not a message it will return and the screen will be updated.
    If the user is not doing anything there will not be a message and the GameEngine will be called.

    So the frame rate is constant on all systems and for all types of user interation (ie the number of messages the user generates) the frame rate is regulated by the use of GetTickCount(). The app compares the current value to the value it got last time it updated the screen (last value+time between frames).
    "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

  5. #5
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Thanks much for the help, I am going to look deeper into the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. input/output
    By dogbert234 in forum Windows Programming
    Replies: 11
    Last Post: 01-26-2005, 05:57 AM
  4. Problem with creating new window, from another window
    By Garfield in forum Windows Programming
    Replies: 6
    Last Post: 01-11-2004, 01:10 PM
  5. Invoking MSWord
    By Donn in forum C Programming
    Replies: 21
    Last Post: 09-08-2001, 04:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21