Jump to content
  • Advertisement
Sign in to follow this  

simple SDL framerate?

This topic is 1413 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

Assuming you're using SDL 2, look up these two functions:

  • SDL_GetPerformanceCounter
  • SDL_GetPerformanceFrequency

This will give you a high resolution timer you can use to calculate how much time has ellapsed since the last time you checked. With some math you can turn that into frames per second (just make sure to not discard frames that haven't fully ellapsed yet).

Share this post


Link to post
Share on other sites

Not really, if I recall correctly there were timers but their resolution was like 1ms (and the documentation warned that it was usually less), that may be enough for framerate if you account for rounding errors I guess.

 

SDL 1.2 is deprecated and not maintained anymore though, you're better off avoiding it if possible. SDL 2.0 is actively maintained and supports modern platforms (including mobile), and fixes several issues that 1.2 used to have. It also has functions to render stuff using the GPU (SDL 1.2 couldn't do that). The biggest loss is lack of CD Audio support, but does anybody still use that for games?

Edited by Sik_the_hedgehog

Share this post


Link to post
Share on other sites

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.

 

I just read LazyFoo's code. His LTimer class is a VERY simple POD type, you should be able to just slap up a C struct and rewrite the member functions as global functions that take a LTimer * parameter, and you're good to go.

Share this post


Link to post
Share on other sites

isn't that what I did in the pastebin?

 

No, it's not. What you did was declare the member variables as global variables and have the global functions manipulate them, treating them like static members of a class. That won't work because in LazyFoo's original code, the member variables are not static at all - each instance of the class has its own member variables, and there are two instances of Timer in main().

Share this post


Link to post
Share on other sites

Well i'm lost here. It was alot easier making a fps limiter and deltatimer in Allegro than it is in SDL. I know in the long run it'll come out good, but i'm really lost on this.

Edited by DrNicholas

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!