int main(int argc,char *argv[])
{
if( initialization() == false )
{
return 1;
}
//Background Of Game
SDL_Surface *background = NULL;
background = load_image("background.jpg");
//Apply Background
apply_surface(0,0,background,screen);
//Apply Message
//Update The Screen
if(SDL_Flip(screen) == -1)
{
returnfalse;
}
//Start Update Timer
fps_upd.start();
//Start FPS_Calc
fps_calc.start();
//Start FPS_Reg
fps_reg.start();
while(quit == false)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
//Quit the program
quit = true;
}
}
//Increment Frame
frame++;
//Regulate Frame Rate
if(fps_reg.get_ticks() < ( 1000 / FRAMES_PER_SECOND ) )
{
//Sleep The Remaining Frame Time
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps_reg.get_ticks() );
}
//If a second has passed since the caption was last updated
if( fps_upd.get_ticks() > 1000 )
{
//The frame rate as a string
std::stringstream caption;
//Calculate the frames per second and create the string
caption << "Average Frames Per Second: " << frame / ( fps_calc.get_ticks() / 1000 );
//Reset the caption
SDL_WM_SetCaption( caption.str().c_str(), NULL );
//Restart the update timer
fps_upd.start();
}
fps_reg.start();
}
clean_up();
return 0;
}
I've deleted the SDL_Delay above,and now I regulated the frame rate.
I set constint FRAMES_PER_SECOND = 20; and now it's runnning at 20fps (seems fine).
Plz check at the code and check for any error.If everything's fine,I'd go to the next step.
I would recommend using the hi-res timer for your platform, or if you're going for multi-platform, making a wrapper around platform specific hi-res timer functionality.
Windows has QueryPerformanceFrequency and QueryPerformanceCounter you can use to calculate FPS and the time it takes to render a scene.
Linux has clock_getres and clock_gettime.
From either, you should be able to calculate the performance of your app without worrying about rollover on an integer counter.