Class: RGame::Engine::Matrix

Inherits:
Object
  • Object
show all
Defined in:
lib/rgame/engine/matrix.rb

Overview

A fixed-size 2-D grid backed by a single flat (row-major) array, addressed as [x, y]. The flat backing keeps the whole grid in one contiguous allocation — cheaper than an array-of-arrays and a straightforward shape to later move into a C-level buffer. Pure; no bounds checking on the hot path (callers stay in range).

This stays Ruby, unlike the engine's old Tensor, which was deleted in favour of the C RGame::Util::Tensor. The reason is only that there is no C Util::Matrix to swap to — Util holds Tensor, Controls and Color. If one is ever written, this is the caller to point at it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, initial: nil) ⇒ Matrix

Returns a new instance of Matrix.



17
18
19
20
21
# File 'lib/rgame/engine/matrix.rb', line 17

def initialize(width, height, initial: nil)
  @width = width
  @height = height
  @data = Array.new(width * height, initial)
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



15
16
17
# File 'lib/rgame/engine/matrix.rb', line 15

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



15
16
17
# File 'lib/rgame/engine/matrix.rb', line 15

def width
  @width
end

Instance Method Details

#[](x, y) ⇒ Object



23
24
25
# File 'lib/rgame/engine/matrix.rb', line 23

def [](x, y)
  @data[(y * @width) + x]
end

#[]=(x, y, value) ⇒ Object



27
28
29
# File 'lib/rgame/engine/matrix.rb', line 27

def []=(x, y, value)
  @data[(y * @width) + x] = value
end