Class: GBTiles::GBR::TileSet::Objects::TileData

Inherits:
GBTiles::GBR::TileSet::Object show all
Defined in:
lib/gbtiles/gbr/tile_set/objects/tile_data.rb

Instance Attribute Summary collapse

Attributes inherited from GBTiles::GBR::TileSet::Object

#object_id, #object_type

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTileData

Returns a new instance of TileData.



14
15
16
# File 'lib/gbtiles/gbr/tile_set/objects/tile_data.rb', line 14

def initialize
  super GBTiles::GBR::TileSet::OBJECT_TYPE[:tile_data]
end

Instance Attribute Details

#color_setObject

Returns the value of attribute color_set.



11
12
13
# File 'lib/gbtiles/gbr/tile_set/objects/tile_data.rb', line 11

def color_set
  @color_set
end

#countObject

Returns the value of attribute count.



10
11
12
# File 'lib/gbtiles/gbr/tile_set/objects/tile_data.rb', line 10

def count
  @count
end

#dataObject

Returns the value of attribute data.



12
13
14
# File 'lib/gbtiles/gbr/tile_set/objects/tile_data.rb', line 12

def data
  @data
end

#heightObject

Returns the value of attribute height.



9
10
11
# File 'lib/gbtiles/gbr/tile_set/objects/tile_data.rb', line 9

def height
  @height
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/gbtiles/gbr/tile_set/objects/tile_data.rb', line 7

def name
  @name
end

#widthObject

Returns the value of attribute width.



8
9
10
# File 'lib/gbtiles/gbr/tile_set/objects/tile_data.rb', line 8

def width
  @width
end

Class Method Details

.initFromBitString(src) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gbtiles/gbr/tile_set/objects/tile_data.rb', line 18

def self.initFromBitString src
  object = GBTiles::GBR::TileSet::Objects::TileData.new

  object.name       = GBTiles::DataType.string!(src, 30)
  object.width      = GBTiles::DataType.word!(src)
  object.height     = GBTiles::DataType.word!(src)
  object.count      = GBTiles::DataType.word!(src)
  object.color_set  = src.slice!(0, 4)
  object.data       = src

  object
end

Instance Method Details

#render_tile(tile_index) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gbtiles/gbr/tile_set/objects/tile_data.rb', line 31

def render_tile tile_index
  tile = []

  tile_data = @data.slice(
    tile_index * @width * @height,
    @width * @height
  )

  (1..@height).each do |row|
    byte_0 = 0x00
    byte_1 = 0x00

    bitmask = 0x80

    (1..@width).each do |col|
      pixel = tile_data[(row - 1) * @height + col - 1]

      case pixel.unpack("C")[0]
      when 0 # Black
      when 1 # Dark Grey
        byte_0 |= bitmask
      when 2 # Light Grey
        byte_1 |= bitmask
      when 3 # White
        byte_0 |= bitmask
        byte_1 |= bitmask
      end

      bitmask >>= 1
    end

    tile << sprintf("0x%02x,0x%02x", byte_0, byte_1)
  end

  tile.join(",")
end