Difference between revisions of "Legacy:SetFrameRate"
From Spherical
(created from http://web.archive.org/web/20111228000549/http://www.spheredev.org/wiki/SetFrameRate) |
(filled) |
||
Line 1: | Line 1: | ||
− | |||
− | |||
Set or unset a limit to the refresh rate of drawing functions outside the map engine. | Set or unset a limit to the refresh rate of drawing functions outside the map engine. | ||
Line 6: | Line 4: | ||
==Usage== | ==Usage== | ||
− | {{Usage | + | {{Usage|func=SetFrameRate|params=fps}} |
− | * ''' | + | * '''fps''' integer. Short for '''F'''rames '''P'''er '''S'''econd, 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== | ==Examples== | ||
− | ( | + | <syntaxhighlight> |
+ | // 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(); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Flashing colors aside, note the difference in the speed of the top and bottom loops due to the previous calls to [[API:SetFrameRate|SetFrameRate]]() above. Here, [[API:GetFrameRate|GetFrameRate]]() is also used to illustrate how to obtain the current frame rate. | ||
==Notes== | ==Notes== | ||
− | ( | + | * Though the main description implies that all drawing functions are affected by this, in fact only [[API:FlipScreen|FlipScreen]]() is limited, or 'throttled'. For example, if <code>SetFrameRate(30);</code> were executed, <code>FlipScreen()</code> could be called at most 30 times in one second, with small, roughly even pauses to distribute the screen updates evenly. |
+ | * [[API:GetFrameRate|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 [[API:MapEngine|MapEngine]](), and any functions that access or modify the map engine's frame rate. | ||
==See also== | ==See also== | ||
− | * | + | * [[API:GetFrameRate|GetFrameRate]]() |
− | * | + | * [[API:ApplyColorMask|ApplyColorMask]]() |
− | * | + | * [[API:AreKeysLeft|AreKeysLeft]]() |
− | * | + | * [[API:CreateColor|CreateColor]]() |
+ | * [[API:FlipScreen|FlipScreen]]() | ||
+ | * [[API:Font/drawText|Font.drawText]]() | ||
+ | * [[API:GetKey|GetKey]]() | ||
+ | * [[API:GetSystemFont|GetSystemFont]]() | ||
+ | * [[API:MapEngine|MapEngine]]() | ||
+ | * [[wikipedia:Frames per second|Frames per second]] on [http://www.wikipedia.org Wikipedia] | ||
+ | * [https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/random Math.random]() | ||
{{API:Video/navbox}} | {{API:Video/navbox}} |
Revision as of 23:18, 24 June 2013
Set or unset a limit to the refresh rate of drawing functions outside the map engine.
Contents
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.
See also
- GetFrameRate()
- ApplyColorMask()
- AreKeysLeft()
- CreateColor()
- FlipScreen()
- Font.drawText()
- GetKey()
- GetSystemFont()
- MapEngine()
- Frames per second on Wikipedia
- Math.random()