Jump to content
  • Advertisement
Sign in to follow this  
Sir Sapo

Timing problem

This topic is 4712 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

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
Advertisement

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
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
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
#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
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!