SDLRuby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +31 -0
  5. data/Rakefile +12 -0
  6. data/lib/SDLRuby/audio.rb +65 -0
  7. data/lib/SDLRuby/cursor.rb +46 -0
  8. data/lib/SDLRuby/display.rb +79 -0
  9. data/lib/SDLRuby/event/accessor.rb +127 -0
  10. data/lib/SDLRuby/event/type.rb +75 -0
  11. data/lib/SDLRuby/event.rb +80 -0
  12. data/lib/SDLRuby/font.rb +227 -0
  13. data/lib/SDLRuby/hint.rb +36 -0
  14. data/lib/SDLRuby/image/image.rb +23 -0
  15. data/lib/SDLRuby/image/include/SDL_image.h.rb +61 -0
  16. data/lib/SDLRuby/image.rb +69 -0
  17. data/lib/SDLRuby/keyboard.rb +58 -0
  18. data/lib/SDLRuby/mixer/channel.rb +43 -0
  19. data/lib/SDLRuby/mixer/chunk.rb +49 -0
  20. data/lib/SDLRuby/mixer/controller.rb +44 -0
  21. data/lib/SDLRuby/mixer/include/SDL_mixer.h.rb +95 -0
  22. data/lib/SDLRuby/mixer/sdl_mixer.rb +60 -0
  23. data/lib/SDLRuby/mixer.rb +87 -0
  24. data/lib/SDLRuby/mouse.rb +52 -0
  25. data/lib/SDLRuby/music.rb +158 -0
  26. data/lib/SDLRuby/palette.rb +49 -0
  27. data/lib/SDLRuby/pixel_formatter.rb +67 -0
  28. data/lib/SDLRuby/rect.rb +11 -0
  29. data/lib/SDLRuby/renderer/accessor.rb +70 -0
  30. data/lib/SDLRuby/renderer/drawer.rb +107 -0
  31. data/lib/SDLRuby/renderer/renderer_info.rb +27 -0
  32. data/lib/SDLRuby/renderer.rb +101 -0
  33. data/lib/SDLRuby/rw_ops/rw_object.rb +128 -0
  34. data/lib/SDLRuby/rw_ops.rb +216 -0
  35. data/lib/SDLRuby/sdl/include/SDL.h.rb +33 -0
  36. data/lib/SDLRuby/sdl/include/SDL_audio.h.rb +49 -0
  37. data/lib/SDLRuby/sdl/include/SDL_blendmode.h.rb +6 -0
  38. data/lib/SDLRuby/sdl/include/SDL_clipboard.h.rb +10 -0
  39. data/lib/SDLRuby/sdl/include/SDL_config.h.rb +13 -0
  40. data/lib/SDLRuby/sdl/include/SDL_cpuinfo.h.rb +27 -0
  41. data/lib/SDLRuby/sdl/include/SDL_endian.h.rb +3 -0
  42. data/lib/SDLRuby/sdl/include/SDL_error.h.rb +9 -0
  43. data/lib/SDLRuby/sdl/include/SDL_events.h.rb +32 -0
  44. data/lib/SDLRuby/sdl/include/SDL_filesystem.h.rb +6 -0
  45. data/lib/SDLRuby/sdl/include/SDL_gamecontroller.h.rb +66 -0
  46. data/lib/SDLRuby/sdl/include/SDL_gesture.h.rb +12 -0
  47. data/lib/SDLRuby/sdl/include/SDL_guid.h.rb +7 -0
  48. data/lib/SDLRuby/sdl/include/SDL_hints.h.rb +14 -0
  49. data/lib/SDLRuby/sdl/include/SDL_joystick.h.rb +69 -0
  50. data/lib/SDLRuby/sdl/include/SDL_keyboard.h.rb +26 -0
  51. data/lib/SDLRuby/sdl/include/SDL_keycode.h.rb +7 -0
  52. data/lib/SDLRuby/sdl/include/SDL_locale.h.rb +6 -0
  53. data/lib/SDLRuby/sdl/include/SDL_main.h.rb +3 -0
  54. data/lib/SDLRuby/sdl/include/SDL_messagebox.h.rb +10 -0
  55. data/lib/SDLRuby/sdl/include/SDL_misc.h.rb +5 -0
  56. data/lib/SDLRuby/sdl/include/SDL_mouse.h.rb +26 -0
  57. data/lib/SDLRuby/sdl/include/SDL_pixels.h.rb +25 -0
  58. data/lib/SDLRuby/sdl/include/SDL_platform.h.rb +3 -0
  59. data/lib/SDLRuby/sdl/include/SDL_power.h.rb +6 -0
  60. data/lib/SDLRuby/sdl/include/SDL_rect.h.rb +17 -0
  61. data/lib/SDLRuby/sdl/include/SDL_render.h.rb +94 -0
  62. data/lib/SDLRuby/sdl/include/SDL_revision.h.rb +2 -0
  63. data/lib/SDLRuby/sdl/include/SDL_rwops.h.rb +34 -0
  64. data/lib/SDLRuby/sdl/include/SDL_scancode.h.rb +5 -0
  65. data/lib/SDLRuby/sdl/include/SDL_sensor.h.rb +25 -0
  66. data/lib/SDLRuby/sdl/include/SDL_shape.h.rb +13 -0
  67. data/lib/SDLRuby/sdl/include/SDL_stdinc.h.rb +24 -0
  68. data/lib/SDLRuby/sdl/include/SDL_surface.h.rb +37 -0
  69. data/lib/SDLRuby/sdl/include/SDL_system.h.rb +6 -0
  70. data/lib/SDLRuby/sdl/include/SDL_syswm.h.rb +16 -0
  71. data/lib/SDLRuby/sdl/include/SDL_timer.h.rb +13 -0
  72. data/lib/SDLRuby/sdl/include/SDL_touch.h.rb +15 -0
  73. data/lib/SDLRuby/sdl/include/SDL_types.h.rb +3 -0
  74. data/lib/SDLRuby/sdl/include/SDL_version.h.rb +6 -0
  75. data/lib/SDLRuby/sdl/include/SDL_video.h.rb +117 -0
  76. data/lib/SDLRuby/sdl/sdl.rb +55 -0
  77. data/lib/SDLRuby/sdl/sdl_audio.rb +75 -0
  78. data/lib/SDLRuby/sdl/sdl_blendmode.rb +10 -0
  79. data/lib/SDLRuby/sdl/sdl_error.rb +5 -0
  80. data/lib/SDLRuby/sdl/sdl_events.rb +470 -0
  81. data/lib/SDLRuby/sdl/sdl_hints.rb +9 -0
  82. data/lib/SDLRuby/sdl/sdl_keyboard.rb +12 -0
  83. data/lib/SDLRuby/sdl/sdl_locale.rb +8 -0
  84. data/lib/SDLRuby/sdl/sdl_message_box.rb +7 -0
  85. data/lib/SDLRuby/sdl/sdl_mouse.rb +32 -0
  86. data/lib/SDLRuby/sdl/sdl_palette.rb +12 -0
  87. data/lib/SDLRuby/sdl/sdl_pixels.rb +138 -0
  88. data/lib/SDLRuby/sdl/sdl_rect.rb +11 -0
  89. data/lib/SDLRuby/sdl/sdl_render.rb +35 -0
  90. data/lib/SDLRuby/sdl/sdl_rw_ops.rb +29 -0
  91. data/lib/SDLRuby/sdl/sdl_scancode.rb +255 -0
  92. data/lib/SDLRuby/sdl/sdl_surface.rb +27 -0
  93. data/lib/SDLRuby/sdl/sdl_version.rb +5 -0
  94. data/lib/SDLRuby/sdl/sdl_video.rb +104 -0
  95. data/lib/SDLRuby/sdl.rb +192 -0
  96. data/lib/SDLRuby/sdl_error.rb +8 -0
  97. data/lib/SDLRuby/surface/pixel_format.rb +51 -0
  98. data/lib/SDLRuby/surface/surface_renderer.rb +26 -0
  99. data/lib/SDLRuby/surface.rb +179 -0
  100. data/lib/SDLRuby/text_input.rb +38 -0
  101. data/lib/SDLRuby/texture.rb +93 -0
  102. data/lib/SDLRuby/ttf/include/SDL_ttf.h.rb +54 -0
  103. data/lib/SDLRuby/ttf/ttf.rb +41 -0
  104. data/lib/SDLRuby/ttf.rb +54 -0
  105. data/lib/SDLRuby/version.rb +5 -0
  106. data/lib/SDLRuby/window/accessor.rb +125 -0
  107. data/lib/SDLRuby/window/controller.rb +53 -0
  108. data/lib/SDLRuby/window/display_info.rb +48 -0
  109. data/lib/SDLRuby/window/flag.rb +42 -0
  110. data/lib/SDLRuby/window/hit_tester.rb +61 -0
  111. data/lib/SDLRuby/window/input.rb +68 -0
  112. data/lib/SDLRuby/window/shaper.rb +68 -0
  113. data/lib/SDLRuby/window/surfacer.rb +37 -0
  114. data/lib/SDLRuby/window/window_renderer.rb +31 -0
  115. data/lib/SDLRuby/window.rb +90 -0
  116. data/lib/SDLRuby.rb +16 -0
  117. data/sig/SDLRuby/event.rbs +104 -0
  118. data/sig/SDLRuby/pixel_formmater.rbs +24 -0
  119. data/sig/SDLRuby/rect.rbs +5 -0
  120. data/sig/SDLRuby/sdl.rbs +606 -0
  121. data/sig/SDLRuby.rbs +4 -0
  122. metadata +168 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7516a8b356b287a6f985881964b1a44f76922354a13d70000b9990dd2a67badd
4
+ data.tar.gz: 358b7f4a6b40bd7a31e7249beaac18f77d3d7a08b21c9802f30203fce36628ed
5
+ SHA512:
6
+ metadata.gz: b5f6c08130fa89f6aa74009a02a5d4a7ada123839f0a2eef2894347ac358e0e75b188bbfd0a657004be3d54207166484b8b842b43edb5ee5dbf6340c4f13f095
7
+ data.tar.gz: 26017bbb155bb41c032396b13e9981ee62c72954b66501b2ab818356a9cedcbb295a74f810f742ecaf83b1ba52dd6c69b658e39859b35443a59f575fe0d72423
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-08-30
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 shinokaro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # SDLRuby
2
+
3
+ SDLRuby is a GEM that allows calling the SDL library from Ruby, enabling window creation, image display, audio playback, and more. It easily manages C pointers as Ruby objects, all written in 100% Ruby. Achieve multimedia programming simplicity.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add SDLRuby
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install SDLRuby
14
+
15
+ ## Usage
16
+
17
+ **⚠️UNDER CONSTRUCTION⚠️**
18
+
19
+ ## Development
20
+
21
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
22
+
23
+ To install this gem onto your local machine, run `bundle exec rake install`.
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/shinokaro/SDLRuby.
28
+
29
+ ## License
30
+
31
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,65 @@
1
+ module SDLRuby
2
+ module Audio
3
+ include SDL
4
+
5
+ class << self
6
+ include SDL
7
+
8
+ def default_device(capture: nil)
9
+ name = SDL.tmp_value("void*")
10
+ spec = SDL_AudioSpec.malloc(Fiddle::RUBY_FREE)
11
+ is_capture = capture ? 1 : 0
12
+ err = SDL. SDL_GetDefaultAudioInfo(name, spec, is_capture)
13
+ raise SDLError if err != 0
14
+
15
+ ptr = name.value
16
+ s = if ptr.null?
17
+ nil
18
+ else
19
+ ptr.free = SDL_FREE
20
+ ptr.to_s.force_encoding("UTF-8")
21
+ end
22
+
23
+ [s, spec.to_h]
24
+ end
25
+
26
+ def devices(capture: nil)
27
+ is_capture = capture ? 1 : 0
28
+ num = SDL.SDL_GetNumAudioDevices(is_capture)
29
+ num.times.map do |idx|
30
+ SDL.SDL_GetAudioDeviceName(idx, is_capture).to_s.force_encoding("UTF-8")
31
+ end
32
+ end
33
+
34
+ def device_spec(name, capture: nil)
35
+ idx = devices(capture: capture).index(name.encode("UTF-8"))
36
+
37
+ return unless idx
38
+
39
+ is_capture = capture ? 1 : 0
40
+ spec = SDL_AudioSpec.malloc(Fiddle::RUBY_FREE)
41
+
42
+ if SDL.SDL_GetAudioDeviceSpec(idx, is_capture, spec) == 0
43
+ spec.to_h
44
+ end
45
+ end
46
+
47
+ def driver
48
+ ptr = SDL.SDL_GetCurrentAudioDriver
49
+ ptr.null? ? nil : ptr.to_s
50
+ end
51
+
52
+ def drivers
53
+ num = SDL.SDL_GetNumAudioDrivers
54
+ num.times.map { |idx| SDL.SDL_GetAudioDriver(idx).to_s }
55
+ end
56
+
57
+ def init(driver)
58
+ err = SDL.SDL_AudioInit(driver)
59
+ raise SDLError if err < 0
60
+ end
61
+
62
+ def quit = SDL.SDL_AudioQuit
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,46 @@
1
+ module SDLRuby
2
+ class Cursor
3
+ include SDL
4
+
5
+ class << self
6
+ def [](obj, hot: nil)
7
+ case obj
8
+ when Surface
9
+ SDL.SDL_CreateColorCursor(obj, *hot && [0, 0])
10
+ when Integer
11
+ SDL.SDL_CreateSystemCursor(obj)
12
+ else
13
+ raise ArgumentError
14
+ end => ptr
15
+
16
+ raise SDLError if ptr.null?
17
+
18
+ new(ptr)
19
+ end
20
+
21
+ def default! = SDL.SDL_SetCursor(SDL.SDL_GetDefaultCursor)
22
+
23
+ def hide = SDL.SDL_ShowCursor(0)
24
+
25
+ def redraw = SDL.SDL_SetCursor(nil)
26
+
27
+ def show = SDL.SDL_ShowCursor(1)
28
+
29
+ def shown? = SDL.SDL_ShowCursor(-1) == 1
30
+ end
31
+
32
+ def initialize(ptr)
33
+ ptr.free = SDL_FREE_CURSOR
34
+ @ptr = ptr
35
+ end
36
+
37
+ def current? = SDL.SDL_GetCursor == @ptr
38
+
39
+ def show
40
+ SDL.SDL_SetCursor(self)
41
+ Cursor.show
42
+ end
43
+
44
+ def to_ptr = @ptr
45
+ end
46
+ end
@@ -0,0 +1,79 @@
1
+ require_relative 'pixel_formatter'
2
+
3
+ module SDLRuby
4
+ class DisplayMode
5
+ include PixelFormatter, SDL
6
+ extend Forwardable
7
+
8
+ def initialize
9
+ @st = SDL_DisplayMode.malloc(Fiddle::RUBY_FREE)
10
+ end
11
+
12
+ def_delegators :@st, :format, :w, :h, :refresh_rate
13
+
14
+ def to_ptr = @st.to_ptr
15
+ end
16
+
17
+ class Display
18
+ class << self
19
+ include SDL
20
+
21
+ # Videoの初期化がされていなければ0を戻す。
22
+ #
23
+ def count = (i = SDL.SDL_GetNumVideoDisplays) < 0 ? 0 : i
24
+
25
+ def orientation(idx) = SDL.SDL_GetDisplayOrientation(idx)
26
+
27
+ def orientation_name(idx)
28
+ case orientation(idx)
29
+ when SDL_ORIENTATION_UNKNOWN then ""
30
+ when SDL_ORIENTATION_LANDSCAPE then "landscape"
31
+ when SDL_ORIENTATION_LANDSCAPE_FLIPPED then "landscape_flipped"
32
+ when SDL_ORIENTATION_PORTRAIT then "portrait"
33
+ when SDL_ORIENTATION_PORTRAIT_FLIPPED then "portrait_flipped"
34
+ else
35
+ ""
36
+ end
37
+ end
38
+ end
39
+
40
+ attr_reader :bounds, :current_display_mode, :desktop_display_mode,
41
+ :display_modes, :name, :orientation, :usable_bounds
42
+
43
+ def initialize(num)
44
+ # この実装では例外を出さない。
45
+ # 基本的にSDLの内部構造体を読み出しているだけであり、
46
+ # 関数からのエラー報告はインデックス境界に関するものである。
47
+ #
48
+ # ディスプレイの状態はユーザのハードウェアやOS操作によって常に変化する。
49
+ # 一纏めにデータを取得する必要がある。
50
+ #
51
+ @bounds = Rect[].tap { |r| SDL.SDL_GetDisplayBounds(num, r) }.to_a
52
+
53
+ @current_display_mode = DisplayMode.new.tap do |st|
54
+ SDL.SDL_GetCurrentDisplayMode(num, st)
55
+ end
56
+
57
+ @desktop_display_mode = DisplayMode.new.tap do |st|
58
+ SDL.SDL_GetDesktopDisplayMode(num, st)
59
+ end
60
+
61
+ @display_modes = SDL.SDL_GetNumDisplayModes(num).times.map do |idx|
62
+ DisplayMode.new.tap { |st| SDL.SDL_GetDisplayMode(num, idx, st) }
63
+ end
64
+
65
+ ptr = SDL.SDL_GetDisplayName(num)
66
+ @name = (ptr.null? ? nil : ptr).to_s.force_encoding("UTF-8")
67
+
68
+ @orientation = Display.orientation(num)
69
+
70
+ @usable_bounds = Rect[].tap do |r|
71
+ SDL.SDL_GetDisplayUsableBounds(num, r)
72
+ end.to_a
73
+
74
+ @num = num
75
+ end
76
+
77
+ def to_i = @num
78
+ end
79
+ end
@@ -0,0 +1,127 @@
1
+ module SDLRuby
2
+ class Event
3
+ module Accessor
4
+ include SDL
5
+
6
+ union_members = SDL_Event.members.inject([]) do |result, (name, members)|
7
+ if members
8
+ # ネストした構造体の場合は親のみを選択する。
9
+ result << [name, members.flatten(1).select { |a| !(Array === a) }]
10
+ end
11
+
12
+ result
13
+ end
14
+
15
+ @modules = Hash.new do |_, key|
16
+ raise KeyError, "undefined SDL_Event member(#{key})"
17
+ end
18
+
19
+ union_members.each do |name, members|
20
+ @modules[name.to_sym] = Module.new do
21
+ extend Forwardable
22
+
23
+ def_delegators :entity, *members
24
+ end
25
+ end
26
+
27
+ class << self
28
+ def [](sym) = @modules[sym]
29
+ end
30
+
31
+ self[:adevice].module_eval do
32
+ def capture? = entity.iscapture != 0
33
+ end
34
+
35
+ self[:cbutton].module_eval do
36
+ def pressed? = state == SDL_PRESSED
37
+ end
38
+
39
+ self[:drop].module_eval do
40
+ undef file
41
+
42
+ def file = char_ptr_to_str(entity.file)
43
+
44
+ alias_method :text, :file
45
+
46
+ def self.extended(obj)
47
+ @_ptr ||= entity.file.tap { |ptr| ptr.free = SDL_FREE unless ptr.null? }
48
+ end
49
+ end
50
+
51
+ self[:edit].module_eval do
52
+ undef text
53
+
54
+ def text = char_ary_to_str(entity.text)
55
+ end
56
+
57
+ self[:editExt].module_eval do
58
+ undef text
59
+
60
+ def text = char_ptr_to_str(entity.text)
61
+
62
+ def self.extended(obj)
63
+ @_ptr ||= entity.text.tap { |ptr| ptr.free = SDL_FREE }
64
+ end
65
+ end
66
+
67
+ self[:jbutton].module_eval do
68
+ def pressed? = state == SDL_PRESSED
69
+ end
70
+
71
+ self[:key].module_eval do
72
+ undef keysym
73
+
74
+ def keysym = entity.keysym.to_h
75
+ end
76
+
77
+ self[:syswm].module_eval do
78
+ undef msg
79
+
80
+ def msg = entity.msg
81
+ end
82
+
83
+ self[:text].module_eval do
84
+ undef text
85
+
86
+ def text = char_ary_to_str(entity.text)
87
+ end
88
+
89
+ self[:user].module_eval do
90
+ undef data1, data2
91
+
92
+ def data1 = entity.data1
93
+
94
+ def data2 = entity.data2
95
+ end
96
+
97
+ # STIの実装としてメンバー全てのゲッターに例外を出さず応答するようにする。
98
+ # これは"y"メソッドにRubyが応答してしまうため。事前にオーバーライドしておく。
99
+ #
100
+ all_members = union_members.map { |a| a.last }.flatten.uniq
101
+
102
+ attr_reader(*all_members)
103
+
104
+ # Ruby like accessor
105
+ #
106
+ alias_method :d_dist, :dDist
107
+ alias_method :d_theta, :dTheta
108
+ alias_method :finger_id, :fingerId
109
+ alias_method :gesture_id, :gestureId
110
+ alias_method :mouse_x, :mouseX
111
+ alias_method :mouse_y, :mouseY
112
+ alias_method :num_fingers, :numFingers
113
+ alias_method :precise_x, :preciseX
114
+ alias_method :precise_y, :preciseY
115
+ alias_method :touch_id, :touchId
116
+ alias_method :window_id, :windowID
117
+
118
+ private def char_ary_to_str(a)
119
+ a[0, a.find_index(0)].pack("C*").force_encoding("UTF-8")
120
+ end
121
+
122
+ private def char_ptr_to_str(ptr)
123
+ ptr.null? ? "" : ptr.to_s.force_encoding("UTF-8")
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,75 @@
1
+ module SDLRuby
2
+ class Event
3
+ module Type
4
+ include SDL
5
+
6
+ QUIT_EVENT = SDL_QUIT..SDL_QUIT
7
+ DISPLAY_EVENT = SDL_DISPLAYEVENT..SDL_DISPLAYEVENT
8
+ WINDOW_EVENT = SDL_WINDOWEVENT..SDL_WINDOWEVENT
9
+ SYSWM_EVENT = SDL_SYSWMEVENT..SDL_SYSWMEVENT
10
+ KEY_EVENT = SDL_KEYDOWN..SDL_KEYUP
11
+ EDIT_EVENT = SDL_TEXTEDITING..SDL_TEXTEDITING
12
+ TEXT_EVENT = SDL_TEXTINPUT..SDL_TEXTINPUT
13
+ EDIT_EXT_EVENT = SDL_TEXTEDITING_EXT..SDL_TEXTEDITING_EXT
14
+ MOTION_EVENT = SDL_MOUSEMOTION..SDL_MOUSEMOTION
15
+ BUTTON_EVENT = SDL_MOUSEBUTTONDOWN..SDL_MOUSEBUTTONUP
16
+ WHEEL_EVENT = SDL_MOUSEWHEEL..SDL_MOUSEWHEEL
17
+ JAXIS_EVENT = SDL_JOYAXISMOTION..SDL_JOYAXISMOTION
18
+ JBALL_EVENT = SDL_JOYBALLMOTION..SDL_JOYBALLMOTION
19
+ JHAT_EVENT = SDL_JOYHATMOTION..SDL_JOYHATMOTION
20
+ JBUTTON_EVENT = SDL_JOYBUTTONDOWN..SDL_JOYBUTTONUP
21
+ JDEVICE_EVENT = SDL_JOYDEVICEADDED..SDL_JOYDEVICEREMOVED
22
+ JBATTERY_EVENT = SDL_JOYBATTERYUPDATED..SDL_JOYBATTERYUPDATED
23
+ CAXIS_EVENT = SDL_CONTROLLERAXISMOTION..SDL_CONTROLLERAXISMOTION
24
+ CBUTTON_EVENT = SDL_CONTROLLERBUTTONDOWN..SDL_CONTROLLERBUTTONUP
25
+ CDEVICE_EVENT = SDL_CONTROLLERDEVICEADDED..SDL_CONTROLLERDEVICEREMAPPED
26
+ CTOUCHPAD_EVENT = SDL_CONTROLLERTOUCHPADDOWN..SDL_CONTROLLERTOUCHPADUP
27
+ CSENSOR_EVENT = SDL_CONTROLLERSENSORUPDATE..SDL_CONTROLLERSENSORUPDATE
28
+ TFINGER_EVENT = SDL_FINGERDOWN..SDL_FINGERMOTION
29
+ DGESTURE_EVENT = SDL_DOLLARGESTURE..SDL_DOLLARRECORD
30
+ MGESTURE_EVENT = SDL_MULTIGESTURE..SDL_MULTIGESTURE
31
+ DROP_EVENT = SDL_DROPFILE..SDL_DROPCOMPLETE
32
+ ADEVICE_EVENT = SDL_AUDIODEVICEADDED..SDL_AUDIODEVICEREMOVED
33
+ SENSOR_EVENT = SDL_SENSORUPDATE..SDL_SENSORUPDATE
34
+ USER_EVENT = SDL_USEREVENT...SDL_LASTEVENT
35
+
36
+ class << self
37
+ def to_sym(type)
38
+ case type
39
+ when DISPLAY_EVENT then :display
40
+ when WINDOW_EVENT then :window
41
+ when KEY_EVENT then :key
42
+ when EDIT_EVENT then :edit
43
+ when EDIT_EXT_EVENT then :editExt
44
+ when TEXT_EVENT then :text
45
+ when MOTION_EVENT then :motion
46
+ when BUTTON_EVENT then :button
47
+ when WHEEL_EVENT then :wheel
48
+ when JAXIS_EVENT then :jaxis
49
+ when JBALL_EVENT then :jball
50
+ when JHAT_EVENT then :jhat
51
+ when JBUTTON_EVENT then :jbutton
52
+ when JDEVICE_EVENT then :jdevice
53
+ when JBATTERY_EVENT then :jbattery
54
+ when CAXIS_EVENT then :caxis
55
+ when CBUTTON_EVENT then :cbutton
56
+ when CDEVICE_EVENT then :cdevice
57
+ when CTOUCHPAD_EVENT then :ctouchpad
58
+ when CSENSOR_EVENT then :csensor
59
+ when ADEVICE_EVENT then :adevice
60
+ when SENSOR_EVENT then :sensor
61
+ when QUIT_EVENT then :quit
62
+ when USER_EVENT then :user
63
+ when SYSWM_EVENT then :syswm
64
+ when TFINGER_EVENT then :tfinger
65
+ when MGESTURE_EVENT then :mgesture
66
+ when DGESTURE_EVENT then :dgesture
67
+ when DROP_EVENT then :drop
68
+ else
69
+ :common
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,80 @@
1
+ require_relative 'event/accessor'
2
+ require_relative 'event/type'
3
+
4
+ module SDLRuby
5
+ class Event
6
+ include Accessor, Fiddle, SDL
7
+
8
+ class << self
9
+ include SDL
10
+
11
+ # SDL_WaitEvent, SDL_WaitEventTimeoutはRubyのスレッドを停止させるため実装しない。
12
+ # ※C関数呼び出し時にGVLを外せれば可能か?
13
+ # SDL_RegisterEventsはSDLに対応するゲッターがないため実装しない。
14
+ # SDL_PushEventは必要ならば実装する。ただしEventコンストラクターを含むため規模が大きい。
15
+ #
16
+ # SDL_AddEventWatch, SDL_SetEventFilterおよびSDL_FilterEventsは実装しない。
17
+ # イベントにポインターを含む場合、RubyのGCの管理下に置く方法が複雑なため。
18
+ # ※現実装ではイベントを取り込んだ時点でポインターをRubyの管理下に置いている。
19
+ # メンバーのポインター管理をファイナライザーに移行できれば可能だろう。
20
+ # ※そしてファイナライザーを設定するかどうか選択する。
21
+ # いずれもSDL_EventPumpを呼び出さなければ動作しない。
22
+ # 非同期にイベントを取得できるわけではない。
23
+ # SDL_PeepEventsは実装しない。
24
+ # SDL_ADDEVENT:
25
+ # イベントのメンバーにポインターを含む場合、それのSDL側へのコピーが必要になる。
26
+ # SDL_PushEventが実装されれば、それが同等の機能を持つ。
27
+ # SDL_PEEKEVENT:
28
+ # SDL_FilterEventsが実装できれば、それが同等の機能を持つ。
29
+ # SDL_GETEVENT:
30
+ # キューの削除が目的だと思う。それを行うならSDL_PollEventを利用して欲しい。
31
+ # イベントキューはコレクションではない。
32
+ #
33
+ # SDL_PeepEventsは実行中にキューをロックすることに注意。
34
+ # また、SDL_SYSWMEVENTのmsgメンバーにコピーが必要か分からない。(要調査)
35
+ # ※このポインターだけSDLが管理しているようだ。
36
+
37
+ def clear = (pump or flash)
38
+
39
+ def count = SDL.SDL_PeepEvents(nil, 0, SDL_PEEKEVENT, 0, -1)
40
+
41
+ def disable(type) = SDL.SDL_EventState(type, 0)
42
+
43
+ def enable(type) = SDL.SDL_EventState(type, 1)
44
+
45
+ def flash = SDL.SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT)
46
+
47
+ def get = poll(e = new)
48
+
49
+ private :new
50
+
51
+ private def poll(e) = (SDL.SDL_PollEvent(e).nonzero? and e.update)
52
+
53
+ def pump = SDL.SDL_PumpEvents
54
+
55
+ alias update pump
56
+
57
+ def quit? = (pump or SDL.SDL_HasEvent(SDL_QUIT) != 0)
58
+ end
59
+
60
+ def initialize
61
+ @st = SDL_Event.malloc(RUBY_FREE)
62
+ end
63
+
64
+ private attr_reader :entity
65
+
66
+ def to_ptr = @st.to_ptr
67
+
68
+ def to_sym = Type.to_sym(@st.type)
69
+
70
+ def update
71
+ unless @entity
72
+ sym = to_sym
73
+ @entity = @st.__send__(sym)
74
+ extend(Accessor[sym])
75
+ end
76
+
77
+ self
78
+ end
79
+ end
80
+ end