Thread: I never noticed this before.

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    363

    I never noticed this before.

    I was looking through the Win32 SDK Referance, when I came across this peculiar tidbit:
    GetMessage does not remove WM_PAINT messages from the queue. The messages remain in the queue until processed.
    Note that the function return value can be TRUE, FALSE, or -1. Thus, you should avoid code like this:
    while (GetMessage( lpMsg, hWnd, 0, 0)) ...
    The possibility of a -1 return value means that such code can lead to fatal application errors.
    Is it just me or does just about every single example of a windows programme use that line of code?

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Hmmm...interesting. I use that line of code. Never had any problems with it. Has anybody?
    1978 Silver Anniversary Corvette

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    So apparently we should be doing

    while(GetMessage(&m, h, 0, 0) > 0)

    I've also noticed that every example I've seen was the way described by shtarker.

    Does this mean we should take care of all the other functions returning this so-called "boolean" value?

    Why couldn't they just stick to 0 and 1??????
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    They realy should tell Petzold. As his MS press WIN32 bible uses it everywhere.

    I use -1 and define it as INVALID. Not in the msg loop though.

    Goes with having a menu in a child window. According to MS you can't.

    From GetMenu() help in MSVS v6
    "If the window is a child window, the return value is undefined."

    In fact you can, but only in Win9x, 2000 and XP don't allow it and refuse to show the window at all.
    Last edited by novacain; 03-27-2002 at 08:53 PM.
    "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
    Join Date
    Jan 2002
    Posts
    363
    Well most the code for my WinMain was appropriated (read copied) from the Win32 Bible, so only every single one of my programs has that loop in it.
    But so far that part of my programme hasn't crashed on me, or atleast i don't think it has.

  6. #6
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Talking Lucky!

    I am so lucky that I don't even use GetMessage heh .

    Thank'z to the fact that I am sort of "self-tought", I use 'PeekMessage', and a boolean variable that i'z only changed in the Message Procedure!

    Maybe a good architecture 2 u'ze now that there i'z this new compication... BUT I have one comment to make about this...

    A logical statement (such as 'if','while','for', ect.), I beleive, will take anything '<=0' as a false...

    So I don't see why:

    while(-1)

    woulden't work...

    ~SPH :-p:-p

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >So I don't see why:

    while(-1)

    woulden't work... <

    Because your beliefs are incorrect and you can't spell for ****.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    PeekMesage()
    does not differentiate WM_QUIT msg's,
    does not allow the app to sleep when no msg's (and therefore uses more CPU resources resulting slower code).
    "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

  9. #9
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    If PeekMessage() causes such a code slow down then why does every real time game use this method for its main loop??? If that was the case then I dont think every single time ive looked at a 3D engine for Win32 it has used this method to do the message loop. Im not saying this is the best way im just pointing out that you should be careful about what you say b/c you dont know what he/she is developing.
    Last edited by xds4lx; 04-03-2002 at 01:01 AM.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Because a game is a constant stream of input or paint msg's. There are always mgs in to be returned and the last thing it wants is to be put to sleep by Windows.

    Game loop checking for WM_QUIT msg's removing msg's as we go. The fact there is extra code in the main msg loop will cause some slow down as the if() and extra while() is evaluated but should not be noticeable.
    Code:
    while( !done )
    {
          while( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
         {
    	TranslateMessage( &msg );
    	DispatchMessage( &msg );
    	if(msg.message == WM_QUIT)
    	   done = 1;
         }
    }
    Games are not the only type of app. Some need to sit and wait to be used without using up your CPU constantly checking for new msg's. Or may have to run 24 /7 waking up as external events require the program.

    (In my answer you will notice I put the slower code in braces as it was a secondary concern to me. If you are running games the last thing you need is _other_ app's slowing your PC down because the programmer used an inappropriate msg loop)
    Last edited by novacain; 04-02-2002 at 12:39 AM.
    "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

  11. #11
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Exclamation Lolz...

    Well actually... thats nowere NEAR how I use it, and also I am so freaking insulted by the 2nd to last post up from here I feal I should not even post very much for my pure RAGE will get me kicked off of this board...

    SPH ...

  12. #12
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Whos post you talking about? Mine or novacains? B/C mine is true!
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  13. #13
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Sorensen is right on both counts.

    FALSE == zero
    TRUE != zero

    therefore -1 == TRUE (in bool terms, not false)

    and as to "self-tought", you should have consulted an expert or at least a spell checker.

    Post how you use PeekMessage() then.
    "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

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: Lolz...

    Originally posted by minime6696
    Well actually... thats nowere NEAR how I use it, and also I am so freaking insulted by the 2nd to last post up from here I feal I should not even post very much for my pure RAGE will get me kicked off of this board...

    SPH ...
    What are you complaining about now?

  15. #15
    Registered User
    Join Date
    Jan 2002
    Posts
    363
    I think he is refering to Sorensen kindly pointing out the invalidity of his argument: "A logical statement (such as 'if','while','for', ect.), I beleive, will take anything '<=0' as a false... "

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting Noticed - hypothetical
    By MadCow257 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-09-2005, 10:41 PM
  2. Ultra chess engine contest
    By yodacpp in forum Projects and Job Recruitment
    Replies: 8
    Last Post: 11-21-2004, 07:58 AM
  3. The DJGPP Syndrome... has anyone noticed it?
    By minime6696 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-27-2002, 08:00 AM
  4. Have you noticed a change in attitude?
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 05-26-2002, 09:41 PM
  5. Characters. Funny looking ones
    By Wiz_Nil in forum C++ Programming
    Replies: 8
    Last Post: 02-25-2002, 12:33 PM