Game Development Community

How to stay at 60 fps, not go higher?

by Dante Falcone · in Torque Game Engine Advanced · 11/15/2008 (5:21 pm) · 8 replies

Hello all, thanks for reading.

I have been looking through the code trying to find how to keep the framerate locked at 60 FPS. This is easy in direct-x, just measure draw call time against last time etc... But I'm having problems tracing this awesome (yet fairly complex) engine down to where that is exactly. Can anyone help?

Basically, ability to detect how long ago the last draw call was, and not draw if it was too soon.

Thanks!

-Dante

About the author

I am a Producer in the Video Game industry and specialize in MMO development, Character Art->Game pipelines, and general support programming.


#1
11/16/2008 (4:09 pm)
In TimeManager::process() in winWindow.cpp:

// The number of frames per second to target in the 
// frame rate limiter in TimeManager::process().
static S32 smFrameLimiter = 60;
 
void TimeManager::process()
{
   // Note: This is a simple frame rate limiter which 
   // sleeps off excess milliseconds when the elapsed 
   // time is less than 1/60th of a second.
   //
   // This is critical on single core CPUs which need
   // the free cycles to process the other threads that
   // are active in your game.
   //
   // It can be disabled by setting smFrameLimiter to a
   // high rate like 1000 fps when you need to do 
   // rendering performance testing, but it should not 
   // be removed.
   //
   #ifndef TORQUE_NVPERFHUD
 
      smFrameLimiter = getMax( 1, smFrameLimiter );
      S32 sleepTime = ( 1000 / smFrameLimiter ) - gTimer.getElapsedMS();
      if ( sleepTime >= 0 )
         Platform::sleep( sleepTime );
 
   #endif
 
   TimeEvent tEvent;
   tEvent.elapsedTime = gTimer.getElapsedMS();
   // etc.

... you can also optionally add this to Platform::init() to expose the setting to script:

// For frame rate limiting in TimeManager::process().
   Con::addVariable( "$Platform::frameLimiter", TypeS32, &smFrameLimiter );
#2
11/16/2008 (5:02 pm)
Great, thanks so much Tom! Much appreciated.
#3
11/16/2008 (6:16 pm)
Hi Tom, I appreciated the reply, but I'm afraid I still need some help, if you may lend your brain again :).

The above function "process()" is not available in winWindow.cpp. I only found it in X86UNIXWindow.cpp, and it looks quite different.

Next problem was that gTimer is an undeclared variable. PlatformTimer.h has a pointer for the TimeManager called "mTimer"

Did you implement this yourself? Any ideas on how to integrate this, only to affect the draw call, would be really helpful.

Thanks much
#4
11/16/2008 (6:33 pm)
I appologize that code was for a pre-Torque 1.7 code base.

Looking in TGEA 1.8 beta (but probably also in 1.7) it seems like it already has this feature in TimeManager::_updateTime().

You need to adjust mForegroundThreshold to 16 which gives you about 60fps limiting of frame rate. mBackgroundThreshold is for when the game does not have focus and is in the background.

These values are not exposed to script... probably something lost in the merge from TGB (which is where i think this code came from).
#5
11/16/2008 (6:56 pm)
Worked like a charm ;).

Thanks Tom
#6
11/18/2008 (12:11 am)
Out of curiosity, what's the benefit of capping FPS?
#7
11/18/2008 (2:03 am)
If you free up CPU time spent in wasted rendering (arguably anything over 60fps is a waste) you have given yourself more cycles for other threaded tasks like physics, AI path finding, skinning, etc.
#8
11/18/2008 (7:44 am)
Yes, that was my intention, as Tom explained. If you ever work on a 360 title, the rule/goal is 60 fps. Older games were usually 30 fps. Taking that movie film is 24 fps, and TV is traditionally 30 fps. The eye reacts a certain way to framerates. 60 is a very pleasing fps.

On the other hand, that theory isn't holding up practically for me right now! In my top-down view game, when I limit to 60fps, it looks rather jarring. I'm not sure if it is graphical artifacts, or periodic delays per second.

Everything is fine in first person view. I'll post a video soon, its really weird.