FPS Counter?

Hi again! xD… my game’s progressing awesomely thanks to all of you :slight_smile:
hehe… o.o but now I wonder xD I’ve been through google, lazy foo’s
tutorials, Aaron’s SDL tutorials, and such, searching for an FPS counter
code and I haven’t found any, and in lazy foo’s site the code is too old and
it throws errors in SDL (it uses “timers”? which I supposed SDL had =/ ).
Going through google all I can find are pseudo-codes and theories, but no
example whatsoever :(. Does anybody here has a simple FPS counter that can
share please? ^^…

Thanks in advance :slight_smile:

  • DARKGuy

Hello DARKGuy,

Thursday, September 14, 2006, 6:24:53 PM, you wrote:

Hi again! xD… my game’s progressing awesomely thanks to all of you :slight_smile:
hehe… o.o but now I wonder xD I’ve been through google, lazy foo’s
tutorials, Aaron’s SDL tutorials, and such, searching for an FPS counter
code and I haven’t found any, and in lazy foo’s site the code is too old and
it throws errors in SDL (it uses “timers”? which I supposed SDL had =/ ).
Going through google all I can find are pseudo-codes and theories, but no
example whatsoever :(. Does anybody here has a simple FPS counter that can
share please? ^^…

It’s quite easy:

while (gamerunning)
{
Uint32 start_time, frame_time;
float fps;

  start_time = SDL_GetTicks();

  // do stuff
  SDL_Flip();

  frame_time = SDL_GetTicks()-start_time;
  fps = (frame time > 0) ? 1000.0f / frame_time : 0.0f;

}

The extra bit in the fps calculation stops a divide by zero should a
frame happen to take less than a millisecond (it’s not likely, but it
can happen)–
Best regards,
Peter mailto:@Peter_Mulholland

Hey! ^^

o.o yay! that works prefectly! :smiley: yeah, I heard about there could be some
divide-by-zero errors but no way how to avoid that one either, now I’m
getting the hang of it too, thanks! :smiley: :smiley: :DOn 9/14/06, Peter Mulholland wrote:

Hello DARKGuy,

Thursday, September 14, 2006, 6:24:53 PM, you wrote:

Hi again! xD… my game’s progressing awesomely thanks to all of you :slight_smile:
hehe… o.o but now I wonder xD I’ve been through google, lazy foo’s
tutorials, Aaron’s SDL tutorials, and such, searching for an FPS counter
code and I haven’t found any, and in lazy foo’s site the code is too old
and
it throws errors in SDL (it uses “timers”? which I supposed SDL had =/
).
Going through google all I can find are pseudo-codes and theories, but
no
example whatsoever :(. Does anybody here has a simple FPS counter that
can
share please? ^^…

It’s quite easy:

while (gamerunning)
{
Uint32 start_time, frame_time;
float fps;

  start_time = SDL_GetTicks();

  // do stuff
  SDL_Flip();

  frame_time = SDL_GetTicks()-start_time;
  fps = (frame time > 0) ? 1000.0f / frame_time : 0.0f;

}

The extra bit in the fps calculation stops a divide by zero should a
frame happen to take less than a millisecond (it’s not likely, but it
can happen)


Best regards,
Peter mailto:darkmatter at freeuk.com


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

No need to divide ot sth…

int fps = 0; int fpsTimer = 0; char *fpsText = new char[ 10 ];

bool running = true; do {

// handle input

int now = SDL_GetTicks();
if ( now > fpsTimer + 1000 ) // count fps in 1 sec (1000 ms)
{
sprintf( fpsText, “%d FPS”, fps ); fpsTimer = now; fps = 0;
}

// draw goes here
// blit fps text

SDL_Flip( display ); fps++;

} while ( running );–

qXy

DARKGuy . wrote:

Hey! ^^

o.o yay! that works prefectly! :smiley: yeah, I heard about there could be
some divide-by-zero errors but no way how to avoid that one either, now
I’m getting the hang of it too, thanks! :smiley: :smiley: :smiley:

Well, that division by zero might happen if your video card is able to
draw so fast the pc records zero time difference. It might happen more
frequently if you’re using an imprecise method such as using a timer
that produces a number of .1 seconds, etc…

And if it’s because of the hardware being too fast, can you tell me your
supplier? :wink:

Simon

DARKGuy . wrote:

Hi again! xD… my game’s progressing awesomely thanks to all of you :slight_smile:
hehe… o.o but now I wonder xD I’ve been through google, lazy foo’s
tutorials, Aaron’s SDL tutorials, and such, searching for an FPS counter
code and I haven’t found any, and in lazy foo’s site the code is too old
and it throws errors in SDL (it uses “timers”? which I supposed SDL had
=/ ). Going through google all I can find are pseudo-codes and theories,
but no example whatsoever :(. Does anybody here has a simple FPS counter
that can share please? ^^…

Thanks in advance :slight_smile:

  • DARKGuy


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

if you need computer-speed independet animations, then one definitely
should have a look at the article from www.gaffer.org:

http://www.gaffer.org/fix-your-timestep/

this article gave me some breakthroughs for my little projects. fixing
the timestep makes life so much easier…

Cool. I was just about to post this question, and then I find out it has
already been posted and answered! =DOn 9/15/06, Peter Mulholland wrote:

Hello DARKGuy,

Thursday, September 14, 2006, 6:24:53 PM, you wrote:

Hi again! xD… my game’s progressing awesomely thanks to all of you :slight_smile:
hehe… o.o but now I wonder xD I’ve been through google, lazy foo’s
tutorials, Aaron’s SDL tutorials, and such, searching for an FPS counter
code and I haven’t found any, and in lazy foo’s site the code is too old
and
it throws errors in SDL (it uses “timers”? which I supposed SDL had =/
).
Going through google all I can find are pseudo-codes and theories, but
no
example whatsoever :(. Does anybody here has a simple FPS counter that
can
share please? ^^…

It’s quite easy:

while (gamerunning)
{
Uint32 start_time, frame_time;
float fps;

  start_time = SDL_GetTicks();

  // do stuff
  SDL_Flip();

  frame_time = SDL_GetTicks()-start_time;
  fps = (frame time > 0) ? 1000.0f / frame_time : 0.0f;

}

The extra bit in the fps calculation stops a divide by zero should a
frame happen to take less than a millisecond (it’s not likely, but it
can happen)


Best regards,
Peter mailto:darkmatter at freeuk.com


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hello DARKGuy,

Thursday, September 14, 2006, 6:24:53 PM, you wrote:

Hi again! xD… my game’s progressing awesomely thanks to all of you
:slight_smile:
hehe… o.o but now I wonder xD I’ve been through google, lazy foo’s
tutorials, Aaron’s SDL tutorials, and such, searching for an FPS
counter
code and I haven’t found any, and in lazy foo’s site the code is too
old and
it throws errors in SDL (it uses “timers”? which I supposed SDL had =/
).
Going through google all I can find are pseudo-codes and theories, but
no
example whatsoever :(. Does anybody here has a simple FPS counter that
can
share please? ^^…

It’s quite easy:

while (gamerunning)
{
Uint32 start_time, frame_time;
float fps;

  start_time = SDL_GetTicks();

  // do stuff
  SDL_Flip();

  frame_time = SDL_GetTicks()-start_time;
  fps = (frame time > 0) ? 1000.0f / frame_time : 0.0f;

}

The extra bit in the fps calculation stops a divide by zero should a
frame happen to take less than a millisecond (it’s not likely, but it
can happen)


Best regards,
Peter mailto:darkmatter at freeuk.com


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Cool. I was just about to post this question, and then I find out it has
already been posted and answered! =D

Wait a min, one extra question:

How would you regulate the frame rate?On 9/22/06, Sahan Chandrasekara <@Sahan_Chandrasekara> wrote:

On 9/15/06, Peter Mulholland wrote:


Sahan Chandrasekara

Get a free sony psp at http://psps.freepay.com/?r=30890215 (not a scam)