Class: RGame::Engine::Matrix
- Inherits:
-
Object
- Object
- RGame::Engine::Matrix
- 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
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #[](x, y) ⇒ Object
- #[]=(x, y, value) ⇒ Object
-
#initialize(width, height, initial: nil) ⇒ Matrix
constructor
A new instance of Matrix.
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
#height ⇒ Object (readonly)
Returns the value of attribute height.
15 16 17 |
# File 'lib/rgame/engine/matrix.rb', line 15 def height @height end |
#width ⇒ Object (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 |