Legacy:SetFrameRate

From Spherical
Revision as of 11:01, 10 June 2015 by DaVince (talk | contribs) (Added FPS recommendation notes)
Jump to: navigation, search

Set or unset a limit to the refresh rate of drawing functions outside the map engine. (So for anything relying on FlipScreen() to render.)

Usage

SetFrameRate(fps);


  • fps integer. Short for Frames Per Second, which limits how often the screen can be refreshed in one second
    sixty (60) is a common value for this.
    If this is set to zero (0), no limit is imposed and the screen will update as fast as possible.

Examples

// Flashing screen example
var r = Math.random;	// for faster typing later
var f = GetSystemFont();

// === Set frame rate ===
SetFrameRate(5);

while (AreKeysLeft()) GetKey();
while (!AreKeysLeft())
{
  // Cheap screen coloring using ApplyColorMask()
  ApplyColorMask(CreateColor(r() * 255, r() * 255, r() * 255));
  f.drawText(0, 0, "fps = " + GetFrameRate());
  FlipScreen();
}
GetKey();

// === Change frame rate ===
SetFrameRate(30);

while (AreKeysLeft()) GetKey();
while (!AreKeysLeft())
{
  ApplyColorMask(CreateColor(r() * 255, r() * 255, r() * 255));
  f.drawText(0, 0, "fps = " + GetFrameRate());
  FlipScreen();
}
GetKey();

Flashing colors aside, note the difference in the speed of the top and bottom loops due to the previous calls to SetFrameRate() above. Here, GetFrameRate() is also used to illustrate how to obtain the current frame rate.

Notes

  • Though the main description implies that all drawing functions are affected by this, in fact only FlipScreen() is limited, or 'throttled'. For example, if SetFrameRate(30); were executed, FlipScreen() could be called at most 30 times in one second, with small, roughly even pauses to distribute the screen updates evenly.
  • GetFrameRate() can be used to obtain the current frame rate.
  • The frames per second set with this function are completely independent of that of the second argument of MapEngine(), and any functions that access or modify the map engine's frame rate.
  • At high frame rates, the recommended value for FPS is 60, as this is the default refresh rate of most monitors on the market and will thus provide the smoothest visuals. Otherwise you might get screen tearing, depending on your video settings and Sphere implementation.
    • If you deliberately use a lower frame rate, you can prevent tearing by setting it to a value that you can multiply to 60. For example: 30, 20, 15, 12, 10.

See also

API:Video/navbox