sdl2_ffi 0.0.3 → 0.0.4

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 (50) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/README.md +31 -17
  4. data/bin/yard +16 -0
  5. data/bin/yardoc +16 -0
  6. data/bin/yri +16 -0
  7. data/graph +566 -0
  8. data/lib/enumerable_constants.rb +82 -0
  9. data/lib/sdl2.rb +74 -117
  10. data/lib/sdl2/audio.rb +10 -7
  11. data/lib/sdl2/clipboard.rb +3 -3
  12. data/lib/sdl2/events.rb +228 -168
  13. data/lib/sdl2/gem_version.rb +1 -1
  14. data/lib/sdl2/haptic.rb +366 -55
  15. data/lib/sdl2/hints.rb +36 -24
  16. data/lib/sdl2/image.rb +4 -10
  17. data/lib/sdl2/init.rb +21 -31
  18. data/lib/sdl2/joystick.rb +15 -12
  19. data/lib/sdl2/keycode.rb +261 -261
  20. data/lib/sdl2/library.rb +96 -0
  21. data/lib/sdl2/mouse.rb +22 -17
  22. data/lib/sdl2/pixel_format.rb +2 -1
  23. data/lib/sdl2/pixels.rb +114 -161
  24. data/lib/sdl2/rect.rb +14 -10
  25. data/lib/sdl2/render.rb +29 -13
  26. data/lib/sdl2/renderer.rb +9 -2
  27. data/lib/sdl2/rwops.rb +11 -7
  28. data/lib/sdl2/scancode.rb +246 -245
  29. data/lib/sdl2/stdinc.rb +213 -0
  30. data/lib/sdl2/surface.rb +23 -7
  31. data/lib/sdl2/ttf.rb +23 -19
  32. data/lib/sdl2/version.rb +8 -2
  33. data/lib/sdl2/video.rb +64 -73
  34. data/lib/sdl2/window.rb +143 -36
  35. data/lib/sdl2_ffi.rb +2 -1
  36. data/sdl2_ffi.gemspec +3 -1
  37. data/test/fixtures/an_example.png +0 -0
  38. data/test/fixtures/background.bmp +0 -0
  39. data/test/fixtures/hello.bmp +0 -0
  40. data/test/functional/examples/test_lazy_foo_examples.rb +123 -0
  41. data/test/functional/examples/test_readme_examples.rb +15 -0
  42. data/test/unit/sdl2/test_haptic.rb +17 -0
  43. data/test/unit/sdl2/test_hints.rb +9 -9
  44. data/test/unit/sdl2/test_init.rb +8 -8
  45. data/test/unit/sdl2/test_log.rb +1 -1
  46. data/test/unit/sdl2/test_pixel_format.rb +3 -1
  47. data/test/unit/sdl2/test_video.rb +3 -3
  48. data/test/unit/sdl2/test_window.rb +4 -4
  49. data/test/unit/test_scratch.rb +2 -2
  50. metadata +37 -16
@@ -0,0 +1,82 @@
1
+ require 'ffi'
2
+ # This module adds a couple static-scope routines to
3
+ # any module that includes it, allowing them to enumerate
4
+ # the constants they define. Please help me make it better
5
+ #
6
+ # An example:
7
+ # module MyEnumeration
8
+ # include EnumerableConstants
9
+ # MY_FIRST_CONSTANT = next_const_value # defaults to 0
10
+ # MY_NEXT_CONSTANT = next_const_value # will be 1
11
+ # MY_THIRD_CONSTANT = next_const_value # will be 2, and the next would be 3 unless
12
+ # MY_FOURTH_CONSTANT = 100 # you assign a value.
13
+ # MY_FIFTH_CONSTANT = next_const_value # will be 101
14
+ # MY_SIXTH_CONSTANT = MY_THIRD_CONSTANT # There are no rules against duplicate values.
15
+ # MY_SEVENTH_CONSTANT = next_const_value # will be 3, be careful you don't unintentionally reuse values.
16
+ # end
17
+ #
18
+ # With this module, you can:
19
+ #
20
+ # MyEnumeration.by_name #=> {:MY_FIRST_CONSTANT=>0,:MY_NEXT_CONSTANT=>1,:MY_...}
21
+ #
22
+ # This is actually a standard function, nothing I added:
23
+ #
24
+ # MyEnumeration.constants #=> [:MY_FIRST_CONSTANT,:MY_SECOND_CONSTANT]
25
+ #
26
+ # Get the last value defined:
27
+ #
28
+ # MyEnumeration.last_const_value #=> 3, based on the example above.
29
+ #
30
+ # Get the next logical value, assuming the last value responds to .next,
31
+ # Otherwise, there will likely be an error.
32
+ # Note: This routine has no effect without assignment to a constant
33
+ # within this EnumerableConstants module/class. i.e.
34
+ #
35
+ # MyEnumeration::MY_EIGTH_CONSTANT = MyEnumeration.next_const_value # 4
36
+ #
37
+ # However, successive calls to just `#next_const_value` will just result in
38
+ # the same value.
39
+ # MyEnumeration.next_const_value # 5
40
+ # MyEnumeration.next_const_value # 5
41
+ module EnumerableConstants
42
+
43
+ # This is the method that injects the new static-scope routines.
44
+ def self.included(base)
45
+ base.module_eval do
46
+
47
+ ##
48
+ # Return the defined constants in a hash keyed by name.
49
+ def self.by_name
50
+ result = {}
51
+ self.constants.each do |constant|
52
+ result[constant] = self.const_get(constant)
53
+ end
54
+ return result
55
+ end
56
+
57
+ # This routine returns a flattened array of alternating symbols and values.
58
+ # The symbols are the CONSTANT names and values are the values defined by that constant.
59
+ def self.flatten_consts
60
+ by_name.to_a.flatten
61
+ end
62
+
63
+ ##
64
+ # Get the last defined value.
65
+ def self.last_const_value
66
+ if self.constants.empty?
67
+ return 0
68
+ else
69
+ return self.const_get(self.constants.last)
70
+ end
71
+ end
72
+
73
+ ##
74
+ # Get the next value in order.
75
+ def self.next_const_value
76
+ last_const_value.next
77
+ end
78
+
79
+ end
80
+ end
81
+
82
+ end
@@ -1,101 +1,13 @@
1
- require 'ffi'
2
1
  require 'sdl2/sdl_module'
3
2
  require 'active_support/inflector'
4
-
5
- # libSDL2's interface
3
+ require 'enumerable_constants'
4
+ require 'sdl2/stdinc'
5
+ # libSDL2's prototypes are attached directly to this module.
6
+ #
6
7
  module SDL2
7
8
  extend FFI::Library
8
- ffi_lib SDL_MODULE
9
-
10
- [:int, :uint8, :int8, :uint16, :int16, :uint32, :int32, :uint64, :int64, :float, :double].each do |type|
11
- typedef :pointer, "p_#{type}".to_sym
12
- end
13
-
14
- #Filter Proc, True when arg equals zero
15
- TRUE_WHEN_ZERO = Proc.new do |result|
16
- result == 0
17
- end
18
-
19
- # Filter Proc, True when arg not null?
20
- TRUE_WHEN_NOT_NULL = Proc.new do |result|
21
- (!result.null?)
22
- end
23
-
24
- # This converts the SDL Function Prototype name "SDL_XxxYyyyyZzz" to ruby's
25
- # "xxx_yyyy_zzz" convetion
26
- def self.api(func_name, args, type, options = {})
27
-
28
- options = {
29
- :error => false,
30
- :filter => TRUE_WHEN_ZERO
31
- }.merge(options)
32
-
33
- camelCaseName = func_name.to_s.gsub('SDL_','')
34
- methodName = ActiveSupport::Inflector.underscore(camelCaseName).to_sym
35
-
36
- self.attach_function methodName, func_name, args, type
37
-
38
- if options[:error]
39
- returns_error(methodName, options[:filter])
40
- end
41
-
42
- if type == :bool
43
- boolean?(methodName)
44
- end
45
-
46
- return methodName
47
- end
48
-
49
- # Returns the 'singleton class' so we can define class-level methods on the
50
- # fly.
51
- # There may be a better place to put this.
52
- def self.metaclass
53
-
54
- class << self; self; end
55
- end
56
-
57
- # Generates an alternative version of methodName that will raise a SDL Error
58
- # when the return value fails the filter test. The alternative version has
59
- # the same name, but with an exclamation mark ("!") at the end, indicating the
60
- # danger.
61
- def self.returns_error(methodName, filter)
62
- metaclass.instance_eval do
63
- define_method "#{methodName}!".to_sym do |*args|
64
- result = self.send(methodName, *args)
65
- raise_error_unless filter.call(result)
66
- result
67
- end
68
- end
69
- end
70
-
71
- # Generates an alternative version of methodName that will return Ruby's `true`
72
- # if the method named returns SDL_true/:true enum value. The alternative
73
- # method has the same name with a question mark ("?") appended, to indicate its
74
- # boolean nature.
75
- def self.boolean?(methodName)
76
- metaclass.instance_eval do
77
- define_method("#{methodName}?".to_sym) do |*args|
78
- self.send(methodName, *args) == :true
79
- end
80
- end
81
- end
82
-
83
- # Define a four character code as a Uint32
84
- #MACRO: SDL_FOURCC(A, B, C, D) \
85
- # ((SDL_static_cast(Uint32, SDL_static_cast(Uint8, (A))) << 0) | \
86
- # (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (B))) << 8) | \
87
- # (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (C))) << 16) | \
88
- # (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (D))) << 24))
89
- def self.fourcc(*args)
90
- bit_cnt = 0
91
- result = 0
92
- args.each do |arg|
93
- arg = arg.codepoints[0] if arg.kind_of? String
94
- result = result | (arg << bit_cnt)
95
- bit_cnt += 8
96
- end
97
- return result
98
- end
9
+ extend Library
10
+ ffi_lib SDL_MODULE
99
11
 
100
12
  # FFI::Struct class with some useful additions.
101
13
  class Struct < FFI::Struct
@@ -115,8 +27,37 @@ module SDL2
115
27
  pointer.free
116
28
  end
117
29
 
30
+ # Define a set of member readers
31
+ # Ex1: `member_readers [:one, :two, :three]`
32
+ # Ex2: `member_readers *members`
33
+ def self.member_readers(*members_to_define)
34
+ #self.class_eval do
35
+ members_to_define.each do |member|
36
+ define_method member do
37
+ [member]
38
+ end
39
+ end
40
+ #end
41
+ end
42
+
43
+ # Define a set of member writers
44
+ # Ex1: `member_writers [:one, :two, :three]`
45
+ # Ex2: `member_writers *members`
46
+ def self.member_writers(*members_to_define)
47
+ members_to_define.each do |member|
48
+ define_method "#{member}=".to_sym do |value|
49
+ self[member]= value
50
+ end
51
+ end
52
+ end
53
+
118
54
  # A human-readable representation of the struct and it's values.
119
55
  def inspect
56
+ return 'nil' if self.null?
57
+
58
+ #binding.pry
59
+ #return self.to_s
60
+
120
61
  report = "struct #{self.class.to_s}{"
121
62
  report += self.class.members.collect do |field|
122
63
  "#{field}->#{self[field].inspect}"
@@ -147,34 +88,18 @@ module SDL2
147
88
 
148
89
  end
149
90
 
150
- # SDL_Bool: SDL's TRUE/FALSE enumeration.
151
- # Ruby does not automatically evaluate 0 as false.
152
- enum :bool, [:false, 0, :true, 1]
153
-
154
- # Raise the current error value as a RuntimeException
155
- def self.raise_error
156
- raise "SDL Error: #{SDL2.get_error()}"
157
- end
158
-
159
- # Conditionally raise an error, unless true
160
- def self.raise_error_unless(condition)
161
- raise_error unless condition
162
- end
163
-
164
- # Conditionally raise an error, unless false
165
- def self.raise_error_if(condition)
166
- raise_error if condition
167
- end
168
91
 
92
+
169
93
  class TypedPointer < Struct
94
+
170
95
  def self.type(kind)
171
96
  layout :value, kind
172
97
  end
173
-
98
+
174
99
  def value
175
100
  self[value]
176
101
  end
177
-
102
+
178
103
  alias_method :deref, :value
179
104
  end
180
105
 
@@ -189,19 +114,19 @@ module SDL2
189
114
  type :int
190
115
  end
191
116
 
192
- #
117
+ #
193
118
  class UInt16Struct < TypedPointer
194
119
  type :uint16
195
120
  end
196
121
 
197
- class UInt32Struct < TypedPointer
122
+ class UInt32Struct < TypedPointer
198
123
  type :uint32
199
124
  end
200
125
 
201
126
  class UInt8Struct < TypedPointer
202
127
  type :uint8
203
128
  end
204
-
129
+
205
130
  # TODO: Review if this is the best place to put it.
206
131
  # BlendMode is defined in a header file that is always included, so I'm
207
132
  # defineing again here.
@@ -222,3 +147,35 @@ module SDL2
222
147
  typedef :int, :count
223
148
 
224
149
  end
150
+
151
+
152
+ require 'sdl2/init'
153
+
154
+ #TODO: require 'sdl2/assert'
155
+ #TODO: require 'sdl2/atomic'
156
+ require 'sdl2/audio'
157
+ require 'sdl2/clipboard'
158
+ require 'sdl2/cpuinfo'
159
+
160
+ #TODO: require 'sdl2/endian'
161
+ require 'sdl2/error'
162
+ require 'sdl2/events'
163
+ require 'sdl2/joystick'
164
+ require 'sdl2/gamecontroller'
165
+ require 'sdl2/haptic'
166
+ require 'sdl2/hints'
167
+
168
+ #TODO?: require 'sdl2/loadso'
169
+ require 'sdl2/log'
170
+
171
+ #TODO: require 'sdl2/messagebox'
172
+ #TODO: require 'sdl2/mutex'
173
+ require 'sdl2/power'
174
+ require 'sdl2/render'
175
+ require 'sdl2/rwops'
176
+
177
+ #TODO: require 'sdl2/system'
178
+ #TODO: require 'sdl2/thread'
179
+ require 'sdl2/timer'
180
+ require 'sdl2/version'
181
+ require 'sdl2/video'
@@ -1,17 +1,20 @@
1
1
  require 'sdl2'
2
- require 'yinum'
2
+ require 'sdl2/rwops'
3
+
3
4
 
4
5
  module SDL2
5
6
 
6
7
  typedef :uint16, :audio_format
7
8
 
8
9
  module Audio
9
- Mask = Enum.new(:AudioMask, {
10
- BITSIZE: (0xFF),
11
- DATATYPE: (1<<8),
12
- ENDIAN: (1<<12),
13
- SIGNED: (1<<15)
14
- })
10
+ module MASK
11
+ include EnumerableConstants
12
+
13
+ BITSIZE= (0xFF)
14
+ DATATYPE= (1<<8)
15
+ ENDIAN= (1<<12)
16
+ SIGNED= (1<<15)
17
+ end
15
18
 
16
19
  def self.bitsize(x)
17
20
  x & Mask.BITSIZE
@@ -5,15 +5,15 @@ module SDL2
5
5
  module Clipboard
6
6
 
7
7
  def self.has
8
- SDL2.has_clipboard_text()
8
+ SDL2.has_clipboard_text
9
9
  end
10
10
 
11
11
  def self.has?
12
- SDL2.has_clipboard_text == :true
12
+ SDL2.has_clipboard_text?
13
13
  end
14
14
 
15
15
  def self.get
16
- SDL2.get_clipboard_text()
16
+ SDL2.get_clipboard_text
17
17
  end
18
18
 
19
19
  def self.set(text)
@@ -7,78 +7,84 @@ require 'sdl2/joystick'
7
7
  require 'sdl2/gamecontroller'
8
8
  require 'sdl2/gesture'
9
9
  require 'sdl2/touch'
10
+ require 'sdl2/syswm/msg'
10
11
 
11
12
  module SDL2
12
-
13
+
13
14
  RELEASED = 0
14
15
  PRESSED = 1
15
-
16
- enum :event_type, [
17
- :firstEvent, 0,
18
-
19
- :quit, 0x100,
20
- :app_terminating,
21
- :app_lowMemory,
22
- :app_willEnterBackground,
23
- :app_didEnterBackground,
24
- :app_willEnterForeground,
25
- :app_didEnterForeground,
26
-
27
- :windowEvent, 0x200,
28
- :syswmEvent,
29
-
30
- :keyDown, 0x300,
31
- :keyUp,
32
- :textEditing,
33
- :textInput,
34
-
35
- :mouseMotion, 0x400,
36
- :mouseButtonDown,
37
- :mouseButtonUp,
38
- :mouseWheel,
39
-
40
- :joyAxisMotion, 0x600,
41
- :joyBallMotion,
42
- :joyHatMotion,
43
- :joyButtonDown,
44
- :joyButtonUp,
45
- :joyDeviceAdded,
46
- :joyDeviceRemoved,
47
-
48
- :controllerAxisMotion, 0x650,
49
- :controllerButtonDown,
50
- :controllerButtonUp,
51
- :controllerDeviceAdded,
52
- :controllerDeviceRemoved,
53
- :controllerDeviceRemapped,
54
-
55
- :fingerDown, 0x700,
56
- :fingerUp,
57
- :fingerMotion,
58
-
59
- :dollarGesture, 0x800,
60
- :dollarRecord,
61
- :dollarMultigesture,
62
-
63
- :clipboardUpdate, 0x900,
64
-
65
- :dropFile, 0x1000,
66
-
67
- :userEvent, 0x8000,
68
-
69
- :lastEvent, 0xFFFF
70
-
71
- ]
72
-
73
- COMMON_EVENT_LAYOUT = [:type, :uint32, :timestamp, :uint32]
74
-
16
+
17
+ # The types of events that can be delivered
18
+ module EVENTTYPE
19
+ include EnumerableConstants
20
+ FIRSTEVENT = 0
21
+
22
+ QUIT = 0x100
23
+
24
+ APP_TERMINATING = next_const_value
25
+ APP_LOWMEMORY = next_const_value
26
+ APP_WILLENTERBACKGROUND = next_const_value
27
+ APP_DIDENTERBACKGROUND = next_const_value
28
+ APP_WILLENTERFOREGROUND = next_const_value
29
+ APP_DIDENTERFOREGROUND = next_const_value
30
+
31
+ WINDOWEVENT = 0x200
32
+ SYSWMEVENT = next_const_value
33
+
34
+ KEYDOWN = 0x300
35
+ KEYUP = next_const_value
36
+ TEXTEDITING = next_const_value
37
+ TEXTINPUT = next_const_value
38
+
39
+ MOUSEMOTION = 0x400
40
+ MOUSEBUTTONDOWN = next_const_value
41
+ MOUSEBUTTONUP = next_const_value
42
+ MOUSEWHEEL = next_const_value
43
+
44
+ JOYAXISMOTION = 0x600
45
+ JOYBALLMOTION = next_const_value
46
+ JOYHATMOTION = next_const_value
47
+ JOYBUTTONDOWN = next_const_value
48
+ JOYBUTTONUP = next_const_value
49
+ JOYDEVICEADDED = next_const_value
50
+ JOYDEVICEREMOVED = next_const_value
51
+
52
+ CONTROLLERAXISMOTION = 0x650
53
+ CONTROLLERBUTTONDOWN = next_const_value
54
+ CONTROLLERBUTTONUP = next_const_value
55
+ CONTROLLERDEVICEADDED = next_const_value
56
+ CONTROLLERDEVICEREMOVED = next_const_value
57
+ CONTROLLERDEVICEREMAPPED = next_const_value
58
+
59
+ FINGERDOWN = 0x700
60
+ FINGERUP = next_const_value
61
+ FINGERMOTION = next_const_value
62
+
63
+ DOLLARGESTURE = 0x800
64
+ DOLLARRECORD = next_const_value
65
+ MULTIGESTURE = next_const_value
66
+
67
+ CLIPBOARDUPDATE = 0x900
68
+
69
+ DROPFILE = 0x1000
70
+
71
+ USEREVENT = 0x8000
72
+
73
+ LASTEVENT = 0xFFFF
74
+ end
75
+
76
+ enum :event_type, EVENTTYPE.flatten_consts
77
+
78
+ # Fields shared by every event
75
79
  class CommonEvent < Struct
76
- layout *COMMON_EVENT_LAYOUT
77
-
80
+ LAYOUT = [:type, :uint32, :timestamp, :uint32]
81
+ layout *LAYOUT
82
+
78
83
  end
79
-
84
+
85
+ # Window state change event data (event.window.*)
80
86
  class WindowEvent < Struct
81
- layout *COMMON_EVENT_LAYOUT + [
87
+ layout *CommonEvent::LAYOUT + [
82
88
  :windowID, :uint32,
83
89
  :event, :uint8,
84
90
  :padding1, :uint8,
@@ -88,9 +94,10 @@ module SDL2
88
94
  :data2, :int32
89
95
  ]
90
96
  end
91
-
97
+
98
+ # Keyboard button event structure (event.key.*)
92
99
  class KeyboardEvent < Struct
93
- layout *COMMON_EVENT_LAYOUT + [
100
+ layout *CommonEvent::LAYOUT + [
94
101
  :windowID, :uint32,
95
102
  :state, :uint8,
96
103
  :repeat, :uint8,
@@ -99,27 +106,30 @@ module SDL2
99
106
  :keysym, Keysym
100
107
  ]
101
108
  end
102
-
109
+
110
+ # Keyboard text editing event structure (event.edit.*)
103
111
  class TextEditingEvent < Struct
104
112
  TEXT_SIZE = 32 # Line 188
105
- layout *COMMON_EVENT_LAYOUT + [
113
+ layout *CommonEvent::LAYOUT + [
106
114
  :windowID, :uint32,
107
115
  :char, [:char, TEXT_SIZE],
108
116
  :start, :int32,
109
117
  :length, :int32
110
118
  ]
111
119
  end
112
-
120
+
121
+ # Keyboard text input event structure (event.text.*)
113
122
  class TextInputEvent < Struct
114
123
  TEXT_SIZE = 32 # Line 203
115
- layout *COMMON_EVENT_LAYOUT + [
124
+ layout *CommonEvent::LAYOUT + [
116
125
  :windowID, :uint32,
117
- :char, [:char, TEXT_SIZE]
126
+ :char, [:char, TEXT_SIZE]
118
127
  ]
119
128
  end
120
-
129
+
130
+ # Mouse motion event structure (event.motion.*)
121
131
  class MouseMotionEvent < Struct
122
- layout *COMMON_EVENT_LAYOUT + [
132
+ layout *CommonEvent::LAYOUT + [
123
133
  :windowID, :uint32,
124
134
  :which, :uint32,
125
135
  :state, :uint32,
@@ -129,9 +139,10 @@ module SDL2
129
139
  :yrel, :int32
130
140
  ]
131
141
  end
132
-
142
+
143
+ # Mouse button event structure (event.button.*)
133
144
  class MouseButtonEvent < Struct
134
- layout *COMMON_EVENT_LAYOUT + [
145
+ layout *CommonEvent::LAYOUT + [
135
146
  :windowID, :uint32,
136
147
  :which, :uint32,
137
148
  :button, :uint8,
@@ -142,18 +153,20 @@ module SDL2
142
153
  :y, :int32
143
154
  ]
144
155
  end
145
-
156
+
157
+ # Mouse wheel event structure (event.wheel.*)
146
158
  class MouseWheelEvent < Struct
147
- layout *COMMON_EVENT_LAYOUT + [
159
+ layout *CommonEvent::LAYOUT + [
148
160
  :windowID, :uint32,
149
161
  :which, :uint32,
150
162
  :x, :int32,
151
163
  :y, :int32
152
- ]
164
+ ]
153
165
  end
154
-
166
+
167
+ # Joystick axis motion event structure (event.jaxis.*)
155
168
  class JoyAxisEvent < Struct
156
- layout *COMMON_EVENT_LAYOUT + [
169
+ layout *CommonEvent::LAYOUT + [
157
170
  :which, :joystick_id,
158
171
  :axis, :uint8,
159
172
  :padding1, :uint8,
@@ -163,9 +176,10 @@ module SDL2
163
176
  :padding4, :uint16
164
177
  ]
165
178
  end
166
-
179
+
180
+ # Joystick trackball motion event structure (event.jball.*)
167
181
  class JoyBallEvent < Struct
168
- layout *COMMON_EVENT_LAYOUT + [
182
+ layout *CommonEvent::LAYOUT + [
169
183
  :which, :joystick_id,
170
184
  :ball, :uint8,
171
185
  :padding1, :uint8,
@@ -175,19 +189,21 @@ module SDL2
175
189
  :yrel, :int16
176
190
  ]
177
191
  end
178
-
192
+
193
+ # Joystick hat position change event structure (event.jhat.*)
179
194
  class JoyHatEvent < Struct
180
- layout *COMMON_EVENT_LAYOUT + [
195
+ layout *CommonEvent::LAYOUT + [
181
196
  :which, :joystick_id,
182
197
  :hat, :uint8,
183
198
  :value, :uint8,
184
199
  :padding1, :uint8,
185
200
  :padding2, :uint8
186
- ]
201
+ ]
187
202
  end
188
-
203
+
204
+ # Joystick button event structure (event.jbutton.*)
189
205
  class JoyButtonEvent < Struct
190
- layout *COMMON_EVENT_LAYOUT + [
206
+ layout *CommonEvent::LAYOUT + [
191
207
  :which, :joystick_id,
192
208
  :button, :uint8,
193
209
  :state, :uint8,
@@ -195,15 +211,17 @@ module SDL2
195
211
  :padding2, :uint8
196
212
  ]
197
213
  end
198
-
214
+
215
+ # Joystick device event structure (event.jdevice.*)
199
216
  class JoyDeviceEvent < Struct
200
- layout *COMMON_EVENT_LAYOUT + [
217
+ layout *CommonEvent::LAYOUT + [
201
218
  :which, :joystick_id
202
- ]
219
+ ]
203
220
  end
204
-
221
+
222
+ # Game controller axis motion event structure (event.caxis.*)
205
223
  class ControllerAxisEvent < Struct
206
- layout *COMMON_EVENT_LAYOUT + [
224
+ layout *CommonEvent::LAYOUT + [
207
225
  :which, :joystick_id,
208
226
  :axis, :uint8,
209
227
  :padding1, :uint8,
@@ -213,25 +231,28 @@ module SDL2
213
231
  :padding4, :uint16
214
232
  ]
215
233
  end
216
-
234
+
235
+ # Game controller button event structure (event.cbutton.*)
217
236
  class ControllerButtonEvent < Struct
218
- layout *COMMON_EVENT_LAYOUT + [
237
+ layout *CommonEvent::LAYOUT + [
219
238
  :which, :joystick_id,
220
239
  :button, :uint8,
221
240
  :state, :uint8,
222
241
  :padding1, :uint8,
223
242
  :padding2, :uint8
224
- ]
243
+ ]
225
244
  end
226
-
245
+
246
+ # Controller device event structure (event.cdevice.*)
227
247
  class ControllerDeviceEvent < Struct
228
- layout *COMMON_EVENT_LAYOUT + [
248
+ layout *CommonEvent::LAYOUT + [
229
249
  :which, :joystick_id
230
250
  ]
231
251
  end
232
-
252
+
253
+ # Touch finger event structure (event.tfinger.*)
233
254
  class TouchFingerEvent < Struct
234
- layout *COMMON_EVENT_LAYOUT + [
255
+ layout *CommonEvent::LAYOUT + [
235
256
  :touchId, :touch_id,
236
257
  :fingerId, :finger_id,
237
258
  :x, :float,
@@ -241,9 +262,10 @@ module SDL2
241
262
  :pressure, :float
242
263
  ]
243
264
  end
244
-
265
+
266
+ # Multiple Finger Gesture Event (event.mgesture.*)
245
267
  class MultiGestureEvent < Struct
246
- layout *COMMON_EVENT_LAYOUT + [
268
+ layout *CommonEvent::LAYOUT + [
247
269
  :touchId, :touch_id,
248
270
  :dTheta, :float,
249
271
  :dDist, :float,
@@ -253,35 +275,43 @@ module SDL2
253
275
  :padding, :uint16
254
276
  ]
255
277
  end
256
-
278
+
279
+ # Dollar Gesture Event (event.dgesture.*)
257
280
  class DollarGestureEvent < Struct
258
- layout *COMMON_EVENT_LAYOUT + [
281
+ layout *CommonEvent::LAYOUT + [
259
282
  :touchId, :touch_id,
260
283
  :gestureId, :gesture_id,
261
284
  :numFingers, :uint32,
262
285
  :error, :float,
263
286
  :x, :float,
264
287
  :y, :float
265
- ]
288
+ ]
266
289
  end
267
-
290
+
291
+ # @brief An event used to request a file open by the system (event.drop.*)
292
+ # This event is disabled by default, you can enable it with
293
+ # SDL_EventState()
294
+ # @note If you enable this event, you must free the filename in the event.
295
+ #
268
296
  class DropEvent < Struct
269
- layout *COMMON_EVENT_LAYOUT + [
297
+ layout *CommonEvent::LAYOUT + [
270
298
  :file, :string
271
- ]
299
+ ]
272
300
  end
273
-
301
+
302
+ # The "quit requested" event
274
303
  class QuitEvent < Struct
275
- layout *COMMON_EVENT_LAYOUT
304
+ layout *CommonEvent::LAYOUT
276
305
  end
277
-
306
+
307
+ # OS Specific event
278
308
  class OSEvent < Struct
279
- layout *COMMON_EVENT_LAYOUT
309
+ layout *CommonEvent::LAYOUT
280
310
  end
281
-
282
-
311
+
312
+ # A user-defined event type (event.user.*)
283
313
  class UserEvent < Struct
284
- layout *COMMON_EVENT_LAYOUT + [
314
+ layout *CommonEvent::LAYOUT + [
285
315
  :windowID, :uint32,
286
316
  :code, :int32,
287
317
  :data1, :pointer,
@@ -289,70 +319,100 @@ module SDL2
289
319
  ]
290
320
  end
291
321
 
322
+ # @brief A video driver dependent system event (event.syswm.*)
323
+ # This event is disabled by default, you can enable it with
324
+ # SDL_EventState()
325
+ #
326
+ # @note If you want to use this event, you should include SDL_syswm.h.
292
327
  class SysWMEvent < Struct
293
- layout *COMMON_EVENT_LAYOUT + [
328
+ layout *CommonEvent::LAYOUT + [
294
329
  :msg, SDL2::SysWM::Msg.by_ref
295
330
  ]
296
- end
297
-
331
+ end
332
+
333
+ # General event UNION (structure)
334
+ # Remember that this is a union, all other structures not related to #type are
335
+ # garbage
298
336
  class Event < FFI::Union
299
337
  layout :type, :uint32,
300
- :common, CommonEvent,
301
- :window, WindowEvent,
302
- :key, KeyboardEvent,
303
- :edit, TextEditingEvent,
304
- :text, TextInputEvent,
305
- :motion, MouseMotionEvent,
306
- :button, MouseButtonEvent,
307
- :wheel, MouseWheelEvent,
308
- :jaxis, JoyAxisEvent,
309
- :jball, JoyBallEvent,
310
- :jbutton, JoyButtonEvent,
311
- :jdevice, JoyDeviceEvent,
312
- :caxis, ControllerAxisEvent,
313
- :cbutton, ControllerButtonEvent,
314
- :cdevice, ControllerDeviceEvent,
315
- :quit, QuitEvent,
316
- :user, UserEvent,
317
- :syswm, SysWMEvent,
318
- :tfinger, TouchFingerEvent,
319
- :mgesture, MultiGestureEvent,
320
- :drop, DropEvent,
321
- :padding, [:uint8, 56] # From SDL_events.h:529
338
+ :common, CommonEvent,
339
+ :window, WindowEvent,
340
+ :key, KeyboardEvent,
341
+ :edit, TextEditingEvent,
342
+ :text, TextInputEvent,
343
+ :motion, MouseMotionEvent,
344
+ :button, MouseButtonEvent,
345
+ :wheel, MouseWheelEvent,
346
+ :jaxis, JoyAxisEvent,
347
+ :jball, JoyBallEvent,
348
+ :jbutton, JoyButtonEvent,
349
+ :jdevice, JoyDeviceEvent,
350
+ :caxis, ControllerAxisEvent,
351
+ :cbutton, ControllerButtonEvent,
352
+ :cdevice, ControllerDeviceEvent,
353
+ :quit, QuitEvent,
354
+ :user, UserEvent,
355
+ :syswm, SysWMEvent,
356
+ :tfinger, TouchFingerEvent,
357
+ :mgesture, MultiGestureEvent,
358
+ :drop, DropEvent,
359
+ :padding, [:uint8, 56] # From SDL_events.h:529
322
360
  end
323
-
324
- enum :eventaction, [:add, :peek, :get]
325
-
326
- attach_function :peep_events, :SDL_PeepEvents, [Event.by_ref, :count, :eventaction, :uint32, :uint32], :int
327
- attach_function :has_event, :SDL_HasEvent, [:uint32], :bool
328
- attach_function :has_events, :SDL_HasEvents, [:uint32, :uint32], :bool
329
- attach_function :flush_event, :SDL_FlushEvent, [:uint32], :void
330
- attach_function :flush_events, :SDL_FlushEvents, [:uint32, :uint32], :void
331
- attach_function :poll_event, :SDL_PollEvent, [Event.by_ref], :int
332
- attach_function :wait_event, :SDL_WaitEvent, [Event.by_ref], :int
333
- attach_function :wait_event_timeout, :SDL_WaitEventTimeout, [Event.by_ref, :count], :int
334
- attach_function :push_event, :SDL_PushEvent, [Event.by_ref, :count], :int
335
-
361
+
362
+ module EVENTACTION
363
+ include EnumerableConstants
364
+ ADD = next_const_value
365
+ PEEK = next_const_value
366
+ GET = next_const_value
367
+ end
368
+
369
+ enum :eventaction, EVENTACTION.flatten_consts
370
+
371
+ ##
372
+ # :class-method: peep_events
373
+ api :SDL_PeepEvents, [Event.by_ref, :count, :eventaction, :uint32, :uint32], :int
374
+ api :SDL_HasEvent, [:uint32], :bool
375
+ api :SDL_HasEvents, [:uint32, :uint32], :bool
376
+ api :SDL_FlushEvent, [:uint32], :void
377
+ api :SDL_FlushEvents, [:uint32, :uint32], :void
378
+ api :SDL_PollEvent, [Event.by_ref], :int
379
+ api :SDL_WaitEvent, [Event.by_ref], :int
380
+ api :SDL_WaitEventTimeout, [Event.by_ref, :count], :int
381
+ api :SDL_PushEvent, [Event.by_ref, :count], :int
382
+
383
+ ##
384
+ # callback event_filter #=> Proc.new do |pointer, event|; return int; end
336
385
  callback :event_filter, [:pointer, Event.by_ref], :int
386
+
387
+ # This simple structure is used for getting the event filter
337
388
  class EventFilterStruct < Struct
338
389
  layout :callback, :event_filter
339
390
  end
391
+
392
+ api :SDL_SetEventFilter, [:event_filter, :pointer], :void
393
+ api :SDL_GetEventFilter, [:pointer, :pointer], :bool
394
+ api :SDL_AddEventWatch, [:event_filter, :pointer], :void
395
+ api :SDL_DelEventWatch, [:event_filter, :pointer], :void
396
+ api :SDL_FilterEvents, [:event_filter, :pointer], :void
397
+
398
+ # Enumeration of event_state query
399
+ module EVENTSTATE
400
+ include EnumerableConstants
401
+ QUERY = -1
402
+ IGNORE = 0
403
+ DISABLE = 0
404
+ ENABLE = 1
405
+ end
340
406
 
341
- attach_function :set_event_filter, :SDL_SetEventFilter, [:event_filter, :pointer], :void
342
- attach_function :get_event_filter, :SDL_GetEventFilter, [:pointer, :pointer], :bool
343
- attach_function :add_event_watch, :SDL_AddEventWatch, [:event_filter, :pointer], :void
344
- attach_function :del_event_watch, :SDL_DelEventWatch, [:event_filter, :pointer], :void
345
- attach_function :filter_events, :SDL_FilterEvents, [:event_filter, :pointer], :void
346
-
347
- QUERY = -1
348
- IGNORE = 0
349
- DISABLE = 0
350
- ENABLE = 1
351
-
352
- attach_function :event_state, :SDL_EventState, [:uint32, :int], :uint8
407
+ enum :event_state, EVENTSTATE.flatten_consts
408
+
409
+ ##
410
+ #
411
+ api :SDL_EventState, [:uint32, :event_state], :uint8
412
+
353
413
  def get_event_state(type)
354
- event_state(type, QUERY)
414
+ event_state(type, EVENTSTATE::QUERY)
355
415
  end
356
-
357
- attach_function :register_events, :SDL_RegisterEvents, [:count], :uint32
416
+
417
+ api :SDL_RegisterEvents, [:count], :uint32
358
418
  end