Class: Burn::Fuel::Rom::Scene

Inherits:
DslBase
  • Object
show all
Defined in:
lib/burn/fuel/rom/scene.rb

Constant Summary collapse

BG =

Where to draw

0
TEXT =

background palette (0x00-0x0f)

1
PALETTE_X1 =

background palette (0x00-0x0f)

5
PALETTE_X2 =

palette A for #screen and #paint method (whcih is a part background palette)

6
PALETTE_X3 =
7
PALETTE_Y1 =
9
PALETTE_Y2 =
10
PALETTE_Y3 =
11
PALETTE_Z1 =
13
PALETTE_Z2 =
14
PALETTE_Z3 =
15
SPRITE =

sprite palette (0x10-0x1f)

17
PALETTE_DEFAULT =
0
PALETTE_X =

palette expression for PPU Attribute table

85
PALETTE_Y =

palette expression for PPU Attribute table

170
PALETTE_Z =

palette expression for PPU Attribute table

255
WHITE =

Color Control

0
LIGHTBLUE =
1
BLUE =
2
PURPLE =
3
PINK =
4
RED =
5
DEEPRED =
6
ORANGE =
7
LIGHTORANGE =
8
DARKGREEN =
9
GREEN =
10
LIGHTGREEN =
11
BLUEGREEN =
12
GRAY =
13
BLACK =
14
DARKEST =

Light Control

0
DARKER =
16
LIGHTER =
32
LIGHTEST =
48

Instance Method Summary collapse

Methods inherited from DslBase

#method_missing

Methods included from Debug

#log

Constructor Details

#initialize(resource_name, context) ⇒ Scene

Returns a new instance of Scene.



47
48
49
# File 'lib/burn/fuel/rom/scene.rb', line 47

def initialize(resource_name, context)
  super(resource_name, context)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Burn::Fuel::DslBase

Instance Method Details

#color(palette, color, lightness = :lighter) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/burn/fuel/rom/scene.rb', line 68

def color(palette, color, lightness=:lighter)
  palette=Scene.const_get(palette.upcase)
  color=Scene.const_get(color.upcase)
  lightness=Scene.const_get(lightness.upcase)
  
  @context.instance_exec {@code_blocks.push "pal_col(#{palette},0x#{(lightness+color).to_s(16)});"}
end

#fade_inObject



55
56
57
58
59
60
61
62
# File 'lib/burn/fuel/rom/scene.rb', line 55

def fade_in
  @context.instance_exec do
    [
      "ppu_on_all();",
      "screen_fade_in();"
    ].each {|p| @code_blocks.push p}
  end
end

#fade_out(sec = 3) ⇒ Object



64
65
66
# File 'lib/burn/fuel/rom/scene.rb', line 64

def fade_out(sec=3)
  @context.instance_exec {@code_blocks.push "screen_fade_out(#{sec.to_s});"}
end

#goto(scene_name) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/burn/fuel/rom/scene.rb', line 149

def goto(scene_name)
  @context.instance_exec do
    [
      "ppu_off();",
      "set_vram_update(0,0);", #clear vram condition for #show
      "vram_adr(0x2000);",     #clear nametable
      "vram_fill(0,1024);",    #clear nametable
      "goto #{scene_name};"
    ].each {|p| @code_blocks.push p}
  end
end

#inline(code) ⇒ Object



199
200
201
# File 'lib/burn/fuel/rom/scene.rb', line 199

def inline(code)
  @context.instance_exec {@code_blocks.push code}
end

#label(string, x = 0, y = 1) ⇒ Object



51
52
53
# File 'lib/burn/fuel/rom/scene.rb', line 51

def label(string, x=0, y=1)
  @context.instance_exec {@code_blocks.push sprintf("put_str(NTADR(%d,%d),\"%s\");", x, y, string.upcase)}
end

#main_loop(rrb_source = nil) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/burn/fuel/rom/scene.rb', line 161

def main_loop(rrb_source=nil)
  rrb_source = File.open("#{@resource_name}.rrb").read if rrb_source.nil? || File.extname(rrb_source) == "#{@resource_name}.rrb"
  
  # preprocess - for show method, all parameters after the 3rd parameter must be converted to String.
  rrb_source = rrb_source.lines.map{|line|
    if /^[\s\t]*show/ =~ line then
      line.chomp.split(",").each_with_index.map{|data, i|
        if i>2 then
          '"' + data + '"'
        else
          data
        end
      }.join(",")
    else
      line.chomp
    end
  }.join("\n")
  
  @context.instance_exec(@context,@resource_name) do |c, resource|
    # preprocess
    [
      "ppu_on_all();",
      "while(1){",
        "ppu_waitnmi(); //wait for next TV frame",
    ].each {|p| @code_blocks.push p}
    
    # parse ruby source code and process them partially
    p = Pxes::Cc65Transpiler.new(Ripper.sexp(rrb_source),c,resource)
    #require 'pp'
    #pp p
    @code_blocks.push p.to_c
    
    # postprocess
    @code_blocks.push "}"
  end
  
end

#paint(dest, palette) ⇒ Object



240
241
242
243
244
245
246
# File 'lib/burn/fuel/rom/scene.rb', line 240

def paint(dest, palette)
  @context.instance_exec(@resource_name) do |resource|
    var_name="screen_#{resource}_color_#{@global.count}"
    @global.push "const unsigned char #{var_name}[6]={0x01,0x"+Scene.const_get(palette.upcase).to_s(16)+",0x01,"+sprintf("0x%.2x",dest.end-dest.begin)+",0x01,0x00};"
    @code_blocks.push "unrle_vram(#{var_name},0x"+(9152+dest.begin).to_s(16)+");" # 9152 is equivalent to 23C0
  end
end

#play(song_title) ⇒ Object Also known as: play_music



76
77
78
79
80
81
# File 'lib/burn/fuel/rom/scene.rb', line 76

def play(song_title)
  @context.instance_exec do
    @global << "extern const unsigned char music_#{song_title}[];"
    @code_blocks << "music_play(music_#{song_title});"
  end
end

#randObject

vcall-ish methods



254
255
256
# File 'lib/burn/fuel/rom/scene.rb', line 254

def rand
  @context.instance_exec {@code_blocks.push "rand8()"}
end

#range(x_from, y_from, x_to, y_to) ⇒ Object

utility methods



262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/burn/fuel/rom/scene.rb', line 262

def range(x_from, y_from, x_to, y_to)
  x_to = x_from if x_to.nil?
  y_to = y_from if y_to.nil?
  
  plots = [x_from,y_from,x_to,y_to].map do |v|
    if v.is_a?(String) && v.end_with?("px") then # x:0-255, y:0:239
      v.to_i / 32
    else
      v.to_i / 4
    end
  end
  
  plots[1]*8+plots[0]..plots[3]*8+plots[2]
end

#screen(map, vars) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/burn/fuel/rom/scene.rb', line 211

def screen(map, vars)
  @context.instance_exec(@resource_name) do |resource|
    current_char = nil
    consective_count = 0
    output = Array.new
    # the last character "\b" is just a dummy to finialize loop process
    (map.gsub(/(\r\n|\r|\n)/,"")+"\b").chars do |c|
      if current_char != c then
        if !current_char.nil? then
          if vars.keys.include?(current_char.to_sym) then
            output.push @pattern_table_index[vars[current_char.to_sym].to_sym] # user defined specific pattern
          else
            output.push 0 # blank pattern
          end
          # repeat to draw same pattern
          output.push 1, consective_count if consective_count>0
        end
        current_char = c
        consective_count = 0
      else
        consective_count += 1
      end
    end
    @global.push "const unsigned char screen_#{resource}["+(output.count+3).to_s+"]={0x01,"+output.map{|p| sprintf "0x%.2x", p}.join(",")+",0x01,0x00};"
    # "unrle_vram" means "decode Run Length Encoding and set them to vram".
    @code_blocks.push "unrle_vram(screen_#{resource},0x2020);" # previously used 0x2000 but it doesn't display very first line of screen.
  end
end

#show(string, x = 0, y = 0, *args) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/burn/fuel/rom/scene.rb', line 87

def show(string, x=0, y=0, *args)
  @context.instance_exec(@resource_name) do |resource|
    nmi_plots = Array.new
    iChrCount=0
    isRequireArgs=false
    vname = resource+"_nmi_list"
    init_name = resource+"_nmi_init"
    
    string.split(//).each do |c|
      if c=='%' then
        isRequireArgs=true
        next
      end
      nmi_plots.push [x+iChrCount,y] if nmi_plots.index([x+iChrCount, y]).nil?
      if !isRequireArgs then
        @code_blocks.push sprintf("#{vname}[%d]=%s;", nmi_plots.index([x+iChrCount,y])*3+2, sprintf("%#x", c.bytes.to_a[0]-32))
        iChrCount+=1
      else
        if c=='d' then
          @code_blocks.push sprintf("#{vname}[%d]=0x10+%s;", nmi_plots.index([x+iChrCount,y])*3+2, args.shift)
          iChrCount+=1
        elsif c=='s' then
          args.shift.split(//).each do |d|
            nmi_plots.push [x+iChrCount,y] if nmi_plots.index([x+iChrCount, y]).nil?
            @code_blocks.push sprintf("#{vname}[%d]=%s;", nmi_plots.index([x+iChrCount,y])*3+2, sprintf("%#x", d.bytes.to_a[0]-32))
            iChrCount+=1
          end
        else
          raise "method #show can't take any data except for %d or %s. Please make sure you are passing correct 4th parameter."
        end
      end
      isRequireArgs=false
    end
    
    if nmi_plots.count>0 then
      # declaration for show method
      @global.push sprintf("static unsigned char #{vname}[%d*3];", nmi_plots.count)
      @global.push sprintf("const unsigned char #{init_name}[%d*3]={", nmi_plots.count)
      nmi_plots.each_with_index do |plot,i|
        @global.push sprintf("MSB(NTADR(%d,%d)),LSB(NTADR(%d,%d)),0%s", plot[0],plot[1],plot[0],plot[1],i<(nmi_plots.count-1)?",":"")
      end
      @global.push "};"
      @code_blocks.unshift "memcpy(#{vname},#{init_name},sizeof(#{init_name}));", sprintf("set_vram_update(%d,#{vname});", nmi_plots.count)
    end
    
  end
end

#sound(effect_name) ⇒ Object Also known as: play_sound



248
249
250
# File 'lib/burn/fuel/rom/scene.rb', line 248

def sound(effect_name)
  @context.instance_exec {@code_blocks.push "sfx_play(SFX_#{effect_name.upcase},0);"}
end

#sprite(resource) ⇒ Object

def sprite_set(x,y,tile_number,palette=0)

@context.instance_exec {@code_blocks.push "oam_spr(#{x},#{y},0x"+tile_number.to_s(16)+",#{palette},0);//0x40 is tile number, i&3 is palette"}

end



207
208
209
# File 'lib/burn/fuel/rom/scene.rb', line 207

def sprite(resource)
  @context.instance_exec {@code_blocks.push "sprite(&#{resource});"}
end

#stopObject



83
84
85
# File 'lib/burn/fuel/rom/scene.rb', line 83

def stop
  @context.instance_exec {@code_blocks.push "music_stop();"}
end

#wait(interval) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/burn/fuel/rom/scene.rb', line 135

def wait(interval)
  #@context.instance_exec do
  #  [
  #    "while(1){",
  #      "ppu_on_all();",
  #      "delay(#{interval});",
  #      "ppu_off();",
  #      "break;",
  #    "}"
  #  ].each {|p| @code_blocks.push p}
  #end
  @context.instance_exec {@code_blocks.push "delay(#{interval});"}
end