Class: Amaterasu::GameBoy::Vram::Tile

Inherits:
Object
  • Object
show all
Defined in:
lib/amaterasu/game_boy/vram/tile.rb

Overview

Models each Tile that lives in the VRAM.

Constant Summary collapse

PIXEL_HEIGHT =
8
PIXEL_WIDTH =
8
SIZE_IN_BYTES =
16
SIZE_BIT_MASK =
0b111
PIXELS_LOOKUP =

Pre-computes all possible 8 pixel values given the low and high bytes for the Game Boy address range (0x0000 - 0xFFFF).

Usage:

Memory values: $3C (Low), $7E (High)
PIXELS_LOOKUP[(0x7E << 8) | 0x3C] #=> [0, 2, 3, 3, 3, 3, 2, 0]
Array.new(65_536) do |idx|
  low_byte  = idx & 0xFF
  high_byte = (idx >> 8) & 0xFF

  Array.new(8) do |i|
    bit = 7 - i
    low_bit = (low_byte >> bit) & 1
    high_bit = (high_byte >> bit) & 1

    (high_bit << 1) | low_bit
  end.freeze
end.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vram_data:, tile_index:) ⇒ Tile

Returns a new instance of Tile.



34
35
36
37
38
# File 'lib/amaterasu/game_boy/vram/tile.rb', line 34

def initialize(vram_data:, tile_index:)
  @vram_data = vram_data
  @tile_index = tile_index
  @base_offset = tile_index * SIZE_IN_BYTES
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



32
33
34
# File 'lib/amaterasu/game_boy/vram/tile.rb', line 32

def data
  @data
end

Instance Method Details

#data_high(current_y) ⇒ Integer

Returns The high byte of the tile at a given row.

Returns:

  • (Integer)

    The high byte of the tile at a given row.



49
50
51
52
53
54
# File 'lib/amaterasu/game_boy/vram/tile.rb', line 49

def data_high(current_y)
  current_tile_y = current_y & SIZE_BIT_MASK
  row_within_tile = current_tile_y * 2

  @vram_data[@base_offset + row_within_tile + 1]
end

#data_low(current_y) ⇒ Integer

Returns The low byte of the tile at a given row.

Returns:

  • (Integer)

    The low byte of the tile at a given row.



41
42
43
44
45
46
# File 'lib/amaterasu/game_boy/vram/tile.rb', line 41

def data_low(current_y)
  current_tile_y = current_y & SIZE_BIT_MASK
  row_within_tile = current_tile_y * 2

  @vram_data[@base_offset + row_within_tile]
end

#inspectObject



60
61
62
63
64
# File 'lib/amaterasu/game_boy/vram/tile.rb', line 60

def inspect
  '#<Tile ' \
    "@tile_index=#{@tile_index} " \
    "@base_offset=#{@base_offset}>"
end

#pixel_row(low_byte, high_byte) ⇒ Object



56
57
58
# File 'lib/amaterasu/game_boy/vram/tile.rb', line 56

def pixel_row(low_byte, high_byte)
  PIXELS_LOOKUP[(high_byte << 8) | low_byte]
end