class DXRubySDL::Window::FPSTimer

FPSTimer is copied from www.kmc.gr.jp/~ohai/fpstimer.rb.

Constants

FPS_COUNT

Attributes

count_sleep[R]
fps[RW]
real_fps[R]
total_skip[R]

Public Class Methods

new(fps = 60, accurary = 10, skip_limit = 15) click to toggle source

fps is the number of frames per second that you want to keep, accurary is the accurary of sleep/SDL.delay in milisecond

# File lib/dxruby_sdl/window/fpstimer.rb, line 17
def initialize(fps = 60, accurary = 10, skip_limit = 15)
  @fps = fps
  @accurary = accurary / 1000.0
  @skip_limit = skip_limit
end

Public Instance Methods

reset() click to toggle source

reset timer, you should call just before starting loop

# File lib/dxruby_sdl/window/fpstimer.rb, line 24
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() { || ... } click to toggle source

execute given block and wait

# File lib/dxruby_sdl/window/fpstimer.rb, line 35
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

Private Instance Methods

calc_real_fps() click to toggle source
# File lib/dxruby_sdl/window/fpstimer.rb, line 77
def calc_real_fps
  @frame_count += 1
  if @frame_count >= FPS_COUNT
    # :nocov:
    @frame_count = 0
    now = get_ticks
    @real_fps = FPS_COUNT / (now - @fps_old)
    @fps_old = now
    # :nocov:
  end
end
get_ticks() click to toggle source
# File lib/dxruby_sdl/window/fpstimer.rb, line 73
def get_ticks
  SDL.getTicks / 1000.0
end
wait(nxt) click to toggle source
# File lib/dxruby_sdl/window/fpstimer.rb, line 62
def wait(nxt)
  while nxt > get_ticks + @accurary
    sleep(@accurary - 0.005)
    @count_sleep += 1
  end

  while nxt > get_ticks
    # busy loop, do nothing
  end
end