Skip to main content
gamedev.net
🔒 Locked

Best Message Loop Method

Started by Skute 1 reply 300+ views
Original Post
Skute
Skute
Hi, i want to know whats the best structure for message loops within your WinMain? Ive got this: PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) while(msg.message != WM_QUIT) { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { // Nothing todo so do the drawing stuff now } } which is quite nice and simple - but it still uses 100% CPU processing, i know this is because of the loop, the only way to get away from that would be to use GetMessage, but i dont want that because it hangs until a message is ready for dispatch. (I presume also thats why it doesnt properly change your mouse cursor when you come from outside a window and then hover over the inside? ie, if you can stretch the sides of your window with that kind of loop, when you bring the mouse across the side and into the main window, it wont change the mouse properly) Any light you can shed onto this area will be greatly received
Mark Ingramhttp://www.mark-ingram.com
Pyabo
Pyabo
Well, there are a few strategies here... the first to consider is that in your else block you could have some sleep() calls. Sleep(0) will give away the rest of the thread''s execution time slice. However, that may adversely affect your game timing. Experiment and find out.

Another way is to use GetMessage(), but have your game loop executing in a seperate thread so that the main execution thread doesn''t need to worry about blocking on GetMessage().

In general, it''s not a big deal to have your game using 100% CPU cycles, because this is just a busy-wait. Anything running in the background is still going to execute, and the user''s attention is really on the game. The only down side is more or less aesthetic. One thing you might consider is if the user switches away (WM_ACTIVATE I think?) or pauses the game, you should sleep() or change to GetMessage() so that the game stops eating 100% cycles, because obviously you don''t need to, and the user might be doing something else.
Skute
Skute
okies - thats great thanks

ill experiment with using GetMessage and creating a new thread in WM_CREATE

second question now:

which thread creation is better:

a) _beginthread()
b) CreateThread()

?

Petzold teaches beginthread as its simpler, but in the long run which will benefit more?

Regards
Mark Ingramhttp://www.mark-ingram.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.