Class: RbSDL2::Window

Inherits:
Object
  • Object
show all
Includes:
PixelFormatEnum, Dialog, Display, Grab, Position, Shape, Size, WindowFlags
Defined in:
lib/rb_sdl2/window.rb,
lib/rb_sdl2/window/grab.rb,
lib/rb_sdl2/window/size.rb,
lib/rb_sdl2/window/shape.rb,
lib/rb_sdl2/window/dialog.rb,
lib/rb_sdl2/window/display.rb,
lib/rb_sdl2/window/hit_test.rb,
lib/rb_sdl2/window/position.rb,
lib/rb_sdl2/window/window_flags.rb

Defined Under Namespace

Modules: Dialog, Display, Grab, Position, Shape, Size, WindowFlags Classes: HitTest

Constant Summary

Constants included from PixelFormatEnum

PixelFormatEnum::ARRAY_ORDERS, PixelFormatEnum::ARRAY_TYPES, PixelFormatEnum::FORMAT_MAP, PixelFormatEnum::INDEXED_TYPES, PixelFormatEnum::PACKED_ORDERS, PixelFormatEnum::PACKED_TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PixelFormatEnum

array_type?, #format_name, #fourcc, fourcc?, #fourcc?, #indexed_color?, indexed_type?, packed_type?, #rgb?, #rgba?, to_fourcc, to_name, to_num, to_order, to_type, with_alpha?

Methods included from WindowFlags

#allow_high_dpi?, #always_on_top?, #borderless?, #foreign?, #fullscreen?, #fullscreen_desktop?, #hidden?, #input_focused?, #input_grabbed?, #maximized?, #minimized?, #mouse_captured?, #mouse_focused?, #opengl?, #popup_menu?, #resizable?, #shown?, #skip_taskbar?, to_num, #tooltip?, #utility?, #vulkan?

Methods included from Dialog

#alert, #confirm, #dialog, #error_alert, #info_alert, #warn_alert

Methods included from Display

#brightness, #brightness=, #display, #display_index, #fullscreen_display_mode, #fullscreen_display_mode=, #gamma=, #gamma_ramp, #gamma_ramp=

Methods included from Grab

#grab=, #grabbed?, #grabbed_keyboard?, #grabbed_mouse?, #keyboard_grab=, #mouse_grab=

Methods included from Position

#position, #position=, #x, #x=, #y, #y=

Methods included from Shape

#alpha_test, #alpha_test?, #color_key, #color_key?, #reverse_alpha_test?, #shape_set, #shaped?

Methods included from Size

#height, #height=, #maximum_size, #maximum_size=, #minimum_size, #minimum_size=, #size, #size=, #width, #width=

Constructor Details

#initialize(num) ⇒ Window

Returns a new instance of Window.



57
58
59
# File 'lib/rb_sdl2/window.rb', line 57

def initialize(num)
  @id = num
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



140
141
142
# File 'lib/rb_sdl2/window.rb', line 140

def id
  @id
end

Class Method Details

.grabbedObject



8
# File 'lib/rb_sdl2/window.rb', line 8

def grabbed = (ptr = ::SDL2.SDL_GetGrabbedWindow).null? ? nil : to_ptr(ptr)

.keyboard_focusedObject



4
# File 'lib/rb_sdl2/window.rb', line 4

def keyboard_focused = (ptr = ::SDL2.SDL_GetKeyboardFocus).null? ? nil : to_ptr(ptr)

.mouse_focusedObject



6
# File 'lib/rb_sdl2/window.rb', line 6

def mouse_focused = (ptr = ::SDL2.SDL_GetMouseFocus).null? ? nil : to_ptr(ptr)

.new(title = nil, x = nil, y = nil, w = 640, h = 480, flags = nil, **opts) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
# File 'lib/rb_sdl2/window.rb', line 10

def new(title = nil, x = nil, y = nil, w = 640, h = 480, flags = nil, **opts)
  x ||= ::SDL2::SDL_WINDOWPOS_CENTERED_MASK
  y ||= ::SDL2::SDL_WINDOWPOS_CENTERED_MASK
  flags ||= WindowFlags.to_num(**opts)
  ptr = ::SDL2.SDL_CreateWindow(title&.to_s, x, y, w, h, flags)
  raise RbSDL2Error if ptr.null?
  to_ptr(ptr)
end

.shaped(title = nil, x = nil, y = nil, w = nil, h = nil, flags = nil, alpha_test: nil, color_key: nil, shape:, **opts) ⇒ Object

w, h は nil の場合は shape に与えられたサーフェィスの w, h を使用する。flags は常に borderless: true, fullscreen: false, resizable: false が設定される。shape には Surface のインスタンス・オブジェクトを与える。作成されたウィンドウは透明である。表示内容は作成後に描画する必要がある。ウィンドウへの操作を扱いたければ HitTest コールバックを設定しコールバック側で処理を行う必要がある。

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rb_sdl2/window.rb', line 24

def shaped(title = nil, x = nil, y = nil, w = nil, h = nil, flags = nil,
           alpha_test: nil, color_key: nil, shape:, **opts)
  flags ||= WindowFlags.to_num(**opts)
  # SDL_CreateShapedWindow は引数 x, y を無視する。そして x = -1000, y = -1000 に強制する。

  # w, h は形状マスクのサイズに合わせる必要がある。

  ptr = ::SDL2.SDL_CreateShapedWindow(title, 0, 0, shape.w, shape.h, flags)
  raise RbSDL2Error if ptr.null?
  to_ptr(ptr).tap do |obj|
    obj.shape_set(shape, alpha_test: alpha_test, color_key: color_key)
    obj.size = [w || shape.w, h || shape.h]
    # 位置の再指定を行いアプリケーションの意図した位置に表示する。

    obj.position = [x || ::SDL2::SDL_WINDOWPOS_CENTERED_MASK,
                    y || ::SDL2::SDL_WINDOWPOS_CENTERED_MASK]
  end
end

.to_id(num) ⇒ Object

Raises:



40
41
42
43
44
45
46
# File 'lib/rb_sdl2/window.rb', line 40

def to_id(num)
  ptr = ::SDL2.SDL_GetWindowFromID(num)
  raise RbSDL2Error, "invalid window id" if ptr.null?
  obj = allocate
  obj.__send__(:initialize, num)
  obj
end

.to_ptr(ptr) ⇒ Object

Raises:



48
49
50
51
52
53
54
# File 'lib/rb_sdl2/window.rb', line 48

def to_ptr(ptr)
  num = ::SDL2.SDL_GetWindowID(ptr)
  raise RbSDL2Error if num == 0
  obj = allocate
  obj.__send__(:initialize, num)
  obj
end

Instance Method Details

#always_on_top=(bool) ⇒ Object



69
70
71
# File 'lib/rb_sdl2/window.rb', line 69

def always_on_top=(bool)
  ::SDL2.SDL_SetWindowAlwaysOnTop(self, bool ? ::SDL2::SDL_TRUE : ::SDL2::SDL_FALSE)
end

#border_sizeObject

Raises:



73
74
75
76
77
78
# File 'lib/rb_sdl2/window.rb', line 73

def border_size
  top_left_bottom_right = Array.new(4) { ::FFI::MemoryPointer.new(:int) }
  err = ::SDL2.SDL_GetWindowBordersSize(self, *top_left_bottom_right)
  raise RbSDL2Error if err < 0
  top_left_bottom_right.map(&:read_int)
end

#bordered=(bool) ⇒ Object



80
81
82
# File 'lib/rb_sdl2/window.rb', line 80

def bordered=(bool)
  ::SDL2.SDL_SetWindowBordered(self, bool ? ::SDL2::SDL_TRUE : ::SDL2::SDL_FALSE)
end

#destroyObject



84
85
86
87
88
# File 'lib/rb_sdl2/window.rb', line 84

def destroy
  return if destroyed?
  HitTest.callback_set(self, nil)
  ::SDL2.SDL_DestroyWindow(self)
end

#destroyed?Boolean

Returns:

  • (Boolean)


90
# File 'lib/rb_sdl2/window.rb', line 90

def destroyed? = ::SDL2.SDL_GetWindowFromID(id).null?

#flagsObject



92
# File 'lib/rb_sdl2/window.rb', line 92

def flags = ::SDL2.SDL_GetWindowFlags(self)

#flash(bool = true) ⇒ Object

Raises:



102
103
104
105
106
107
# File 'lib/rb_sdl2/window.rb', line 102

def flash(bool = true)
  operation = bool ? ::SDL2::SDL_FLASH_UNTIL_FOCUSED : ::SDL2::SDL_FLASH_CANCEL
  err = ::SDL2.SDL_FlashWindow(self, operation)
  raise RbSDL2Error if err < 0
  bool
end

#flash!Object

Raises:



109
110
111
112
113
# File 'lib/rb_sdl2/window.rb', line 109

def flash!
  err = ::SDL2.SDL_FlashWindow(self, ::SDL2::SDL_FLASH_BRIEFLY)
  raise RbSDL2Error if err < 0
  self
end

#formatObject



97
# File 'lib/rb_sdl2/window.rb', line 97

def format = ::SDL2.SDL_GetWindowPixelFormat(self)

#fullscreenObject

Raises:



115
116
117
118
119
# File 'lib/rb_sdl2/window.rb', line 115

def fullscreen
  err = ::SDL2.SDL_SetWindowFullscreen(self, ::SDL2::SDL_WINDOW_FULLSCREEN)
  raise RbSDL2Error if err < 0
  self
end

#fullscreen_desktopObject

Raises:



121
122
123
124
125
# File 'lib/rb_sdl2/window.rb', line 121

def fullscreen_desktop
  err = ::SDL2.SDL_SetWindowFullscreen(self, ::SDL2::SDL_WINDOW_FULLSCREEN_DESKTOP)
  raise RbSDL2Error if err < 0
  self
end

#hideObject



127
128
129
130
# File 'lib/rb_sdl2/window.rb', line 127

def hide
  ::SDL2.SDL_HideWindow(self)
  self
end

#hit_test_callback_set(*args) ⇒ Object



134
# File 'lib/rb_sdl2/window.rb', line 134

def hit_test_callback_set(*args) = HitTest.callback_set(self, *args)

#icon=(surface) ⇒ Object



136
137
138
# File 'lib/rb_sdl2/window.rb', line 136

def icon=(surface)
  ::SDL2.SDL_SetWindowIcon(self, surface)
end

#maximizeObject



142
143
144
145
# File 'lib/rb_sdl2/window.rb', line 142

def maximize
  ::SDL2.SDL_MaximizeWindow(self)
  self
end

#minimizeObject



147
148
149
150
# File 'lib/rb_sdl2/window.rb', line 147

def minimize
  ::SDL2.SDL_MinimizeWindow(self)
  self
end

#mouse_position=(x_y) ⇒ Object



152
153
154
# File 'lib/rb_sdl2/window.rb', line 152

def mouse_position=(x_y)
  ::SDL2.SDL_WarpMouseInWindow(self, *x_y)
end

#opacityObject

Raises:



156
157
158
159
160
161
# File 'lib/rb_sdl2/window.rb', line 156

def opacity
  out_opacity = ::FFI::MemoryPointer.new(:float)
  err = ::SDL2.SDL_GetWindowOpacity(self, out_opacity)
  raise RbSDL2Error if err < 0
  out_opacity.read_float
end

#opacity=(val) ⇒ Object

ウィンドウの透明度を変更する。値は 0.0 から 1.0。値が低いほど透明になる。

Raises:



164
165
166
167
# File 'lib/rb_sdl2/window.rb', line 164

def opacity=(val)
  err = ::SDL2.SDL_SetWindowOpacity(self, val)
  raise RbSDL2Error if err < 0
end


169
170
171
172
# File 'lib/rb_sdl2/window.rb', line 169

def popup
  ::SDL2.SDL_RaiseWindow(self)
  self
end

#resizable=(bool) ⇒ Object



174
175
176
# File 'lib/rb_sdl2/window.rb', line 174

def resizable=(bool)
  ::SDL2.SDL_SetWindowResizable(self, bool ? ::SDL2::SDL_TRUE : ::SDL2::SDL_FALSE)
end

#restoreObject



178
179
180
181
# File 'lib/rb_sdl2/window.rb', line 178

def restore
  ::SDL2.SDL_RestoreWindow(self)
  self
end

#showObject



183
184
185
186
# File 'lib/rb_sdl2/window.rb', line 183

def show
  ::SDL2.SDL_ShowWindow(self)
  self
end

#surfaceObject



188
189
190
191
192
193
194
195
196
197
# File 'lib/rb_sdl2/window.rb', line 188

def surface
  ptr = ::SDL2.SDL_GetWindowSurface(self)
  if ptr.null?
    @surface = nil
    raise RbSDL2Error
  end
  # SDL_Surface は参照カウンターで管理されているため Ruby 側でポインターを保持している限り

  # 同アドレスに違う SDL_Surface が作成されることはない。安全にキャッシュできる。

  ptr == @surface&.to_ptr ? @surface : @surface = Surface.to_ptr(ptr)
end

#titleObject



199
# File 'lib/rb_sdl2/window.rb', line 199

def title = ::SDL2.SDL_GetWindowTitle(self).read_string.force_encoding(Encoding::UTF_8)

#title=(obj) ⇒ Object



201
202
203
# File 'lib/rb_sdl2/window.rb', line 201

def title=(obj)
  ::SDL2.SDL_SetWindowTitle(self, obj&.to_s&.encode(Encoding::UTF_8))
end

#to_ptrObject

Raises:



205
206
207
208
209
# File 'lib/rb_sdl2/window.rb', line 205

def to_ptr
  ptr = ::SDL2.SDL_GetWindowFromID(id)
  raise RbSDL2Error, "Invalid window id or window was destroyed" if ptr.null?
  ptr
end

#to_surfaceObject

ウィンドウのサーフェィスのコピーを戻す。このメソッドはウィンドウのスクリーンショットが欲しい場合に使う。



213
# File 'lib/rb_sdl2/window.rb', line 213

def to_surface = surface.then { |s| convert(s.format) }

#update(rect = nil) {|surface| ... } ⇒ Object

surface メソッドを実行後に Window のサイズ変更があった場合、update メソッドを実行するとエラーになる。このメソッドは self を戻す。

Yields:



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rb_sdl2/window.rb', line 217

def update(rect = nil)
  yield(surface) if block_given?
  # SDL_UpdateWindowSurface, SDL_UpdateWindowSurfaceRects の *初回* 呼び出しの前に

  # SDL_GetWindowSurface が必要になる。

  surface unless @surface

  err = if rect
          ::SDL2.SDL_UpdateWindowSurfaceRects(self, Rect.new(*rect), 1)
        else
          ::SDL2.SDL_UpdateWindowSurface(self)
        end
  # SDL_GetWindowSurface の後に Window のサイズ変更があった場合はエラーになる。

  if err < 0
    @surface = nil
    raise RbSDL2Error
  end
  self
end

#windowedObject

Raises:



236
237
238
239
240
# File 'lib/rb_sdl2/window.rb', line 236

def windowed
  err = ::SDL2.SDL_SetWindowFullscreen(self, 0)
  raise RbSDL2Error if err < 0
  self
end