# Framerate in SDL

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

## Recommended Posts

Can someone explain how I could create a framerate for display and how I would go about locking it?

##### Share on other sites
Take a look at my post here for the FPS function on generating the FPS. You will need to modify the line:

// Set the window title bar to our string    SetWindowText(g_hWnd, strFrameRate); // If you are not running fullscreen mode, you would change this line to one that would use DirectX fonts!

With
// Set the window title bar to our string   SDL_WM_SetCaption( strFrameRate, 0 ); // If you are not running fullscreen mode, you would change this line to one that would use SDL fonts!

I will let someone else explain the locking part.

- Drew

##### Share on other sites
Well if im not mistaken regarding locking the framerate there are two ways u can do it.

a) You can have ur application waste time itself.
b) Wait for vertical Sync from the monitor(display device).

Regarding a), you can simply put the application into a loop constantly checking the CPU clock. When the interval between the time at the end of the last frame and the current time equal the interval that you want you exit the loop.

With b), im not sure for SDL but im sure it is possible, ofcourse waiting for vertical sync ties you to the refresh rate of the monitor and so this is likely to be only set frequencies. Whereas with the above solution it can be whatever you want.

Hope this helps and no doubt someone can elaborate more on this...

ace

##### Share on other sites
I haven't had a lot of experience on framerate lock, but did a little experimenting a while back. I found the best thing to do was to have the sprites update according to a timer, either by interpolation if it's 3d and you can do that, or else by switching frames, and leave the the screen flip go as fast as it can. It will give the smoothest appearance yet still keep things timed.

##### Share on other sites
If you want to lock it in at a Fixed Frame Rate, the best way I've found is to record GetTicks, and add a value to it. At 30fps for example, you want to increase this value by 33, 33, and 34 (so you don't run at 31fps). Then you just perform an SDL_Delay every frame for the difference of the Next Update Time and the Actual Time. Of course, you'll have to perform some other math to handle stuff like slowdowns and frameskips. Heres some pseudocode, since I forget the spelling of some function names.

int nextframe;int framecount;int init_frame(){  nextframe = 0; /* First frame is a "Catch up." */  framecount = 3;}int flip_frame(){  int thisframe = SDL_GetTicks();  if (thisframe < nextframe) {    nextframe += 33;  } else {    nextframe = thisframe + 33; /* Should handle "Catch-ups" */  }  framecount-=1;  if (!framecount) {    framecount = 3;    nextframe += 1;  }  SDL_Delay( nextframe - thisframe );  SDL_Flip( my_surface );  return 0;}