PeekMessage & GetMessage

Started by
7 comments, last by Prozak 22 years, 7 months ago
Hi all, I''m reading LaMothe''s book "Trick of the Windows Game Programming Gurus", and this is what I''ve got to ask. Isn''t it better and cleaner looking to setup a thread as our Main coding area, and replace the peekmessage aproach by the getmessage one? Just a thought... Hugo Ferreira UniteK Future "Concentrate, and you can see it. If you see it, then it is possible. If it is possible, you can Achieve it."
Advertisement
PeekMessage I belive is best used for a game loop and I believe that it is faster, than GetMessage which you also use to get messages, But I think PeekMessage gives more control.

Eric Wright o0Programmer0o
AcidRain Productions
http://www.acidrainproductions.com
Eric Wright o0Programmer0o
K, I read up on PeekMessage and GetMEssage online. From what I have read PeekMessage is better because it''s more geared toward your exact application whether than windows. So I guess you coudl easily tell if someone alt-tabbed out of your app or something. But GetMessage is for it appears to be looking at EVERYTHING that goes through. Don''t take my word for any of this but these are just my thoughts on it.

http://www.sxlist.com/techref/os/win/api/win32/func/src/f67_6.htm

http://www.sxlist.com/techref/os/win/api/win32/func/src/f34_14.htm

Eric Wright o0Programmer0o
AcidRain Productions
http://www.acidrainproductions.com
Eric Wright o0Programmer0o
Hey I found the answer . At http://www.geocities.com/SiliconValley/Way/3390/dos2win.html

in the GetMessage vs PeekMessage what it says on the site is GetMessage waits for a message while PeekMessage doesn''t.

Eric Wright o0Programmer0o
AcidRain Productions
http://www.acidrainproductions.com
Eric Wright o0Programmer0o
You know, you can find all this info in the Windows Platform SDK (Online here: http://msdn.microsoft.com/library Download here: http://www.microsoft.com/msdownload/platformsdk/sdkupdate/ )

The easiest way to find out about a win32 API function is to have the Platform SDK documentation on your computer (i.e. downloaded) and then simply type the function name in the help index of the library. simple yo

Ian Halliday yoPWEENED
I believe you all got it wrong, I believe what pentium3id meant is to create a thread that handles messages, or make the message pump a thread, using the CreateThread function, then using GetMessage inside the thread.

I dont see how this might be cleaner thought, it involves creating the thread function, and all the init part, the code in Lamothe''s book is pretty good, does it its job, why change what is not broken? also, you dont want to deal with thread syncronization, and all that stuff for something so trivial, dont worry, on a later chapter Lamothe gives you good advice on using threads, just keep reading, its a great book in my opinion.
Of course, Microsoft COULD make thier code cleaner, but why? They never have to worry. As long as they know that there is no better language out there, why bother making it better? I agree, windows code is disgusting, Bill Gate''s can stick Visual C++ up his arse. If code wasnt so messy I would enjoy programming alot more. Windows code should be more like OpenGL.

Too bad I have to use it anyway(since, I don''t plan on changing operating systems any time soon, not to mention, all of the good books and tutorials are for windows programming )

"I''ve sparred with creatures from the nine hells themselves... I barely plan on breaking a sweat here, today."~Drizzt Do''Urden
------------------------------Put THAT in your smoke and pipe it
ok.
My OS is Win2000 professional, and i just did
a check on the code i'm doing.
Its a small game-common-functions-wrapper-kind-of-thing...
for personal use.
It does something processor intensive for 1 sec, then rests
for 3 secs (Sleep(3000); )

check these pics, i'll explain next:
cause there seems to be some problems displaying the pics, here are the links:
http://unitek3000.tripod.com/Images/PM.jpg
http://unitek3000.tripod.com/Images/GM.jpg
just paste them into your browser.





Ok, ok, what am I saying here?
I have 2 situations:
1)Using peekmessage in an infinite loop, that also
takes care of calling MainFunction (the game's main function)

or
2) Setting up a thread, called MainFunction (guess what this does), and using GetMessage, which only gets called by
windows from time to time.

I did the alterations and ran the completely equal programs
(except for getmessage and peekmessage, duh!), and
the graphics I got on my dual processor computer
can be seen above.

Peekmessage strains the processing flow by "grabing"
resources, naggin' windows, allways asking "Is there
something for me in the mail?".

If look at at the pictures, you can see that PM is allways
50% higher than GM.

I think u guys have some programming experience, so
try to see if there is some logic fault in my thinking here.

Hugo Ferreira
http://unitek3000.tripod.com

Edited by - pentium3id on September 5, 2001 9:53:09 PM
The problem is that your "Game Loop" does nothing.

What happens is that GetMessage() will block the thread until a message arrives. PeekMessage() returns immediately, whether there''s a message waiting or not.

The way I understand you have things set up, you''ve got two threads in both tests. What''s happening is that in the GetMessage() test, both your threads spend most of their time blocked, and the game loop only runs for a second every now and then (hence the spikes in your processor usage).

In the PeekMessage() test, you''ve got the message loop running all the time, so your processor usage is at 100% all the time (for that thread). Because you''ve got two processors, it seems to be switching between CPUs randomly, so it looks like both are at 50% all the time.

In a normal game, the game loop never blocks, so your processor usage is always at 100%. PeekMessage() is used at the end of the game loop to check if any messages have come through since the last loop. A game doesn''t usually recieve many messages (since you''re using DirectInput for input, so no WM_MOUSEMOVE messages and your screen is updating every loop, so no WM_PAINT messages) so the PeekMessage() call usually returns straight away without getting any messages. Having GetMessage() blocked in another is a bit of overkill, for the same reason.

So, your test is quite misleading - if you made the game loop busy (i.e. don''t block for 3 seconds with Sleep()) and put the PeekMessage() call after each iteration of the loop, you''d see no difference at all, both would run at 100% processor. You''d also see no difference in frame rate either, since a PeekMessage() call with no messages waiting is basically free.

codeka.com - Just click it.

This topic is closed to new replies.

Advertisement