Skip to main content
gamedev.net
🔒 Locked

Loop question

Started by DreamC Sep 2, 2008 at 3:18 PM 5 replies 700+ views
Original Post
DreamC
DreamC
I'm a beginner in this field of coding, but i have some experience in coding overal. All i need atm is to create a simple application, it mainly needs to output some data (for example i want to test it with a radar type application - i have 10+ moving objects and a field where they are drawn with GDI+). All i've come to - i have a GetMessage loop for handling some user input. So the question is - how is it better to calculate logic and render the screen? Should i implement some timers or there is another way? As i have already tried to put logic\render code into message loop using PeekMessage, but because of that app consumes 100% CPU - that's just awfull... Guide me please ;)
Cornstalks
Cornstalks
Quote:
Original post by DreamC
As i have already tried to put logic\render code into message loop using PeekMessage, but because of that app consumes 100% CPU - that's just awfull...


What's wrong with using 100% of the CPU? All that means is that there aren't other programs that need the CPU, so why not let your program used the unused CPU? If you really feel like you need to limit the CPU, you can put Sleep() in the main loop or enable vertical synchronization (vsynch). I haven't used GDI+, but it probably has some vsynch options. Timers would work to, though without the Sleep()/vsynch function they'll still use 100% of the CPU.

One thing some people do with timers is render as quickly as the program is running, but only update every 16ish milliseconds.

[Edited by - MikeTacular on September 2, 2008 5:12:57 PM]
Spoonbender
Spoonbender
Quote:
Original post by MikeTacular
Quote:
Original post by DreamC
As i have already tried to put logic\render code into message loop using PeekMessage, but because of that app consumes 100% CPU - that's just awfull...


What's wrong with using 100% of the CPU? All that means is that there aren't other programs that need the CPU, so why not let your program used the unused CPU?

Just to elaborate on this a bit, think about how the OS runs processes. It has a timer internally, and every once in a while, it pauses the currently running process, and picks another one to run. If you have N processes running on your computer, you only have a "right" to 1/N of the CPU time (assuming all processes run at the same priority). The only way you can get more CPU time than that is if no one else wants it. If all the other processes instantly go back to sleep as soon as they get a slice of CPU time, it'll be your turn faster. You use up your entire timeslice, and then the OS goes round again. You get 100% CPU time not because your app is "greedy" or "behaving badly", but because it's available, because no other process needs more CPU time than they get already.

So using 100% CPU is not in itself a bad thing. In games it is often desirable (because if you use all available CPU, you get the highest possible framerate), and you're not hurting other processes. They're first in line to take their share of the CPU time, after all. You're only eating the leftovers, so to speak.

However, *if* you want to limit CPU usage, there are several ways to do it.
One is to use GetMessage instead of PeekMessage. Then your app gets blocked on the message loop until messages are available for you to act on. Most applications (not games) do this.
Alternatively, you can call Sleep() somewhere in your main loop. Sleep(0) tells the OS that you're giving up the rest of your current timeslice, so it can move on to running the next process in line, but without skipping you the next time around. Called with any other number, it tells the OS to skip your process for (at least) that many milliseconds. In your case, the former is probably the most appropriate.

But ask yourself if and why 100% CPU usage is unacceptable.
DreamC
DreamC
As i see it - i dont need peekmessage in the loop - i dont have to trace mouse or smt like that - l\rbutton is enough for me.

But with the getmessage loop i have to do smth with logic\render calculations.

100% cpu usage may be accepted when its a full-screen game ot u have nothing else but hte game. But my app is not intended as a main app you run - it will just stay somewhere on the screen and show some results. Also 100% cpu is a problem for notebooks as it will run out of battery more quick.

I tried PeekM with sleep - its alll the same.

Is there any other way to run logic 100 times a sec and render picture 20-60 times?
DreamC
DreamC
Found as i think the thing i need - MsgWaitForMultipleObjectsEx.
As i see using it i will be able to handle messages as soon as they appear and do some timer'ed duties. Also it souldnt consume cpu while in wait state.

Any info about that thing? Or other suggestions?

Thanks beforehand.
Cornstalks
Cornstalks
Quote:
Original post by DreamC

I tried PeekM with sleep - its alll the same.


What did you use as your sleep value? If you called Sleep(16) each time in your loop, your program would not be able to run over 62.5 frames per second. It should also use a fraction of the CPU. I just ran a simple test program with a while (true) loop that just called Sleep(16), and the Windows Task Manager never showed the program using more than 1% of the CPU.
DreamC
DreamC
Well, frankly i dont like the idea with Sleep at all, as Messages should be handled immediately and the problem with synhronization of 60 hz render and 100 hz logic.

Anything about MsgWaitForMultipleObjectsEx

Topic Locked

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

Sign in to reply to this topic.