Class: DXRubySDL::Window::FPSTimer
- Inherits:
-
Object
- Object
- DXRubySDL::Window::FPSTimer
- Includes:
- Singleton
- Defined in:
- lib/dxruby_sdl/window/fpstimer.rb
Overview
FPSTimer is copied from www.kmc.gr.jp/~ohai/fpstimer.rb.
Constant Summary collapse
- FPS_COUNT =
10
Instance Attribute Summary collapse
-
#count_sleep ⇒ Object
readonly
Returns the value of attribute count_sleep.
-
#fps ⇒ Object
Returns the value of attribute fps.
-
#real_fps ⇒ Object
readonly
Returns the value of attribute real_fps.
-
#total_skip ⇒ Object
readonly
Returns the value of attribute total_skip.
Instance Method Summary collapse
-
#initialize(fps = 60, accurary = 10, skip_limit = 15) ⇒ FPSTimer
constructor
fps
is the number of frames per second that you want to keep,accurary
is the accurary of sleep/SDL.delay in milisecond. -
#reset ⇒ Object
reset timer, you should call just before starting loop.
-
#wait_frame ⇒ Object
execute given block and wait.
Constructor Details
#initialize(fps = 60, accurary = 10, skip_limit = 15) ⇒ FPSTimer
fps
is the number of frames per second that you want to keep, accurary
is the accurary of sleep/SDL.delay in milisecond
18 19 20 21 22 |
# File 'lib/dxruby_sdl/window/fpstimer.rb', line 18 def initialize(fps = 60, accurary = 10, skip_limit = 15) @fps = fps @accurary = accurary / 1000.0 @skip_limit = skip_limit end |
Instance Attribute Details
#count_sleep ⇒ Object (readonly)
Returns the value of attribute count_sleep.
15 16 17 |
# File 'lib/dxruby_sdl/window/fpstimer.rb', line 15 def count_sleep @count_sleep end |
#fps ⇒ Object
Returns the value of attribute fps.
13 14 15 |
# File 'lib/dxruby_sdl/window/fpstimer.rb', line 13 def fps @fps end |
#real_fps ⇒ Object (readonly)
Returns the value of attribute real_fps.
14 15 16 |
# File 'lib/dxruby_sdl/window/fpstimer.rb', line 14 def real_fps @real_fps end |
#total_skip ⇒ Object (readonly)
Returns the value of attribute total_skip.
14 15 16 |
# File 'lib/dxruby_sdl/window/fpstimer.rb', line 14 def total_skip @total_skip end |
Instance Method Details
#reset ⇒ Object
reset timer, you should call just before starting loop
25 26 27 28 29 30 31 32 33 |
# File 'lib/dxruby_sdl/window/fpstimer.rb', line 25 def reset @old = get_ticks @skip = 0 @real_fps = @fps @frame_count = 0 @fps_old = @old @count_sleep = 0 @total_skip = 0 end |
#wait_frame ⇒ Object
execute given block and wait
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dxruby_sdl/window/fpstimer.rb', line 36 def wait_frame now = get_ticks nxt = @old + (1.0 / @fps) if nxt > now yield @skip = 0 wait(nxt) @old = nxt elsif @skip > @skip_limit # :nocov: yield @skip = 0 @old = get_ticks # :nocov: else # :nocov: @skip += 1 @total_skip += 1 @old = nxt # :nocov: end calc_real_fps end |