Class: Gosu::Clock
- Inherits:
-
Object
- Object
- Gosu::Clock
- Defined in:
- lib/movie_maker/gosu_clock.rb
Overview
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.
Instance Attribute Summary collapse
-
#start ⇒ Object
readonly
The runtime when the Clock was initialized.
-
#target_frametime ⇒ Object
The target frametime (milliseconds/frame).
-
#ticks ⇒ Object
readonly
The number of times #tick has been called.
Class Method Summary collapse
- .delay(time) ⇒ Object
-
.runtime ⇒ Object
Make Rubygame::Clock work with GOSUs “milliseconds”.
Instance Method Summary collapse
-
#framerate ⇒ Object
call-seq: framerate() -> Numeric.
-
#initialize {|_self| ... } ⇒ Clock
constructor
Create a new Clock instance.
-
#lifetime ⇒ Object
call-seq: lifetime() -> Numeric.
-
#target_framerate ⇒ Object
Returns the current target framerate (frames/second).
-
#target_framerate=(framerate) ⇒ Object
Sets the target number of frames per second to
framerate
. -
#tick ⇒ Object
Returns the number of milliseconds since you last called this method.
Constructor Details
Instance Attribute Details
#start ⇒ Object (readonly)
The runtime when the Clock was initialized.
30 31 32 |
# File 'lib/movie_maker/gosu_clock.rb', line 30 def start @start end |
#target_frametime ⇒ Object
The target frametime (milliseconds/frame). See #tick
55 56 57 |
# File 'lib/movie_maker/gosu_clock.rb', line 55 def target_frametime @target_frametime end |
#ticks ⇒ Object (readonly)
The number of times #tick has been called.
32 33 34 |
# File 'lib/movie_maker/gosu_clock.rb', line 32 def ticks @ticks end |
Class Method Details
.delay(time) ⇒ Object
39 40 41 42 43 |
# File 'lib/movie_maker/gosu_clock.rb', line 39 def self.delay(time) sleep_time = time/1000.0 sleep_time = 0 if sleep_time < 0 sleep(sleep_time) end |
.runtime ⇒ Object
Make Rubygame::Clock work with GOSUs “milliseconds”
35 36 37 |
# File 'lib/movie_maker/gosu_clock.rb', line 35 def self.runtime Gosu::milliseconds end |
Instance Method Details
#framerate ⇒ Object
call-seq: framerate() -> Numeric
Return the actual framerate (frames per second) recorded by the Clock. See #tick.
TODO: sample only a few seconds in the past, instead of the entire lifetime of the Clock.
97 98 99 100 101 102 |
# File 'lib/movie_maker/gosu_clock.rb', line 97 def framerate # below is same as: return @ticks / (lifetime / 1000.0) return 1000.0 * @ticks / lifetime() rescue ZeroDivisionError return 0 end |
#lifetime ⇒ Object
call-seq: lifetime() -> Numeric
Returns time in milliseconds since this Clock instance was created.
86 87 88 |
# File 'lib/movie_maker/gosu_clock.rb', line 86 def lifetime Clock.runtime() - @start end |
#target_framerate ⇒ Object
Returns the current target framerate (frames/second). This is an alternate way to access @target_frametime. Same as: 1000.0 / #target_frametime
60 61 62 63 64 65 66 67 68 |
# File 'lib/movie_maker/gosu_clock.rb', line 60 def target_framerate if @target_frametime 1000.0 / @target_frametime else nil end rescue ZeroDivisionError return nil end |
#target_framerate=(framerate) ⇒ Object
Sets the target number of frames per second to framerate
. This is an alternate way to access @target_frametime. Same as: #target_frametime = 1000.0 / framerate
73 74 75 76 77 78 79 80 81 |
# File 'lib/movie_maker/gosu_clock.rb', line 73 def target_framerate=( framerate ) if framerate @target_frametime = 1000.0 / framerate else @target_frametime = nil end rescue ZeroDivisionError @target_frametime = nil end |
#tick ⇒ Object
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.)
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/movie_maker/gosu_clock.rb', line 127 def tick() passed = Clock.runtime() - @last_tick # how long since the last tick? if @target_frametime return Clock.delay(@target_frametime - passed) + passed end return passed ensure @last_tick = Clock.runtime() @ticks += 1 end |