[Solved] Frame rate cap and calculation of
Posted: Sun May 31, 2009 4:15 am
Hey guys, I'm having difficulty following two of lazyfoo's tutorial's together. Independently I can either cap the fps on my pong game or I can view the fps, but doing both at the same time is presenting difficulties.
Here is my relevant code: (I removed some irrelevant code as to make it easier to find my mistake)
If i put the fps.start(); before the while(quit == false) I get 130fps showing up.
If i put the fps.start(); inside the while(quit == false) main loop (like lazyfoo's tutorial shows for one of the tutorials) I never get anything showing up on my screen.
Where is my logic off?
Here is my relevant code: (I removed some irrelevant code as to make it easier to find my mistake)
Code: Select all
int main(int argc, char* args[])
{
bool quit = false;
float frame = 0.0;
Timer fps;
Timer update;
update.start();
if( init() == false) // initialize SDL subsystem, if they failed exit with error code 0x01
{
return 1;
}
if(load_Files() == false) // if failed exit with error code 0x01
{
return 1;
}
while(quit == false)
{
fps.start();
apply_surface(0, 0, background, screen);
if(fps.get_ticks() > 1000)
{
std::stringstream caption;
caption << "Average Frames Per Second: " << frame / ( fps.get_ticks() / 1000.f );
if(pushFont(caption.str().c_str(),100,100,message,screen,font,textColor) == false) // this fn writes the font to the screen.
{
return 1; // error rendering text.
}
//Restart the update timer
update.start();
}
if(fps.get_ticks() < (1000 / FRAMES_PER_SECOND))
{
//Sleep the remaining frame time
SDL_Delay((1000 / FRAMES_PER_SECOND) - fps.get_ticks());
}
//Swap the current and back buffer.
if(SDL_Flip(screen) == -1)
{
return 1;
}
frame++;
}
cleanUp();
return 0; // no errors
}
If i put the fps.start(); inside the while(quit == false) main loop (like lazyfoo's tutorial shows for one of the tutorials) I never get anything showing up on my screen.
Where is my logic off?