Class: SDL2::Window

Inherits:
Struct
  • Object
show all
Defined in:
lib/sdl2/window.rb

Overview

System Window A rectangular area you can blit into.

Defined Under Namespace

Modules: FLAGS Classes: Data

Constant Summary collapse

DEFAULT =

These are the defaults a Window is created with unless overridden

{
 title: "SDL2::Window",
 x: :CENTERED,
 y: :CENTERED,
 width: 320,
 height: 240,
 flags: 0
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Struct

#==, cast, #free, #to_s, #update_members

Methods included from StructHelper

#member_readers, #member_writers

Constructor Details

#initialize(*args, &block) ⇒ Window

Construct a new window.



144
145
146
# File 'lib/sdl2/window.rb', line 144

def initialize(*args, &block)
  super(*args, &block)
end

Class Method Details

.create(options = {}) ⇒ Object

Construct a new window with given:

*  title: The caption to use for the window
*  x: The x-position of the window
*  y: The y-position of the window
*  w: The width of the window
*  h: The height of the window
*  flags: Window Flags to use in construction


165
166
167
168
169
170
# File 'lib/sdl2/window.rb', line 165

def self.create(options = {})            
  o = DEFAULT.merge(options)
  Debug.log(self){"Creating with options: #{o.inspect}"}
  # TODO: Log unused option keys      
  SDL2.create_window!(o[:title], o[:x], o[:y], o[:width], o[:height], o[:flags])
end

.create_from(data) ⇒ Object

Constructs a new window from arbitrary system-specific structure

*  data: Some system-specific pointer

See SDL Documentation



175
176
177
178
# File 'lib/sdl2/window.rb', line 175

def self.create_from(data)
  Debug.log(self){"Creating from data: #{data.inspect}"}
  create_window_from!(data)
end

.create_with_renderer(w, h, flags) ⇒ Object

Constructs both a window and a renderer

*  w: The Width of the pair to create
*  h: The Height of the pair to create
*  flags: Window flags to utilize in creation


190
191
192
193
194
195
196
197
198
199
# File 'lib/sdl2/window.rb', line 190

def self.create_with_renderer(w, h, flags)
  
  window = Window.new
  renderer = Renderer.new
  if SDL2.create_window_and_renderer(w,h,flags,window,renderer) == 0
    [window, renderer]
  else
    nil
  end
end

.from_id(id) ⇒ Object

Returns the identified window already created

*  id: The window identifier to retrieve


182
183
184
# File 'lib/sdl2/window.rb', line 182

def self.from_id(id)
  get_window_from_id!(id)
end

.release(pointer) ⇒ Object

Release memory utilized by structure



202
203
204
205
# File 'lib/sdl2/window.rb', line 202

def self.release(pointer)
  fc = caller.first      
  Debug.log(self){"Release ignored from #{fc}"}
end

Instance Method Details

#brightnessObject

Return the brightness level



219
220
221
# File 'lib/sdl2/window.rb', line 219

def brightness
  SDL2.get_window_brightness(self)
end

#brightness=(level) ⇒ Object

Set the brightness level



224
225
226
227
# File 'lib/sdl2/window.rb', line 224

def brightness=(level)
  Debug.log(self){"Setting brightness to: #{level}"}
  SDL2.set_window_brightness(self, level.to_f)
end

#current_sizeObject

Get the window’s current size

Returns:

  • Array => [<width>, <height>]



323
324
325
326
327
# File 'lib/sdl2/window.rb', line 323

def current_size()
  size = 2.times.map{TypedPointer::Int.new}
  SDL2::get_window_size(self, *size)
  size.map(&:value)    
end

#current_size=(size) ⇒ Object

Set the window’s current size

*  size: A array containing the [width,height]


340
341
342
# File 'lib/sdl2/window.rb', line 340

def current_size=(size)
  SDL2.set_window_size(self, size[0], size[1])
end

#dataObject

The Window’s data manager.



139
140
141
# File 'lib/sdl2/window.rb', line 139

def data
  @data ||= Data.new(self)
end

#destroyObject

Tell SDL we are done with the window. Any further use could result in a crash.



208
209
210
211
212
213
214
# File 'lib/sdl2/window.rb', line 208

def destroy
  unless self.null?
    SDL2.destroy_window(self)        
  else
    Debug.log(self){"Destruction of window null window requested."}
  end
end

#displayObject

Get the display associated with this window



242
243
244
# File 'lib/sdl2/window.rb', line 242

def display
  Display[display_index]
end

#display_indexObject

Get the display index associated with this window



237
238
239
# File 'lib/sdl2/window.rb', line 237

def display_index
  SDL2.get_window_display_index(self)
end

#display_modeObject

Get a copy of the DisplayMode structure



230
231
232
233
234
# File 'lib/sdl2/window.rb', line 230

def display_mode
  dm = SDL2::Display::Mode.new
  SDL2.get_window_display_mode!(self, dm)
  dm
end

#flagsObject

Get the window flags



247
248
249
# File 'lib/sdl2/window.rb', line 247

def flags
  SDL2.get_window_flags(self)
end

#fullscreen=(flags) ⇒ Object

Set the window’s FULLSCREEN mode flags.



394
395
396
# File 'lib/sdl2/window.rb', line 394

def fullscreen=(flags)
  SDL2.set_window_fullscreen(self, flags)
end

#grab=(value) ⇒ Object

Set the input grab mode



257
258
259
# File 'lib/sdl2/window.rb', line 257

def grab=(value)
  SDL2.set_window_grab(self, value)
end

#grab?Boolean

The window’s input grab mode

Returns:

  • (Boolean)


252
253
254
# File 'lib/sdl2/window.rb', line 252

def grab?
  SDL2.get_window_grab?(self)
end

#heightObject



333
334
335
# File 'lib/sdl2/window.rb', line 333

def height
  current_size[1]
end

#hideObject

Hide the window



282
283
284
# File 'lib/sdl2/window.rb', line 282

def hide
  SDL2.hide_window(self)
end

#icon=(surface) ⇒ Object

Set the window’s icon from a surface



312
313
314
# File 'lib/sdl2/window.rb', line 312

def icon=(surface)
  set_window_icon(self, surface)
end

#idObject

Get the window identifier



262
263
264
# File 'lib/sdl2/window.rb', line 262

def id
  SDL2.get_window_id(self)
end

#maximizeObject

Maximize the window



287
288
289
# File 'lib/sdl2/window.rb', line 287

def maximize
  SDL2.maximize_window(self)
end

#maximum_sizeObject

Get the window’s maximum_size

Returns:

  • Array => [<width>, <height>]



346
347
348
349
350
# File 'lib/sdl2/window.rb', line 346

def maximum_size
  size = 2.times.map{TypedPointer::Int.new}
  SDL2::get_window_maximum_size(self, *size)
  size.map(&:value)
end

#maximum_size=(size) ⇒ Object

Set the window’s maximum size

*  size: A array containing the [width,height]


354
355
356
# File 'lib/sdl2/window.rb', line 354

def maximum_size=(size)
  SDL2.set_window_maximum_size(self, size[0], size[1])
end

#minimizeObject

Minimize the window



292
293
294
# File 'lib/sdl2/window.rb', line 292

def minimize
  SDL2.minimize_window(self)
end

#minimum_sizeObject

Get the window’s minimum size

Returns:

  • Array => [<width>, <height>]



360
361
362
363
364
# File 'lib/sdl2/window.rb', line 360

def minimum_size
  size = 2.times.map{TypedPointer::Int.new}
  SDL2::get_window_minimum_size(self, *size)
  size.map(&:value)
end

#minimum_size=(size) ⇒ Object

Set the window’s minimum size

*  size: A array containing the [width,height]


368
369
370
# File 'lib/sdl2/window.rb', line 368

def minimum_size=(size)
  SDL2.set_window_minimum_size(self, size[0], size[1])
end

#pixel_formatObject

Get the window pixel format



267
268
269
# File 'lib/sdl2/window.rb', line 267

def pixel_format
  SDL2.get_window_pixel_format(self)
end

#positionObject

Get the window’s position

Returns:

  • Array => [<x>, <y>]



374
375
376
377
378
379
380
# File 'lib/sdl2/window.rb', line 374

def position
  position = [TypedPointer::Int.new, TypedPointer::Int.new]
  SDL2::get_window_position(self, position[0], position[1])
  x, y = position[0][:value], position[1][:value]
  position.each(&:free)
  [x, y]
end

#position=(location) ⇒ Object

Set the window’s position

*  size: A array containing the [x,y]


384
385
386
# File 'lib/sdl2/window.rb', line 384

def position=(location)
  SDL2::set_window_position(self, location[0],location[1])
end

#raise_aboveObject

Raise the window



297
298
299
# File 'lib/sdl2/window.rb', line 297

def raise_above
  SDL2.raise_window(self)
end

#rendererObject

Returns the renderer associated with this window



401
402
403
# File 'lib/sdl2/window.rb', line 401

def renderer
  SDL2.get_renderer(self)
end

#renderer_to_surface(renderer = renderer) ⇒ Object

Utility function that returns an SDL2::Surface of a given render. Defaults to the renderer returned by SDL_GetRenderer(window=self) Added by BadQuanta originally for approval testing.



407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/sdl2/window.rb', line 407

def renderer_to_surface(renderer = renderer)
  w, h = renderer.output_size
  fmt = surface.format
  surface = SDL2::Surface.create_rgb(0,w,h,
    fmt.bits_per_pixel,
    fmt.r_mask,
    fmt.g_mask,
    fmt.b_mask,
    fmt.a_mask
  )
  SDL2.render_read_pixels!(renderer, nil, fmt.format, surface.pixels, surface.pitch)
  surface
end

#restoreObject

Restore the window



302
303
304
# File 'lib/sdl2/window.rb', line 302

def restore
  SDL2.restore_window(self)
end

#showObject

Show the window



307
308
309
# File 'lib/sdl2/window.rb', line 307

def show
  SDL2.show_window(self)
end

#surfaceObject

Return the surface associated with the window



389
390
391
# File 'lib/sdl2/window.rb', line 389

def surface
  SDL2.get_window_surface(self)
end

#titleObject

Get the window title caption



272
273
274
# File 'lib/sdl2/window.rb', line 272

def title
  SDL2.get_window_title(self)
end

#title=(value) ⇒ Object

Set the window title caption



277
278
279
# File 'lib/sdl2/window.rb', line 277

def title=(value)
  SDL2.set_window_title(self, value)
end

#update_surfaceObject

Update the window’s surface



317
318
319
# File 'lib/sdl2/window.rb', line 317

def update_surface()
  SDL2.update_window_surface!(self)      
end

#widthObject



329
330
331
# File 'lib/sdl2/window.rb', line 329

def width
  current_size[0]
end