Class | Rubygame::Clock |
In: |
ext/rubygame/rubygame_shared.c
lib/rubygame/clock.rb |
Parent: | Object |
Clock provides class methods for tracking running time and delaying execution of the program for specified time periods. This is used to provide a consistent framerate, prevent the program from using all the processor time, etc.
Clock also provides instance methods to make it convenient to monitor and limit application framerate. See tick.
time: | how many milliseconds to delay. |
gran: | the granularity (in milliseconds) to assume for the system. A smaller value should use less CPU time, but if it‘s lower than the actual system granularity, this function might wait too long. The default, 12 ms, has a fairly low risk of over-waiting for many systems. |
Use the CPU to more accurately wait for the given period. Returns the actual delay time, in milliseconds. This function is more accurate than wait, but is also somewhat more CPU-intensive.
The Rubygame timer system will be initialized when you call this function, if it has not been already.
time: | how many milliseconds to wait. |
Wait approximately the given time (the accuracy depends upon processor scheduling, but 10ms is common). Returns the actual delay time, in milliseconds. This method is less CPU-intensive than delay, but is slightly less accurate.
The Rubygame timer system will be initialized when you call this function, if it has not been already.
Returns the current target framerate (frames/second). This is an alternate way to access @target_frametime. Same as: 1000.0 / target_frametime
Returns the number of milliseconds since you last called this method.
You must call this method once per frame (i.e. per iteration of your main loop) if you want to use the framerate monitoring and/or framerate limiting features.
Framerate monitoring allows you to check the framerate (frames per second) with the framerate method.
Framerate limiting allows you to prevent the application from running too fast (and using 100% of processor time) by pausing the program very briefly each frame. The pause duration is calculated each frame to maintain a constant framerate.
Framerate limiting is only enabled if you have set the target_framerate= or target_frametime=. If you have done that, this method will automatically perform the delay each time you call it.
(Please note that no effort is made to correct a framerate which is slower than the target framerate. Clock can‘t make your code run faster, only slow it down if it is running too fast.)