class TerminalGameEngine::Frame

Attributes

rows[R]

Public Class Methods

clear_screen() click to toggle source
# File lib/terminal_game_engine/frame.rb, line 55
def self.clear_screen
  print "\033[2J"
end
disable_cursor() click to toggle source
# File lib/terminal_game_engine/frame.rb, line 59
def self.disable_cursor
  print "\x1B[?25l"
end
enable_cursor() click to toggle source
# File lib/terminal_game_engine/frame.rb, line 63
def self.enable_cursor
  print "\x1B[?25h"
end
move_cursor(x, y) click to toggle source
# File lib/terminal_game_engine/frame.rb, line 51
def self.move_cursor(x, y)
  print "\033[#{y+1};#{x+1}H"
end
new(width, height) click to toggle source
# File lib/terminal_game_engine/frame.rb, line 5
def initialize(width, height)
  @rows = height.times.map { ' ' * width }
end
setup(disable_cursor: true) click to toggle source
# File lib/terminal_game_engine/frame.rb, line 67
def self.setup(disable_cursor: true)
  clear_screen
  disable_cursor if disable_cursor

  $stdin.raw!
  at_exit do
    puts "\r"
    enable_cursor if disable_cursor
    $stdin.cooked!
    system 'stty sane'
  end

  trap 'WINCH' do
    clear_screen
  end
end

Public Instance Methods

draw(x, y, sprite) click to toggle source
# File lib/terminal_game_engine/frame.rb, line 17
def draw(x, y, sprite)
  lines = sprite.split("\n")

  lines.each_with_index do |line, i|
    if line.size > 0 && y+lines.size <= self.height && y+i >= 0
      # crop when drawing off left
      if x < 0
        line = line[x.abs..-1]
        x = 0
      end

      # crop when drawing off right
      if x+line.size-1 >= self.width
        line = line[0..(self.width-1)-(x+line.size)]
      end

      @rows[y+i][x..x+line.size-1] = line
    end
  end
end
draw_center(y, sprite) click to toggle source
# File lib/terminal_game_engine/frame.rb, line 38
def draw_center(y, sprite)
  sprite_width = sprite.split("\n").first.size
  x = self.width / 2 - sprite_width / 2
  draw x, y, sprite
end
height() click to toggle source
# File lib/terminal_game_engine/frame.rb, line 13
def height
  @rows.size
end
render() click to toggle source
# File lib/terminal_game_engine/frame.rb, line 44
def render
  @rows.each_with_index do |row, i|
    Frame.move_cursor 0, i
    print row
  end
end
width() click to toggle source
# File lib/terminal_game_engine/frame.rb, line 9
def width
  @rows.first.size
end