Sign in to follow this  
khoadd

Realtime Main Loop Structure

Recommended Posts

khoadd    316
I saw many tutorials give main loop like this:
[CODE]
while(msg.message != WM_QUIT) {
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
// Update & Draw Code
}
}
[/CODE]
My question is :
When we press and hold a button, msg KEYDOWN will be sent to message queue repeatedly. So we always can pick a msg. There is no chance to come to "Update & Draw Code". So how can we update and draw when we hold down a button. Edited by KhoaVN

Share this post


Link to post
Share on other sites
mark ds    1786
You're correct that the message is sent repeatedly - but that isn't the same as continuously.

Whilst it may seem like a flood of messages to you and I, to a PC it's an occassional message to process, leaving a lot of spare time to 'update and draw'.



(too early on a saturday morning to spell!) Edited by mark ds

Share this post


Link to post
Share on other sites
khoadd    316
Thank Mark but i still miss sth:
In your example:
Inbox == message queue
1 email == 1 msg
checking == peeking

"once a day" : you limit a check in 1 time unit (1 day).
while "peek message" : it loops continuously. It means there will be more "peeks" in 1 time unit.

I still not find out blank time for "update and draw". [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img] Edited by KhoaVN

Share this post


Link to post
Share on other sites
Brother Bob    10344
The key repeat message is only sent on the order of 10 to 15 messages per second. If you can process the key down message in less than one tenth or one fifteenth of a second (or however many messages there are per second), the next time PeekMessage is called there won't be another key down message and you will proceed to update and draw. Update and draw will be reached until the next tenth of fifteenth of a second has passed and a new key down message is passed to your application.

Share this post


Link to post
Share on other sites
mark ds    1786
I just did a quick test measuring the number of times a WM_KEYDOWN was processed compared the the 'update and draw' section of code.

I got 182 WM_KEYDOWN messages processed, compared to 54,398 'update and draw'!!!



Scrap the above timings - I tried again with minimal updates in the main loop. I got the following:

153 WM_KEYDOWN vs 1,528,448 updates. Edited by mark ds

Share this post


Link to post
Share on other sites
mark ds    1786
[quote name='KhoaVN' timestamp='1341055485' post='4954281']

while "peek message" : it loop continuously. It means there will be more "peeks" in 1 time unit.
[/quote]

No - most of the time peekmessage will return fasle, allowing the rest of the code to execute.

Share this post


Link to post
Share on other sites
Cygon    1219
As others already said, the key repeats are few and far between for a computer -- they aren't generated by PeekMessage(), but by a microchip inside your keyboard or an internal timer in Windows/Linux.

Let's make the following assumption: Your game runs really slow, 0.1 fps (10 seconds from one frame to the next) and your keyboard is set really fast, like 30 repeats/second:
- Whilst the game is updating and drawing, 300 WM_KEYDOWN messages will enter the queue.
- Then the game processes all 300 messages. This takes somewhere around 0 milliseconds.
- Now the message queue is empty again, PeekMessage() returns FALSE and the game works on the next frame for 10 seconds

To actually stop your game from drawing and updating, someone would have to send hundreds of thousands of messages to your window, at least as fast as you can process them. Edited by Cygon

Share this post


Link to post
Share on other sites
khoadd    316
Thank you very much.
Thank Mark
Thank Bob
Thank Cygon
I figure out what it is now. Thank All.
I made a test follow your explanations and the result likes you guys said. Thanks :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this