SDL basic application consumes about 50% CPU (Linux)

Any idea? :frowning:

Use SDL_Delay to free cpu…

Sepho a ?crit :> Any idea? Sad



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

Try usinf SDL_Sleep(2); /* I dont recall the correct name though */ to avoid
ofuscating the cpu…It worked for me-----
Vladimir

SDL_Delay was the correct one ^-^-----
Vladimir

:slight_smile:
I was 50% too (in fact I guess you’re 100% of a dual core cpu) and I
manage to get something like 1.5% with SDL_Delay!!!

Vladimir a ?crit :>

SDL_Delay was the correct one ^-^


Vladimir



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

Thanks all!

I need ti use that function inside while loop, is it?

Thanks!

Yes, definitely.
And you can use its parameter to control frame rate too…

Sepho a ?crit :> Thanks all!

I need ti use that function inside while loop, is it?

Thanks!


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

Thanks!

AKHRES Nader wrote:> Yes, definitely.

And you can use its parameter to control frame rate too…

Sepho a ?crit :

Thanks all!

I need ti use that function inside while loop, is it?

Thanks!


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


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

AKHRES Nader wrote:

Yes, definitely.
And you can use its parameter to control frame rate too…

Hey could you explain about the framerate…I noticed that a while ago but
I’m not even close to complete understanding

Sepho…I noticed that your gui is in spanish…hablas espa?ol?

Thanks!-----
Vladimir

Vladimir wrote:

Sepho…I noticed that your gui is in spanish…hablas espa?ol?

Thanks!

Vladimir

Yep! I’m spanish [Laughing]

Hello from France, spanish boys!

while(ESCAPE_CONDITION_HERE)
{
//draw using sdl
SDL_Delay(1000/FPS_WANTED); //this will wait but free time will be
given back to OS (it will use it to run other process)

}

Hope this helps.

Nad

Sepho a ?crit :>

Vladimir wrote:

Sepho…I noticed that your gui is in spanish…hablas espa?ol?

Thanks!

Vladimir

Yep! I’m spanish Laughing


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

Well, the issue here is that it will only ever be ‘FPS_WANTED’ at the
maximum. Depending on what’s going on in the while loop, you’ll probably
be slower than the desired FPS.

What I do, when programming it this way is: (1) capture the current
time (SDL_GetTicks()), (2) do all of the other stuff in my while loop
(game logic, event handling, screen updates), (3) do an SDL_Delay() of
what’s left.

That is, if I took 1/60th of a second (16ms) to do all my work, and I want
my framerate to be 50fps, I need to SDL_Delay() for 4ms, not 20ms!!!

do
{
then = SDL_GetTicks();

/* ... do everything ... */

now = SDL_GetTicks();
if (now < then + (1000 / FPS))
  SDL_Delay(now - then + (1000 / FPS));

}
while (!done);

Of course, also keep in mind that SDL_Delay() has granularity problems,
it’s hard to sync with VBLANK, etc. etc. All that stuff I happily ignore.

-bill!On Tue, Sep 22, 2009 at 09:37:23PM +0200, AKHRES Nader wrote:

Hello from France, spanish boys!

while(ESCAPE_CONDITION_HERE)
{
//draw using sdl
SDL_Delay(1000/FPS_WANTED); //this will wait but free time will be
given back to OS (it will use it to run other process)

}

Hello Bill,

you’re right, I’ve just simplified to show the SDL_Delay usage…
Please note correct waiting (=remaining time) is “then + (1000 / FPS) -
now” according to your code.

About SDL delay, it’s said to be 10 ms granularity in SDL doc due to OS
granularity…
I’ve started to use both SDL_Delay ((remaining_time/10) *
10)+SDL_GetTicks (remaining_time% 10) for the waiting… but my
experiment shows SDL_Delay alone is enough and I don’t need very precise
frame rate in my 2D application use case…
This way I get the less cpu usage… and I’ve tried both HW or SW surfaces.

If anyone I’ve a better way to deal with frame rate, let us know…

Nad

Bill Kendrick a ?crit :> On Tue, Sep 22, 2009 at 09:37:23PM +0200, AKHRES Nader wrote:

Hello from France, spanish boys!

while(ESCAPE_CONDITION_HERE)
{
//draw using sdl
SDL_Delay(1000/FPS_WANTED); //this will wait but free time will be
given back to OS (it will use it to run other process)

}

Well, the issue here is that it will only ever be ‘FPS_WANTED’ at the
maximum. Depending on what’s going on in the while loop, you’ll probably
be slower than the desired FPS.

What I do, when programming it this way is: (1) capture the current
time (SDL_GetTicks()), (2) do all of the other stuff in my while loop
(game logic, event handling, screen updates), (3) do an SDL_Delay() of
what’s left.

That is, if I took 1/60th of a second (16ms) to do all my work, and I want
my framerate to be 50fps, I need to SDL_Delay() for 4ms, not 20ms!!!

do
{
then = SDL_GetTicks();

/* ... do everything ... */

now = SDL_GetTicks();
if (now < then + (1000 / FPS))
  SDL_Delay(now - then + (1000 / FPS));

}
while (!done);

Of course, also keep in mind that SDL_Delay() has granularity problems,
it’s hard to sync with VBLANK, etc. etc. All that stuff I happily ignore.

-bill!


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

I think this has been discussed a bit before, so you might find
something in the archives. I always just suggest that you measure (or
assume) the granularity of SDL_Delay, then when you’re in your loop,
you subtract that from the amount you would normally delay (as per
Bill’s message) and then burn the rest of the time in an empty while
loop.

You’re in the majority, though, when you say that it’s not really
necessary for your purposes. I usually just do it the ‘Bill’ way.

Jonny DOn Tue, Sep 22, 2009 at 5:09 PM, AKHRES Nader <nader.akhres at laposte.net> wrote:

Hello Bill,

you’re right, I’ve just simplified to show the SDL_Delay usage…
Please note correct waiting (=remaining time) is ?“then + (1000 / FPS) -
now” according to your code.

About SDL delay, it’s said to be 10 ms granularity in SDL doc due to OS
granularity…
I’ve started to use both SDL_Delay ((remaining_time/10) * 10)+SDL_GetTicks
(remaining_time% 10) for the waiting… but my experiment shows SDL_Delay
alone is enough and I don’t need very precise frame rate in my 2D
application use case…
This way I get the less cpu usage… and I’ve tried both HW or SW surfaces.

If anyone I’ve a better way to deal with frame rate, let us know…

Nad

Bill Kendrick a ?crit :

On Tue, Sep 22, 2009 at 09:37:23PM +0200, AKHRES Nader wrote:

Hello from France, spanish boys!

while(ESCAPE_CONDITION_HERE)
{
? //draw using sdl
? SDL_Delay(1000/FPS_WANTED); //this will wait but free time will be
given back to OS (it will use it to run other process)

}

Well, the issue here is that it will only ever be ‘FPS_WANTED’ at the
maximum. ?Depending on what’s going on in the while loop, you’ll probably
be slower than the desired FPS.

What I do, when programming it this way is: ?(1) capture the current
time (SDL_GetTicks()), (2) do all of the other stuff in my while loop
(game logic, event handling, screen updates), (3) do an SDL_Delay() of
what’s left.

That is, if I took 1/60th of a second (16ms) to do all my work, and I want
my framerate to be 50fps, I need to SDL_Delay() for 4ms, not 20ms!!!

?do
?{
? ?then = SDL_GetTicks();

? ?/* … do everything … */

? ?now = SDL_GetTicks();
? ?if (now < then + (1000 / FPS))
? ? ?SDL_Delay(now - then + (1000 / FPS));
?}
?while (!done);

Of course, also keep in mind that SDL_Delay() has granularity problems,
it’s hard to sync with VBLANK, etc. etc. ?All that stuff I happily ignore.

-bill!


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


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

Hi,

A few weeks ago I wrote a little function for just this purpose - it
basically just uses SDL_Delay() in order to exit a minimum of
"loop_msec" milliseconds after it exited the previous time it was
called, using static Uint32s to keep the timing data between
invocations. To use it, just throw one in at the end of the loop to
be throttled:

To cap loop rate at 100 per sec:

while(!done)
{
//do stuff, draw, etc…

Throttle(10);
}

Here’s the function - feel free to use it anyway you wish. Also, if
anyone spots anything I did wrong, please let me know. It is modified
from Bill’s method just by using the time at the end of the previous
loop instead of the time at the beginning of the current loop (should
be same within resolution of timer), and by sticking it into a
function.

void Throttle(int loop_msec)
{
static Uint32 now_t, last_t; //These will be zero first time through
int wait_t;

//Target loop time must be between 0 and 1000 msec:
if(loop_msec < 0)
loop_msec = 0;
if(loop_msec > 1000)
loop_msec = 1000;

//See if we need to wait:
now_t = SDL_GetTicks();
if (now_t < (last_t + loop_msec))
{
wait_t = (last_t + loop_msec) - now_t;
//Avoid problem if we somehow wrap past uint32 size (at 49.7 days!)
if(wait_t < 0)
wait_t = 0;
if(wait_t > loop_msec)
wait_t = loop_msec;
SDL_Delay(wait_t);
}
last_t = SDL_GetTicks();
}

Cheers,

David Bruce

If anyone I’ve a better way to deal with frame rate, let us know…

i am using SDL_WaitEvent and a timer for my nes emu right now. right now
it works, but not perfected. also you can run into problems if the code
takes too long per frame, you have to check for that.

matt

I used to do that…In my current project I changed that to the way in the
"game coding complete" book. It basically runs all the time just using a
delay funcion with a very low value to avoid consuming 100% of the CPU…and
then using the pretty much the same logic already posted to calculate the
"when to draw"…If you want I can post some code (I’m not in my home right
now)

I’m using this in my current project and it runs fine…but sometimes I
notice a few “bumps” (1 or 2 px max) in the movement. I dont know if its the
framerate control code or the movement code (or both >_<) its not very
noticeable at first but I’m kind of a perfectionist…anyway I will post my
game soon -or so I hope- so that you can dissect it.-----
Vladimir

I’m interested to see both (frame rate control code+your game)!

Vladimir a ?crit :> I used to do that…In my current project I changed that to the way in

the “game coding complete” book. It basically runs all the time just
using a delay funcion with a very low value to avoid consuming 100% of
the CPU…and then using the pretty much the same logic already posted
to calculate the “when to draw”…If you want I can post some code
(I’m not in my home right now)

I’m using this in my current project and it runs fine…but sometimes
I notice a few “bumps” (1 or 2 px max) in the movement. I dont know if
its the framerate control code or the movement code (or both >_<) its
not very noticeable at first but I’m kind of a perfectionist…anyway
I will post my game soon -or so I hope- so that you can dissect it.


Vladimir


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

I got bumps in the framerate as well, in only one of my projects.
When I did what I mentioned last here (calculating SDL_Delay
resolution and burning time in an empty loop), that went away
completely. Though if you’re looking for a better solution, I think a
fixed framerate might help. Check out David Olofson’s Fixed Rate Pig:
http://olofson.net/mixed.html

Jonny DOn Tue, Sep 22, 2009 at 10:29 PM, Vladimir wrote:

I used to do that…In my current project I changed that to the way in the
"game coding complete" book. It basically runs all the time just using a
delay funcion with a very low value to avoid consuming 100% of the CPU…and
then using the pretty much the same logic already posted to calculate the
"when to draw"…If you want I can post some code (I’m not in my home right
now)

I’m using this in my current project and it runs fine…but sometimes I
notice a few “bumps” (1 or 2 px max) in the movement. I dont know if its the
framerate control code or the movement code (or both >_<) its not very
noticeable at first but I’m kind of a perfectionist…anyway I will post? my
game soon -or so I hope- so that you can dissect it.


Vladimir


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