summaryrefslogtreecommitdiffhomepage
path: root/dragon/draw.rb
blob: 128624d7e5d72b5e5ca0fefe5319b9900817a4ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
198
199
200
201
202
203
204
205
206
207
208
209
210
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# Contributors outside of DragonRuby who also hold Copyright: Nick Sandberg
# Copyright 2019 DragonRuby LLC
# MIT License
# draw.rb has been released under MIT (*only this file*).

module GTK
  class Runtime
    module Draw

      def execute_draw_order pass
        # Don't change this draw order unless you understand
        # the implications.
        render_solids pass
        render_static_solids pass
        render_sprites pass
        render_static_sprites pass
        render_primitives pass
        render_static_primitives pass
        render_labels pass
        render_static_labels pass
        render_lines pass
        render_static_lines pass
        render_borders pass
        render_static_borders pass
      end

      def primitives pass
        if $top_level.respond_to? :primitives_override
          return $top_level.tick_render @args, pass
        end

        execute_draw_order pass

        if !$gtk.production
          # pass.debug.each        { |r| draw_primitive r }
          idx = 0
          length = pass.debug.length
          while idx < length
            draw_primitive (pass.debug.at idx)
            idx += 1
          end

          # pass.static_debug.each { |r| draw_primitive r }
          idx = 0
          length = pass.static_debug.length
          while idx < length
            draw_primitive (pass.static_debug.at idx)
            idx += 1
          end
        end

        # pass.reserved.each          { |r| draw_primitive r }
        idx = 0
        length = pass.reserved.length
        while idx < length
          draw_primitive (pass.reserved.at idx)
          idx += 1
        end

        # pass.static_reserved.each   { |r| draw_primitive r }
        idx = 0
        length = pass.static_reserved.length
        while idx < length
          draw_primitive (pass.static_reserved.at idx)
          idx += 1
        end
      rescue Exception => e
        pause!
        pretty_print_exception_and_export! e
      end


      def render_solids pass
        # pass.solids.each            { |s| draw_solid s }
        # while loops are faster than each with block
        idx = 0
        length = pass.solids.length
        while idx < pass.solids.length
          draw_solid (pass.solids.at idx) # accessing an array using .value instead of [] is faster
          idx += 1
        end
      end

      def render_static_solids pass
        # pass.static_solids.each     { |s| draw_solid s }
        idx = 0
        length = pass.static_solids.length
        while idx < length
          draw_solid (pass.static_solids.at idx)
          idx += 1
        end
      end

      def render_sprites pass
        # pass.sprites.each           { |s| draw_sprite s }
        idx = 0
        length = pass.sprites.length
        while idx < length
          draw_sprite (pass.sprites.at idx)
          idx += 1
        end
      end

      def render_static_sprites pass
        # pass.static_sprites.each    { |s| draw_sprite s }
        idx = 0
        length = pass.static_sprites.length
        while idx < length
          draw_sprite (pass.static_sprites.at idx)
          idx += 1
        end
      end

      def render_primitives pass
        # pass.primitives.each        { |p| draw_primitive p }
        idx = 0
        length = pass.primitives.length
        while idx < length
          draw_primitive (pass.primitives.at idx)
          idx += 1
        end
      end

      def render_static_primitives pass
        # pass.static_primitives.each { |p| draw_primitive p }
        idx = 0
        length = pass.static_primitives.length
        while idx < length
          draw_primitive (pass.static_primitives.at idx)
          idx += 1
        end
      end

      def render_labels pass
        # pass.labels.each            { |l| draw_label l }
        idx = 0
        length = pass.labels.length
        while idx < length
          draw_label (pass.labels.at idx)
          idx += 1
        end
      end

      def render_static_labels pass
        # pass.static_labels.each     { |l| draw_label l }
        idx = 0
        length = pass.static_labels.length
        while idx < length
          draw_label (pass.static_labels.at idx)
          idx += 1
        end
      end

      def render_lines pass
        # pass.lines.each             { |l| draw_line l }
        idx = 0
        length = pass.lines.length
        while idx < length
          draw_line (pass.lines.at idx)
          idx += 1
        end
      end
      
      def render_static_lines pass
        # pass.static_lines.each      { |l| draw_line l }
        idx = 0
        length = pass.static_lines.length
        while idx < pass.static_lines.length
          draw_line (pass.static_lines.at idx)
          idx += 1
        end
      end

      def render_borders pass
        # pass.borders.each           { |b| draw_border b }
        idx = 0
        length = pass.borders.length
        while idx < length
          draw_border (pass.borders.at idx)
          idx += 1
        end
      end

      def render_static_borders pass
        # pass.static_borders.each    { |b| draw_border b }
        idx = 0
        length = pass.static_borders.length
        while idx < length
          draw_border (pass.static_borders.at idx)
          idx += 1
        end
      end

      def draw_solid s
        return unless s
        if s.respond_to? :draw_override
          s.draw_override @ffi_draw
        else
          @ffi_draw.draw_solid s.x, s.y, s.w, s.h, s.r, s.g, s.b, s.a
        end
      rescue Exception => e
        raise_conversion_for_rendering_failed s, e, :solid
      end

      def draw_sprite s
        return unless s
        if s.respond_to? :draw_override
          s.draw_override @ffi_draw
        else
          @ffi_draw.draw_sprite_3 s.x, s.y, s.w, s.h,
                                  s.path.s_or_default,
                                  s.angle,
                                  s.a, s.r, s.g, s.b,
                                  s.tile_x, s.tile_y, s.tile_w, s.tile_h,
                                  !!s.flip_horizontally, !!s.flip_vertically,
                                  s.angle_anchor_x, s.angle_anchor_y,
                                  s.source_x, s.source_y, s.source_w, s.source_h
        end
      rescue Exception => e
        raise_conversion_for_rendering_failed s, e, :sprite
      end

      def draw_screenshot s
        return unless s
        if s.respond_to? :draw_override
          s.draw_override @ffi_draw
        else
          @ffi_draw.draw_screenshot s.path.s_or_default,
                                    s.x, s.y, s.w, s.h,
                                    s.angle,
                                    s.a, s.r, s.g, s.b,
                                    s.tile_x, s.tile_y, s.tile_w, s.tile_h,
                                    !!s.flip_horizontally, !!s.flip_vertically,
                                    s.angle_anchor_x, s.angle_anchor_y,
                                    s.source_x, s.source_y, s.source_w, s.source_h
        end
      rescue Exception => e
        raise_conversion_for_rendering_failed s, e, :screenshot
      end

      def draw_label l
        return unless l
        if l.respond_to? :draw_override
          l.draw_override @ffi_draw
        else
          @ffi_draw.draw_label l.x, l.y, l.text.s_or_default,
                               l.size_enum, l.alignment_enum,
                               l.r, l.g, l.b, l.a,
                               l.font.s_or_default(nil)
        end
      rescue Exception => e
        raise_conversion_for_rendering_failed l, e, :label
      end

      def draw_line l
        return unless l
        if l.respond_to? :draw_override
          l.draw_override @ffi_draw
        else
          @ffi_draw.draw_line l.x, l.y, l.x2, l.y2, l.r, l.g, l.b, l.a
        end
      rescue Exception => e
        raise_conversion_for_rendering_failed l, e, :line
      end

      def draw_border s
        return unless s
        if s.respond_to? :draw_override
          s.draw_override @ffi_draw
        else
          @ffi_draw.draw_border s.x, s.y, s.w, s.h, s.r, s.g, s.b, s.a
        end
      rescue Exception => e
        raise_conversion_for_rendering_failed s, e, :border
      end

      def draw_screenshots
        @args.outputs.screenshots.each { |s| draw_screenshot s }
      end

      def pixel_arrays
        @args.pixel_arrays.each { |k,v|
          if v.pixels.length == (v.width * v.height)  # !!! FIXME: warning? exception? Different API?
            @ffi_draw.upload_pixel_array k.to_s, v.width.to_i, v.height.to_i, v.pixels
          end
        }
      rescue Exception => e
        pause!
        pretty_print_exception_and_export! e
      end

    end
  end
end