sdl2_ffi 0.0.6 → 0.0.7

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 (204) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-version +1 -1
  3. data/Gemfile +0 -6
  4. data/Guardfile +0 -45
  5. data/README.md +11 -21
  6. data/lib/bad_sdl.rb +4 -0
  7. data/lib/{sdl2 → bad_sdl}/application.rb +47 -35
  8. data/lib/{sdl2 → bad_sdl}/engine.rb +20 -3
  9. data/lib/{sdl2 → bad_sdl}/engine/block_engine.rb +2 -2
  10. data/lib/bad_sdl/engine/engines.rb +51 -0
  11. data/lib/enumerable_constants.rb +4 -2
  12. data/lib/sdl2.rb +26 -247
  13. data/lib/sdl2/blendmode.rb +18 -0
  14. data/lib/sdl2/color.rb +1 -3
  15. data/lib/sdl2/debug.rb +58 -0
  16. data/lib/sdl2/display.rb +30 -23
  17. data/lib/sdl2/error.rb +6 -4
  18. data/lib/sdl2/event.rb +229 -0
  19. data/lib/sdl2/event/abstract.rb +11 -0
  20. data/lib/sdl2/event/action.rb +19 -0
  21. data/lib/sdl2/event/common.rb +10 -0
  22. data/lib/sdl2/event/controller_axis.rb +20 -0
  23. data/lib/sdl2/event/controller_button.rb +17 -0
  24. data/lib/sdl2/event/controller_device.rb +14 -0
  25. data/lib/sdl2/event/dollar_gesture.rb +19 -0
  26. data/lib/sdl2/event/drop.rb +18 -0
  27. data/lib/sdl2/event/joy_axis.rb +20 -0
  28. data/lib/sdl2/event/joy_ball.rb +20 -0
  29. data/lib/sdl2/event/joy_button.rb +18 -0
  30. data/lib/sdl2/event/joy_device.rb +14 -0
  31. data/lib/sdl2/event/joy_hat.rb +18 -0
  32. data/lib/sdl2/event/keyboard.rb +18 -0
  33. data/lib/sdl2/event/mouse_button.rb +21 -0
  34. data/lib/sdl2/event/mouse_motion.rb +19 -0
  35. data/lib/sdl2/event/mouse_wheel.rb +17 -0
  36. data/lib/sdl2/event/multi_gesture.rb +20 -0
  37. data/lib/sdl2/event/os.rb +12 -0
  38. data/lib/sdl2/event/quit.rb +12 -0
  39. data/lib/sdl2/event/state.rb +13 -0
  40. data/lib/sdl2/event/sys_wm.rb +15 -0
  41. data/lib/sdl2/event/text_editing.rb +17 -0
  42. data/lib/sdl2/event/text_input.rb +15 -0
  43. data/lib/sdl2/event/touch_finger.rb +20 -0
  44. data/lib/sdl2/event/type.rb +62 -0
  45. data/lib/sdl2/event/user.rb +15 -0
  46. data/lib/sdl2/event/window.rb +19 -0
  47. data/lib/sdl2/events.rb +23 -538
  48. data/lib/sdl2/gem_version.rb +1 -1
  49. data/lib/sdl2/haptic.rb +3 -3
  50. data/lib/sdl2/hints.rb +3 -1
  51. data/lib/sdl2/image.rb +20 -20
  52. data/lib/sdl2/init.rb +5 -8
  53. data/lib/sdl2/joystick.rb +104 -78
  54. data/lib/sdl2/joystick/axes.rb +22 -0
  55. data/lib/sdl2/joystick/balls.rb +24 -0
  56. data/lib/sdl2/joystick/buttons.rb +22 -0
  57. data/lib/sdl2/joystick/components.rb +23 -0
  58. data/lib/sdl2/joystick/hats.rb +22 -0
  59. data/lib/sdl2/keyboard.rb +11 -10
  60. data/lib/sdl2/keycode.rb +70 -70
  61. data/lib/sdl2/library.rb +8 -7
  62. data/lib/sdl2/log.rb +6 -0
  63. data/lib/sdl2/managed_struct.rb +17 -0
  64. data/lib/sdl2/mixer.rb +16 -12
  65. data/lib/sdl2/mixer/chunk.rb +14 -3
  66. data/lib/sdl2/mouse.rb +21 -41
  67. data/lib/sdl2/pixel_format.rb +4 -4
  68. data/lib/sdl2/pixels.rb +59 -47
  69. data/lib/sdl2/power.rb +1 -1
  70. data/lib/sdl2/rect.rb +4 -5
  71. data/lib/sdl2/render.rb +85 -170
  72. data/lib/sdl2/renderer.rb +337 -20
  73. data/lib/sdl2/rwops.rb +89 -30
  74. data/lib/sdl2/stdinc.rb +18 -21
  75. data/lib/sdl2/struct.rb +184 -0
  76. data/lib/sdl2/struct_helper.rb +32 -0
  77. data/lib/sdl2/surface.rb +15 -14
  78. data/lib/sdl2/syswm.rb +26 -10
  79. data/lib/sdl2/syswm/msg.rb +22 -4
  80. data/lib/sdl2/texture.rb +106 -0
  81. data/lib/sdl2/touch.rb +16 -20
  82. data/lib/sdl2/ttf.rb +29 -29
  83. data/lib/sdl2/ttf/sdl_ttf_module.rb +1 -1
  84. data/lib/sdl2/typed_pointer.rb +45 -0
  85. data/lib/sdl2/union.rb +7 -0
  86. data/lib/sdl2/video.rb +97 -183
  87. data/lib/sdl2/window.rb +140 -90
  88. data/lib/struct_array.rb +42 -0
  89. data/sdl2_ffi.gemspec +3 -3
  90. data/spec/fixtures/approvals/lazyfoonet_lesson_03_advanced_timers/has_a_message.approved.png +0 -0
  91. data/spec/fixtures/approvals/lazyfoonet_lesson_07_true_type_fonts/draws_the_message_to_the_screen.approved.png +0 -0
  92. data/spec/fixtures/approvals/lazyfoonet_lesson_14_advanced_timers/has_a_message.approved.png +0 -0
  93. data/spec/fixtures/approvals/lazyfoonet_lesson_14_advanced_timers/has_drawn_something.approved.png +0 -0
  94. data/spec/fixtures/approvals/sdl2/sdl2_renderer/should_be_able_to_clear.approved.png +0 -0
  95. data/spec/fixtures/approvals/sdl2/sdl2_renderer/should_be_able_to_draw_a_line_connecting_many_points.approved.png +0 -0
  96. data/spec/fixtures/approvals/sdl2/sdl2_renderer/should_be_able_to_draw_a_point.approved.png +0 -0
  97. data/spec/fixtures/approvals/sdl2/sdl2_renderer/should_be_able_to_draw_a_single_filled_rectangle.approved.png +0 -0
  98. data/spec/fixtures/approvals/sdl2/sdl2_renderer/should_be_able_to_draw_a_single_pixel_line.approved.png +0 -0
  99. data/spec/fixtures/approvals/sdl2/sdl2_renderer/should_be_able_to_draw_a_single_rectangle.approved.png +0 -0
  100. data/spec/fixtures/approvals/sdl2/sdl2_renderer/should_be_able_to_draw_many_filled_rectangles.approved.png +0 -0
  101. data/spec/fixtures/approvals/sdl2/sdl2_renderer/should_be_able_to_draw_many_rectangles_at_once.approved.png +0 -0
  102. data/spec/fixtures/approvals/sdl2/sdl2_renderer/should_be_able_to_draw_points.approved.png +0 -0
  103. data/spec/fixtures/approvals/sdl2/sdl2_renderer/should_be_able_to_render_textures.approved.png +0 -0
  104. data/spec/fixtures/approvals/sdl2/sdl2_renderer/should_be_able_to_rotate_and_flip_while_rendering_textures.approved.png +0 -0
  105. data/spec/fixtures/approvals/sdl2/sdl2_renderer/should_have_a_clip_rect.approved.png +0 -0
  106. data/spec/fixtures/approvals/sdl2_renderer/should_be_able_to_clear.approved.png +0 -0
  107. data/spec/fixtures/approvals/sdl2_renderer/should_be_able_to_draw_a_line_connecting_many_points.approved.png +0 -0
  108. data/spec/fixtures/approvals/sdl2_renderer/should_be_able_to_draw_a_point.approved.png +0 -0
  109. data/spec/fixtures/approvals/sdl2_renderer/should_be_able_to_draw_a_single_filled_rectangle.approved.png +0 -0
  110. data/spec/fixtures/approvals/sdl2_renderer/should_be_able_to_draw_a_single_pixel_line.approved.png +0 -0
  111. data/spec/fixtures/approvals/sdl2_renderer/should_be_able_to_draw_a_single_rectangle.approved.png +0 -0
  112. data/spec/fixtures/approvals/sdl2_renderer/should_be_able_to_draw_many_filled_rectangles.approved.png +0 -0
  113. data/spec/fixtures/approvals/sdl2_renderer/should_be_able_to_draw_many_rectangles_at_once.approved.png +0 -0
  114. data/spec/fixtures/approvals/sdl2_renderer/should_be_able_to_draw_points.approved.png +0 -0
  115. data/spec/fixtures/approvals/sdl2_renderer/should_be_able_to_render_textures.approved.png +0 -0
  116. data/spec/fixtures/approvals/sdl2_renderer/should_be_able_to_rotate_and_flip_while_rendering_textures.approved.png +0 -0
  117. data/spec/fixtures/approvals/sdl2_renderer/should_have_a_clip_rect.approved.png +0 -0
  118. data/spec/fixtures/images/finger.png +0 -0
  119. data/spec/fixtures/music/beat.wav +0 -0
  120. data/spec/fixtures/sounds/high.wav +0 -0
  121. data/spec/fixtures/sounds/low.wav +0 -0
  122. data/spec/fixtures/sounds/medium.wav +0 -0
  123. data/spec/fixtures/sounds/scratch.wav +0 -0
  124. data/spec/functional/lazy_foo_tutorial/lazy_foo_01_hello_world_spec.rb +4 -6
  125. data/spec/functional/lazy_foo_tutorial/lazy_foo_02_optimized_images_spec.rb +3 -4
  126. data/spec/functional/lazy_foo_tutorial/lazy_foo_03_extension_libraries_spec.rb +2 -2
  127. data/spec/functional/lazy_foo_tutorial/lazy_foo_04_event_driven_programming_spec.rb +3 -3
  128. data/spec/functional/lazy_foo_tutorial/lazy_foo_05_color_keying_spec.rb +1 -1
  129. data/spec/functional/lazy_foo_tutorial/lazy_foo_06_clip_blitting_and_sprite_sheets_spec.rb +1 -1
  130. data/spec/functional/lazy_foo_tutorial/lazy_foo_07_true_type_fonts_spec.rb +3 -2
  131. data/spec/functional/lazy_foo_tutorial/lazy_foo_08_key_presses_spec.rb +9 -7
  132. data/spec/functional/lazy_foo_tutorial/lazy_foo_09_mouse_events_spec.rb +11 -8
  133. data/spec/functional/lazy_foo_tutorial/lazy_foo_10_key_states_spec.rb +5 -4
  134. data/spec/functional/lazy_foo_tutorial/lazy_foo_11_playing_sounds_spec.rb +17 -9
  135. data/spec/functional/lazy_foo_tutorial/lazy_foo_12_timing_spec.rb +8 -7
  136. data/spec/functional/lazy_foo_tutorial/lazy_foo_13_advanced_timers_spec.rb +85 -0
  137. data/spec/functional/lazy_foo_tutorial/lazy_foo_14_regulating_frame_rate_spec.rb +67 -0
  138. data/spec/functional/lazy_foo_tutorial/lazy_foo_15_calculating_frame_rate_spec.rb +48 -0
  139. data/spec/functional/lazy_foo_tutorial/lazy_foo_16_motion_spec.rb +95 -0
  140. data/spec/functional/lazy_foo_tutorial/lazy_foo_helper.rb +3 -1
  141. data/spec/functional/lazy_foo_tutorial/timer.rb +55 -0
  142. data/spec/lib/bad_sdl/application_spec.rb +22 -0
  143. data/spec/{unit → lib}/sdl2/color_spec.rb +5 -1
  144. data/spec/lib/sdl2/display_spec.rb +6 -0
  145. data/spec/lib/sdl2/event_spec.rb +102 -0
  146. data/spec/lib/sdl2/events_spec.rb +12 -0
  147. data/spec/lib/sdl2/joystick_spec.rb +130 -0
  148. data/spec/lib/sdl2/keyboard_spec.rb +22 -0
  149. data/spec/lib/sdl2/mouse_spec.rb +21 -0
  150. data/spec/lib/sdl2/render_spec.rb +62 -0
  151. data/spec/lib/sdl2/renderer_spec.rb +224 -0
  152. data/spec/lib/sdl2/sdl2_helper.rb +5 -0
  153. data/spec/lib/sdl2/texture_spec.rb +65 -0
  154. data/{test/unit/sdl2/test_video.rb → spec/lib/sdl2/video_spec.rb} +82 -57
  155. data/spec/lib/sdl2/window_spec.rb +42 -0
  156. data/spec/{unit → lib}/unit_helper.rb +0 -0
  157. data/spec/png_writer.rb +9 -1
  158. data/spec/spec_helper.rb +5 -3
  159. metadata +201 -132
  160. data/lib/sdl2/engine/engines.rb +0 -46
  161. data/test/approvals/clip_blitting_and_sprite_sheets.received.bmp +0 -0
  162. data/test/approvals/color_keying_example.approved.png +0 -0
  163. data/test/approvals/hello_world_example_hello.approved.png +0 -0
  164. data/test/approvals/hello_world_example_screen.approved.png +0 -0
  165. data/test/approvals/optimized_surface_loading.approved.png +0 -0
  166. data/test/fixtures/an_example.png +0 -0
  167. data/test/fixtures/background.bmp +0 -0
  168. data/test/fixtures/background.jpg +0 -0
  169. data/test/fixtures/color_bars.jpg +0 -0
  170. data/test/fixtures/foo.jpg +0 -0
  171. data/test/fixtures/hello.bmp +0 -0
  172. data/test/fixtures/sprites.jpg +0 -0
  173. data/test/fixtures/x.png +0 -0
  174. data/test/functional/examples/lazyfoo.net_s/test_clip_blitting_and_sprite_sheets.rb +0 -50
  175. data/test/functional/examples/lazyfoo.net_s/test_color_keying.rb +0 -42
  176. data/test/functional/examples/test_lazy_foo_examples.rb +0 -195
  177. data/test/functional/examples/test_readme_examples.rb +0 -15
  178. data/test/test_helper.rb +0 -13
  179. data/test/unit/sdl2/test_assert.rb +0 -10
  180. data/test/unit/sdl2/test_audio.rb +0 -9
  181. data/test/unit/sdl2/test_clipboard.rb +0 -13
  182. data/test/unit/sdl2/test_cpuinfo.rb +0 -11
  183. data/test/unit/sdl2/test_error.rb +0 -20
  184. data/test/unit/sdl2/test_events.rb +0 -31
  185. data/test/unit/sdl2/test_haptic.rb +0 -27
  186. data/test/unit/sdl2/test_hints.rb +0 -50
  187. data/test/unit/sdl2/test_image.rb +0 -9
  188. data/test/unit/sdl2/test_init.rb +0 -29
  189. data/test/unit/sdl2/test_keyboard.rb +0 -9
  190. data/test/unit/sdl2/test_log.rb +0 -80
  191. data/test/unit/sdl2/test_palette.rb +0 -26
  192. data/test/unit/sdl2/test_pixel_format.rb +0 -38
  193. data/test/unit/sdl2/test_pixels.rb +0 -24
  194. data/test/unit/sdl2/test_power.rb +0 -11
  195. data/test/unit/sdl2/test_rect.rb +0 -19
  196. data/test/unit/sdl2/test_render.rb +0 -58
  197. data/test/unit/sdl2/test_surface.rb +0 -88
  198. data/test/unit/sdl2/test_syswm.rb +0 -11
  199. data/test/unit/sdl2/test_timer.rb +0 -9
  200. data/test/unit/sdl2/test_ttf.rb +0 -11
  201. data/test/unit/sdl2/test_version.rb +0 -16
  202. data/test/unit/sdl2/test_window.rb +0 -155
  203. data/test/unit/test_scratch.rb +0 -19
  204. data/test/unit/test_sdl2.rb +0 -16
@@ -0,0 +1,18 @@
1
+ module SDL2
2
+ module BLENDMODE
3
+ include EnumerableConstants
4
+ NONE = 0x00000000
5
+ BLEND = 0x00000001
6
+ ADD = 0x00000002
7
+ MOD = 0x00000004
8
+ end
9
+ # TODO: Review if this is the best place to put it.
10
+ # BlendMode is defined in a header file that is always included, so I'm
11
+ # defineing again here.
12
+ enum :blend_mode, BLENDMODE.flatten_consts
13
+
14
+ class SDL2::TypedPointer::BlendMode < SDL2::TypedPointer
15
+ layout :value, :blend_mode
16
+ end
17
+
18
+ end
@@ -20,10 +20,8 @@ module SDL2
20
20
  # If possible, convert argument into a SDL::Color
21
21
  def self.cast(something)
22
22
  if something.kind_of? Array
23
- something.map!(&:to_i)
24
23
  result = new
25
- result.set(*something)
26
-
24
+ result.set(*(something.map(&:to_i)))
27
25
  return result
28
26
  else
29
27
  return super
@@ -0,0 +1,58 @@
1
+ module SDL2
2
+ # This class manages logging output
3
+ module Debug
4
+
5
+ ENABLED = Hash.new do
6
+ false # Always defaults to false
7
+ end
8
+
9
+ STATE = {
10
+ log_to: STDERR,
11
+ recursion: 0,
12
+ last_caller: [],
13
+ }
14
+
15
+ ##
16
+ # Returns true if the instance
17
+ # It will return true if the object inherits from a class enabled for debugging.
18
+ def self.enabled?(obj)
19
+ # Immediately return true if we are debugging this instance specifically.
20
+ return true if ENABLED[obj]
21
+ # Otherwise, check if the object is a class or get it's class.
22
+ klass = obj.kind_of?(Class) ? obj : obj.class
23
+ # And until there is no more superclass.
24
+ until klass == nil
25
+ # Return true if the class is enabled for debugging.
26
+ return true if ENABLED[klass]
27
+ # Otherwise keep searching.
28
+ klass = klass.superclass
29
+ end
30
+ # If we got this far, it is a bust.
31
+ false
32
+ end
33
+
34
+ ##
35
+ # Takes an instance or a class to enable for debugging.
36
+ # NOTE: If you only enable a single instance, then you will not see messages from the class-level.
37
+ def self.enable(obj)
38
+ ENABLED[obj] = true
39
+ end
40
+
41
+ ##
42
+ # This will log a message from a method.
43
+ # The source object is required (usually self) and this is how debugging is controlled.
44
+ # The message must be specified in a block which will never be evaluated unless debugging is enabled for the source.
45
+ # The source class will be identified along with the method name calling log in the output.
46
+ def self.log(src, &msg_block)
47
+ return unless klass = enabled?(src)
48
+ method_name = /.*`(.*)'/.match(caller.first)[1]
49
+ is_class = src.kind_of?(Class)
50
+ klass = is_class ? src : src.class
51
+ msg_start = "#{klass.to_s}#{is_class ? '::' : '#'}#{method_name} >"
52
+ msg = msg_block.nil? ? "No message" : msg_block.call
53
+ STATE[:log_to].puts(caller.first)
54
+ STATE[:log_to].puts(msg_start + msg)
55
+ end
56
+ end
57
+
58
+ end
@@ -3,22 +3,24 @@ require 'sdl2/display/modes'
3
3
  require 'sdl2/display/mode'
4
4
 
5
5
  module SDL2
6
-
6
+ ##
7
7
  # Abstract representation of what SDL calls a "Display"
8
8
  class Display
9
-
10
- attr_reader :id
11
-
9
+ ##
10
+ # Every display has an id, an index.
11
+ attr_reader :id
12
+ ##
13
+ # Initialize a new display for index display_id
12
14
  def initialize(display_id)
13
15
  @id = display_id.to_i # It should be an integer.
14
-
15
16
  end
16
-
17
-
17
+ ##
18
+ # Every display has many modes
18
19
  def modes
19
20
  @modes ||= Modes.new(self)
20
21
  end
21
-
22
+ ##
23
+ # Get the display instance for index display_id
22
24
  def self.[](display_id)
23
25
  if (idx = display_id.to_i) < count
24
26
  return Display.new(display_id)
@@ -26,32 +28,37 @@ module SDL2
26
28
  return nil
27
29
  end
28
30
  end
29
-
31
+ ##
32
+ # Get the number of displays
30
33
  def self.count
31
- SDL2.get_num_video_displays()
34
+ SDL2.get_num_video_displays!()
32
35
  end
33
-
34
- def self.count!
35
- total = count
36
- SDL2.raise_error_if total < 0
37
- return total
36
+ ##
37
+ # An alias for count, the number of displays
38
+ def self.num
39
+ self.count
38
40
  end
39
-
41
+ ##
42
+ # Return the first display
40
43
  def self.first
41
44
  self[0]
42
45
  end
43
-
46
+ ##
47
+ # Retrieve a display mode closest to a requested ideal.
48
+ # May return nil
44
49
  def closest_display_mode(wanted)
45
50
  closest = SDL2::Display::Mode.new # Make a new structure.
46
51
  return SDL2.get_closest_display_mode(@id, wanted, closest)
47
52
  end
48
-
49
- def closest_display_mode!(wanted)
50
- found = closest_display_mode(wanted)
51
- SDL2.raise_error_if(found.nil?)
52
- return found
53
+ ##
54
+ # Get the current display mode
55
+ def current_display_mode
56
+ display_mode = SDL2::Display::Mode.new
57
+ SDL2.get_current_display_mode!(@id, display_mode)
58
+ display_mode
53
59
  end
54
-
60
+ ##
61
+ # Returns the bounds
55
62
  def bounds
56
63
  rect = SDL2::Rect.new
57
64
  if SDL2.get_display_bounds(@id, rect) == 0
@@ -1,15 +1,17 @@
1
1
  require 'sdl2'
2
2
 
3
+ ##
4
+ # sdl_error.h - Error handling routines.
3
5
  module SDL2
4
-
6
+
5
7
  ##
6
- #
8
+ # Remove current SDL error string.
7
9
  api :SDL_ClearError, [], :void
8
10
  ##
9
- #
11
+ # Retrieve current SDL error string.
10
12
  api :SDL_GetError, [], :string
11
13
  ##
12
- #
14
+ # Set current SDL error string.
13
15
  api :SDL_SetError, [:string, :varargs], :int
14
16
 
15
17
  end
@@ -0,0 +1,229 @@
1
+ module SDL2
2
+ ##
3
+ # # Object oriented representation of SDL_Event union
4
+ class Event < Union
5
+ end
6
+ ##
7
+ # ## Event Enumerations/Constants:
8
+ # - TYPE
9
+ # - ACTION
10
+ # - STATE
11
+ class Event
12
+ autoload(:TYPE, 'sdl2/event/type')
13
+ autoload(:ACTION, 'sdl2/event/action')
14
+ autoload(:STATE, 'sdl2/event/state')
15
+ end
16
+ enum :event_type, Event::TYPE.flatten_consts
17
+ enum :eventaction, Event::ACTION.flatten_consts
18
+
19
+ ##
20
+ # Sub-Structures
21
+ class Event
22
+ autoload(:Abstract, 'sdl2/event/abstract')
23
+ autoload(:Common, 'sdl2/event/common')
24
+ autoload(:Window, 'sdl2/event/window')
25
+ autoload(:Keyboard, 'sdl2/event/keyboard')
26
+ autoload(:TextEditing,'sdl2/event/text_editing')
27
+ autoload(:TextInput,'sdl2/event/text_input')
28
+ autoload(:MouseMotion,'sdl2/event/mouse_motion')
29
+ autoload(:MouseButton,'sdl2/event/mouse_button')
30
+ autoload(:MouseWheel,'sdl2/event/mouse_wheel')
31
+ autoload(:JoyAxis, 'sdl2/event/joy_axis')
32
+ autoload(:JoyBall, 'sdl2/event/joy_ball')
33
+ autoload(:JoyHat, 'sdl2/event/joy_hat')
34
+ autoload(:JoyButton, 'sdl2/event/joy_button')
35
+ autoload(:JoyDevice, 'sdl2/event/joy_device')
36
+ autoload(:ControllerAxis, 'sdl2/event/controller_axis')
37
+ autoload(:ControllerButton, 'sdl2/event/controller_button')
38
+ autoload(:ControllerDevice, 'sdl2/event/controller_device')
39
+ autoload(:TouchFinger, 'sdl2/event/touch_finger')
40
+ autoload(:MultiGesture, 'sdl2/event/multi_gesture')
41
+ autoload(:DollarGesture, 'sdl2/event/dollar_gesture')
42
+ autoload(:Drop,'sdl2/event/drop')
43
+ autoload(:Quit,'sdl2/event/quit')
44
+ autoload(:OS, 'sdl2/event/os')
45
+ autoload(:User, 'sdl2/event/user')
46
+ autoload(:SysWM, 'sdl2/event/sys_wm')
47
+ end
48
+ ##
49
+ # Union Layout
50
+ class Event
51
+ layout :type, :event_type,
52
+ :common, Common,
53
+ :window, Window,
54
+ :key, Keyboard,
55
+ :edit, TextEditing,
56
+ :text, TextInput,
57
+ :motion, MouseMotion,
58
+ :button, MouseButton,
59
+ :wheel, MouseWheel,
60
+ :jaxis, JoyAxis,
61
+ :jball, JoyBall,
62
+ :jbutton, JoyButton,
63
+ :jdevice, JoyDevice,
64
+ :caxis, ControllerAxis,
65
+ :cbutton, ControllerButton,
66
+ :cdevice, ControllerDevice,
67
+ :quit, Quit,
68
+ :user, User,
69
+ :syswm, SysWM,
70
+ :tfinger, TouchFinger,
71
+ :mgesture, MultiGesture,
72
+ :drop, Drop,
73
+ :padding, [:uint8, 56] # From SDL_events.h:529
74
+
75
+ member_readers *members
76
+ member_writers *members
77
+
78
+ ##
79
+ # Set the state of processing event types:
80
+ def self.state(type,state=:QUERY)
81
+ SDL2.event_state(type,state)
82
+ end
83
+ ##
84
+ # Polls for currently pending events.
85
+ # @returns SDL2::Event or nil if there are no events.
86
+ def self.poll()
87
+ tmp_event = SDL2::Event.new
88
+ unless SDL2.poll_event?(tmp_event)
89
+ tmp_event.pointer.free
90
+ tmp_event = nil
91
+ end
92
+ return tmp_event # May be nil if SDL2.poll_event fails.
93
+ end
94
+ ##
95
+ # Use this function to filter events on current event Que
96
+ # removing any events for which this filter returns 0 (Zero)
97
+ # If called without arguments, it will check and return any
98
+ # currently defined global filters and associated user data
99
+ # as: [filter, data]
100
+ def self.filter(user_data = nil, &event_filter)
101
+ if event_filter
102
+ SDL2.filter_events(event_filter, user_data)
103
+ else
104
+ event_filter = SDL2::TypedPointer::EventFilter.new
105
+ user_data = SDL2::TypedPointer::Pointer.new
106
+ if SDL2.get_event_filter?(event_filter, user_data)
107
+ [event_filter.value, user_data.value]
108
+ else
109
+ false
110
+ end
111
+ end
112
+ end
113
+ ##
114
+ # This routine sets the global event filter, it expects
115
+ # either a single lambda parameter or an array with
116
+ # or paired with a user_data_pointer
117
+ def self.filter=(filter_lambda, user_data_pointer = nil)
118
+ SDL2.set_event_filter(filter_lambda, user_data_pointer)
119
+ end
120
+ ##
121
+ # Check if this event type, or a range of event types exist
122
+ def self.has?(type, type_end = nil)
123
+ unless type_end
124
+ SDL2.has_event?(type)
125
+ else
126
+ SDL2.has_events?(type, type_end)
127
+ end
128
+ end
129
+ ##
130
+ # Flush this kind of event (or this range of event types) from the que
131
+ def self.flush(type, type_end = nil)
132
+ unless type_end
133
+ SDL2.flush_event(type)
134
+ else
135
+ SDL2.flush_events(type, type_end)
136
+ end
137
+ end
138
+ ##
139
+ # Add an event to the que
140
+ def self.push(event)
141
+ event = Event.cast(event) unless event.kind_of? Event
142
+ SDL2.push_event!(event)
143
+ end
144
+ ##
145
+ # General Utility for Peek/Get/Del/Add many events
146
+ def self.peep(events = nil, num_events = nil, action = :PEEK, first_event = :FIRSTEVENT, last_event = :LASTEVENT)
147
+ if events.is_a?(Array)
148
+ events = SDL2::StructArray.clone_from(events, SDL2::Event)
149
+ end
150
+ num_events ||= events.try(:count)
151
+ raise 'num_events must be specified unless events responds to count' if num_events.nil?
152
+ events ||= SDL2::StructArray.new(SDL2::Event, num_events) unless num_events == 0
153
+ returned = SDL2.peep_events!(events.nil? ? nil : events.first, num_events, action, first_event, last_event)
154
+ events.nil? ? Array.new(returned, nil) : events.first(returned)
155
+ end
156
+ ##
157
+ # Peek at events, default maximum to return at once is 10
158
+ def self.peek(count = 10, f = :FIRSTEVENT, l = :LASTEVENT)
159
+ self.peep(nil, count, :PEEK, f, l)
160
+ end
161
+ ##
162
+ # Add a bunch of events, expects them as arguments so
163
+ # make sure you add that astrik/star: `SDL2::Event.add(*array)`
164
+ def self.add(*events)
165
+ self.peep(events, nil, :ADD, f, l)
166
+ end
167
+ ##
168
+ # Get a bunch of events, defaults to 10 at once
169
+ def self.get(count = 10, f = :FIRSTEVENT, l = :LASTEVENT)
170
+ self.peep(nil, count, :GET, f, l)
171
+ end
172
+ ##
173
+ # Pump the events.
174
+ def self.pump
175
+ SDL2.pump_events()
176
+ end
177
+ ##
178
+ # Indicates if a Quit event is waiting in the que.
179
+ def self.quit_requested?
180
+ self.pump()
181
+ # Peek into the event que and return the count of quit events.
182
+ # Return true if that is greater than zero.
183
+ self.peep(nil,0,:PEEK,:QUIT,:QUIT).count > 0
184
+ end
185
+ ##
186
+ # Coerce some value into an Event
187
+ def self.cast(something)
188
+ SDL2::Debug.log(self){"Casting Something: #{something.inspect}"}
189
+ if something.kind_of? Abstract
190
+ return something.to_event
191
+ elsif something.kind_of? Hash
192
+ raise "Must have type : #{something.inspect}" unless something.has_key? :type
193
+ tmp = self.new
194
+ fields = members & something.keys
195
+ SDL2::Debug.log(self){"Using fields: #{fields.inspect}"}
196
+ fields.each do |field|
197
+ if tmp[field].kind_of? Struct and something[field].kind_of? Hash
198
+ tmp[field].update_members(something[field])
199
+ else
200
+ tmp[field] = something[field]
201
+ end
202
+ end
203
+ return tmp
204
+ else
205
+ raise "Is not an Abstract!: #{something.inspect}"
206
+ end
207
+ end
208
+
209
+ def ==(other)
210
+ if other.kind_of?(Hash)
211
+ # False if there are fields that do not exist
212
+ return false unless (other.keys - members).empty?
213
+
214
+ (other.keys & members).each do |field|
215
+ return false unless self[field] == other[field]
216
+ end
217
+
218
+ return true #if we get this far
219
+
220
+ else
221
+
222
+ return super(other)
223
+
224
+ end
225
+ end
226
+
227
+ end
228
+
229
+ end
@@ -0,0 +1,11 @@
1
+ module SDL2
2
+ class Event
3
+ class Abstract < Struct
4
+ SHARED = [:type, :event_type, :timestamp, :uint32]
5
+
6
+ def to_event
7
+ SDL2::Event.new(self.pointer)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ module SDL2
2
+ class Event
3
+ ##
4
+ #
5
+ # Constants for use with SDL2::peep_events() as eventaction
6
+ module ACTION
7
+ include EnumerableConstants
8
+ ##
9
+ # Causes the creation of messages
10
+ ADD = next_const_value
11
+ ##
12
+ # Retrieves events without removing them
13
+ PEEK = next_const_value
14
+ ##
15
+ # Retrieves events and removes them
16
+ GET = next_const_value
17
+ end
18
+ end
19
+ end