Class: Chingu::FPSCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/chingu/fpscounter.rb

Overview

Calculates a fps and a tick-time for use in update-calls register_tick() must be called every game loop iteration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFPSCounter

Returns a new instance of FPSCounter



43
44
45
46
47
48
49
50
51
# File 'lib/chingu/fpscounter.rb', line 43

def initialize
	@current_second = Gosu::milliseconds / 1000
	@accum_fps = 0
	@fps = 0
    @ticks = 0
    
    @milliseconds_since_last_tick = 0
	@last_value = Gosu::milliseconds
end

Instance Attribute Details

#fpsObject (readonly) Also known as: framerate

Frames per second, access with $window.fps or $window.framerate



35
36
37
# File 'lib/chingu/fpscounter.rb', line 35

def fps
  @fps
end

#milliseconds_since_last_tickObject (readonly) Also known as: dt

Returns the value of attribute milliseconds_since_last_tick



29
30
31
# File 'lib/chingu/fpscounter.rb', line 29

def milliseconds_since_last_tick
  @milliseconds_since_last_tick
end

#ticksObject (readonly)

Total amount of game iterations (ticks)



41
42
43
# File 'lib/chingu/fpscounter.rb', line 41

def ticks
  @ticks
end

Instance Method Details

#register_tickObject

This should be called once every game-iteration, preferable in update()



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chingu/fpscounter.rb', line 56

def register_tick
	@accum_fps += 1
    @ticks += 1
	current_second = Gosu::milliseconds / 1000
	if current_second != @current_second
		@current_second = current_second
		@fps = @accum_fps
		@accum_fps = 0
	end

    #
    # Calculate how many milliseconds passed since last game loop iteration.
    #
	@milliseconds_since_last_tick = Gosu::milliseconds - @last_value
	@last_value = Gosu::milliseconds
	return @milliseconds_since_last_tick
end