Class: LowFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/support/low_frame.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(renderer:, fps: 30, show_output: true) ⇒ LowFrame

Returns a new instance of LowFrame.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/support/low_frame.rb', line 8

def initialize(renderer:, fps: 30, show_output: true)
  @renderer = renderer
  @show_output = show_output

  # Millisecond duration of each frame. We lose a small amount of precision dropping the decimal.
  @frame_time = ((1.0 / fps) * 1000).to_i

  row_count, column_count = IO.console.winsize
  @screen_size = { row_count:, column_count: }

  @last_frame = nil

  setup if renderer && show_output
end

Instance Attribute Details

#rendererObject (readonly)

Returns the value of attribute renderer.



6
7
8
# File 'lib/support/low_frame.rb', line 6

def renderer
  @renderer
end

#screen_sizeObject (readonly)

Returns the value of attribute screen_size.



6
7
8
# File 'lib/support/low_frame.rb', line 6

def screen_size
  @screen_size
end

Instance Method Details

#exitObject



41
42
43
44
45
46
47
# File 'lib/support/low_frame.rb', line 41

def exit
  trap('INT') {
    reset
    system 'clear'
    exit
  }
end

#renderObject



23
24
25
26
27
28
29
30
# File 'lib/support/low_frame.rb', line 23

def render
  if @last_frame.nil? || (current_timestamp - @last_frame) >= @frame_time
    system 'clear' if @show_output

    @last_frame = current_timestamp
    @renderer.render(screen_size: @screen_size)
  end
end

#resetObject



37
38
39
# File 'lib/support/low_frame.rb', line 37

def reset
  print "\e[?25h\e[0m" # Show cursor and reset colors.
end

#setupObject



32
33
34
35
# File 'lib/support/low_frame.rb', line 32

def setup
  print "\e[?25l" # Hide cursor.
  system 'clear'
end