Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Deuce

An empty GameLoop() takes 100% CPU Usage?

This topic is 5439 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Whoever read this book, "The Zen of Direct3D Game Programming" by Peter Walsh, or whoever knows a ''lot'' about game programming... ... are my applications supposed to take 100% CPU Usage? This is how the author lays out the structure of a game: int GameInit() { // initializations go here } int GameLoop() { // anything about the game-in-progress goes here } int GameShutdown() { //de-initialization } int WINAPI WinMain( ... ) { ... // window creation and stuff GameInit(); while( TRUE ) { if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { if ( msg.message == WM_QUIT ) break; TranslateMessage( &msg ); DispatchMessage( &msg ); } else { GameLoop(); } } GameShutdown(); } Now... just like that, with empty GameLoop(), the program, of course, takes 100% CPU Usage. Imagine if I put anything in GameLoop(), not only 100% CPU Usage, but also the computer is very slow in responding to anything I do. I''m just asking about the general structure. Is it supposed to be like that? Is there any function that I can pass control to Windows for a while, like VB''s DoEvents() or something. [edited by - Deuce on November 27, 2003 7:11:59 AM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
100% cpu usage does not necessarily mean slow performance. it just means that there is a process (or more) that''s always in the active state. call WaitMessage in the msg loop to put ur app to in a wait state until more messages come in.

Share this post


Link to post
Share on other sites
Your applications are not necessarily ''supposed'' to take 100% cpu usage, of course

The problem with this often used game loop is that there is always some processing to do: if there are no windows messages, GameLoop() is called. You can imagine that if GameLoop() doesnt actually do anything, it''s called a LOT, while in the case it does input handling, physics, AI and rendering, it may be called a lot less frequent. Like 80 times a second perhaps.

To prevent your application from using 100% processing power, you can limit the number of frames per second by checking whether enough time has passed since the last time GameLoop() was called, and do nothing or ''sleep'' a bit otherwise.

Share this post


Link to post
Share on other sites
You know the task bar at the bottom (WinXP here). The app starts off with a little animation at the task bar, the bar stretches out to reveal the caption of the window. That little animation, with this 100% usage, takes a lot more to load, and even more to animate. I tried to play a movie in the background, it took more than double the time to load to the extent that I got bored and turned off my app.

Now your suggestin with WaitMessage; let''s say I made a Tetris-clone, where would I put that WaitMessage? In GameLoop()? In the PeekMessage() loop? And what does it exactly do? Gives Windows some time to execute other apps?

Share this post


Link to post
Share on other sites
Sponzie,

I tried and made a Pause() function on my own. Just pass a number of milliseconds and it will while() that amount of milliseconds. But again, that while() loop is useless CPU usage. How do delay my app while at the same time give Windows the ability to execute other apps?

Share this post


Link to post
Share on other sites
Sleep(/*time in ms*/);// etc...
and waiting for messages (using WaitMessage) could be a stupid idea, when your app doesnt get a timer msg it could happen that it stops working till the next message arives...

T2k

Share this post


Link to post
Share on other sites
Well I realize that what I normally do is a quick hack that may not be the best way to go, but try putting in a sleep(x) call. I believe that during sleep() the OS can process other messages etc.


edit: this link may be interesting:
http://www.gamedev.net/reference/programming/features/winmode2/page4.asp

[edited by - Sponzie on November 27, 2003 7:45:50 AM]

Share this post


Link to post
Share on other sites
You guys are right. Sleep(0) was the best solution. It still takes 100%, but when I try to do something else (run WMPlayer or whatever) it''s a lot smoother as if I didn''t have a 100%-usage app in the back.


As far as this program I have is concerned (which constantly draws random GDI rectangles on my window with no pause whatsoever), Sleep(0) does the job. Somehow reduces the usage of my app and gives more time to other programs. But I''m thinking later, when I have a game that involves more than just 20 lines of code in GameLoop(), won''t Sleep(0) slow down my game? If it does, it''s unpredictable. You can''t tell how much it will be slowed down. Does anyone know what I mean? I don''t know, I''m still a beginner in game-programming techniques.


By the way, thanks for that last link -- helped!

Share this post


Link to post
Share on other sites
What i actually do is a sleep(x) where x is my frame interval - processing time, but you should be carefull that sleep has different lattencies for 95-98-xp-2k an even for faster/slower cpus (so i check what''s the minimum accurated sleep in a custom sleep function, take the averange all the time blahblahblah)
That allows you to do a nice tetris with 5 or 10% cpu usage on a p3 1gz (yes... time figures varies... gdi vs dx, etc etc)
Is ok to use 100% cpu on a game but my prefeered way is to release cpu if possible for the other applications that may be running (aka video playback comming from a different thread or process inside your game, an external mp3 player like winamp, etc)

-Mat

Share this post


Link to post
Share on other sites
This has to do with the way PeekMessage operates. What''s happening is that you are calling PeekMessage repeatedly during one time slice. Remember that a each active process is allocated a certain amount of time to execute. When that time is up, execution switches to the next active process.

Internally, if no messages are present in the message queue, PeekMessage Yields the to the next process, then when control comes back it returns NULL. Yield() is different than Sleep(), and has an impact on other processes that prevents them from getting their window messages. GetMessage actually puts the process to Sleep, which is why a normal Windows message loop doesn''t suffer the same problems you have with the typical game loop.

So by not calling Sleep(0), you are creating a situation where other applications are not getting their messages, thereby becoming sluggish. Sleep(0) alleviates that situation. Conversely, when you have a lot of processing going on during a time slice and PeekMessage is called less often, you don''t run into the problem. So with a standard game loop, you''re better off doing a lot of processing rather than very little.

To understand more, read this article.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!