Sign in to follow this  
Sir Sapo

Timing problem

Recommended Posts

Sir Sapo    769
I am trying to limit the FPS on my game to a value that I specify, but I have run into a bit of a problem... I made this timer that I would run before my render function, and all was going well until I realized that clock() returns an integer, which totally threw off all of my calculations (it will run at either full or 1 FPS). Here's my function:
void wait ( double seconds )
{
  double endwait;
  endwait = clock () + seconds * CLK_TCK ;
  while (clock() < endwait) {}
}

Is there any function that will do that is like clock(), but returns as a double or something? Thanks in advance.

Share this post


Link to post
Share on other sites
Sir Sapo    769

void wait ( int seconds )
{
DWORD dwStart = GetTickCount();

while( GetTickCount() - dwStart < (1000/seconds) ) {}

}




Would this limit the FPS of my game to the seconds parameter if I put this function in the gameloop?

Share this post


Link to post
Share on other sites
xor    516
No.

You're going about it the wrong way.
You don't render, wait a few miliseconds then render again, that's the wrong approach, render times will still variate across different machines.

You render, store the time when that render started, and when X miliseconds pass from that time, you render again, and when X miliseconds pass from the previous render, you render again, and so on.
If you wish for instance the game to run at 60 FPS, X=1000/60.

Share this post


Link to post
Share on other sites
Sir Sapo    769
Ok i think I see now, hows this:


//declared at beginning of program
bool rendered = false;
DWORD renderstart;
int FPS;

if(!rendered)
{
RenderScreen();
renderstart = GetTickCount();
rendered = true;
}

if(rendered)
{
if(GetTickCount() - renderstart > FPS)
rendered = false;
}




Share this post


Link to post
Share on other sites
xor    516
#define FPS 60

starttime=GetTickCount(); //this variable is useless, it's just to make it clearer
render();
next_frame=starttime+1000/FPS;

while(game_is_on)
{
if(GetTickCount()>next_frame)
{
render();
next_frame+=1000/FPS;
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this