rb_sdl2 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -0
  3. data/README.md +1 -1
  4. data/lib/rb_sdl2/audio/audio.rb +162 -0
  5. data/lib/rb_sdl2/audio/audio_buffer.rb +12 -19
  6. data/lib/rb_sdl2/audio/audio_device.rb +17 -23
  7. data/lib/rb_sdl2/audio/audio_spec.rb +38 -6
  8. data/lib/rb_sdl2/audio/audio_spec_reader.rb +10 -0
  9. data/lib/rb_sdl2/clipboard.rb +31 -31
  10. data/lib/rb_sdl2/cpu_info.rb +34 -17
  11. data/lib/rb_sdl2/cursor.rb +105 -24
  12. data/lib/rb_sdl2/display.rb +16 -16
  13. data/lib/rb_sdl2/display_mode.rb +1 -1
  14. data/lib/rb_sdl2/error.rb +11 -4
  15. data/lib/rb_sdl2/errors.rb +7 -0
  16. data/lib/rb_sdl2/event/event.rb +130 -0
  17. data/lib/rb_sdl2/event/event_filter.rb +34 -70
  18. data/lib/rb_sdl2/event/event_pointer.rb +26 -0
  19. data/lib/rb_sdl2/event/event_queue.rb +97 -120
  20. data/lib/rb_sdl2/event/event_type.rb +147 -205
  21. data/lib/rb_sdl2/filesystem.rb +8 -9
  22. data/lib/rb_sdl2/hint.rb +26 -24
  23. data/lib/rb_sdl2/keyboard/keyboard.rb +50 -0
  24. data/lib/rb_sdl2/keyboard/mod_state.rb +68 -0
  25. data/lib/rb_sdl2/keyboard/state.rb +39 -0
  26. data/lib/rb_sdl2/message_box.rb +69 -102
  27. data/lib/rb_sdl2/mouse/global_mouse.rb +10 -3
  28. data/lib/rb_sdl2/mouse/mouse.rb +64 -0
  29. data/lib/rb_sdl2/mouse/mouse_button.rb +13 -11
  30. data/lib/rb_sdl2/mouse/mouse_class.rb +9 -17
  31. data/lib/rb_sdl2/mouse/mouse_wheel.rb +21 -37
  32. data/lib/rb_sdl2/mouse/relative_mouse.rb +15 -1
  33. data/lib/rb_sdl2/palette.rb +12 -12
  34. data/lib/rb_sdl2/pixel_format_enum.rb +87 -55
  35. data/lib/rb_sdl2/platform.rb +1 -1
  36. data/lib/rb_sdl2/power_info.rb +34 -23
  37. data/lib/rb_sdl2/rect.rb +7 -1
  38. data/lib/rb_sdl2/ref_count_pointer.rb +25 -10
  39. data/lib/rb_sdl2/rw_ops/rw_file.rb +19 -0
  40. data/lib/rb_sdl2/rw_ops/rw_memory.rb +18 -0
  41. data/lib/rb_sdl2/rw_ops/rw_object.rb +126 -0
  42. data/lib/rb_sdl2/rw_ops/rw_ops.rb +104 -0
  43. data/lib/rb_sdl2/rw_ops/rw_ops_pointer.rb +14 -0
  44. data/lib/rb_sdl2/sdl.rb +67 -28
  45. data/lib/rb_sdl2/sdl_pointer.rb +22 -0
  46. data/lib/rb_sdl2/surface/blend_mode.rb +41 -41
  47. data/lib/rb_sdl2/surface/pixel_format.rb +34 -33
  48. data/lib/rb_sdl2/surface.rb +280 -265
  49. data/lib/rb_sdl2/text_input.rb +21 -15
  50. data/lib/rb_sdl2/timer.rb +36 -31
  51. data/lib/rb_sdl2/version.rb +3 -3
  52. data/lib/rb_sdl2/video.rb +17 -5
  53. data/lib/rb_sdl2/window/accessor.rb +135 -0
  54. data/lib/rb_sdl2/window/display.rb +8 -8
  55. data/lib/rb_sdl2/window/flash.rb +22 -0
  56. data/lib/rb_sdl2/window/hit_test.rb +22 -43
  57. data/lib/rb_sdl2/window/shape.rb +55 -40
  58. data/lib/rb_sdl2/window/state.rb +178 -0
  59. data/lib/rb_sdl2/window/window.rb +149 -0
  60. data/lib/rb_sdl2.rb +79 -17
  61. data/rb_sdl2.gemspec +4 -4
  62. metadata +31 -35
  63. data/lib/rb_sdl2/audio/audio_allowed_changes.rb +0 -17
  64. data/lib/rb_sdl2/audio/audio_format.rb +0 -23
  65. data/lib/rb_sdl2/audio.rb +0 -132
  66. data/lib/rb_sdl2/cursor/color_cursor.rb +0 -19
  67. data/lib/rb_sdl2/cursor/cursor_class.rb +0 -24
  68. data/lib/rb_sdl2/cursor/cursor_pointer.rb +0 -12
  69. data/lib/rb_sdl2/cursor/default_cursor.rb +0 -18
  70. data/lib/rb_sdl2/cursor/system_cursor.rb +0 -45
  71. data/lib/rb_sdl2/event.rb +0 -161
  72. data/lib/rb_sdl2/keyboard/key_mod.rb +0 -37
  73. data/lib/rb_sdl2/keyboard/keyboard_state.rb +0 -34
  74. data/lib/rb_sdl2/keyboard.rb +0 -50
  75. data/lib/rb_sdl2/mouse/window_mouse.rb +0 -17
  76. data/lib/rb_sdl2/mouse.rb +0 -74
  77. data/lib/rb_sdl2/rw_ops/rw_operator.rb +0 -105
  78. data/lib/rb_sdl2/rw_ops.rb +0 -149
  79. data/lib/rb_sdl2/screen_saver.rb +0 -11
  80. data/lib/rb_sdl2/window/dialog.rb +0 -19
  81. data/lib/rb_sdl2/window/grab.rb +0 -23
  82. data/lib/rb_sdl2/window/position.rb +0 -38
  83. data/lib/rb_sdl2/window/size.rb +0 -59
  84. data/lib/rb_sdl2/window/window_flags.rb +0 -78
  85. data/lib/rb_sdl2/window.rb +0 -242
@@ -1,137 +1,114 @@
1
1
  module RbSDL2
2
- class Event
3
- module EventQueue
4
- class << self
5
- require_relative 'event_type'
6
-
7
- def clear
8
- # type が ::SDL2::SDL_DROPFILE, ::SDL2::SDL_DROPTEXT の場合に
9
- # イベントに含まれる file メンバーのポインターが開放されない。そのため Ruby 側で開放処理を行う。
10
- event = Event.new
11
- while peep(event, ::SDL2::SDL_GETEVENT,
12
- type: ::SDL2::SDL_DROPFILE..::SDL2::SDL_DROPTEXT) > 0
13
- event.clear
14
- end
15
- ::SDL2.SDL_FlushEvents(*EventType.minmax)
16
- end
17
-
18
- def count(type: nil) = peep(nil, ::SDL2::SDL_PEEKEVENT, type: type)
19
- alias length count
20
- alias size count
21
-
22
- def deq(non_block = false, type: nil)
23
- event = Event.new
24
- while peep(event, ::SDL2::SDL_GETEVENT, type: type) == 0
25
- raise ThreadError, Error.message if non_block
26
- end
27
- event
28
- end
29
-
30
- # ブロックにはイベントキューにあるイベントが渡される。
31
- # ブロックへ渡されるイベントはコピーされたものでありブロックの外へ持ち出すことができる。
32
- # このメソッドは SDL のイベントキューをロックする。
33
- def each = block_given? ? reject! { |event| yield(event.dup); true } : to_enum
34
-
35
- def empty? = length == 0
36
-
37
- # イベントキューが一杯の時に 例外
38
- def enq(event, non_block = false)
39
- event_copy(event) do |copy|
40
- while peep(copy, ::SDL2::SDL_ADDEVENT) == 0
41
- raise ThreadError, Error.message if non_block
42
- end
43
- event
44
- end
2
+ class EventQueue
3
+ require_relative 'event'
4
+ require_relative 'event_pointer'
5
+ require_relative 'event_type'
6
+
7
+ class << self
8
+ def clear
9
+ # SDL_FlushEvents() SDL_DROPFILE, SDL_DROPTEXT の場合に file メンバーのポインターは開放しない。
10
+ # そのため Ruby 側でポインターの開放を行う。
11
+ ptr = EventPointer.malloc
12
+ ref_ptr = ptr + ::SDL::DropEvent.offset_of(:file)
13
+ while ::SDL.PeepEvents(ptr, 1, ::SDL::GETEVENT, ::SDL::DROPFILE, ::SDL::DROPTEXT) > 0
14
+ ::SDL.free(ref_ptr)
45
15
  end
46
-
47
- private def event_copy(event)
48
- copy = event.dup
49
- yield(copy).tap do
50
- # 成功した場合はメンバーポインターはキューにコピーされた。
51
- # ブロックの実行が成功したらメンバーポインターの開放を防ぐ。
52
- if event.drop_file? || event.drop_text?
53
- copy.to_ptr.autorelease = false
54
- ::SDL2.SDL_free(copy)
55
- end
56
- end
16
+ # SDL_FlushEvents() は SDL_TEXTEDITING_EXT の場合に text メンバーのポインターは開放しない。
17
+ # そのため Ruby 側でポインターの開放を行う。
18
+ ref_ptr = ptr + ::SDL::TextEditingExtEvent.offset_of(:text)
19
+ while ::SDL.PeepEvents(ptr, 1, ::SDL::GETEVENT, ::SDL::TEXTEDITING_EXT, ::SDL::TEXTEDITING_EXT) > 0
20
+ ::SDL.free(ref_ptr)
57
21
  end
22
+ ::SDL.FlushEvents(::SDL::FIRSTEVENT, ::SDL::LASTEVENT)
23
+ end
58
24
 
59
- private def main_thread!
60
- if Thread.main != Thread.current
61
- raise ThreadError, "the current thread is not the main thread"
62
- end
63
- end
25
+ def count
26
+ num = ::SDL.PeepEvents(nil, 0, ::SDL::PEEKEVENT, ::SDL::FIRSTEVENT, ::SDL::LASTEVENT)
27
+ raise RbSDL2Error if num < 0
28
+ num
29
+ end
64
30
 
65
- private def peep(event, action, type: nil)
66
- min_type, max_type = case type
67
- when nil
68
- EventType.minmax
69
- when Range
70
- type.minmax
71
- else
72
- [type, type]
73
- end
74
- num = ::SDL2.SDL_PeepEvents(event, event ? 1 : 0, action, min_type, max_type)
75
- raise RbSDL2Error if num < 0
76
- num
77
- end
31
+ def exist?(type = nil)
32
+ ::SDL.HasEvents(*EventType.to_types(type)) == ::SDL::TRUE
33
+ end
78
34
 
79
- def poll
80
- main_thread!
81
- event = Event.new
82
- ::SDL2::SDL_PollEvent(event).nonzero? && event
83
- end
84
- alias get poll
35
+ def poll
36
+ ptr = EventPointer.malloc
37
+ ::SDL.PollEvent(ptr).nonzero? && Event.to_ptr(ptr)
38
+ end
85
39
 
86
- def pump
87
- main_thread!
88
- ::SDL2.SDL_PumpEvents
89
- end
40
+ # メインスレッドから呼び出した方が良い。
41
+ def pump = ::SDL.PumpEvents
42
+
43
+ # イベントをキューに入れる。enq との違いは push ではイベントコールバックを起動する。
44
+ # 成功した場合は引数のイベントを戻す。フィルターされた場合は nil を戻す。
45
+ # キューに入れることが失敗したら例外が発生する。
46
+ def push!(event)
47
+ ptr = EventPointer.copy(event.to_ptr)
48
+ num = ::SDL.PushEvent(ptr)
49
+ raise RbSDL2Error if num < 0
50
+ num > 0 ? event : nil
51
+ end
90
52
 
91
- # イベントをキューに入れる。enq との違いは push ではイベントコールバックを起動する。
92
- # イベントがキューに入った場合は引数のイベントを戻す。フィルターされた場合は nil を戻す。
93
- # キューに入れることを失敗したら例外が発生する。
94
- def push(event)
95
- event_copy(event) do |copy|
96
- num = ::SDL2.SDL_PushEvent(copy)
97
- if num > 0
98
- event
99
- elsif num == 0
100
- nil
101
- else
102
- raise RbSDL2Error
103
- end
104
- end
53
+ def wait(sec = nil)
54
+ ptr = EventPointer.malloc
55
+ if sec.nil?
56
+ ::SDL.WaitEvent(ptr).nonzero? && Event.to_ptr(ptr)
57
+ elsif sec >= 0
58
+ ::SDL.WaitEventTimeout(ptr, sec * 1000).nonzero? && Event.to_ptr(ptr)
59
+ else
60
+ raise ArgumentError
105
61
  end
62
+ end
63
+ end
106
64
 
107
- def quit?
108
- pump
109
- ::SDL2.SDL_HasEvent(::SDL2::SDL_QUIT) == ::SDL2::SDL_TRUE
110
- end
65
+ # Thread::SizedQueue のように振る舞う。
111
66
 
112
- require_relative 'event_filter'
67
+ def initialize(type = nil)
68
+ @min_type, @max_type = EventType.to_types(type)
69
+ end
113
70
 
114
- # ブロックへ与えられたイベントは外に持ち出すことはできない。
115
- # このメソッドは SDL のイベントキューをロックする。
116
- # ブロックの戻り値が false の場合、イベントはキューから取り除かれる。
117
- # userdata に渡されたオブジェクトはポインターに変換可能なものである必要がある。
118
- def reject!(userdata = nil)
119
- func = EventFilter.new { |event| yield(event) || (event.clear; nil) }
120
- ::SDL2.SDL_FilterEvents(func, userdata)
121
- end
71
+ attr_reader :min_type, :max_type
122
72
 
123
- def wait(sec = nil)
124
- main_thread!
125
- event = Event.new
126
- if sec.nil?
127
- ::SDL2::SDL_WaitEvent(event).nonzero? && event
128
- elsif sec >= 0
129
- ::SDL2::SDL_WaitEventTimeout(event, sec * 1000).nonzero? && event
130
- else
131
- raise ArgumentError
132
- end
133
- end
73
+ def length
74
+ # SDL_PeepEvents に SDL_PEEKEVENT を与えた時のエラー条件
75
+ # - イベントシステムがシャットダウンしている場合。
76
+ # - イベントキューのロックが取得できない場合。
77
+ num = ::SDL.PeepEvents(nil, 0, ::SDL::PEEKEVENT, min_type, max_type)
78
+ raise RbSDL2Error if num < 0
79
+ num
80
+ end
81
+ alias size length
82
+
83
+ def empty? = length == 0
84
+
85
+ # 戻り値は不定(定義なし)
86
+ def push(event, non_block = false)
87
+ # SDL_PeepEvents に SDL_ADDEVENT を与えた時ののエラー条件
88
+ # - イベントシステムがシャットダウンしている場合。
89
+ # - イベントキューのロックが取得できない場合。
90
+ # 以下の条件の場合はエラーではなく 0 が戻る。
91
+ # - イベントキューのイベント数が上限に達した場合。
92
+ # - イベントキューが追加のメモリーが確保できない場合。
93
+ ptr = EventPointer.copy(event.to_ptr)
94
+ while ::SDL.PeepEvents(ptr, 1, ::SDL::ADDEVENT, min_type, max_type) <= 0
95
+ raise RbSDL2Error if non_block
96
+ end
97
+ end
98
+ alias << push
99
+ alias enq push
100
+
101
+ def pop(non_block = false)
102
+ ptr = EventPointer.malloc
103
+ # SDL_PeepEvents に SDL_GETEVENT を与えた時ののエラー条件
104
+ # - イベントシステムがシャットダウンしている場合。
105
+ # - イベントキューのロックが取得できない場合。
106
+ while ::SDL.PeepEvents(ptr, 1, ::SDL::GETEVENT, min_type, max_type) <= 0
107
+ raise RbSDL2Error if non_block
134
108
  end
109
+ Event.to_ptr(ptr)
135
110
  end
111
+ alias deq pop
112
+ alias shift pop
136
113
  end
137
114
  end
@@ -1,271 +1,213 @@
1
- # frozen_string_literal: true
2
1
  module RbSDL2
3
- class Event
4
- module EventType
5
- RANGE = ::SDL2::SDL_FIRSTEVENT..::SDL2::SDL_LASTEVENT
6
- EVENT_TYPES = RANGE.first.succ...::SDL2::SDL_LASTEVENT
7
- COMMON_EVENT_TYPES = EVENT_TYPES.first...::SDL2::SDL_USEREVENT
8
- USER_EVENT_TYPES = ::SDL2::SDL_USEREVENT...EVENT_TYPES.last
9
-
10
- default_type = -> (_, key) do
11
- case key
12
- when 0 then :common
13
- when COMMON_EVENT_TYPES then :common
14
- when USER_EVENT_TYPES then :user
15
- else raise KeyError, "(#{key})"
2
+ module EventType
3
+ COMMON_EVENT_TYPES = ::SDL::FIRSTEVENT.succ...::SDL::USEREVENT
4
+ USER_EVENT_TYPES = ::SDL::USEREVENT...::SDL::LASTEVENT
5
+
6
+ table = [
7
+ [::SDL::QUIT, ::SDL::QuitEvent, "SDL_QUIT"],
8
+ [::SDL::APP_TERMINATING, ::SDL::CommonEvent, "SDL_APP_TERMINATING"],
9
+ [::SDL::APP_LOWMEMORY, ::SDL::CommonEvent, "SDL_APP_LOWMEMORY"],
10
+ [::SDL::APP_WILLENTERBACKGROUND, ::SDL::CommonEvent, "SDL_APP_WILLENTERBACKGROUND"],
11
+ [::SDL::APP_DIDENTERBACKGROUND, ::SDL::CommonEvent, "SDL_APP_DIDENTERBACKGROUND"],
12
+ [::SDL::APP_WILLENTERFOREGROUND, ::SDL::CommonEvent, "SDL_APP_WILLENTERFOREGROUND"],
13
+ [::SDL::APP_DIDENTERFOREGROUND, ::SDL::CommonEvent, "SDL_APP_DIDENTERFOREGROUND"],
14
+ [::SDL::LOCALECHANGED, ::SDL::CommonEvent, "SDL_LOCALECHANGED"],
15
+ [::SDL::DISPLAYEVENT, ::SDL::DisplayEvent, "SDL_DISPLAYEVENT"],
16
+ [::SDL::WINDOWEVENT, ::SDL::WindowEvent, "SDL_WINDOWEVENT"],
17
+ [::SDL::SYSWMEVENT, ::SDL::SysWMEvent, "SDL_SYSWMEVENT"],
18
+ [::SDL::KEYDOWN, ::SDL::KeyboardEvent, "SDL_KEYDOWN"],
19
+ [::SDL::KEYUP, ::SDL::KeyboardEvent, "SDL_KEYUP"],
20
+ [::SDL::TEXTEDITING, ::SDL::TextEditingEvent, "SDL_TEXTEDITING"],
21
+ [::SDL::TEXTINPUT, ::SDL::TextInputEvent, "SDL_TEXTINPUT"],
22
+ [::SDL::KEYMAPCHANGED, ::SDL::CommonEvent, "SDL_KEYMAPCHANGED"],
23
+ [::SDL::TEXTEDITING_EXT, ::SDL::TextEditingExtEvent, "SDL_TEXTEDITING_EXT"],
24
+ [::SDL::MOUSEMOTION, ::SDL::MouseMotionEvent, "SDL_MOUSEMOTION"],
25
+ [::SDL::MOUSEBUTTONDOWN, ::SDL::MouseButtonEvent, "SDL_MOUSEBUTTONDOWN"],
26
+ [::SDL::MOUSEBUTTONUP, ::SDL::MouseButtonEvent, "SDL_MOUSEBUTTONUP"],
27
+ [::SDL::MOUSEWHEEL, ::SDL::MouseWheelEvent, "SDL_MOUSEWHEEL"],
28
+ [::SDL::JOYAXISMOTION, ::SDL::JoyAxisEvent, "SDL_JOYAXISMOTION"],
29
+ [::SDL::JOYBALLMOTION, ::SDL::JoyBallEvent, "SDL_JOYBALLMOTION"],
30
+ [::SDL::JOYHATMOTION, ::SDL::JoyHatEvent, "SDL_JOYHATMOTION"],
31
+ [::SDL::JOYBUTTONDOWN, ::SDL::JoyButtonEvent, "SDL_JOYBUTTONDOWN"],
32
+ [::SDL::JOYBUTTONUP, ::SDL::JoyButtonEvent, "SDL_JOYBUTTONUP"],
33
+ [::SDL::JOYDEVICEADDED, ::SDL::JoyDeviceEvent, "SDL_JOYDEVICEADDED"],
34
+ [::SDL::JOYDEVICEREMOVED, ::SDL::JoyDeviceEvent, "SDL_JOYDEVICEREMOVED"],
35
+ [::SDL::CONTROLLERAXISMOTION, ::SDL::ControllerAxisEvent, "SDL_CONTROLLERAXISMOTION"],
36
+ [::SDL::CONTROLLERBUTTONDOWN, ::SDL::ControllerButtonEvent, "SDL_CONTROLLERBUTTONDOWN"],
37
+ [::SDL::CONTROLLERBUTTONUP, ::SDL::ControllerButtonEvent, "SDL_CONTROLLERBUTTONUP"],
38
+ [::SDL::CONTROLLERDEVICEADDED, ::SDL::ControllerDeviceEvent, "SDL_CONTROLLERDEVICEADDED"],
39
+ [::SDL::CONTROLLERDEVICEREMOVED, ::SDL::ControllerDeviceEvent, "SDL_CONTROLLERDEVICEREMOVED"],
40
+ [::SDL::CONTROLLERDEVICEREMAPPED, ::SDL::ControllerDeviceEvent, "SDL_CONTROLLERDEVICEREMAPPED"],
41
+ [::SDL::CONTROLLERTOUCHPADDOWN, ::SDL::ControllerTouchpadEvent, "SDL_CONTROLLERTOUCHPADDOWN"],
42
+ [::SDL::CONTROLLERTOUCHPADMOTION, ::SDL::ControllerTouchpadEvent, "SDL_CONTROLLERTOUCHPADMOTION"],
43
+ [::SDL::CONTROLLERTOUCHPADUP, ::SDL::ControllerTouchpadEvent, "SDL_CONTROLLERTOUCHPADUP"],
44
+ [::SDL::CONTROLLERSENSORUPDATE, ::SDL::ControllerSensorEvent, "SDL_CONTROLLERSENSORUPDATE"],
45
+ [::SDL::FINGERDOWN, ::SDL::TouchFingerEvent, "SDL_FINGERDOWN"],
46
+ [::SDL::FINGERUP, ::SDL::TouchFingerEvent, "SDL_FINGERUP"],
47
+ [::SDL::FINGERMOTION, ::SDL::TouchFingerEvent, "SDL_FINGERMOTION"],
48
+ [::SDL::DOLLARGESTURE, ::SDL::DollarGestureEvent, "SDL_DOLLARGESTURE"],
49
+ [::SDL::DOLLARRECORD, ::SDL::DollarGestureEvent, "SDL_DOLLARRECORD"],
50
+ [::SDL::MULTIGESTURE, ::SDL::MultiGestureEvent, "SDL_MULTIGESTURE"],
51
+ [::SDL::CLIPBOARDUPDATE, ::SDL::CommonEvent, "SDL_CLIPBOARDUPDATE"],
52
+ [::SDL::DROPFILE, ::SDL::DropEvent, "SDL_DROPFILE"],
53
+ [::SDL::DROPTEXT, ::SDL::DropEvent, "SDL_DROPTEXT"],
54
+ [::SDL::DROPBEGIN, ::SDL::DropEvent, "SDL_DROPBEGIN"],
55
+ [::SDL::DROPCOMPLETE, ::SDL::DropEvent, "SDL_DROPCOMPLETE"],
56
+ [::SDL::AUDIODEVICEADDED, ::SDL::AudioDeviceEvent, "SDL_AUDIODEVICEADDED"],
57
+ [::SDL::AUDIODEVICEREMOVED, ::SDL::AudioDeviceEvent, "SDL_AUDIODEVICEREMOVED"],
58
+ [::SDL::SENSORUPDATE, ::SDL::SensorEvent, "SDL_SENSORUPDATE"],
59
+ [::SDL::RENDER_TARGETS_RESET, ::SDL::CommonEvent, "SDL_RENDER_TARGETS_RESET"],
60
+ [::SDL::RENDER_DEVICE_RESET, ::SDL::CommonEvent, "SDL_RENDER_DEVICE_RESET"],
61
+ [::SDL::POLLSENTINEL, ::SDL::CommonEvent, "SDL_POLLSENTINEL"],
62
+ ]
63
+
64
+ default_klass = -> (_, key) { USER_EVENT_TYPES === key ? ::SDL::UserEvent : ::SDL::CommonEvent }
65
+
66
+ ENTITY_MAP = Hash.new(&default_klass).merge!(table.map { |a| a.first(2) }.to_h).freeze
67
+
68
+ NAME_MAP = Hash.new.merge!(table.map { |c, _, s| [c, s.freeze] }.to_h).freeze
69
+
70
+ class << self
71
+ def disable(num) = ::SDL.EventState(num, ::SDL::DISABLE) == ::SDL::ENABLE
72
+
73
+ def enable(num) = ::SDL.EventState(num, ::SDL::ENABLE) == ::SDL::DISABLE
74
+
75
+ def ignore?(num) = ::SDL.EventState(num, ::SDL::QUERY) == ::SDL::IGNORE
76
+
77
+ def register_events(num)
78
+ if ::SDL.RegisterEvents(num) == 0xFFFFFFFF
79
+ raise RbSDL2Error, "unable to register user events because too many requests"
16
80
  end
17
81
  end
18
82
 
19
- MEMBER_MAP = Hash.new(&default_type).merge!(
20
- ::SDL2::SDL_AUDIODEVICEADDED => :adevice,
21
- ::SDL2::SDL_AUDIODEVICEREMOVED => :adevice,
22
- ::SDL2::SDL_DISPLAYEVENT => :display,
23
- ::SDL2::SDL_KEYDOWN => :key,
24
- ::SDL2::SDL_KEYUP => :key,
25
- ::SDL2::SDL_CONTROLLERAXISMOTION => :caxis,
26
- ::SDL2::SDL_CONTROLLERBUTTONDOWN => :cbutton,
27
- ::SDL2::SDL_CONTROLLERBUTTONUP => :cbutton,
28
- ::SDL2::SDL_CONTROLLERDEVICEADDED => :cdevice,
29
- ::SDL2::SDL_CONTROLLERDEVICEREMAPPED => :cdevice,
30
- ::SDL2::SDL_CONTROLLERDEVICEREMOVED => :cdevice,
31
- ::SDL2::SDL_CONTROLLERSENSORUPDATE => :csensor,
32
- ::SDL2::SDL_CONTROLLERTOUCHPADDOWN => :ctouchpad,
33
- ::SDL2::SDL_CONTROLLERTOUCHPADMOTION => :ctouchpad,
34
- ::SDL2::SDL_CONTROLLERTOUCHPADUP => :ctouchpad,
35
- ::SDL2::SDL_DOLLARGESTURE => :dgesture,
36
- ::SDL2::SDL_DOLLARRECORD => :dgesture,
37
- ::SDL2::SDL_DROPBEGIN => :drop,
38
- ::SDL2::SDL_DROPCOMPLETE => :drop,
39
- ::SDL2::SDL_DROPFILE => :drop,
40
- ::SDL2::SDL_DROPTEXT => :drop,
41
- ::SDL2::SDL_FINGERDOWN => :tfinger,
42
- ::SDL2::SDL_FINGERMOTION => :tfinger,
43
- ::SDL2::SDL_FINGERUP => :tfinger,
44
- ::SDL2::SDL_JOYAXISMOTION => :jaxis,
45
- ::SDL2::SDL_JOYBALLMOTION => :jball,
46
- ::SDL2::SDL_JOYBUTTONDOWN => :jbutton,
47
- ::SDL2::SDL_JOYBUTTONUP => :jbutton,
48
- ::SDL2::SDL_JOYDEVICEADDED => :jdevice,
49
- ::SDL2::SDL_JOYDEVICEREMOVED => :jdevice,
50
- ::SDL2::SDL_JOYHATMOTION => :jhat,
51
- ::SDL2::SDL_MOUSEBUTTONDOWN => :button,
52
- ::SDL2::SDL_MOUSEBUTTONUP => :button,
53
- ::SDL2::SDL_MOUSEMOTION => :motion,
54
- ::SDL2::SDL_MOUSEWHEEL => :wheel,
55
- ::SDL2::SDL_MULTIGESTURE => :mgesture,
56
- ::SDL2::SDL_QUIT => :quit,
57
- ::SDL2::SDL_SENSORUPDATE => :sensor,
58
- ::SDL2::SDL_SYSWMEVENT => :syswm,
59
- ::SDL2::SDL_TEXTEDITING => :edit,
60
- ::SDL2::SDL_TEXTINPUT => :text,
61
- ::SDL2::SDL_WINDOWEVENT => :window,
62
- ).freeze
63
-
64
- default_name = -> (_, key) do
65
- case key
66
- when 0 then ""
67
- when COMMON_EVENT_TYPES then "common_event"
68
- when USER_EVENT_TYPES then "user_event"
69
- else raise KeyError, "(#{key})"
83
+ def to_name(num)
84
+ case num
85
+ when COMMON_EVENT_TYPES then NAME_MAP[num]
86
+ when USER_EVENT_TYPES then "SDL_USEREVENT".freeze
87
+ else nil
70
88
  end
71
89
  end
72
90
 
73
- @name_map = Hash.new(&default_name).merge!(
74
- ::SDL2::SDL_APP_DIDENTERBACKGROUND => "app_did_enter_background",
75
- ::SDL2::SDL_APP_DIDENTERFOREGROUND => "app_did_enter_foreground",
76
- ::SDL2::SDL_APP_LOWMEMORY => "app_low_memory",
77
- ::SDL2::SDL_APP_TERMINATING => "app_terminating",
78
- ::SDL2::SDL_APP_WILLENTERBACKGROUND => "app_will_enter_background",
79
- ::SDL2::SDL_APP_WILLENTERFOREGROUND => "app_will_enter_foreground",
80
- ::SDL2::SDL_AUDIODEVICEADDED => "audio_device_added",
81
- ::SDL2::SDL_AUDIODEVICEREMOVED => "audio_device_removed",
82
- ::SDL2::SDL_CLIPBOARDUPDATE => "clipboard_update",
83
- ::SDL2::SDL_CONTROLLERAXISMOTION => "controller_axis_motion",
84
- ::SDL2::SDL_CONTROLLERBUTTONDOWN => "controller_button_down",
85
- ::SDL2::SDL_CONTROLLERBUTTONUP => "controller_button_up",
86
- ::SDL2::SDL_CONTROLLERDEVICEADDED => "controller_device_added",
87
- ::SDL2::SDL_CONTROLLERDEVICEREMAPPED => "controller_device_remapped",
88
- ::SDL2::SDL_CONTROLLERDEVICEREMOVED => "controller_device_removed",
89
- ::SDL2::SDL_CONTROLLERSENSORUPDATE => "controller_sensor_update",
90
- ::SDL2::SDL_CONTROLLERTOUCHPADDOWN => "controller_touchpad_down",
91
- ::SDL2::SDL_CONTROLLERTOUCHPADMOTION => "controller_touchpad_motion",
92
- ::SDL2::SDL_CONTROLLERTOUCHPADUP => "controller_touchpad_up",
93
- ::SDL2::SDL_DISPLAYEVENT => "display_event",
94
- ::SDL2::SDL_DOLLARGESTURE => "dollar_gesture",
95
- ::SDL2::SDL_DOLLARRECORD => "dollar_record",
96
- ::SDL2::SDL_DROPBEGIN => "drop_begin",
97
- ::SDL2::SDL_DROPCOMPLETE => "drop_complete",
98
- ::SDL2::SDL_DROPFILE => "drop_file",
99
- ::SDL2::SDL_DROPTEXT => "drop_text",
100
- ::SDL2::SDL_FINGERDOWN => "finger_down",
101
- ::SDL2::SDL_FINGERUP => "finger_up",
102
- ::SDL2::SDL_FINGERMOTION => "finger_motion",
103
- ::SDL2::SDL_JOYAXISMOTION => "joy_axis_motion",
104
- ::SDL2::SDL_JOYBALLMOTION => "joy_ball_motion",
105
- ::SDL2::SDL_JOYBUTTONDOWN => "joy_button_down",
106
- ::SDL2::SDL_JOYBUTTONUP => "joy_button_up",
107
- ::SDL2::SDL_JOYDEVICEADDED => "joy_device_added",
108
- ::SDL2::SDL_JOYDEVICEREMOVED => "joy_device_removed",
109
- ::SDL2::SDL_JOYHATMOTION => "joy_hat_motion",
110
- ::SDL2::SDL_KEYDOWN => "key_down",
111
- ::SDL2::SDL_KEYUP => "key_up",
112
- ::SDL2::SDL_KEYMAPCHANGED => "keymap_changed",
113
- ::SDL2::SDL_LOCALECHANGED => "locale_changed",
114
- ::SDL2::SDL_MULTIGESTURE => "multi_gesture",
115
- ::SDL2::SDL_MOUSEBUTTONDOWN => "mouse_button_down",
116
- ::SDL2::SDL_MOUSEBUTTONUP => "mouse_button_up",
117
- ::SDL2::SDL_MOUSEMOTION => "mouse_motion",
118
- ::SDL2::SDL_MOUSEWHEEL => "mouse_wheel",
119
- ::SDL2::SDL_QUIT => "quit",
120
- ::SDL2::SDL_RENDER_DEVICE_RESET => "render_device_reset",
121
- ::SDL2::SDL_RENDER_TARGETS_RESET => "render_targets_reset",
122
- ::SDL2::SDL_SENSORUPDATE => "sensor_update",
123
- ::SDL2::SDL_SYSWMEVENT => "sys_wm_event",
124
- ::SDL2::SDL_TEXTEDITING => "text_editing",
125
- ::SDL2::SDL_TEXTINPUT => "text_input",
126
- ::SDL2::SDL_WINDOWEVENT => "window_event",
127
- )
128
-
129
- @mutex = Mutex.new
130
-
131
- class << self
132
- def define_user_event(name = "user_event")
133
- @mutex.synchronize do
134
- num = ::SDL2::SDL_RegisterEvents(1)
135
- if num == 0xFFFFFFFF
136
- raise RbSDL2Error, "unable to register user events because too many requests"
137
- end
138
- @name_map[num] = name
139
- num
140
- end
91
+ def to_types(obj)
92
+ case obj
93
+ when nil then [::SDL::FIRSTEVENT, ::SDL::LASTEVENT]
94
+ when Integer then [obj, obj]
95
+ when Range then obj.minmax
96
+ else raise ArgumentError
141
97
  end
98
+ end
99
+ end
142
100
 
143
- def disable(type) = ::SDL2.SDL_EventState(type, ::SDL2::SDL_DISABLE) == ::SDL2::SDL_ENABLE
144
-
145
- def enable(type) = ::SDL2.SDL_EventState(type, ::SDL2::SDL_ENABLE) == ::SDL2::SDL_DISABLE
146
-
147
- def ignore?(type) = ::SDL2.SDL_EventState(type, ::SDL2::SDL_QUERY) == ::SDL2::SDL_IGNORE
148
-
149
- def minmax = RANGE.minmax
150
-
151
- def to_name(num) = @name_map[num]
152
-
153
- def to_num(obj)
154
- num = @name_map.key(obj.to_s)
155
- raise ArgumentError unless num
156
- num
157
- end
101
+ def app_did_enter_background? = ::SDL::APP_DIDENTERBACKGROUND == type
158
102
 
159
- def to_type(num) = MEMBER_MAP[num]
160
- end
103
+ def app_did_enter_foreground? = ::SDL::APP_DIDENTERFOREGROUND == type
161
104
 
162
- def app_did_enter_background? = ::SDL2::SDL_APP_DIDENTERBACKGROUND == type
105
+ def app_low_memory? = ::SDL::APP_LOWMEMORY == type
163
106
 
164
- def app_did_enter_foreground? = ::SDL2::SDL_APP_DIDENTERFOREGROUND == type
107
+ def app_terminating? = ::SDL::APP_TERMINATING == type
165
108
 
166
- def app_low_memory? = ::SDL2::SDL_APP_LOWMEMORY == type
109
+ def app_will_enter_background? = ::SDL::APP_WILLENTERBACKGROUND == type
167
110
 
168
- def app_terminating? = ::SDL2::SDL_APP_TERMINATING == type
111
+ def app_will_enter_foreground? = ::SDL::APP_WILLENTERFOREGROUND == type
169
112
 
170
- def app_will_enter_background? = ::SDL2::SDL_APP_WILLENTERBACKGROUND == type
113
+ def audio_device_added? = ::SDL::AUDIODEVICEADDED == type
171
114
 
172
- def app_will_enter_foreground? = ::SDL2::SDL_APP_WILLENTERFOREGROUND == type
115
+ def audio_device_removed? = ::SDL::AUDIODEVICEREMOVED == type
173
116
 
174
- def audio_device_added? = ::SDL2::SDL_AUDIODEVICEADDED == type
117
+ def clipboard_update? = ::SDL::CLIPBOARDUPDATE == type
175
118
 
176
- def audio_device_removed? = ::SDL2::SDL_AUDIODEVICEREMOVED == type
119
+ def controller_axis_motion? = ::SDL::CONTROLLERAXISMOTION == type
177
120
 
178
- def clipboard_update? = ::SDL2::SDL_CLIPBOARDUPDATE == type
121
+ def controller_button_down? = ::SDL::CONTROLLERBUTTONDOWN == type
179
122
 
180
- def controller_axis_motion? = ::SDL2::SDL_CONTROLLERAXISMOTION == type
123
+ def controller_button_up? = ::SDL::CONTROLLERBUTTONUP == type
181
124
 
182
- def controller_button_down? = ::SDL2::SDL_CONTROLLERBUTTONDOWN == type
125
+ def controller_device_added? = ::SDL::CONTROLLERDEVICEADDED == type
183
126
 
184
- def controller_button_up? = ::SDL2::SDL_CONTROLLERBUTTONUP == type
127
+ def controller_device_remapped? = ::SDL::CONTROLLERDEVICEREMAPPED == type
185
128
 
186
- def controller_device_added? = ::SDL2::SDL_CONTROLLERDEVICEADDED == type
129
+ def controller_device_removed? = ::SDL::CONTROLLERDEVICEREMOVED == type
187
130
 
188
- def controller_device_remapped? = ::SDL2::SDL_CONTROLLERDEVICEREMAPPED == type
131
+ def controller_sensor_update? = ::SDL::CONTROLLERSENSORUPDATE == type
189
132
 
190
- def controller_device_removed? = ::SDL2::SDL_CONTROLLERDEVICEREMOVED == type
133
+ def controller_touchpad_down? = ::SDL::CONTROLLERTOUCHPADDOWN == type
191
134
 
192
- def controller_sensor_update? = ::SDL2::SDL_CONTROLLERSENSORUPDATE == type
135
+ def controller_touchpad_motion? = ::SDL::CONTROLLERTOUCHPADMOTION == type
193
136
 
194
- def controller_touchpad_down? = ::SDL2::SDL_CONTROLLERTOUCHPADDOWN == type
137
+ def controller_touchpad_up? = ::SDL::CONTROLLERTOUCHPADUP == type
195
138
 
196
- def controller_touchpad_motion? = ::SDL2::SDL_CONTROLLERTOUCHPADMOTION == type
139
+ def display_event? = ::SDL::DISPLAYEVENT == type
197
140
 
198
- def controller_touchpad_up? = ::SDL2::SDL_CONTROLLERTOUCHPADUP == type
141
+ def dollar_gesture? = ::SDL::DOLLARGESTURE == type
199
142
 
200
- def display_event? = ::SDL2::SDL_DISPLAYEVENT == type
143
+ def dollar_record? = ::SDL::DOLLARRECORD == type
201
144
 
202
- def dollar_gesture? = ::SDL2::SDL_DOLLARGESTURE == type
145
+ def drop_begin? = ::SDL::DROPBEGIN == type
203
146
 
204
- def dollar_record? = ::SDL2::SDL_DOLLARRECORD == type
147
+ def drop_complete? = ::SDL::DROPCOMPLETE == type
205
148
 
206
- def drop_begin? = ::SDL2::SDL_DROPBEGIN == type
149
+ def drop_file? = ::SDL::DROPFILE == type
207
150
 
208
- def drop_complete? = ::SDL2::SDL_DROPCOMPLETE == type
151
+ def drop_text? = ::SDL::DROPTEXT == type
209
152
 
210
- def drop_file? = ::SDL2::SDL_DROPFILE == type
153
+ def finger_down? = ::SDL::FINGERDOWN == type
211
154
 
212
- def drop_text? = ::SDL2::SDL_DROPTEXT == type
155
+ def finger_motion? = ::SDL::FINGERMOTION == type
213
156
 
214
- def finger_down? = ::SDL2::SDL_FINGERDOWN == type
157
+ def finger_up? = ::SDL::FINGERUP == type
215
158
 
216
- def finger_up? = ::SDL2::SDL_FINGERUP == type
159
+ def joy_axis_motion? = ::SDL::JOYAXISMOTION == type
217
160
 
218
- def finger_motion? = ::SDL2::SDL_FINGERMOTION == type
161
+ def joy_ball_motion? = ::SDL::JOYBALLMOTION == type
219
162
 
220
- def joy_axis_motion? = ::SDL2::SDL_JOYAXISMOTION == type
163
+ def joy_button_down? = ::SDL::JOYBUTTONDOWN == type
221
164
 
222
- def joy_ball_motion? = ::SDL2::SDL_JOYBALLMOTION == type
165
+ def joy_button_up? = ::SDL::JOYBUTTONUP == type
223
166
 
224
- def joy_button_down? = ::SDL2::SDL_JOYBUTTONDOWN == type
167
+ def joy_device_added? = ::SDL::JOYDEVICEADDED == type
225
168
 
226
- def joy_button_up? = ::SDL2::SDL_JOYBUTTONUP == type
169
+ def joy_device_removed? = ::SDL::JOYDEVICEREMOVED == type
227
170
 
228
- def joy_device_added? = ::SDL2::SDL_JOYDEVICEADDED == type
171
+ def joy_hat_motion? = ::SDL::JOYHATMOTION == type
229
172
 
230
- def joy_device_removed? = ::SDL2::SDL_JOYDEVICEREMOVED == type
173
+ def key_down? = ::SDL::KEYDOWN == type
231
174
 
232
- def joy_hat_motion? = ::SDL2::SDL_JOYHATMOTION == type
175
+ def key_up? = ::SDL::KEYUP == type
233
176
 
234
- def key_down? = ::SDL2::SDL_KEYDOWN == type
177
+ def keymap_changed? = ::SDL::KEYMAPCHANGED == type
235
178
 
236
- def key_up? = ::SDL2::SDL_KEYUP == type
179
+ def locale_changed? = ::SDL::LOCALECHANGED == type
237
180
 
238
- def keymap_changed? = ::SDL2::SDL_KEYMAPCHANGED == type
181
+ def multi_gesture? = ::SDL::MULTIGESTURE == type
239
182
 
240
- def locale_changed? = ::SDL2::SDL_LOCALECHANGED == type
183
+ def mouse_button_down? = ::SDL::MOUSEBUTTONDOWN == type
241
184
 
242
- def multi_gesture? = ::SDL2::SDL_MULTIGESTURE == type
185
+ def mouse_button_up? = ::SDL::MOUSEBUTTONUP == type
243
186
 
244
- def mouse_button_down? = ::SDL2::SDL_MOUSEBUTTONDOWN == type
187
+ def mouse_motion? = ::SDL::MOUSEMOTION == type
245
188
 
246
- def mouse_button_up? = ::SDL2::SDL_MOUSEBUTTONUP == type
189
+ def mouse_wheel? = ::SDL::MOUSEWHEEL == type
247
190
 
248
- def mouse_motion? = ::SDL2::SDL_MOUSEMOTION == type
191
+ def poll_sentinel? = ::SDL::POLLSENTINEL == type
249
192
 
250
- def mouse_wheel? = ::SDL2::SDL_MOUSEWHEEL == type
193
+ def quit? = ::SDL::QUIT == type
251
194
 
252
- def quit? = ::SDL2::SDL_QUIT == type
195
+ def render_device_reset? = ::SDL::RENDER_DEVICE_RESET == type
253
196
 
254
- def render_device_reset? = ::SDL2::SDL_RENDER_DEVICE_RESET == type
197
+ def render_targets_reset? = ::SDL::RENDER_TARGETS_RESET == type
255
198
 
256
- def render_targets_reset? = ::SDL2::SDL_RENDER_TARGETS_RESET == type
199
+ def sensor_update? = ::SDL::SENSORUPDATE == type
257
200
 
258
- def sensor_update? = ::SDL2::SDL_SENSORUPDATE == type
201
+ def sys_wm_event? = ::SDL::SYSWMEVENT == type
259
202
 
260
- def sys_wm_event? = ::SDL2::SDL_SYSWMEVENT == type
203
+ def text_editing? = ::SDL::TEXTEDITING == type
261
204
 
262
- def text_editing? = ::SDL2::SDL_TEXTEDITING == type
205
+ def text_editing_ext? = ::SDL::TEXTEDITING_EXT == type
263
206
 
264
- def text_input? = ::SDL2::SDL_TEXTINPUT == type
207
+ def text_input? = ::SDL::TEXTINPUT == type
265
208
 
266
- def user_event? = USER_EVENT_TYPES === type
209
+ def user_event? = USER_EVENT_TYPES === type
267
210
 
268
- def window_event? = ::SDL2::SDL_WINDOWEVENT == type
269
- end
211
+ def window_event? = ::SDL::WINDOWEVENT == type
270
212
  end
271
213
  end