class EZDraw::Window

Attributes

fill[W]

…however, traditional foo= writers are also provided

font[W]

…however, traditional foo= writers are also provided

stroke[W]

…however, traditional foo= writers are also provided

Public Class Methods

_destroy(win, ren, buf, dflag) click to toggle source
# 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
cleanup() click to toggle source
# File lib/ezdraw.rb, line 190
def self.cleanup
 EZDraw.requires_init
 @@instances.each {|win| win.close}
 @@instances = []
end
new(size=:default, title=:default, *opts) click to toggle source
# 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
parse_rect(r) click to toggle source
# File lib/ezdraw.rb, line 303
def self.parse_rect(r)
end

Public Instance Methods

auto_update(enable_p) click to toggle source
# 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) click to toggle source
# File lib/ezdraw.rb, line 358
def auto_update=(enable_p)
 auto_update(enable_p)
end
auto_update?() click to toggle source
# File lib/ezdraw.rb, line 347
def auto_update?
 @auto_update
end
circle(x, y, r) click to toggle source
# 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
clear() click to toggle source
# File lib/ezdraw.rb, line 261
def clear
 self.render_draw_color = fill
 SDL2.SDL_RenderClear(@ren)
 need_update
end
close() click to toggle source
# File lib/ezdraw.rb, line 186
def close
 self.class._destroy(@win, @ren, @buf, @dflag)
end
draw(&block) click to toggle source

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?

# File lib/ezdraw.rb, line 366
def draw(&block)
 auto_update(false)
 block.call
 auto_update(true)
end
fill(color=nil) click to toggle source
# File lib/ezdraw.rb, line 203
def fill(color=nil)
 color ? @fill=color : @fill
end
font(fnt=nil) click to toggle source
# File lib/ezdraw.rb, line 207
def font(fnt=nil)
 fnt ? @font=fnt : @font
end
get_window_size() click to toggle source
# 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
height() click to toggle source
# File lib/ezdraw.rb, line 235
def height
 get_window_size[1]
end
image(x, y, img) click to toggle source
# 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) click to toggle source
# 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_update() click to toggle source
# File lib/ezdraw.rb, line 372
def need_update
 @need_update = true
 update if auto_update?
end
pop_context() click to toggle source
# 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_context() click to toggle source
# 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) click to toggle source
# 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
stroke(color=nil) click to toggle source

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…

# File lib/ezdraw.rb, line 199
def stroke(color=nil)
 color ? @stroke=color : @stroke
end
text(x, y, text) click to toggle source

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

# 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
update() click to toggle source
# 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
width() click to toggle source
# File lib/ezdraw.rb, line 231
def width
 get_window_size[0]
end

Private Instance Methods

get_renderer_info() click to toggle source
# File lib/ezdraw.rb, line 239
def get_renderer_info
 p_rinfo = FFI::MemoryPointer.new :uint8, SDL2::SDL_RendererInfo.size, false
 SDL2::SDL_GetRendererInfo(@ren, p_rinfo)
 rinfo = SDL2::SDL_RendererInfo.new p_rinfo
end
render_draw_color() click to toggle source
# File lib/ezdraw.rb, line 246
def render_draw_color
 pr = FFI::MemoryPointer.new :uint8
 pg = FFI::MemoryPointer.new :uint8
 pb = FFI::MemoryPointer.new :uint8
 pa = FFI::MemoryPointer.new :uint8
 SDL2::SDL_GetRenderDrawColor(@ren, pr, pg, pb, pa)
 [pr.get_uint8(0), pg.get_uint8(0), pb.get_uint8(0), pa.get_uint8(0)]
end
render_draw_color=(rgba) click to toggle source
# File lib/ezdraw.rb, line 256
def render_draw_color=(rgba)
 SDL2::SDL_SetRenderDrawColor(@ren, rgba[0], rgba[1], rgba[2], rgba[3])
end