Method: EZDraw::Window#initialize

Defined in:
lib/ezdraw.rb

#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