High cpu usage

My game uses 100% of cpu, and I was was wondering how can I lower it. The
game isn’t complex. My main loop looks like this:

while(tskmgr->running)
{
tskmgr->update();
tskmgr->draw();
}

thanks,–
Adam Stevenson

Are you trying to manage framerate? Or are you just plowing ahead at
whatever speed the OS allows you?–
Lilith

On 7/9/2007 at 5:08 PM, “Adam Stevenson” wrote:
My game uses 100% of cpu, and I was was wondering how can I lower it.
The
game isn’t complex. My main loop looks like this:

while(tskmgr->running)
{
tskmgr->update();
tskmgr->draw();
}

thanks,

“Are you trying to manage framerate? Or are you just plowing ahead at
whatever speed the OS allows you?”

Right now full speed. I am wondering about some of the popular methods of
managing the frame rate if that would lower the cpu usage.–
Adam Stevenson

Drawing is intensive, updating i guess is less.
So it is looping all the time through an intensive process. Slow it
down and cpu usage will decrease, as well as frame rate.

You have to make a deal here and accept a certain level of fps. 200
might not be necessary if you see what i mean, try lowering below 60
or 30.

Though, don’t put static values for the wait, as they will only work
on your pc…
A google on “sdl framerate” should yield some good examples…

SimonOn 7/9/07, Adam Stevenson wrote:

My game uses 100% of cpu, and I was was wondering how can I lower it. The
game isn’t complex. My main loop looks like this:

while(tskmgr->running)
{
tskmgr->update();
tskmgr->draw();
}

thanks,


Adam Stevenson


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

It’s just a tetris game, so frame rate doesn’t need to be high. Is the most
popular solution to throw an SDL_Delay in there?–
Adam Stevenson

That yields to the OS. It’s a good idea, versus a tight while() loop :)On Mon, Jul 09, 2007 at 05:27:54PM -0500, Adam Stevenson wrote:

It’s just a tetris game, so frame rate doesn’t need to be high. Is the most
popular solution to throw an SDL_Delay in there?


-bill!
bill at newbreedsoftware.com
http://www.newbreedsoftware.com/

The solution i use is to take the precise time (SDL_Ticks) at the
start of the loop, do all your operations, then at the end check how
much time that took. Say you want 30 fps, the operation cant take
more than 1/30 = 33ms. So if your operation took 6 ms, you need to
wait 27ms. Simple, but not super precise. You can also do a loop to
wait some ms over and over until you have reached the 33ms.

Basically, the sleep says to the cpu: “I won’t work for that amount of
time” and work = cpu usage. So anything else than sleep, for the pc,
is work.

SimonOn 7/9/07, Adam Stevenson wrote:

It’s just a tetris game, so frame rate doesn’t need to be high. Is the most
popular solution to throw an SDL_Delay in there?


Adam Stevenson


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Well, sort of… Games that need smooth animation will try to get the
highest frame rate possible, but preferably locked to the display
refresh rate. SDL will try to give you this if you ask for a double
buffered hardware display surface, but unfortunately, many platforms
and drivers have retrace sync disabled on the driver level by
default. Not much an application can do about that, usually - and
it’s really something that should be configured by the user anyway.
(The only real error here is the stupid default configurations…)

So, what you do is either just allow the game to run at full speed, or
you throttle the frame rate by looking at SDL_GetTicks() and
adjusting with SDL_Delay() as needed to maintain the desired frame
rate. Both methods have issues, though. Running at full speed will
have your game considered a CPU hog, and as a result, any background
process will get the CPU for extended periods of time whenever it
decides to do something. Using frame rate throttling may result in
more annoying tearing artifacts (if the frame rate lands to close to
the display refresh rate), and less smooth animation if the frame
rate is lower than the refresh rate.

Now, in your case, it might be a better idea to do it more
like “normal” GUI applications; ie update only when things change.
For games that only update a few times per second, and/or as a direct
response to user input, this is a simple and effective solution, and
doesn’t need any tuning (throttling frame rate selection etc) to work
as intended.

//David Olofson - Programmer, Composer, Open Source Advocate

.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --'On Tuesday 10 July 2007, Adam Stevenson wrote:

It’s just a tetris game, so frame rate doesn’t need to be high. Is
the most popular solution to throw an SDL_Delay in there?

Adam,
use SDL_Delay(x) to insert “pauses” in your code…(after the drawing routine is usually a good spot.) This will return control back to the OS, and reduce CPU usage considerably.
x can be any value in milliseconds you like, but 10 is a good starting place. THere’s no reason to run your game at >100FPS anyway… at least not that I can see off-hand.
-Dave O----- Original Message -----
From: Adam Stevenson
To: SDL at lists.libsdl.org
Sent: Monday, July 09, 2007 5:08 PM
Subject: [SDL] High cpu usage

My game uses 100% of cpu, and I was was wondering how can I lower it. The game isn’t complex. My main loop looks like this:

while(tskmgr->running)
{
tskmgr->update();
tskmgr->draw();
}

thanks,


Adam Stevenson



SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Well, my monitor /can/ do 1024x768 at 200Hz, but that probably is just a
silly value :smiley: looks at motion-blur shadersOn Mon, 2007-07-09 at 17:48 -0500, David Olsen wrote:

Adam,
use SDL_Delay(x) to insert “pauses” in your code…(after the drawing
routine is usually a good spot.) This will return control back to the
OS, and reduce CPU usage considerably.
x can be any value in milliseconds you like, but 10 is a good
starting place. THere’s no reason to run your game at >100FPS
anyway… at least not that I can see off-hand.


To help you stay safe and secure online, we’ve developed the all new Yahoo! Security Centre. http://uk.security.yahoo.com

Thanks for the help guys. Now that I got gameplay down and this timing
issue resolved, you guys will be playing soon, don’t worry.–
Adam Stevenson

tetris should only need to update the changes, like when a block is
moving down or a line removed. you only need to change very little.
and if its a slow level you only need to update every second, more if
its a faster level.

perhaps try this http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fAddTimer

mattOn Mon, 9 Jul 2007 17:27:54 -0500 “Adam Stevenson” wrote:

It’s just a tetris game, so frame rate doesn’t need to be high. Is
the most popular solution to throw an SDL_Delay in there?

what platform(s) are you supporting ? and what is the license ?On Mon, 9 Jul 2007 19:57:37 -0500 “Adam Stevenson” wrote:

Thanks for the help guys. Now that I got gameplay down and this
timing issue resolved, you guys will be playing soon, don’t worry.

I only have windows to test it on, but I am using regular SDL, so I am
assuming supporting other platforms would just be a matter of compiling it.
I haven’t decided on a license yet.–
Adam Stevenson

well, if you post the source, i can test on linux and osx if you like,
and if its not to large (many source files) maybe write a makefile.

what is regular sdl ?

/me wonders if he has irregular sdl

/me hates the abuse of regular

mattOn Mon, 9 Jul 2007 21:54:21 -0500 “Adam Stevenson” wrote:

I only have windows to test it on, but I am using regular SDL, so I am
assuming supporting other platforms would just be a matter of
compiling it. I haven’t decided on a license yet.

Alexi Romanoff would have a fit if I did it any other way. I meant the only
library I am using is SDL. I still have to add the highscore table and
sound effects though. Matt, I will post the source code when it’s ready,
and thanks for offering to help. Any other tips on releasing SDL projects?–
Adam Stevenson

Hi
I had the same problem and i found this on the sdl mailing list , so In
your main loop add somthing like this

this_tick = SDL_GetTicks();
if ( this_tick < next_tick )
{
SDL_Delay(next_tick-this_tick);
}
next_tick = this_tick + (1000/FRAMES_PER_SEC);
SDL_Flip(gScreen);

Trish x

In your main loop add somthing like this

this_tick = SDL_GetTicks();
if ( this_tick < next_tick )
{
SDL_Delay(next_tick-this_tick);
}
next_tick = this_tick + (1000/FRAMES_PER_SEC);
SDL_Flip(gScreen);

Trish x

Alright yeah, thanks for the code.–
Adam Stevenson

"I made a Tetris game using SDL for the PC Windows platform."
Yeah, it looks really good. The reason I am making my version is because no
free tetris clones (that I could find) had features of modern versions, and
I just want to play the game.–
Adam Stevenson

"What “modern” features are you looking for in a Tetris game?"
Hold, tspins, a good interface.–
Adam Stevenson