Class: EZDraw::Window

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

Constant Summary collapse

@@instances =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size = :default, title = :default, *opts) ⇒ Window

Returns a new instance of Window.



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
# File 'lib/ezdraw.rb', line 122

def initialize(size=:default, title=:default, *opts)
 EZDraw.requires_init

 if size == :default
  # TODO: use SDL's GetCurrentDisplayMode and make default proportional (30% ?) the size of screen
  w = h = 500
 else
  w, h = size
 end

 if title == :default
  title = $PROGRAM_NAME
 end

 # TODO: couldn't find an elegant way to put this option in the interface
 # perhaps kv args? but then how to pass fullscreen?
 x = y = SDL2::SDL_WINDOWPOS_UNDEFINED

 wflags = 0
 wflags |= SDL2::SDL_WINDOW_FULLSCREEN if opts.include? :fullscreen

 @win = SDL2.SDL_CreateWindow(title, x, y, w, h, wflags)

 # TODO: add renderer options to interface
 # eg. :software, :accelerated, :vsync
 @ren = SDL2.SDL_CreateRenderer(@win, -1, SDL2::SDL_RENDERER_ACCELERATED | SDL2::SDL_RENDERER_TARGETTEXTURE)

 sz = get_window_size
 rinfo = get_renderer_info
 @buf = SDL2::SDL_CreateTexture(@ren, rinfo[:texture_formats][0],
                                SDL2::SDL_TEXTUREACCESS_TARGET, sz[0], sz[1])
 SDL2.SDL_SetRenderTarget(@ren, @buf)

 @fill = White
 @stroke = Black
 @font = EZDraw::default_font
 @dc_stack = []

 self.render_draw_color = fill
 SDL2.SDL_RenderClear(@ren)

 auto_update(true)

 # finalizer
 @dflag = [false]
 ObjectSpace.define_finalizer(self, proc {|id|
  self.class._destroy(@win, @ren, @buf, @dflag)
 })
 @@instances << self
end

Instance Attribute Details

#fill(color = nil) ⇒ Object



203
204
205
# File 'lib/ezdraw.rb', line 203

def fill(color=nil)
 color ? @fill=color : @fill
end

#font(fnt = nil) ⇒ Object



207
208
209
# File 'lib/ezdraw.rb', line 207

def font(fnt=nil)
 fnt ? @font=fnt : @font
end

#stroke(color = nil) ⇒ Object

NOTE: these accessors are to circumvent the method/local-variable ambiguity that results with “stroke = color”, and thus needs to be “self.stroke = color” however…



199
200
201
# File 'lib/ezdraw.rb', line 199

def stroke(color=nil)
 color ? @stroke=color : @stroke
end

Class Method Details

._destroy(win, ren, buf, dflag) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ezdraw.rb', line 173

def self._destroy(win, ren, buf, dflag)
 if dflag[0]
  #EZDraw.logger.debug("(already destroyed window #{win})")
  return
 end
 EZDraw.logger.debug("destroy window #{win}")

 SDL2.SDL_DestroyTexture(buf)
 SDL2.SDL_DestroyRenderer(ren)
 SDL2.SDL_DestroyWindow(win)
 dflag[0] = true
end

.cleanupObject



190
191
192
193
194
# File 'lib/ezdraw.rb', line 190

def self.cleanup
 EZDraw.requires_init
 @@instances.each {|win| win.close}
 @@instances = []
end

.parse_rect(r) ⇒ Object



303
304
# File 'lib/ezdraw.rb', line 303

def self.parse_rect(r)
end

Instance Method Details

#auto_update(enable_p) ⇒ Object



351
352
353
354
355
356
# File 'lib/ezdraw.rb', line 351

def auto_update(enable_p)
 # force an update if enabling auto
 update if enable_p and not @auto_update

 @auto_update = enable_p
end

#auto_update=(enable_p) ⇒ Object



358
359
360
# File 'lib/ezdraw.rb', line 358

def auto_update=(enable_p)
 auto_update(enable_p)
end

#auto_update?Boolean

Returns:

  • (Boolean)


347
348
349
# File 'lib/ezdraw.rb', line 347

def auto_update?
 @auto_update
end

#circle(x, y, r) ⇒ Object



277
278
279
280
281
# File 'lib/ezdraw.rb', line 277

def circle(x, y, r)
 SDL2::Gfx.filledCircleRGBA(@ren, x, y, r, fill[0], fill[1], fill[2], fill[3])
 SDL2::Gfx.circleRGBA(@ren, x, y, r, stroke[0], stroke[1], stroke[2], stroke[3])
 need_update
end

#clearObject



261
262
263
264
265
# File 'lib/ezdraw.rb', line 261

def clear
 self.render_draw_color = fill
 SDL2.SDL_RenderClear(@ren)
 need_update
end

#closeObject



186
187
188
# File 'lib/ezdraw.rb', line 186

def close
 self.class._destroy(@win, @ren, @buf, @dflag)
end

#draw(&block) ⇒ Object

draw all simultaneously and update at the end BUG: should nest but doesn’t BUG: if the block meddles with auto_update, the contract fails perhaps an auto_update override-lock mechanism to fix this?



366
367
368
369
370
# File 'lib/ezdraw.rb', line 366

def draw(&block)
 auto_update(false)
 block.call
 auto_update(true)
end

#get_window_sizeObject



224
225
226
227
228
229
# File 'lib/ezdraw.rb', line 224

def get_window_size
 p_w = FFI::MemoryPointer.new :int
 p_h = FFI::MemoryPointer.new :int
 SDL2.SDL_GetWindowSize(@win, p_w, p_h)
 [p_w.get_int(0), p_h.get_int(0)]
end

#heightObject



235
236
237
# File 'lib/ezdraw.rb', line 235

def height
 get_window_size[1]
end

#image(x, y, img) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/ezdraw.rb', line 306

def image(x, y, img)

 if img.is_a? String
  img = Image.new(img)
 end

 sfc = img.instance_exec {self.sfc}

 tex = SDL2.SDL_CreateTextureFromSurface(@ren, sfc)
 #w_p = FFI::MemoryPointer.new :int, 1, false
 #h_p = FFI::MemoryPointer.new :int, 1, false
 #err = SDL2.SDL_QueryTexture(tex, nil, nil, w_p, h_p)
 #raise "SDL_QueryTexture: #{SDL2::SDL_GetError}" if err != 0
 #img_w, img_h = w_p.get_int(0), h_p.get_int(0)

 src_r = dst_r = nil 
 
 if x.is_a? Numeric
  dst_r = SDL2::SDL_Rect.new
  dst_r[:x], dst_r[:y], dst_r[:w], dst_r[:h] = x, y, img.width, img.height
 else
  if x.is_a? Array
   src_r = SDL2::SDL_Rect.new
   src_r[:x], src_r[:y], src_r[:w], src_r[:h] = x[0], x[1], img.width, img.height
   src_r[:w], src_r[:h] = x[2], x[3] if x.length == 4
  end

  if y.is_a? Array
   dst_r = SDL2::SDL_Rect.new
   dst_r[:x], dst_r[:y], dst_r[:w], dst_r[:h] = y[0], y[1], img.width, img.height
   dst_r[:w], dst_r[:h] = y[2], y[3] if y.length == 4
  end 
end

 SDL2.SDL_RenderCopy(@ren, tex, src_r, dst_r)

 # BUG: cleanup after exception
 SDL2.SDL_DestroyTexture(tex)
 need_update
end

#line(x0, y0, x1, y1) ⇒ Object



267
268
269
270
# File 'lib/ezdraw.rb', line 267

def line(x0, y0, x1, y1)
 SDL2::Gfx.lineRGBA(@ren, x0, y0, x1, y1, stroke[0], stroke[1], stroke[2], stroke[3])
 need_update
end

#need_updateObject



372
373
374
375
# File 'lib/ezdraw.rb', line 372

def need_update
 @need_update = true
 update if auto_update?
end

#pop_contextObject



218
219
220
221
222
# File 'lib/ezdraw.rb', line 218

def pop_context
 raise "dc stack empty" if @dc_stack.length == 0
 @stroke, @fill, @font, auto_up = @dc_stack.pop
 auto_update(auto_up)
end

#push_contextObject



214
215
216
# File 'lib/ezdraw.rb', line 214

def push_context
 @dc_stack.push([@stroke.clone, @fill.clone, @font, @auto_update])
end

#rect(x0, y0, x1, y1) ⇒ Object



272
273
274
275
# File 'lib/ezdraw.rb', line 272

def rect(x0, y0, x1, y1)
 SDL2::Gfx.boxRGBA(@ren, x0, y0, x1, y1, fill[0], fill[1], fill[2], fill[3])
 SDL2::Gfx.rectangleRGBA(@ren, x0, y0, x1, y1, stroke[0], stroke[1], stroke[2], stroke[3])
end

#text(x, y, text) ⇒ Object

x,y => draw location (or…) x,y => src_rect, dst_rect arrays



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/ezdraw.rb', line 285

def text(x, y, text)
 sfc = @font.render(text, stroke)
 tex = SDL2.SDL_CreateTextureFromSurface(@ren, sfc)

 w_p = FFI::MemoryPointer.new :int, 1, false
 h_p = FFI::MemoryPointer.new :int, 1, false
 err = SDL2.SDL_QueryTexture(tex, nil, nil, w_p, h_p)
 raise "SDL_QueryTexture: #{SDL2::SDL_GetError}" if err != 0

 r = SDL2::SDL_Rect.new
 r[:x] = x
 r[:y] = y
 r[:w] = w_p.get_int(0)
 r[:h] = h_p.get_int(0)
 SDL2.SDL_RenderCopy(@ren, tex, nil, r)
 need_update
end

#updateObject



377
378
379
380
381
382
383
384
# File 'lib/ezdraw.rb', line 377

def update
 return if not @need_update
 SDL2.SDL_SetRenderTarget(@ren, nil)
 SDL2.SDL_RenderCopy(@ren, @buf, nil, nil)
 SDL2.SDL_RenderPresent(@ren)
 SDL2.SDL_SetRenderTarget(@ren, @buf)
 @need_update = false
end

#widthObject



231
232
233
# File 'lib/ezdraw.rb', line 231

def width
 get_window_size[0]
end