Control update loop/FPS

Hey all, I am trying to cap my update loop so it updates the game every 16ms or 60fps.

I've got a time class and I am able to start/stop & get the elapsed time in seconds. Currently its averaging around 0.004. I'm not sure the best way to go about this so could use some help please!

P.S should the render of a game ever be capped? Or just the update, i guess a game should be able to draw everything fast as possible..

Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Time::startTimer()
{
	QueryPerformanceCounter(&m_startTime);
	return;
}

void Time::stopTimer()
{
	QueryPerformanceCounter(&m_stopTime);
	return;
}

double Time::getSeconds() const
{
	return (m_stopTime.QuadPart - m_startTime.QuadPart) * m_freq;
};
Update()
{
myTimer.startTimer();
DO THE UPDATES.
myTimer.stopTimer();
myTimer.getSeconds();
}
Last edited on
any advice please!
I don't have time at the moment to properly answer your question, but I have a link that might be interesting reading for you:
http://gafferongames.com/game-physics/fix-your-timestep/
In most cases, you should cap the fps to avoid tearing.

The proper way to do it is to wait for vertical synchronization signal, which is generated just after the pixel data is sent to your monitor.

So, you should check the graphics library that you are using (which one are you using?) whether it provides a function that pauses until vsync.

When the vsync signal is generated, the function will exit, and at that point in time you are free to update the screen. Waiting for vsync will automatically cap the framerate to refresh rate of your display device.
I don't have time at the moment to properly answer your question, but I have a link that might be interesting reading for you:
http://gafferongames.com/game-physics/fix-your-timestep/


thanks for the link, had a read, im still a bit confused to be honest, trying to figure out how to implement it correctly is proving hard.

So, you should check the graphics library that you are using (which one are you using?)


I'm just creating a game in c++ console, with no graphics library. Trying to grasp c++ first before I move on to some libraries.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
class TimerMsec
{	
	unsigned resetTime; //in ticks
	
	public:
	TimerMsec() {}	

	void Reset()
	{	resetTime=clock();  }	

	unsigned Get()
	{	return unsigned((clock()-resetTime)*1000.0/CLOCKS_PER_SEC);	}	
};


1) Create timer
2) Call Reset to start the timer
3) Call Get as many times as you like to get the elepased time in milliseconds

Include <time.h> so that you can call clock()
Last edited on
thanks very much for the help, 'Kevin C'. Its appreciated!

Question:

Im stuck on incorporating this into the update loop so it runs at a specific ms, I.E 0.016ms.
At the moment it runs at 200+ FPS.

Is QueryPerformanceCounter more accurate or better to use?
Since I have already created that class I can also receive the MS/Seconds from that also.
Last edited on
Update loop? What would that be?

If you want the update to last longer, I think that the Sleep function is available on Win32 to pause the program.

Honestly, I dont know whether QueryPerformanceCounter is better. It might be.

If you would like to limit fps, you have to wait in the main loop:
1
2
3
4
5
6
7
8
9
10
11
12
TimerMsec timer;
timer.Reset();

/*main loop*/ 
    {
    while(timer.Get()<16)
        Sleep(1); //do nothing
    timer.Reset();

    //rest of code goes here
 
    }
Last edited on
Ah, maybe I wasn't clear.

I will try what you have suggested with the FPS also. Thanks so much.

Yeah, I'd like to limit the FPS and limit the amount of time the update loop is ran, it was being done every 0.004ms from when I timed how long it took to run through the whole updates.
Topic archived. No new replies allowed.