simple SDL framerate?

Started by
29 comments, last by NicholasLopez 9 years, 9 months ago

I don't know anymore. I have a "good" delta timing system.


typedef struct g_timer {
	Uint32 start_ticks;
	Uint32 ticks;
	int paused;
} g_timer;

Uint32 timer_reset(g_timer *t) {
	t->ticks = 0;
	t->start_ticks = SDL_GetTicks();
	t->paused = 0;
 
	return t->start_ticks;
}
 
int timer_pause(g_timer *t) {
	t->paused = !(t->paused);
	t->ticks = SDL_GetTicks() - t->start_ticks;
	return t->paused;
}
	 
Uint32 timer_ticks(g_timer *t) {
	if (!(t->paused))
		t->ticks = SDL_GetTicks() - t->start_ticks;
	return t->ticks;
}

float dt = 0;

/* In the main process */
int main( int argc, char* args[] ) {
...everything that goes on...
timer_reset(&timer);
srand(timer.start_ticks);
Uint32 delta = 0;
Uint32 last_frame = SDL_GetTicks();
Uint32 last_run_time = 0;
	while(!quit) {
	...everything that goes on...
	timer_reset(&timer);
	...more stuff...
	delta = timer_ticks(&timer);
	dt = delta/1000.f;
	last_run_time = SDL_GetTicks() - last_frame;
            if( fps_capped == true )
    		if( last_run_time > 1000/updates_per_second ) {
			last_frame += last_run_time;
			refresh_screen();
		}
            else
                refresh_screen();
	}
...stop sdl...
return 0;
}

I think I copied everything correctly. I have a good timestep. It's mostly smooth minus the stutter a couple times per second (if fps is capped). When I tried it out on Windows 98 (just wanted to see if it would run at all), there were no tick issues at all. But there were tick issues in Windows 2000, XP, Vista, 7, and 8.1. Is there a more accurate way? I know some of you explain that vsynch will handle that, but I want to learn how to do it as well. I think the problem arises from that 1000/60 is actually 16.667 and last_run_time is usually 17 when it refreshes the screen (should the fps be capped).

This topic is closed to new replies.

Advertisement