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