System slows down when program runs

Started by
9 comments, last by TempusElf 21 years, 5 months ago
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++.
Advertisement
sleep(5) - try values ranging from 1 to 10, goodluck finding a number that works the same on all pc''s =\

--Fireking

Owner/Leader
Genetics 3rd Dimension Development
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
http://www.codeguru.com/system/IdleLoop.html this might help.


do your loop then doevents.
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
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.
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]
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.
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.
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.
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

This topic is closed to new replies.

Advertisement