Class: InteractiveTerm::ScreenBuffer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ ScreenBuffer

Returns a new instance of ScreenBuffer.



195
196
197
198
199
200
# File 'lib/interactive_term.rb', line 195

def initialize(width, height)
  @width = width
  @height = height
  # There might be a reason to use an actual noop character instead of space
  @buffer = @height.times.map { @width.times.map {" "}}
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



193
194
195
# File 'lib/interactive_term.rb', line 193

def buffer
  @buffer
end

Instance Method Details

#deltas(other_screen_buffer) ⇒ Object

Returns the character from other_screen_buffer when a delta is found, so this is not commutative Assumes other_screen_buffer is the same size as @buffer



224
225
226
227
228
229
230
# File 'lib/interactive_term.rb', line 224

def deltas(other_screen_buffer)
  deltas = []
  @height.times do |y|; @width.times do |x|;
    deltas << [other_screen_buffer.buffer[y][x], x, y] if @buffer[y][x] != other_screen_buffer.buffer[y][x]
  end; end;
  deltas
end

#matrix_safe_insert(matrix, char, x, y) ⇒ Object

Inserts the given char at x, y of matrix Return without doing anything if x y is out of bounds



212
213
214
215
216
# File 'lib/interactive_term.rb', line 212

def matrix_safe_insert(matrix, char, x, y)
  return if x < 0 || x >= matrix.first.size
  return if y < 0 || y >= matrix.size
  matrix[y][x] = char
end

#pretty_printObject



218
219
220
# File 'lib/interactive_term.rb', line 218

def pretty_print
  @height.times {|y| @width.times {|x| print @buffer[y][x]}; puts nil}
end

#render(sprites) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/interactive_term.rb', line 202

def render(sprites)
  sprites.each do |sprite|; x_pos = sprite.x; y_pos = sprite.y;
    sprite.iterate do |char, x, y|
      matrix_safe_insert(@buffer, char, x_pos + x, y_pos + y)
    end
  end
end