Class: Burn::Generator::Rom::CSource

Inherits:
Object
  • Object
show all
Includes:
Debug
Defined in:
lib/burn/generator/rom/c_source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Debug

#log

Constructor Details

#initialize(workspace_root) ⇒ CSource

Returns a new instance of CSource.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/burn/generator/rom/c_source.rb', line 8

def initialize(workspace_root)
  # Generic
  @workspace_root = workspace_root
  @global = Array.new
  @code_blocks = Array.new
  
  # Scene related
  @pattern_tables = Array.new
  @pattern_table_pointer = 80 #0x50 equivalent
  @pattern_table_index = Hash.new
end

Instance Attribute Details

#code_blocksObject (readonly)

Returns the value of attribute code_blocks.



6
7
8
# File 'lib/burn/generator/rom/c_source.rb', line 6

def code_blocks
  @code_blocks
end

#globalObject (readonly)

Returns the value of attribute global.



6
7
8
# File 'lib/burn/generator/rom/c_source.rb', line 6

def global
  @global
end

Instance Method Details

#generateObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/burn/generator/rom/c_source.rb', line 24

def generate
  # Initialization for codeblocks
  @code_blocks.unshift "init();"
  
  # generate source code
  File.write(
    "#{@workspace_root}/tmp/burn/main.c", 
    File.read(File.dirname(__FILE__) + "/template/template.c")
      .gsub(/__@__MAIN__@__/, @code_blocks.join("\n"))
      .gsub(/__@__GLOBAL__@__/, @global.join("\n"))
  )
  
  # save pattern tables associated with source code
  pattern_header = []
  File.open("#{@workspace_root}/tmp/burn/asset/tileset.chr", "rb") do |f|
    pattern_header << f.read(16*80)
  end
  File.open("#{@workspace_root}/tmp/burn/asset/tileset.chr", "wb") do |f|
    f << pattern_header[0]
    @pattern_tables.each do |p|
      f << p
    end
    (8192-16*80-@pattern_tables.count*16).times do |i|
      f << ["0x00"].pack("B*")
    end
  end
  
end

#get_contextObject



20
21
22
# File 'lib/burn/generator/rom/c_source.rb', line 20

def get_context
  self
end