Hello, I have been doing alot of stuff in Allegro, I want to learn about SDL. I have been having a hard time getting the right type of framerate counter in my SDL program. This is what I did in Allegro:
/* Framerate Processes */
volatile int ticks = 0;
void ticker()
{
ticks++;
}
END_OF_FUNCTION(ticker)
volatile int game_time = 0;
void game_time_ticker()
{
game_time++;
}
END_OF_FUNCTION(game_time_ticker)
const int updates_per_second = 60;
/* This is the deltatime, used for timing */
double dt;
inside the main function in my Allegro program, I did this (code has been butchered so you see things related to framerate):
int main(void) {
/* Initialize Allegro */
allegro_init();
install_timer();
/* Initialize Framerate */
LOCK_VARIABLE(ticks);
LOCK_FUNCTION(ticker);
install_int_ex(ticker, BPS_TO_TIMER(updates_per_second));
LOCK_VARIABLE(game_time);
LOCK_FUNCTION(game_time_ticker);
install_int_ex(game_time_ticker, BPS_TO_TIMER(10)); /* The game is 1/10 seconds */
int fps = 0;
int frames_done = 0;
int old_time = 0;
int frames_array[10];
int frame_index = 0;
int ii;
for(ii = 0; ii < 10; ii++)
frames_array[ii] = 0;
/* Other stuff*/
other_stuff();
/* Run Game Loop */
while ( quit == false ) {
while(ticks == 0) {
rest(1);
}
while(ticks > 0)
{
int old_ticks = ticks;
ticks--;
if(old_ticks <= ticks)
break;
}
if(game_time >= old_time + 1)
{
fps -= frames_array[frame_index];
frames_array[frame_index] = frames_done;
fps += frames_done;
frame_index = (frame_index + 1) % 10;
frames_done = 0;
old_time += 1;
}
/* do stuff */
do_stuff();
/* set dt */
render_var( 16, 184, TEXT_LEFT, "FPS:", fps, true );
dt = 1.0f/(updates_per_second*1.0f);
frames_done++;
}
/* Finish up */
return 0;
}
END_OF_MAIN()
I cannot for the life of me figure out how to do the equivalent in SDL, every attempt (that i've deleted as well) always came down to using SDL_Delay(1000/framerate) in the end, and while the speed seemed good, every 1 second the game would stop for one frame, and that really bothered me.
I've spent a couple days looking for a simple SDL framerate example, haven't been able to find any. If you can point me in the right direction (not lazyfoo's, i've tried his, and he is C++ whereas I want to do it in C), that would be great, thanks.