PDA

View Full Version : Need help with a short & simple C function



crazyfuturamanoob
January 16th, 2009, 07:08 PM
Because I don't like complicated objects and stuff when it can be done with just one function,
I tried to convert this C++ thing into one neat C function: http://lazyfoo.net/SDL_tutorials/lesson14/index.php]

And so I got this function:


void tick( int framesPerSecond )
{
static int startTicks = -1;

// when this is called first time,
// save the current time for future use
if ( startTicks == -1 )
{
startTicks = SDL_GetTicks();
}
// when this is called the second time,
// calculate time elapsed and fps, and sleep the spare time
else
{
int time_passed = ( SDL_GetTicks() - startTicks );
SDL_Delay( (1000/framesPerSecond) - time_passed );
// reset startTicks to say it's first time again
startTicks = -1;
}

// not needed but I just like to type it
return;
}

I use that function like this in my main loop:


while ( ! done )
{
tick(30);

redraw_screen();
update_stuff();
blahblahblah

tick(30);
}


The program should stay at 30 fps, I tested it, and it seems to be working correctly.

I'd like to do this without SDL functions, with just standard C libraries, so I wouldn't need another unnecessary extra-dependency.

How would it look like when SDL_GetTicks() and SDL_Delay() are replaced with usleep and some other std stuff?

hod139
January 16th, 2009, 10:55 PM
http://ubuntuforums.org/showthread.php?t=296142

crazyfuturamanoob
January 17th, 2009, 01:24 PM
edit: posted into wrong thread