Class: DXRubyRP5::RenderTarget

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, _bgcolor = [0, 0, 0, 0]) ⇒ RenderTarget

Returns a new instance of RenderTarget.



7
8
9
10
11
12
# File 'lib/dxruby_rp5/render_target.rb', line 7

def initialize(width, height, _bgcolor = [0, 0, 0, 0])
  _bgcolor = to_rp5_color(_bgcolor)
  @bgcolor = $app.color(*_bgcolor)
  @_surface = $app.create_graphics(width, height)
  @queue = []
end

Instance Attribute Details

#_surfaceObject (readonly)

Returns the value of attribute _surface.



5
6
7
# File 'lib/dxruby_rp5/render_target.rb', line 5

def _surface
  @_surface
end

#bgcolorObject

Returns the value of attribute bgcolor.



4
5
6
# File 'lib/dxruby_rp5/render_target.rb', line 4

def bgcolor
  @bgcolor
end

Instance Method Details

#draw(x, y, image, z = 0) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/dxruby_rp5/render_target.rb', line 41

def draw(x, y, image, z = 0)
  return if image.nil?
  draw_task = {
    :proc => lambda { @_surface.image(image._surface, x, y) },
    :z => z
  }
  @queue << draw_task
end

#draw_tile(basex, basey, map, image_arr, startx, starty, sizex, sizey, z = 0) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dxruby_rp5/render_target.rb', line 50

def draw_tile(basex, basey, map, image_arr, startx, starty, sizex, sizey, z = 0)
  image_arr = image_arr.flatten
  first_img = image_arr[0]
  w, h = first_img.width, first_img.height
  startx_mod_w = startx % w
  starty_mod_h = starty % h

  from_i = starty_mod_h < 0 ? -1 : 0
  to_i   = sizey + (starty_mod_h <= 0 ?  0 : 1)
  from_j = startx_mod_w < 0 ? -1 : 0
  to_j   = sizex + (startx_mod_w <= 0 ?  0 : 1)

  y = basey - (starty_mod_h < 0 ? h + starty_mod_h : starty_mod_h)
  init_x = basex - (startx_mod_w < 0 ? w + startx_mod_w : startx_mod_w)

  (from_i..to_i).each do |i|
    if (i + starty / h) < 0
      my = (((i + starty / h) % map.size) + map.size) % map.size
    else
      my = (i + starty / h) % map.size
    end
    map_row = map[my]

    x = init_x
    (from_j..to_j).each do |j|
      if (j + startx / w) < 0
        mx = (((j + startx / w) % map_row.size) + map_row.size) % map_row.size
      else
        mx = (j + startx / w) % map_row.size
      end
      idx = map_row[mx]

      map_img = image_arr[idx]
      self.draw(x, y, map_img, z)

      x += w
    end
    y += h
  end
end

#heightObject



18
19
20
# File 'lib/dxruby_rp5/render_target.rb', line 18

def height
  return @_surface.height
end

#updateObject



31
32
33
34
35
36
37
38
39
# File 'lib/dxruby_rp5/render_target.rb', line 31

def update
  @_surface.begin_draw()
  @_surface.background(@bgcolor)
  @queue.sort { |a, b| a[:z] <=> b[:z] }.each do |task|
    task[:proc].call
  end
  @queue.clear
  @_surface.end_draw()
end

#widthObject



14
15
16
# File 'lib/dxruby_rp5/render_target.rb', line 14

def width
  return @_surface.width
end