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
@@ -7,23 +7,6 @@ require 'sdl2/display/mode'
7
7
 
8
8
  module SDL2
9
9
 
10
- # Window Flags Constants
11
- module WINDOW
12
- include EnumerableConstants
13
- FULLSCREEN = 0x00000001
14
- OPENGL = 0x00000002
15
- SHOWN = 0x00000004
16
- HIDDEN = 0x00000008
17
- BORDERLESS = 0x00000010
18
- RESIZABLE = 0x00000020
19
- MINIMIZED = 0x00000040
20
- MAXIMIZED = 0x00000080
21
- INPUT_GRABBED = 0x00000100
22
- INPUT_FOCUS = 0x00000200
23
- MOUSE_FOCUS = 0x00000400
24
- FULLSCREEN_DESKTOP = ( FULLSCREEN | 0x00001000 )
25
- FOREIGN = 0x00000800
26
- end
27
10
 
28
11
  # Used to indicate that you don't care what the window position is.
29
12
  module WINDOWPOS
@@ -62,28 +45,45 @@ module SDL2
62
45
  # Event subtype for window events
63
46
  module WINDOWEVENT
64
47
  include EnumerableConstants
65
- NONE
66
- SHOWN
67
- HIDDEN
68
- EXPOSED
69
- MOVED
70
- RESIZED
71
- SIZE_CHANGED
72
- MINIMIZED
73
- MAXIMIZED
74
- RESTORED
75
- ENTER
76
- LEAVE
77
- FOCUS_GAINED
78
- FOCUS_LOST
79
- CLOSE
48
+ NONE = next_const_value
49
+ SHOWN = next_const_value
50
+ HIDDEN = next_const_value
51
+ EXPOSED = next_const_value
52
+ MOVED = next_const_value
53
+ RESIZED = next_const_value
54
+ SIZE_CHANGED = next_const_value
55
+ MINIMIZED = next_const_value
56
+ MAXIMIZED = next_const_value
57
+ RESTORED = next_const_value
58
+ ENTER = next_const_value
59
+ LEAVE = next_const_value
60
+ FOCUS_GAINED = next_const_value
61
+ FOCUS_LOST = next_const_value
62
+ CLOSE = next_const_value
80
63
  end
81
64
 
82
65
  # System Window
83
66
  # A rectangular area you can blit into.
84
67
  class Window < Struct
85
- include WINDOW
86
-
68
+ ##
69
+ # Window Flags Constants
70
+ module FLAGS
71
+ include EnumerableConstants
72
+ FULLSCREEN = 0x00000001
73
+ OPENGL = 0x00000002
74
+ SHOWN = 0x00000004
75
+ HIDDEN = 0x00000008
76
+ BORDERLESS = 0x00000010
77
+ RESIZABLE = 0x00000020
78
+ MINIMIZED = 0x00000040
79
+ MAXIMIZED = 0x00000080
80
+ INPUT_GRABBED = 0x00000100
81
+ INPUT_FOCUS = 0x00000200
82
+ MOUSE_FOCUS = 0x00000400
83
+ FULLSCREEN_DESKTOP = ( FULLSCREEN | 0x00001000 )
84
+ FOREIGN = 0x00000800
85
+ end
86
+
87
87
  layout :magic, :pointer,
88
88
  :id, :uint32,
89
89
  :title, :string,
@@ -109,68 +109,86 @@ module SDL2
109
109
  :driverdata, :pointer,
110
110
  :prev, :pointer,
111
111
  :next, :pointer
112
-
112
+ ##
113
113
  # A simple wrapper for data objects associated with a window.
114
114
  class Data
115
-
115
+ ##
116
116
  # Create a data object manager for a given window
117
117
  def initialize(for_window)
118
118
  @for_window = for_window
119
119
  end
120
-
120
+ ##
121
121
  # Return the data named
122
122
  def named(name)
123
123
  SDL2.get_window_data(@for_window, name.to_s)
124
124
  end
125
-
125
+ ##
126
+ #
126
127
  alias_method :[], :named
127
-
128
+ ##
128
129
  # Set the data named to value specified.
129
130
  def named=(name, value)
130
131
  SDL2.set_window_data(@for_window, name.to_s, value.to_s)
131
132
  end
132
-
133
+ ##
134
+ #
133
135
  alias_method :[]=, :named=
134
136
  end
135
-
137
+ ##
136
138
  # The Window's data manager.
137
- attr_reader :data
138
-
139
+ def data
140
+ @data ||= Data.new(self)
141
+ end
142
+ ##
139
143
  # Construct a new window.
140
144
  def initialize(*args, &block)
141
145
  super(*args, &block)
142
- @data = Data.new(self)
143
146
  end
144
-
147
+ ##
148
+ # These are the defaults a Window is created with unless overridden
149
+ DEFAULT = {
150
+ title: "SDL2::Window",
151
+ x: :CENTERED,
152
+ y: :CENTERED,
153
+ width: 320,
154
+ height: 240,
155
+ flags: 0
156
+ }
157
+ ##
145
158
  # Construct a new window with given:
146
- # @param title: The caption to use for the window
147
- # @param x: The x-position of the window
148
- # @param y: The y-position of the window
149
- # @param w: The width of the window
150
- # @param h: The height of the window
151
- # @param flags: Window Flags to use in construction
152
- def self.create(title ='', x = 0, y = 0, w = 100, h = 100, flags = 0)
153
- SDL2.create_window!(title, x, y, w, h, flags)
154
- end
155
-
159
+ # * title: The caption to use for the window
160
+ # * x: The x-position of the window
161
+ # * y: The y-position of the window
162
+ # * w: The width of the window
163
+ # * h: The height of the window
164
+ # * flags: Window Flags to use in construction
165
+ def self.create(options = {})
166
+ o = DEFAULT.merge(options)
167
+ Debug.log(self){"Creating with options: #{o.inspect}"}
168
+ # TODO: Log unused option keys
169
+ SDL2.create_window!(o[:title], o[:x], o[:y], o[:width], o[:height], o[:flags])
170
+ end
171
+ ##
156
172
  # Constructs a new window from arbitrary system-specific structure
157
- # @param data: Some system-specific pointer
173
+ # * data: Some system-specific pointer
158
174
  # See SDL Documentation
159
175
  def self.create_from(data)
176
+ Debug.log(self){"Creating from data: #{data.inspect}"}
160
177
  create_window_from!(data)
161
178
  end
162
-
179
+ ##
163
180
  # Returns the identified window already created
164
- # @param id: The window identifier to retrieve
181
+ # * id: The window identifier to retrieve
165
182
  def self.from_id(id)
166
183
  get_window_from_id!(id)
167
184
  end
168
-
185
+ ##
169
186
  # Constructs both a window and a renderer
170
- # @param w: The Width of the pair to create
171
- # @param h: The Height of the pair to create
172
- # @param flags: Window flags to utilize in creation
187
+ # * w: The Width of the pair to create
188
+ # * h: The Height of the pair to create
189
+ # * flags: Window flags to utilize in creation
173
190
  def self.create_with_renderer(w, h, flags)
191
+
174
192
  window = Window.new
175
193
  renderer = Renderer.new
176
194
  if SDL2.create_window_and_renderer(w,h,flags,window,renderer) == 0
@@ -179,18 +197,24 @@ module SDL2
179
197
  nil
180
198
  end
181
199
  end
182
-
200
+ ##
183
201
  # Release memory utilized by structure
184
202
  def self.release(pointer)
185
- destroy_window(self.new pointer)
203
+ fc = caller.first
204
+ Debug.log(self){"Release ignored from #{fc}"}
186
205
  end
187
-
206
+ ##
207
+ # Tell SDL we are done with the window. Any further use could result in a crash.
188
208
  def destroy
189
- destroy_window(self)
209
+ unless self.null?
210
+ SDL2.destroy_window(self)
211
+ else
212
+ Debug.log(self){"Destruction of window null window requested."}
213
+ end
190
214
  end
191
215
 
192
- alias_method :destroy, :free
193
216
 
217
+ ##
194
218
  # Return the brightness level
195
219
  def brightness
196
220
  SDL2.get_window_brightness(self)
@@ -198,18 +222,15 @@ module SDL2
198
222
 
199
223
  # Set the brightness level
200
224
  def brightness=(level)
225
+ Debug.log(self){"Setting brightness to: #{level}"}
201
226
  SDL2.set_window_brightness(self, level.to_f)
202
227
  end
203
228
 
204
229
  # Get a copy of the DisplayMode structure
205
230
  def display_mode
206
231
  dm = SDL2::Display::Mode.new
207
- if SDL2.get_window_display_mode(self, dm) == 0
208
- return dm
209
- else
210
- dm.pointer.free
211
- return nil
212
- end
232
+ SDL2.get_window_display_mode!(self, dm)
233
+ dm
213
234
  end
214
235
 
215
236
  # Get the display index associated with this window
@@ -300,14 +321,22 @@ module SDL2
300
321
  # Get the window's current size
301
322
  # @return Array => [<width>, <height>]
302
323
  def current_size()
303
- w_struct, h_struct = IntStruct.new, IntStruct.new
304
- SDL2::get_window_size(self, w_struct, h_struct)
305
- w, h = w_struct[:value], h_struct[:value]
306
- [w, h]
324
+ size = 2.times.map{TypedPointer::Int.new}
325
+ SDL2::get_window_size(self, *size)
326
+ size.map(&:value)
327
+ end
328
+
329
+ def width
330
+ current_size[0]
331
+ end
332
+
333
+ def height
334
+ current_size[1]
307
335
  end
308
336
 
337
+
309
338
  # Set the window's current size
310
- # @param size: A array containing the [width,height]
339
+ # * size: A array containing the [width,height]
311
340
  def current_size=(size)
312
341
  SDL2.set_window_size(self, size[0], size[1])
313
342
  end
@@ -315,14 +344,13 @@ module SDL2
315
344
  # Get the window's maximum_size
316
345
  # @return Array => [<width>, <height>]
317
346
  def maximum_size
318
- w_struct, h_struct = IntStruct.new, IntStruct.new
319
- SDL2::get_window_maximum_size(self, w_struct, h_struct)
320
- w, h = w_struct[:value], h_struct[:value]
321
- [w, h]
347
+ size = 2.times.map{TypedPointer::Int.new}
348
+ SDL2::get_window_maximum_size(self, *size)
349
+ size.map(&:value)
322
350
  end
323
351
 
324
352
  # Set the window's maximum size
325
- # @param size: A array containing the [width,height]
353
+ # * size: A array containing the [width,height]
326
354
  def maximum_size=(size)
327
355
  SDL2.set_window_maximum_size(self, size[0], size[1])
328
356
  end
@@ -330,14 +358,13 @@ module SDL2
330
358
  # Get the window's minimum size
331
359
  # @return Array => [<width>, <height>]
332
360
  def minimum_size
333
- w_struct, h_struct = IntStruct.new, IntStruct.new
334
- SDL2::get_window_minimum_size(self, w_struct, h_struct)
335
- w, h = w_struct[:value], h_struct[:value]
336
- [w, h]
361
+ size = 2.times.map{TypedPointer::Int.new}
362
+ SDL2::get_window_minimum_size(self, *size)
363
+ size.map(&:value)
337
364
  end
338
365
 
339
366
  # Set the window's minimum size
340
- # @param size: A array containing the [width,height]
367
+ # * size: A array containing the [width,height]
341
368
  def minimum_size=(size)
342
369
  SDL2.set_window_minimum_size(self, size[0], size[1])
343
370
  end
@@ -345,7 +372,7 @@ module SDL2
345
372
  # Get the window's position
346
373
  # @return Array => [<x>, <y>]
347
374
  def position
348
- position = [IntStruct.new, IntStruct.new]
375
+ position = [TypedPointer::Int.new, TypedPointer::Int.new]
349
376
  SDL2::get_window_position(self, position[0], position[1])
350
377
  x, y = position[0][:value], position[1][:value]
351
378
  position.each(&:free)
@@ -353,7 +380,7 @@ module SDL2
353
380
  end
354
381
 
355
382
  # Set the window's position
356
- # @param size: A array containing the [x,y]
383
+ # * size: A array containing the [x,y]
357
384
  def position=(location)
358
385
  SDL2::set_window_position(self, location[0],location[1])
359
386
  end
@@ -367,6 +394,29 @@ module SDL2
367
394
  def fullscreen=(flags)
368
395
  SDL2.set_window_fullscreen(self, flags)
369
396
  end
397
+ ##
398
+
399
+ ##
400
+ # Returns the renderer associated with this window
401
+ def renderer
402
+ SDL2.get_renderer(self)
403
+ end
404
+ # Utility function that returns an SDL2::Surface of a given render.
405
+ # Defaults to the renderer returned by SDL_GetRenderer(window=self)
406
+ # Added by BadQuanta originally for approval testing.
407
+ def renderer_to_surface(renderer = renderer)
408
+ w, h = renderer.output_size
409
+ fmt = surface.format
410
+ surface = SDL2::Surface.create_rgb(0,w,h,
411
+ fmt.bits_per_pixel,
412
+ fmt.r_mask,
413
+ fmt.g_mask,
414
+ fmt.b_mask,
415
+ fmt.a_mask
416
+ )
417
+ SDL2.render_read_pixels!(renderer, nil, fmt.format, surface.pixels, surface.pitch)
418
+ surface
419
+ end
370
420
  end
371
421
 
372
422
  end
@@ -0,0 +1,42 @@
1
+ module SDL2
2
+ class StructArray
3
+
4
+ def self.clone_from(array, struct_class)
5
+ cloned_array = self.new(struct_class, array.count)
6
+ array.each_with_index do |item, idx|
7
+ cloned_array[idx].update_members(item)
8
+ end
9
+ cloned_array
10
+ end
11
+
12
+
13
+ def initialize(struct_class, count)
14
+ @count = count
15
+ @struct_class = struct_class
16
+ # TODO: Make sure this will free the memory when this
17
+ # struct array is garbage collected.
18
+ @pointer = FFI::MemoryPointer.new(struct_class, @count)
19
+ end
20
+
21
+ attr_reader :pointer, :count
22
+
23
+ def [](idx)
24
+ raise "Invalid index #{idx}, count is #{@count}" unless (0 <= idx) and (idx < @count)
25
+ @struct_class.new(pointer + (idx*@struct_class.size))
26
+ end
27
+
28
+ def first(count = nil)
29
+ if count.nil?
30
+ self[0]
31
+ else
32
+ count.times.map do |idx|
33
+ self[idx]
34
+ end
35
+ end
36
+ end
37
+
38
+ def last
39
+ self[@count-1]
40
+ end
41
+ end
42
+ end
@@ -20,15 +20,15 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency 'ffi'
22
22
  spec.add_dependency 'activesupport'
23
-
23
+
24
+ spec.add_development_dependency 'approvals'
24
25
  spec.add_development_dependency 'bundler', '~> 1.3'
25
- spec.add_development_dependency 'minitest', '~> 5.0.7' # TODO: Remove Minitest?
26
26
  spec.add_development_dependency 'rspec', '~> 2.0'
27
27
  spec.add_development_dependency 'rake'
28
28
  spec.add_development_dependency 'pry'
29
29
  spec.add_development_dependency 'guard'
30
- #spec.add_development_dependency 'guard-minitest'
31
30
  spec.add_development_dependency 'guard-rspec'
31
+ spec.add_development_dependency 'ruby-prof'
32
32
  spec.add_development_dependency 'yard'
33
33
 
34
34