Class: SDC::Limiter

Inherits:
Object
  • Object
show all
Defined in:
lib/core/Limiter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max: 720, renders_per_second: 60, ticks_per_second: 60, gc_per_second: 60) ⇒ Limiter

Returns a new instance of Limiter.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/core/Limiter.rb', line 8

def initialize(max: 720, renders_per_second: 60, ticks_per_second: 60, gc_per_second: 60)
	@counter = 0
	@temp_counter = 0
	@max = max

	@renders_per_second = renders_per_second
	@ticks_per_second = ticks_per_second
	@gc_per_second = gc_per_second

	# Intervals are rounded down, for now
	@render_interval = (@max / @renders_per_second).to_i
	@tick_interval = (@max / @ticks_per_second).to_i
	@gc_interval = (@max / @gc_per_second).to_i

	@update_block = nil
	@draw_block = nil
	@gc_block = nil
end

Instance Attribute Details

#gc_per_secondObject (readonly)

Returns the value of attribute gc_per_second.



6
7
8
# File 'lib/core/Limiter.rb', line 6

def gc_per_second
  @gc_per_second
end

#maxObject (readonly)

Returns the value of attribute max.



6
7
8
# File 'lib/core/Limiter.rb', line 6

def max
  @max
end

#renders_per_secondObject (readonly)

Returns the value of attribute renders_per_second.



6
7
8
# File 'lib/core/Limiter.rb', line 6

def renders_per_second
  @renders_per_second
end

#ticks_per_secondObject (readonly)

Returns the value of attribute ticks_per_second.



6
7
8
# File 'lib/core/Limiter.rb', line 6

def ticks_per_second
  @ticks_per_second
end

Instance Method Details

#change_renders_per_second(new_value) ⇒ Object

Allow for changes via options, if necessary



40
41
42
43
# File 'lib/core/Limiter.rb', line 40

def change_renders_per_second(new_value)
	@renders_per_second = new_value
	@render_interval = (@max / @renders_per_second).to_i
end

#set_draw_routine(&block) ⇒ Object



31
32
33
# File 'lib/core/Limiter.rb', line 31

def set_draw_routine(&block)
	@draw_block = block
end

#set_gc_routine(&block) ⇒ Object



35
36
37
# File 'lib/core/Limiter.rb', line 35

def set_gc_routine(&block)
	@gc_block = block
end

#set_update_routine(&block) ⇒ Object



27
28
29
# File 'lib/core/Limiter.rb', line 27

def set_update_routine(&block)
	@update_block = block
end

#tickObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/core/Limiter.rb', line 45

def tick
	@timer = Time.now if !@timer

	is_update_frame = (@counter % @tick_interval == 0)
	is_draw_frame = (@counter % @render_interval == 0)
	is_gc_frame = (@counter % @gc_interval == 0)

	scheduled_frame = is_update_frame || is_draw_frame || is_gc_frame

	if is_update_frame then
		@update_block&.call
	end

	if is_draw_frame then
		@draw_block&.call
	end

	if is_gc_frame then
		@gc_block&.call
	end

	@counter += 1

	if @counter == @max then
		@counter = 0
	end

	# Check how advanced the timer is, if this is an scheduled frame
	# If the frame got executed too fast, some time can be stalled
	# The non-scheduled frames then stabilize the framerate to a fixed pace
	# A frame lasting longer than scheduled will therefore not be catched up
	# This prevents the framerate boosting beyond the schedule

	if scheduled_frame then
		while (Time.now - @timer) < (@temp_counter + 1) / @max do
		end
		@temp_counter = 0
		@timer = Time.now
	else
		@temp_counter += 1
	end

	return true
end