rb_sdl2 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE.txt +17 -0
  4. data/README.md +40 -0
  5. data/lib/rb_sdl2/audio/audio_allowed_changes.rb +17 -0
  6. data/lib/rb_sdl2/audio/audio_buffer.rb +49 -0
  7. data/lib/rb_sdl2/audio/audio_device.rb +44 -0
  8. data/lib/rb_sdl2/audio/audio_format.rb +23 -0
  9. data/lib/rb_sdl2/audio/audio_spec.rb +29 -0
  10. data/lib/rb_sdl2/audio.rb +132 -0
  11. data/lib/rb_sdl2/clipboard.rb +44 -0
  12. data/lib/rb_sdl2/cpu_info.rb +39 -0
  13. data/lib/rb_sdl2/cursor/color_cursor.rb +19 -0
  14. data/lib/rb_sdl2/cursor/cursor_class.rb +24 -0
  15. data/lib/rb_sdl2/cursor/cursor_pointer.rb +12 -0
  16. data/lib/rb_sdl2/cursor/default_cursor.rb +18 -0
  17. data/lib/rb_sdl2/cursor/system_cursor.rb +45 -0
  18. data/lib/rb_sdl2/cursor.rb +39 -0
  19. data/lib/rb_sdl2/display.rb +103 -0
  20. data/lib/rb_sdl2/display_mode.rb +35 -0
  21. data/lib/rb_sdl2/error.rb +9 -0
  22. data/lib/rb_sdl2/event/event_filter.rb +83 -0
  23. data/lib/rb_sdl2/event/event_queue.rb +137 -0
  24. data/lib/rb_sdl2/event/event_type.rb +271 -0
  25. data/lib/rb_sdl2/event.rb +161 -0
  26. data/lib/rb_sdl2/filesystem.rb +40 -0
  27. data/lib/rb_sdl2/hint.rb +24 -0
  28. data/lib/rb_sdl2/keyboard/key_mod.rb +37 -0
  29. data/lib/rb_sdl2/keyboard/keyboard_state.rb +34 -0
  30. data/lib/rb_sdl2/keyboard.rb +50 -0
  31. data/lib/rb_sdl2/message_box.rb +121 -0
  32. data/lib/rb_sdl2/mouse/global_mouse.rb +18 -0
  33. data/lib/rb_sdl2/mouse/mouse_button.rb +24 -0
  34. data/lib/rb_sdl2/mouse/mouse_class.rb +33 -0
  35. data/lib/rb_sdl2/mouse/mouse_wheel.rb +53 -0
  36. data/lib/rb_sdl2/mouse/relative_mouse.rb +12 -0
  37. data/lib/rb_sdl2/mouse/window_mouse.rb +17 -0
  38. data/lib/rb_sdl2/mouse.rb +74 -0
  39. data/lib/rb_sdl2/palette.rb +70 -0
  40. data/lib/rb_sdl2/pixel_format_enum.rb +73 -0
  41. data/lib/rb_sdl2/platform.rb +7 -0
  42. data/lib/rb_sdl2/power_info.rb +36 -0
  43. data/lib/rb_sdl2/rect.rb +14 -0
  44. data/lib/rb_sdl2/ref_count_pointer.rb +20 -0
  45. data/lib/rb_sdl2/rw_ops/rw_operator.rb +102 -0
  46. data/lib/rb_sdl2/rw_ops.rb +124 -0
  47. data/lib/rb_sdl2/screen_saver.rb +11 -0
  48. data/lib/rb_sdl2/sdl.rb +32 -0
  49. data/lib/rb_sdl2/surface/blend_mode.rb +41 -0
  50. data/lib/rb_sdl2/surface/pixel_format.rb +89 -0
  51. data/lib/rb_sdl2/surface.rb +244 -0
  52. data/lib/rb_sdl2/text_input.rb +15 -0
  53. data/lib/rb_sdl2/timer.rb +22 -0
  54. data/lib/rb_sdl2/version.rb +13 -0
  55. data/lib/rb_sdl2/video.rb +25 -0
  56. data/lib/rb_sdl2/window/dialog.rb +19 -0
  57. data/lib/rb_sdl2/window/display.rb +85 -0
  58. data/lib/rb_sdl2/window/grab.rb +23 -0
  59. data/lib/rb_sdl2/window/hit_test.rb +52 -0
  60. data/lib/rb_sdl2/window/position.rb +38 -0
  61. data/lib/rb_sdl2/window/shape.rb +76 -0
  62. data/lib/rb_sdl2/window/size.rb +59 -0
  63. data/lib/rb_sdl2/window/window_flags.rb +78 -0
  64. data/lib/rb_sdl2/window.rb +242 -0
  65. data/lib/rb_sdl2.rb +53 -0
  66. data/rb_sdl2.gemspec +19 -0
  67. metadata +135 -0
@@ -0,0 +1,76 @@
1
+ module RbSDL2
2
+ class Window
3
+ module Shape
4
+ private def shape_mode
5
+ mode = ::SDL2::SDL_WindowShapeMode.new
6
+ num = ::SDL2.SDL_GetShapedWindowMode(self, mode)
7
+ # SDL_GetShapedWindowMode は SDL_INVALID_SHAPE_ARGUMENT を返すことはない。
8
+ if num == ::SDL2::SDL_NONSHAPEABLE_WINDOW
9
+ raise RbSDL2Error, "unshaped window"
10
+ elsif num == ::SDL2::SDL_WINDOW_LACKS_SHAPE
11
+ raise RbSDL2Error, "window lacks shape"
12
+ elsif num < 0
13
+ raise RbSDL2Error
14
+ end
15
+ mode
16
+ end
17
+
18
+ def alpha_test
19
+ shape_mode[:parameters][:binarizationCutoff] unless shaped? && color_key?
20
+ end
21
+
22
+ def alpha_test?
23
+ shaped? && [::SDL2::ShapeModeBinarizeAlpha,
24
+ ::SDL2::ShapeModeDefault].include?(shape_mode[:mode])
25
+ end
26
+
27
+ def color_key
28
+ shape_mode[:parameters][:colorKey].values if shaped? && color_key?
29
+ end
30
+
31
+ def color_key?
32
+ shaped? && ::SDL2::ShapeModeColorKey == shape_mode[:mode]
33
+ end
34
+
35
+ def reverse_alpha_test?
36
+ shaped? && ::SDL2::ShapeModeReverseBinarizeAlpha == shape_mode[:mode]
37
+ end
38
+
39
+ # サーフェスにアルファ―チャンネルが含まれていない場合、color_key オプションを与える必要がある。
40
+ # 形状マスクの設定を行う。ウィンドウの描画域は透明なピクセルで埋められている。
41
+ # このメソッド呼び出しの成功後にウィンドウのサーフェスへ描画を行う必要がある。
42
+ # 描画方法はウィンドウのサーフェスまたはレンダラーのどちらを使用してもよい。
43
+ def shape_set(surface, alpha_test: nil, color_key: nil)
44
+ mode = ::SDL2::SDL_WindowShapeMode.new.tap do |st|
45
+ st[:mode] = if color_key
46
+ st[:parameters][:colorKey].tap { |c| c[:r], c[:g], c[:b] = color_key }
47
+ ::SDL2::ShapeModeColorKey
48
+ elsif alpha_test&.>= 0
49
+ st[:parameters][:binarizationCutoff] = alpha_test
50
+ ::SDL2::ShapeModeBinarizeAlpha
51
+ elsif alpha_test&.< 0
52
+ st[:parameters][:binarizationCutoff] = alpha_test.abs
53
+ ::SDL2::ShapeModeReverseBinarizeAlpha
54
+ else
55
+ ::SDL2::ShapeModeDefault
56
+ end
57
+ end
58
+ err = ::SDL2.SDL_SetWindowShape(self, surface, mode)
59
+ # SDL_SetWindowShape は SDL_WINDOW_LACKS_SHAPE を返すことはない。
60
+ if err == ::SDL2::SDL_NONSHAPEABLE_WINDOW
61
+ raise RbSDL2Error, "unshaped window"
62
+ elsif err == ::SDL2::SDL_INVALID_SHAPE_ARGUMENT
63
+ raise RbSDL2Error,
64
+ "Invalid shape argument. \
65
+ The size of the window and the size of the surface do not match. \
66
+ Or the color key is not specified in the surface without alpha channel."
67
+ elsif err < 0
68
+ raise RbSDL2Error
69
+ end
70
+ surface
71
+ end
72
+
73
+ def shaped? = ::SDL2.SDL_IsShapedWindow(self) == ::SDL2::SDL_TRUE
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,59 @@
1
+ module RbSDL2
2
+ class Window
3
+ module Size
4
+ def height
5
+ ptr = ::FFI::MemoryPointer.new(:int)
6
+ ::SDL2.SDL_GetWindowSize(self, nil, ptr)
7
+ ptr.read_int
8
+ end
9
+ alias h height
10
+
11
+ def height=(num)
12
+ self.size = [w, num]
13
+ end
14
+ alias h= height=
15
+
16
+ def maximum_size
17
+ w_h = Array.new(2) { ::FFI::MemoryPointer.new(:int) }
18
+ ::SDL2.SDL_GetWindowMaximumSize(self, *w_h)
19
+ w_h.map(&:read_int)
20
+ end
21
+
22
+ def maximum_size=(w_h)
23
+ ::SDL2.SDL_SetWindowMaximumSize(self, *w_h)
24
+ end
25
+
26
+ def minimum_size
27
+ w_h = Array.new(2) { ::FFI::MemoryPointer.new(:int) }
28
+ ::SDL2.SDL_GetWindowMinimumSize(self, *w_h)
29
+ w_h.map(&:read_int)
30
+ end
31
+
32
+ def minimum_size=(w_h)
33
+ ::SDL2.SDL_SetWindowMinimumSize(self, *w_h)
34
+ end
35
+
36
+ def size
37
+ w_h = Array.new(2) { ::FFI::MemoryPointer.new(:int) }
38
+ ::SDL2.SDL_GetWindowSize(self, *w_h)
39
+ w_h.map(&:read_int)
40
+ end
41
+
42
+ def size=(w_h)
43
+ ::SDL2.SDL_SetWindowSize(self, *w_h)
44
+ end
45
+
46
+ def width
47
+ ptr = ::FFI::MemoryPointer.new(:int)
48
+ ::SDL2.SDL_GetWindowSize(self, ptr, nil)
49
+ ptr.read_int
50
+ end
51
+ alias w width
52
+
53
+ def width=(num)
54
+ self.size = [num, h]
55
+ end
56
+ alias w= width=
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,78 @@
1
+ module RbSDL2
2
+ class Window
3
+ module WindowFlags
4
+ class << self
5
+ def to_num(allow_high_dpi: false, always_on_top: false, borderless: false, foreign: false,
6
+ fullscreen: false, fullscreen_desktop: false, hidden: false, input_focus: false,
7
+ input_grabbed: false, maximized: false, minimized: false, mouse_capture: false,
8
+ mouse_focus: false, opengl: false, popup_menu: false, resizable: false,
9
+ shown: false, skip_taskbar: false, tooltip: false, utility: false, vulkan: false)
10
+ 0 |
11
+ (allow_high_dpi ? ::SDL2::SDL_WINDOW_ALLOW_HIGHDPI : 0) |
12
+ (always_on_top ? ::SDL2::SDL_WINDOW_ALWAYS_ON_TOP : 0) |
13
+ (borderless ? ::SDL2::SDL_WINDOW_BORDERLESS : 0) |
14
+ (foreign ? ::SDL2::SDL_WINDOW_FOREIGN : 0) |
15
+ (fullscreen ? ::SDL2::SDL_WINDOW_FULLSCREEN : 0) |
16
+ (fullscreen_desktop ? ::SDL2::SDL_WINDOW_FULLSCREEN_DESKTOP : 0) |
17
+ (hidden ? ::SDL2::SDL_WINDOW_HIDDEN : 0) |
18
+ (input_focus ? ::SDL2::SDL_WINDOW_INPUT_FOCUS : 0) |
19
+ (input_grabbed ? ::SDL2::SDL_WINDOW_INPUT_GRABBED : 0) |
20
+ (maximized ? ::SDL2::SDL_WINDOW_MAXIMIZED : 0) |
21
+ (minimized ? ::SDL2::SDL_WINDOW_MINIMIZED : 0) |
22
+ (mouse_capture ? ::SDL2::SDL_WINDOW_MOUSE_CAPTURE : 0) |
23
+ (mouse_focus ? ::SDL2::SDL_WINDOW_MOUSE_FOCUS : 0) |
24
+ (opengl ? ::SDL2::SDL_WINDOW_OPENGL : 0) |
25
+ (popup_menu ? ::SDL2::SDL_WINDOW_POPUP_MENU : 0) |
26
+ (resizable ? ::SDL2::SDL_WINDOW_RESIZABLE : 0) |
27
+ (shown ? ::SDL2::SDL_WINDOW_SHOWN : 0) |
28
+ (skip_taskbar ? ::SDL2::SDL_WINDOW_SKIP_TASKBAR : 0) |
29
+ (tooltip ? ::SDL2::SDL_WINDOW_TOOLTIP : 0) |
30
+ (utility ? ::SDL2::SDL_WINDOW_UTILITY : 0) |
31
+ (vulkan ? ::SDL2::SDL_WINDOW_VULKAN : 0)
32
+ end
33
+ end
34
+
35
+ def allow_high_dpi? = ::SDL2::SDL_WINDOW_ALLOW_HIGHDPI & flags != 0
36
+
37
+ def always_on_top? = ::SDL2::SDL_WINDOW_ALWAYS_ON_TOP & flags != 0
38
+
39
+ def borderless? = ::SDL2::SDL_WINDOW_BORDERLESS & flags != 0
40
+
41
+ def foreign? = ::SDL2::SDL_WINDOW_FOREIGN & flags != 0
42
+
43
+ def fullscreen? = ::SDL2::SDL_WINDOW_FULLSCREEN & flags != 0
44
+
45
+ def fullscreen_desktop? = ::SDL2::SDL_WINDOW_FULLSCREEN_DESKTOP & flags != 0
46
+
47
+ def hidden? = ::SDL2::SDL_WINDOW_HIDDEN & flags != 0
48
+
49
+ def input_focused? = ::SDL2::SDL_WINDOW_INPUT_FOCUS & flags != 0
50
+
51
+ def input_grabbed? = ::SDL2::SDL_WINDOW_INPUT_GRABBED & flags != 0
52
+
53
+ def maximized? = ::SDL2::SDL_WINDOW_MAXIMIZED & flags != 0
54
+
55
+ def minimized? = ::SDL2::SDL_WINDOW_MINIMIZED & flags != 0
56
+
57
+ def mouse_captured? = ::SDL2::SDL_WINDOW_MOUSE_CAPTURE & flags != 0
58
+
59
+ def mouse_focused? = ::SDL2::SDL_WINDOW_MOUSE_FOCUS & flags != 0
60
+
61
+ def opengl? = ::SDL2::SDL_WINDOW_OPENGL & flags != 0
62
+
63
+ def popup_menu? = ::SDL2::SDL_WINDOW_POPUP_MENU & flags != 0
64
+
65
+ def resizable? = ::SDL2::SDL_WINDOW_RESIZABLE & flags != 0
66
+
67
+ def skip_taskbar? = ::SDL2::SDL_WINDOW_SKIP_TASKBAR & flags != 0
68
+
69
+ def shown? = ::SDL2::SDL_WINDOW_SHOWN & flags != 0
70
+
71
+ def tooltip? = ::SDL2::SDL_WINDOW_TOOLTIP & flags != 0
72
+
73
+ def utility? = ::SDL2::SDL_WINDOW_UTILITY & flags != 0
74
+
75
+ def vulkan? = ::SDL2::SDL_WINDOW_VULKAN & flags != 0
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,242 @@
1
+ module RbSDL2
2
+ class Window
3
+ class << self
4
+ def keyboard_focused = (ptr = ::SDL2.SDL_GetKeyboardFocus).null? ? nil : to_ptr(ptr)
5
+
6
+ def mouse_focused = (ptr = ::SDL2.SDL_GetMouseFocus).null? ? nil : to_ptr(ptr)
7
+
8
+ def grabbed = (ptr = ::SDL2.SDL_GetGrabbedWindow).null? ? nil : to_ptr(ptr)
9
+
10
+ def new(title = nil, x = nil, y = nil, w = 640, h = 480, flags = nil, **opts)
11
+ x ||= ::SDL2::SDL_WINDOWPOS_CENTERED_MASK
12
+ y ||= ::SDL2::SDL_WINDOWPOS_CENTERED_MASK
13
+ flags ||= WindowFlags.to_num(**opts)
14
+ ptr = ::SDL2.SDL_CreateWindow(title&.to_s, x, y, w, h, flags)
15
+ raise RbSDL2Error if ptr.null?
16
+ to_ptr(ptr)
17
+ end
18
+
19
+ # w, h は nil の場合は shape に与えられたサーフェィスの w, h を使用する。
20
+ # flags は常に borderless: true, fullscreen: false, resizable: false が設定される。
21
+ # shape には Surface のインスタンス・オブジェクトを与える。
22
+ # 作成されたウィンドウは透明である。表示内容は作成後に描画する必要がある。
23
+ # ウィンドウへの操作を扱いたければ HitTest コールバックを設定しコールバック側で処理を行う必要がある。
24
+ def shaped(title = nil, x = nil, y = nil, w = nil, h = nil, flags = nil,
25
+ alpha_test: nil, color_key: nil, shape:, **opts)
26
+ flags ||= WindowFlags.to_num(**opts)
27
+ # SDL_CreateShapedWindow は引数 x, y を無視する。そして x = -1000, y = -1000 に強制する。
28
+ # w, h は形状マスクのサイズに合わせる必要がある。
29
+ ptr = ::SDL2.SDL_CreateShapedWindow(title, 0, 0, shape.w, shape.h, flags)
30
+ raise RbSDL2Error if ptr.null?
31
+ to_ptr(ptr).tap do |obj|
32
+ obj.shape_set(shape, alpha_test: alpha_test, color_key: color_key)
33
+ obj.size = [w || shape.w, h || shape.h]
34
+ # 位置の再指定を行いアプリケーションの意図した位置に表示する。
35
+ obj.position = [x || ::SDL2::SDL_WINDOWPOS_CENTERED_MASK,
36
+ y || ::SDL2::SDL_WINDOWPOS_CENTERED_MASK]
37
+ end
38
+ end
39
+
40
+ def to_id(num)
41
+ ptr = ::SDL2.SDL_GetWindowFromID(num)
42
+ raise RbSDL2Error, "invalid window id" if ptr.null?
43
+ obj = allocate
44
+ obj.__send__(:initialize, num)
45
+ obj
46
+ end
47
+
48
+ def to_ptr(ptr)
49
+ num = ::SDL2.SDL_GetWindowID(ptr)
50
+ raise RbSDL2Error if num == 0
51
+ obj = allocate
52
+ obj.__send__(:initialize, num)
53
+ obj
54
+ end
55
+ end
56
+
57
+ def initialize(num)
58
+ @id = num
59
+ end
60
+
61
+ require_relative 'window/dialog'
62
+ require_relative 'window/display'
63
+ require_relative 'window/grab'
64
+ require_relative 'window/position'
65
+ require_relative 'window/shape'
66
+ require_relative 'window/size'
67
+ include Dialog, Display, Grab, Position, Shape, Size
68
+
69
+ def always_on_top=(bool)
70
+ ::SDL2.SDL_SetWindowAlwaysOnTop(self, bool ? ::SDL2::SDL_TRUE : ::SDL2::SDL_FALSE)
71
+ end
72
+
73
+ def border_size
74
+ top_left_bottom_right = Array.new(4) { ::FFI::MemoryPointer.new(:int) }
75
+ err = ::SDL2.SDL_GetWindowBordersSize(self, *top_left_bottom_right)
76
+ raise RbSDL2Error if err < 0
77
+ top_left_bottom_right.map(&:read_int)
78
+ end
79
+
80
+ def bordered=(bool)
81
+ ::SDL2.SDL_SetWindowBordered(self, bool ? ::SDL2::SDL_TRUE : ::SDL2::SDL_FALSE)
82
+ end
83
+
84
+ def destroy
85
+ return if destroyed?
86
+ HitTest.callback_set(self, nil)
87
+ ::SDL2.SDL_DestroyWindow(self)
88
+ end
89
+
90
+ def destroyed? = ::SDL2.SDL_GetWindowFromID(id).null?
91
+
92
+ def flags = ::SDL2.SDL_GetWindowFlags(self)
93
+
94
+ require_relative 'window/window_flags'
95
+ include WindowFlags
96
+
97
+ def format = ::SDL2.SDL_GetWindowPixelFormat(self)
98
+
99
+ require_relative "pixel_format_enum"
100
+ include PixelFormatEnum
101
+
102
+ def flash(bool = true)
103
+ operation = bool ? ::SDL2::SDL_FLASH_UNTIL_FOCUSED : ::SDL2::SDL_FLASH_CANCEL
104
+ err = ::SDL2.SDL_FlashWindow(self, operation)
105
+ raise RbSDL2Error if err < 0
106
+ bool
107
+ end
108
+
109
+ def flash!
110
+ err = ::SDL2.SDL_FlashWindow(self, ::SDL2::SDL_FLASH_BRIEFLY)
111
+ raise RbSDL2Error if err < 0
112
+ self
113
+ end
114
+
115
+ def fullscreen
116
+ err = ::SDL2.SDL_SetWindowFullscreen(self, ::SDL2::SDL_WINDOW_FULLSCREEN)
117
+ raise RbSDL2Error if err < 0
118
+ self
119
+ end
120
+
121
+ def fullscreen_desktop
122
+ err = ::SDL2.SDL_SetWindowFullscreen(self, ::SDL2::SDL_WINDOW_FULLSCREEN_DESKTOP)
123
+ raise RbSDL2Error if err < 0
124
+ self
125
+ end
126
+
127
+ def hide
128
+ ::SDL2.SDL_HideWindow(self)
129
+ self
130
+ end
131
+
132
+ require_relative 'window/hit_test'
133
+
134
+ def hit_test_callback_set(*args) = HitTest.callback_set(self, *args)
135
+
136
+ def icon=(surface)
137
+ ::SDL2.SDL_SetWindowIcon(self, surface)
138
+ end
139
+
140
+ attr_reader :id
141
+
142
+ def maximize
143
+ ::SDL2.SDL_MaximizeWindow(self)
144
+ self
145
+ end
146
+
147
+ def minimize
148
+ ::SDL2.SDL_MinimizeWindow(self)
149
+ self
150
+ end
151
+
152
+ def mouse_position=(x_y)
153
+ ::SDL2.SDL_WarpMouseInWindow(self, *x_y)
154
+ end
155
+
156
+ def opacity
157
+ out_opacity = ::FFI::MemoryPointer.new(:float)
158
+ err = ::SDL2.SDL_GetWindowOpacity(self, out_opacity)
159
+ raise RbSDL2Error if err < 0
160
+ out_opacity.read_float
161
+ end
162
+
163
+ # ウィンドウの透明度を変更する。値は 0.0 から 1.0。値が低いほど透明になる。
164
+ def opacity=(val)
165
+ err = ::SDL2.SDL_SetWindowOpacity(self, val)
166
+ raise RbSDL2Error if err < 0
167
+ end
168
+
169
+ def popup
170
+ ::SDL2.SDL_RaiseWindow(self)
171
+ self
172
+ end
173
+
174
+ def resizable=(bool)
175
+ ::SDL2.SDL_SetWindowResizable(self, bool ? ::SDL2::SDL_TRUE : ::SDL2::SDL_FALSE)
176
+ end
177
+
178
+ def restore
179
+ ::SDL2.SDL_RestoreWindow(self)
180
+ self
181
+ end
182
+
183
+ def show
184
+ ::SDL2.SDL_ShowWindow(self)
185
+ self
186
+ end
187
+
188
+ def surface
189
+ ptr = ::SDL2.SDL_GetWindowSurface(self)
190
+ if ptr.null?
191
+ @surface = nil
192
+ raise RbSDL2Error
193
+ end
194
+ # SDL_Surface は参照カウンターで管理されているため Ruby 側でポインターを保持している限り
195
+ # 同アドレスに違う SDL_Surface が作成されることはない。安全にキャッシュできる。
196
+ ptr == @surface&.to_ptr ? @surface : @surface = Surface.to_ptr(ptr)
197
+ end
198
+
199
+ def title = ::SDL2.SDL_GetWindowTitle(self).read_string.force_encoding(Encoding::UTF_8)
200
+
201
+ def title=(obj)
202
+ ::SDL2.SDL_SetWindowTitle(self, obj&.to_s&.encode(Encoding::UTF_8))
203
+ end
204
+
205
+ def to_ptr
206
+ ptr = ::SDL2.SDL_GetWindowFromID(id)
207
+ raise RbSDL2Error, "Invalid window id or window was destroyed" if ptr.null?
208
+ ptr
209
+ end
210
+
211
+ # ウィンドウのサーフェィスのコピーを戻す。
212
+ # このメソッドはウィンドウのスクリーンショットが欲しい場合に使う。
213
+ def to_surface = surface.then { |s| convert(s.format) }
214
+
215
+ # surface メソッドを実行後に Window のサイズ変更があった場合、update メソッドを実行するとエラーになる。
216
+ # このメソッドは self を戻す。
217
+ def update(rect = nil)
218
+ yield(surface) if block_given?
219
+ # SDL_UpdateWindowSurface, SDL_UpdateWindowSurfaceRects の *初回* 呼び出しの前に
220
+ # SDL_GetWindowSurface が必要になる。
221
+ surface unless @surface
222
+
223
+ err = if rect
224
+ ::SDL2.SDL_UpdateWindowSurfaceRects(self, Rect.new(*rect), 1)
225
+ else
226
+ ::SDL2.SDL_UpdateWindowSurface(self)
227
+ end
228
+ # SDL_GetWindowSurface の後に Window のサイズ変更があった場合はエラーになる。
229
+ if err < 0
230
+ @surface = nil
231
+ raise RbSDL2Error
232
+ end
233
+ self
234
+ end
235
+
236
+ def windowed
237
+ err = ::SDL2.SDL_SetWindowFullscreen(self, 0)
238
+ raise RbSDL2Error if err < 0
239
+ self
240
+ end
241
+ end
242
+ end
data/lib/rb_sdl2.rb ADDED
@@ -0,0 +1,53 @@
1
+ module RbSDL2
2
+ require 'sdl2'
3
+ require_relative 'rb_sdl2/audio'
4
+ require_relative 'rb_sdl2/clipboard'
5
+ require_relative 'rb_sdl2/cpu_info'
6
+ require_relative 'rb_sdl2/cursor'
7
+ require_relative 'rb_sdl2/display'
8
+ require_relative 'rb_sdl2/display_mode'
9
+ require_relative 'rb_sdl2/error'
10
+ require_relative 'rb_sdl2/event'
11
+ require_relative 'rb_sdl2/filesystem'
12
+ require_relative 'rb_sdl2/hint'
13
+ require_relative 'rb_sdl2/keyboard'
14
+ require_relative 'rb_sdl2/message_box'
15
+ require_relative 'rb_sdl2/mouse'
16
+ require_relative 'rb_sdl2/palette'
17
+ require_relative 'rb_sdl2/pixel_format_enum'
18
+ require_relative 'rb_sdl2/platform'
19
+ require_relative 'rb_sdl2/power_info'
20
+ require_relative 'rb_sdl2/rect'
21
+ require_relative 'rb_sdl2/rw_ops'
22
+ require_relative 'rb_sdl2/screen_saver'
23
+ require_relative 'rb_sdl2/sdl'
24
+ require_relative 'rb_sdl2/surface'
25
+ require_relative 'rb_sdl2/text_input'
26
+ require_relative 'rb_sdl2/timer'
27
+ require_relative 'rb_sdl2/window'
28
+ require_relative 'rb_sdl2/video'
29
+ require_relative 'rb_sdl2/version'
30
+
31
+ class RbSDL2Error < StandardError
32
+ def initialize(error_message = Error.message) = super
33
+ end
34
+
35
+ class << self
36
+ def init = SDL.init
37
+
38
+ def load(path)
39
+ ::SDL2.load_lib(path)
40
+ # オーディオデバイスを閉じ忘れるとアプリケーションの終了時にメモリーアクセス違反を起こす。
41
+ # アプリケーションが強制終了した場合を考慮し終了処理を設定する。
42
+ at_exit { ::SDL2.SDL_Quit }
43
+ end
44
+
45
+ def loop
46
+ while true
47
+ Event.pump
48
+ yield
49
+ Event.clear
50
+ end
51
+ end
52
+ end
53
+ end
data/rb_sdl2.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "rb_sdl2"
5
+ spec.version = "0.1.0"
6
+ spec.author = "shinokaro"
7
+ spec.email = "shinokaro@hotmail.co.jp"
8
+ spec.summary = "Multimedia library with SDL."
9
+ spec.description = "RbSDL2 treats the functions and pointers provided by SDL as Ruby objects."
10
+ spec.license = "Zlib"
11
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.0.0")
12
+
13
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
14
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
15
+ end
16
+
17
+ spec.add_dependency "ffi", ">= 1.15.0"
18
+ spec.add_dependency "sdl2-bindings", ">= 0.0.7"
19
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rb_sdl2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - shinokaro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-10-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.15.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.15.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sdl2-bindings
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.7
41
+ description: RbSDL2 treats the functions and pointers provided by SDL as Ruby objects.
42
+ email: shinokaro@hotmail.co.jp
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - CHANGELOG.md
48
+ - LICENSE.txt
49
+ - README.md
50
+ - lib/rb_sdl2.rb
51
+ - lib/rb_sdl2/audio.rb
52
+ - lib/rb_sdl2/audio/audio_allowed_changes.rb
53
+ - lib/rb_sdl2/audio/audio_buffer.rb
54
+ - lib/rb_sdl2/audio/audio_device.rb
55
+ - lib/rb_sdl2/audio/audio_format.rb
56
+ - lib/rb_sdl2/audio/audio_spec.rb
57
+ - lib/rb_sdl2/clipboard.rb
58
+ - lib/rb_sdl2/cpu_info.rb
59
+ - lib/rb_sdl2/cursor.rb
60
+ - lib/rb_sdl2/cursor/color_cursor.rb
61
+ - lib/rb_sdl2/cursor/cursor_class.rb
62
+ - lib/rb_sdl2/cursor/cursor_pointer.rb
63
+ - lib/rb_sdl2/cursor/default_cursor.rb
64
+ - lib/rb_sdl2/cursor/system_cursor.rb
65
+ - lib/rb_sdl2/display.rb
66
+ - lib/rb_sdl2/display_mode.rb
67
+ - lib/rb_sdl2/error.rb
68
+ - lib/rb_sdl2/event.rb
69
+ - lib/rb_sdl2/event/event_filter.rb
70
+ - lib/rb_sdl2/event/event_queue.rb
71
+ - lib/rb_sdl2/event/event_type.rb
72
+ - lib/rb_sdl2/filesystem.rb
73
+ - lib/rb_sdl2/hint.rb
74
+ - lib/rb_sdl2/keyboard.rb
75
+ - lib/rb_sdl2/keyboard/key_mod.rb
76
+ - lib/rb_sdl2/keyboard/keyboard_state.rb
77
+ - lib/rb_sdl2/message_box.rb
78
+ - lib/rb_sdl2/mouse.rb
79
+ - lib/rb_sdl2/mouse/global_mouse.rb
80
+ - lib/rb_sdl2/mouse/mouse_button.rb
81
+ - lib/rb_sdl2/mouse/mouse_class.rb
82
+ - lib/rb_sdl2/mouse/mouse_wheel.rb
83
+ - lib/rb_sdl2/mouse/relative_mouse.rb
84
+ - lib/rb_sdl2/mouse/window_mouse.rb
85
+ - lib/rb_sdl2/palette.rb
86
+ - lib/rb_sdl2/pixel_format_enum.rb
87
+ - lib/rb_sdl2/platform.rb
88
+ - lib/rb_sdl2/power_info.rb
89
+ - lib/rb_sdl2/rect.rb
90
+ - lib/rb_sdl2/ref_count_pointer.rb
91
+ - lib/rb_sdl2/rw_ops.rb
92
+ - lib/rb_sdl2/rw_ops/rw_operator.rb
93
+ - lib/rb_sdl2/screen_saver.rb
94
+ - lib/rb_sdl2/sdl.rb
95
+ - lib/rb_sdl2/surface.rb
96
+ - lib/rb_sdl2/surface/blend_mode.rb
97
+ - lib/rb_sdl2/surface/pixel_format.rb
98
+ - lib/rb_sdl2/text_input.rb
99
+ - lib/rb_sdl2/timer.rb
100
+ - lib/rb_sdl2/version.rb
101
+ - lib/rb_sdl2/video.rb
102
+ - lib/rb_sdl2/window.rb
103
+ - lib/rb_sdl2/window/dialog.rb
104
+ - lib/rb_sdl2/window/display.rb
105
+ - lib/rb_sdl2/window/grab.rb
106
+ - lib/rb_sdl2/window/hit_test.rb
107
+ - lib/rb_sdl2/window/position.rb
108
+ - lib/rb_sdl2/window/shape.rb
109
+ - lib/rb_sdl2/window/size.rb
110
+ - lib/rb_sdl2/window/window_flags.rb
111
+ - rb_sdl2.gemspec
112
+ homepage:
113
+ licenses:
114
+ - Zlib
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 3.0.0
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubygems_version: 3.2.22
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Multimedia library with SDL.
135
+ test_files: []