Class: SnesUtils::Tmx2Snes

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

Instance Method Summary collapse

Constructor Details

#initialize(file_path, big_char: false, palette: 0, v: false, h: false, p: false) ⇒ Tmx2Snes

Returns a new instance of Tmx2Snes.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tmx2snes/tmx2snes.rb', line 3

def initialize(file_path, big_char:false, palette:0, v:false, h:false, p:false)
  raise unless File.file? file_path
  @file_path = file_path
  @file_dir = File.dirname(@file_path)
  @file_name = File.basename(@file_path, File.extname(@file_path))
  @tilemap = []
  raise if palette < 0 || palette > 7
  @palette = palette
  @v_flip = v ? "1" : "0"
  @h_flip = h ? "1" : "0"
  @prio_bg3 = p ? "1" : "0"
  tnm = big_char ? 2 : 1 # big_char : 16x16 tiles. otherwise, 8x8 tiles
  row_offset = 16 * (tnm - 1) # Skip a row in case of 16x16 tiles ( tile #9 starts at index 32)

  CSV.foreach(@file_path) do |row|
    raise if row.length != 32
    @tilemap += row.map { |r| (r.to_i)*tnm + row_offset * ((r.to_i)/8).to_i }
  end

  raise if @tilemap.length != 32*32
  dummy = 1023 # TODO: check max tile per tileset
  raise if @tilemap.map { |t| t < 0 || t > dummy }.include? true
end

Instance Method Details

#tile_to_data(tile) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/tmx2snes/tmx2snes.rb', line 27

def tile_to_data tile
  tile_name = "%010b" % tile
  palette_name = "%03b" % @palette

  tile_hl = @v_flip + @h_flip + @prio_bg3 + palette_name + tile_name
  tile_data = tile_hl.scan(/.{8}/)
  tile_data.reverse.map { |b| "%02x" % b.to_i(2) }

end

#tilemap_to_dataObject



37
38
39
40
41
42
43
# File 'lib/tmx2snes/tmx2snes.rb', line 37

def tilemap_to_data
  bg_sc_data = []
  @tilemap.each do |tile|
    bg_sc_data.push(tile_to_data(tile))
  end
  bg_sc_data
end

#writeObject



45
46
47
48
49
50
# File 'lib/tmx2snes/tmx2snes.rb', line 45

def write
  out = File.expand_path("#{@file_name}.map", @file_dir)
  File.open(out, 'w+b') do |file|
    file.write([tilemap_to_data.join].pack('H*'))
  end
end