Class: RbSDL2::Window

Inherits:
Object
  • Object
show all
Includes:
PixelFormatEnum, Accessor, Display, Flash, Shape, State
Defined in:
lib/rb_sdl2/window/flash.rb,
lib/rb_sdl2/window/shape.rb,
lib/rb_sdl2/window/state.rb,
lib/rb_sdl2/window/window.rb,
lib/rb_sdl2/window/display.rb,
lib/rb_sdl2/window/accessor.rb,
lib/rb_sdl2/window/hit_test.rb

Defined Under Namespace

Modules: Accessor, Display, Flash, Shape, State Classes: HitTest

Constant Summary collapse

SDL_WINDOW_FULLSCREEN =
0x00000001
SDL_WINDOW_OPENGL =
0x00000002
SDL_WINDOW_SHOWN =
0x00000004
SDL_WINDOW_HIDDEN =
0x00000008
SDL_WINDOW_BORDERLESS =
0x00000010
SDL_WINDOW_RESIZABLE =
0x00000020
SDL_WINDOW_MINIMIZED =
0x00000040
SDL_WINDOW_MAXIMIZED =
0x00000080
SDL_WINDOW_MOUSE_GRABBED =
0x00000100
SDL_WINDOW_INPUT_FOCUS =
0x00000200
SDL_WINDOW_MOUSE_FOCUS =
0x00000400
SDL_WINDOW_FULLSCREEN_DESKTOP =
SDL_WINDOW_FULLSCREEN | 0x00001000
SDL_WINDOW_FOREIGN =
0x00000800
SDL_WINDOW_ALLOW_HIGHDPI =
0x00002000
SDL_WINDOW_MOUSE_CAPTURE =
0x00004000
SDL_WINDOW_ALWAYS_ON_TOP =
0x00008000
SDL_WINDOW_SKIP_TASKBAR =
0x00010000
SDL_WINDOW_UTILITY =
0x00020000
SDL_WINDOW_TOOLTIP =
0x00040000
SDL_WINDOW_POPUP_MENU =
0x00080000
SDL_WINDOW_KEYBOARD_GRABBED =
0x00100000
SDL_WINDOW_VULKAN =
0x10000000
SDL_WINDOW_METAL =
0x20000000
SDL_WINDOW_INPUT_GRABBED =
SDL_WINDOW_MOUSE_GRABBED
SDL_WINDOWPOS_CENTERED_MASK =
0x2FFF0000
SDL_HITTEST_NORMAL =
0
SDL_HITTEST_DRAGGABLE =
1
SDL_HITTEST_RESIZE_TOPLEFT =
2
SDL_HITTEST_RESIZE_TOP =
3
SDL_HITTEST_RESIZE_TOPRIGHT =
4
SDL_HITTEST_RESIZE_RIGHT =
5
SDL_HITTEST_RESIZE_BOTTOMRIGHT =
6
SDL_HITTEST_RESIZE_BOTTOM =
7
SDL_HITTEST_RESIZE_BOTTOMLEFT =
8
SDL_HITTEST_RESIZE_LEFT =
9

Constants included from PixelFormatEnum

PixelFormatEnum::FORMAT_MAP, PixelFormatEnum::INDEXED_TYPES, PixelFormatEnum::WITH_ALPHA

Constants included from Flash

Flash::SDL_FLASH_BRIEFLY, Flash::SDL_FLASH_CANCEL, Flash::SDL_FLASH_UNTIL_FOCUSED

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PixelFormatEnum

#format_name, #fourcc, #fourcc?, #indexed_color?, #rgb?, #rgba?, to_name, to_num

Methods included from Accessor

#border_size, #height, #height=, #icon=, #maximum_size, #maximum_size=, #minimum_size, #minimum_size=, #mouse_rect, #mouse_rect=, #opacity, #opacity=, #position, #position=, #size, #size=, #title, #title=, #width, #width=, #x, #x=, #y, #y=

Methods included from Display

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

Methods included from Flash

#flash, #flash!

Methods included from Shape

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

Methods included from State

#allow_high_dpi?, #always_on_top=, #always_on_top?, #bordered=, #borderless?, #current!, #flags, #flags?, #foreign?, #fullscreen, #fullscreen?, #fullscreen_desktop, #fullscreen_desktop?, #grab=, #grabbed?, #grabbed_keyboard?, #grabbed_mouse?, #hidden?, #hide, #input_focused?, #input_grabbed?, #keyboard_grab=, #keyboard_grabbed?, #maximize, #maximized?, #metal?, #minimize, #minimized?, #mouse_captured?, #mouse_focused?, #mouse_grab=, #mouse_grabbed?, #opengl?, #popup_menu?, #resizable=, #resizable?, #restore, #show, #shown?, #skip_taskbar?, to_flags, #tooltip?, #utility?, #vulkan?, #windowed

Constructor Details

#initialize(num) ⇒ Window

Returns a new instance of Window.



66
67
68
# File 'lib/rb_sdl2/window/window.rb', line 66

def initialize(num)
  @window_id = num
end

Instance Attribute Details

#window_idObject (readonly) Also known as: id

Returns the value of attribute window_id.



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

def window_id
  @window_id
end

Class Method Details

.grabbedObject



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

def grabbed = (ptr = ::SDL.GetGrabbedWindow).null? ? nil : to_ptr(ptr)

.keyboard_focusedObject



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

def keyboard_focused = (ptr = ::SDL.GetKeyboardFocus).null? ? nil : to_ptr(ptr)

.mouse_focusedObject



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

def mouse_focused = (ptr = ::SDL.GetMouseFocus).null? ? nil : to_ptr(ptr)

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

Raises:



18
19
20
21
22
23
24
# File 'lib/rb_sdl2/window/window.rb', line 18

def new(title = "", x = nil, y = nil, w = 640, h = 480, flags: nil, **opts)
  ptr = ::SDL.CreateWindow(SDL.str_to_sdl(title),
                           x || SDL_WINDOWPOS_CENTERED_MASK, y || SDL_WINDOWPOS_CENTERED_MASK,
                           w, h, flags || State.to_flags(**opts))
  raise RbSDL2Error if ptr.null?
  to_ptr(ptr)
end

.shaped(title = "", 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:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rb_sdl2/window/window.rb', line 31

def shaped(title = "", x = nil, y = nil, w = nil, h = nil, flags: nil,
           alpha_test: nil, color_key: nil, shape:, **opts)
  size = [w || shape.w, h || shape.h]
  # w, h は形状マスクのサイズに合わせる必要がある。
  ptr = ::SDL.CreateShapedWindow(SDL.str_to_sdl(title), 0, 0, *size,
                                 flags || State.to_flags(**opts))
  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 = size
    # CreateShapedWindow は引数 x, y を無視する。そして x = -1000, y = -1000 に強制する。
    # 位置の再指定を行いアプリケーションの意図した位置に表示する。
    obj.position = [x || SDL_WINDOWPOS_CENTERED_MASK, y || SDL_WINDOWPOS_CENTERED_MASK]
  end
end

.to_id(num) ⇒ Object

Raises:



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

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

.to_ptr(ptr) ⇒ Object

Raises:



55
56
57
58
59
60
61
# File 'lib/rb_sdl2/window/window.rb', line 55

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

Instance Method Details

#==(other) ⇒ Object



70
71
72
73
74
75
# File 'lib/rb_sdl2/window/window.rb', line 70

def ==(other)
  # ウィンドウのポインターアドレスは SDL によって再利用されることがある。
  # 最初に window_id を比較しなければならない。
  other.respond_to?(:window_id) && other.window_id == window_id ||
    other.respond_to?(:to_ptr) && other.to_ptr == to_ptr
end

#destroyObject



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

def destroy = ::SDL.DestroyWindow(::SDL.GetWindowFromID(@window_id))

#destroyed?Boolean

Returns:

  • (Boolean)


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

def destroyed? = ::SDL.GetWindowFromID(@window_id).null?

#formatObject



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

def format = ::SDL.GetWindowPixelFormat(self)

#hit_test=(obj) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rb_sdl2/window/window.rb', line 88

def hit_test=(obj)
  # dup されて片方で無効にした場合、もう一方にコールバックオブジェクトの拘束がおきる。
  # これはほおっておくことにする。
  @hit_test_callback = if obj
                         func = HitTest.new(obj)
                         err = ::SDL.SetWindowHitTest(self, func, nil)
                         raise RbSDL2Error if err < 0
                         func
                       end
  @hit_test_object = obj
end

#mouse_position=(x_y) ⇒ Object



100
101
102
# File 'lib/rb_sdl2/window/window.rb', line 100

def mouse_position=(x_y)
  ::SDL.WarpMouseInWindow(self, *x_y)
end

#surfaceObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/rb_sdl2/window/window.rb', line 104

def surface
  ptr = ::SDL.GetWindowSurface(self)
  if ptr.null?
    @surface = nil
    raise RbSDL2Error
  end
  # Surface は参照カウンターで管理されているため Ruby 側でポインターを保持している限り
  # 同アドレスに違う Surface が作成されることはない。安全にキャッシュできる。
  ptr == @surface&.to_ptr ? @surface : @surface = Surface.to_ptr(ptr)
end

#to_ptrObject

Raises:



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

def to_ptr
  ptr = ::SDL.GetWindowFromID(@window_id)
  raise RbSDL2Error, "Invalid window id or window was destroyed" if ptr.null?
  ptr
end

#to_surfaceObject

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



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

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

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

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

Yields:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rb_sdl2/window/window.rb', line 127

def update(rect = nil)
  yield(surface) if block_given?
  # UpdateWindowSurface, UpdateWindowSurfaceRects の *初回* 呼び出しの前に
  # GetWindowSurface が必要になる。
  surface unless @surface

  err = if rect
          ::SDL.UpdateWindowSurfaceRects(self, Rect.new(*rect), 1)
        else
          ::SDL.UpdateWindowSurface(self)
        end
  # GetWindowSurface の後に Window のサイズ変更があった場合はエラーになる。
  if err < 0
    @surface = nil
    raise RbSDL2Error
  end
  self
end