Archived

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

TempusElf

System slows down when program runs

Recommended Posts

I''ve been experimenting with GDI for a while and now I''m trying to make a simple game that updates and renders even when there are no messages in the queue. Here is my main loop:
  
while (boolGameOn)
    {
        if (PeekMessage (&messages, NULL, 0, 0, PM_REMOVE))
        {
        	if (messages.message==WM_QUIT)
        	{ break;}
        TranslateMessage(&messages);
        DispatchMessage(&messages);
        }
        else
        {
            //This is where I update and render

        }
    }
  
So it runs and renders all fast but now my problem is that it seems to slow the rest of my system down a lot. For example, if I go to move the window, it waits a few seconds then moves. Or if I hit the ''X'' it waits a second and then closes. When my window moves, the programs underneath seem to have trouble repainting themselves. In VB, I would just through a DoEvents statement in there somewhere but I have no idea how to solve this problem in C++.

Share this post


Link to post
Share on other sites
hey,

Sorry, you want to speed your program up?

How do sleep or loops speed your system up?

Hmm.. my suggestion is to try some benchmarking to see whats going on inside your code.
(Not that i have any idea hwat the GDI is ..)

hope I was some help,

cheers,
Fonz

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just take out the ''else''. Your window is getting messages all the time. If you only do game stuff when there is no message then of course it is slowing down. Just process a message and then do game stuff and it should be much better.

Share this post


Link to post
Share on other sites
You're eating up too much CPU with that loop. PeekMessage() alone can be evil. You need to know when to use GetMessage() as well, and when to give up time slices (Sleep(0) hack/trick/feature?).

[edited by - aggregate on November 3, 2002 12:25:51 AM]

Share this post


Link to post
Share on other sites
umm...guys, this is the windows main loop, he goes out of the loop and the program exits. This is like standard stuff here.


1. He takes the else statement out, then the window just processes events, and doesn''t do the game part, because you have to go into a perpetual loop to do the message processing. Once you leave the program is over, so taking else out would just draw a blank window and call WndProc when a message is needed.That''s not what he wants to happen here. If no message is in the queue he wants the game to play.


2. If you use GetMessage() then the same thing applies. When GetMessage is called it sits there until a message is processed, thereby always being true cause it will just sit there. So you put GetMessage instead of PeekMessage you have no game loop.


3.You must have edited or something, cause I don''t see a Sleep call anywhere and everyone is saying it is in there, but if it''s only set at 10 milliseconds then that shouldn''t show too much.


your problem probably lies elsewhere I''m guessing. If windows aren''t repainting, you might check your WNDCLASSEX definition to see how you have that set up.

Share this post


Link to post
Share on other sites
Some very odd suggestions so far. =/

I have one suggestion that may or may not help. Right now you are only retrieving one message per game cycle from the message queue. If your game cycle is taking a long time, there may be lots of messages stacking up into the queue. Try putting PeekMessages in a while loop to make sure you get all the messages from the queue:

  
while( boolGameOn ) {
while( PeekMessage(...) ) {
// handle message

}
// do game stuff - render, AI, colldet, etc.

}


The issue with programs underneath having problems repainting themselves probably has nothing to do with this, its more likely that they''re being swapped to disk and you have to wait for them to swap back so they can redraw their window contents.

Unless you are using a system with cooperative threading or you are changing the priority of your main thread, using sleep() shouldn''t affect anything.

Share this post


Link to post
Share on other sites
I also use PeekMessage() function in my game loop (and I even use a while loop that reads all messages before updating the game) and there are no problems with system slowing down or other windows repainting, but I remember I had those problems, when there was a SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); call at the beginning of my program.

Share this post


Link to post
Share on other sites
Thanks for all the suggestions! it really helped me think about the problem... I''m going to try a few things later today... I have a lot of confidence in Z01''s suggestion... I''m starting to think that my main problem is that I''m relying on BitBlt to draw the whole window (600x400 pixels) and FillRect to clear an offscreen bitmap... maybe I should look for alternative methods or even start using a different graphics library

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This question seems to be one of the most commonly asked questions I''ve seen here. The problem itself is pretty obvious but the solution is rarely as obvious.

A game application is usually ment to run in full screen mode, so if it''s running in full screen it''s rare that you''ll use any other program on your system. This is the reason a common game loop looks like this :

while( !game.done())
{
}

As you can see this creates somewhat of an infinate loop that does not exit out until the game is over with. Now because there is no timer allowing for other processes to get some CPU time while the infinate loop is running your game will continue to eat the majority of CPU time, slowing the rest of your system down to a stand still. Now others have mentioned using a game timer. And although a game timer is great, it will not fix your problem because it doesn''t free up CPU time for other processes. The Sleep() function is a nice little function for use with these types of problems. However this function does not provide consistant results. It basically tells the other applications "Ok it''s your turn", but doesn''t keep a strict watch at how much CPU the other processes take up. This can effect your frame rate alot. The best solution to your problem is to "pause" your game if it is minimized or out of "focus" when in windowed mode. Durring this pause phase you can call Sleep() and give up as much CPU time to other processes as you need with out it adversely effecting your frame rate. And no one should notice a system wide slow down if the game''s full screen or currently in focus. Here is how you would do it.

while( !game.done() )
{
// translate and dispatch all your messages.

if( !game.paused() )
{
game.run();
}
else
{
// Check to see if user unpaused the game by keypress,
// maximizing the window, or getting the focus.

// if they did not.
Sleep(300);

}

}

Share this post


Link to post
Share on other sites