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
@@ -1,4 +1,4 @@
1
1
 
2
2
  module SDL2
3
- GEM_VERSION = "0.0.3"
3
+ GEM_VERSION = "0.0.4"
4
4
  end
@@ -1,45 +1,176 @@
1
1
  require 'sdl2'
2
2
  require 'sdl2/joystick'
3
- require 'yinum'
4
3
 
5
4
  module SDL2
6
5
 
7
6
  class Haptic < Struct
8
- Features = Enum.new(:HapticFeatures, {
9
- CONSTANT: (1<<0),
10
- SINE: (1<<1),
11
- LEFTRIGHT: (1<<2),
12
- TRIANGLE: (1<<3),
13
- SAWTOOTHUP: (1<<4),
14
- SAWTOOTHDOWN: (1<<5),
15
- RAMP: (1<<6),
16
- SPRING: (1<<7),
17
- DAMPER: (1<<8),
18
- INERTIA: (1<<9),
19
- FRICTION: (1<<10),
20
- CUSTOM: (1<<11),
21
- GAIN: (1<<12),
22
- AUTOCENTER: (1<<13),
23
- STATUS: (1<<14),
24
- PAUSE: (1<<15)
25
- })
26
-
27
- DirectionType = Enum.new(:HapticDirectionType, {
28
- POLAR: 0,
29
- CARTESIAN: 1,
30
- SPHERICAL: 2
31
- })
32
-
33
7
 
34
- RunEffect = Enum.new(:HapticRunEffect, {
35
- INFINITY: 4294967295
36
- })
8
+ # Different haptic features a device can have
9
+ module FEATURES
10
+ include EnumerableConstants
11
+ # Constant haptic effect
12
+ CONSTANT= (1<<0)
13
+ # Periodic haptic effect that simulates sine waves.
14
+ SINE= (1<<1)
15
+ # Haptic effect for direct control over high/low frequency motors.
16
+ LEFTRIGHT= (1<<2)
17
+ # Peridodic haptic effect that simulates triangular waves.
18
+ TRIANGLE= (1<<3)
19
+ # Periodic haptic effect that simulates saw tooth up waves.
20
+ SAWTOOTHUP= (1<<4)
21
+ # Periodic haptic effect that simulates saw tooth down waves.
22
+ SAWTOOTHDOWN= (1<<5)
23
+ # Ramp haptic effect
24
+ RAMP= (1<<6)
25
+ # Condition haptic effect that simulates a spring. Effect is based on axes
26
+ # position.
27
+ SPRING= (1<<7)
28
+ # Condition haptic effect that simulates dampening. Effect is based on the
29
+ # axes velocity.
30
+ DAMPER= (1<<8)
31
+ # Condition haptic effect that simulates inertia. Effect is based on the
32
+ # axes acceleration.
33
+ INERTIA= (1<<9)
34
+ # Condition haptic effect that simulates friction. Effect is based on the
35
+ # axes movement.
36
+ FRICTION= (1<<10)
37
+ # Custom effect is supported.
38
+ CUSTOM= (1<<11)
39
+ # Device supports setting the global gain.
40
+ GAIN= (1<<12)
41
+ # Device supports setting autocenter
42
+ AUTOCENTER= (1<<13)
43
+ # Device can be queried for effect status
44
+ STATUS= (1<<14)
45
+ # Device can be paused
46
+ PAUSE= (1<<15)
47
+ end
48
+
49
+ # Direction encodings
50
+ module DIRECTION
51
+ include EnumerableConstants
52
+ # Uses polar coordinates for the direction.
53
+ POLAR= 0
54
+ # Uses cartesian coordinates for the direction.
55
+ CARTESIAN= 1
56
+ # Uses spherical coordinates for the direction.
57
+ SPHERICAL= 2
58
+ end
59
+
60
+ # So that they can be accessed by: SDL2::Haptic::XXXXX, emulating
61
+ # SDL_HAPTIC_XXXX
62
+ include FEATURES
63
+ include DIRECTION
64
+
65
+ # Used to play a device an infinite number of times.
66
+ # TODO: Review if this works.
67
+ INFINITY = 4294967295
37
68
 
69
+ # Structure that represents a haptic direction.
70
+ # The following was copied directly from SDL_haptic.h, v2.0.0 and
71
+ # translated by BadQuanta into Ruby code using sdl2_ffi.
72
+ # Directions can be specified by:
73
+ # - SDL2::Haptic::POLAR : Specified by polar coordinates.
74
+ # - SDL2::Haptic::CARTESIAN : Specified by cartesian coordinates.
75
+ # - SDL2::Haptic::SPHERICAL : Specified by spherical coordinates.
76
+ #
77
+ # Cardinal directions of the haptic device are relative to the positioning
78
+ # of the device. North is considered to be away from the user.
79
+ #
80
+ # The following diagram represents the cardinal directions:
81
+ # \verbatim
82
+ # .--.
83
+ # |__| .-------.
84
+ # |=.| |.-----.|
85
+ # |--| || ||
86
+ # | | |'-----'|
87
+ # |__|~')_____('
88
+ # [ COMPUTER ]
89
+ #
90
+ #
91
+ # North (0,-1)
92
+ # ^
93
+ # |
94
+ # |
95
+ # (1,0) West <----[ HAPTIC ]----> East (-1,0)
96
+ # |
97
+ # |
98
+ # v
99
+ # South (0,1)
100
+ #
101
+ #
102
+ # [ USER ]
103
+ # \|||/
104
+ # (o o)
105
+ # ---ooO-(_)-Ooo---
106
+ # \endverbatim
107
+ #
108
+ # If type is SDL::Haptic::POLAR, direction is encoded by hundredths of a
109
+ # degree starting north and turning clockwise. SDL::Haptic::POLAR only uses
110
+ # the first \c dir parameter. The cardinal directions would be:
111
+ # - North: 0 (0 degrees)
112
+ # - East: 9000 (90 degrees)
113
+ # - South: 18000 (180 degrees)
114
+ # - West: 27000 (270 degrees)
115
+ #
116
+ # If type is SDL::Haptic::CARTESIAN, direction is encoded by three positions
117
+ # (X axis, Y axis and Z axis (with 3 axes)). SDL::Haptic::CARTESIAN uses
118
+ # the first three \c dir parameters. The cardinal directions would be:
119
+ # - North: 0,-1, 0
120
+ # - East: -1, 0, 0
121
+ # - South: 0, 1, 0
122
+ # - West: 1, 0, 0
123
+ #
124
+ # The Z axis represents the height of the effect if supported, otherwise
125
+ # it's unused. In cartesian encoding (1, 2) would be the same as (2, 4), you
126
+ # can use any multiple you want, only the direction matters.
127
+ #
128
+ # If type is SDL::Haptic::SPHERICAL, direction is encoded by two rotations.
129
+ # The first two \c dir parameters are used. The \c dir parameters are as
130
+ # follows (all values are in hundredths of degrees):
131
+ # - Degrees from (1, 0) rotated towards (0, 1).
132
+ # - Degrees towards (0, 0, 1) (device needs at least 3 axes).
133
+ #
134
+ #
135
+ # Example of force coming from the south with all encodings (force coming
136
+ # from the south means the user will have to pull the stick to counteract):
137
+ #
138
+ # @code
139
+ #
140
+ # direction = SDL2::Haptic::Direction.new
141
+ #
142
+ # # Cartesian directions
143
+ # direction.type = SDL2::Haptic::CARTESIAN # Using cartesian direction encoding.
144
+ # direction.dir[0] = 0 # X position
145
+ # direction.dir[1] = 1 # Y position
146
+ #
147
+ # # Polar directions
148
+ # direction.type = SDL2::Haptic::POLAR #We'll be using polar direction encoding.
149
+ # direction.dir[0] = 18000 # Polar only uses first parameter
150
+ #
151
+ # # Spherical coordinates
152
+ # direction.type = SDL2::Haptic::SPHERICAL # Spherical encoding
153
+ # direction.dir[0] = 9000 # Since we only have two axes we don't need more parameters
154
+ #
155
+ # @code
156
+ #
157
+ #
158
+ # \sa SDL_HAPTIC_POLAR
159
+ # \sa SDL_HAPTIC_CARTESIAN
160
+ # \sa SDL_HAPTIC_SPHERICAL
161
+ # \sa SDL_HapticEffect
162
+ # \sa SDL_HapticNumAxes
163
+ # END OF SOMETHING COPIED AND PASTED FROM SDL_haptic.h !!!!!!!!!!!!!!!!!
38
164
  class Direction < Struct
39
165
  layout :type, :uint8,
40
166
  :dir, [:int32, 3] # Magic #3 form line 442
167
+
168
+ member_readers *members # Read all
169
+ member_writers *members # Write all
41
170
  end
42
171
 
172
+ # Identical starting elements shared between structures.
173
+ # You can ignore this constant.
43
174
  COMMON_LAYOUT = [:type, :uint16,
44
175
  :direction, Direction,
45
176
 
@@ -50,30 +181,110 @@ module SDL2
50
181
  :interval, :uint16
51
182
  ]
52
183
 
184
+ # Identical ending elements shared between structures.
185
+ # You can ignore this constant.
53
186
  COMMON_ENVELOPE = [
54
187
  :attack_length, :uint16,
55
188
  :attack_level, :uint16,
56
189
  :fade_length, :uint16,
57
190
  :fade_level, :uint16
58
191
  ]
59
-
192
+ # The struct is exclusive to the CONSTANT effect.
193
+ #
194
+ # A constant effect applies a constant force in the specified direction
195
+ # to the joystick.
60
196
  class Constant < Struct
61
- layout *COMMON_EVENT_LAYOUT+[
197
+ layout *COMMON_LAYOUT+[
62
198
  :level, :int16
63
199
  ] + COMMON_ENVELOPE
64
200
  end
65
201
 
202
+ # A structure containing a template for a Periodic effect.
203
+ #
204
+ # The struct handles the following effects:
205
+ # - SDL2::Haptic::SINE
206
+ # - SDL2::Haptic::LEFTRIGHT
207
+ # - SDL2::Haptic::TRIANGLE
208
+ # - SDL2::Haptic::SAWTOOTHUP
209
+ # - SDL2::Haptic::SAWTOOTHDOWN
210
+ #
211
+ # A periodic effect consists in a wave-shaped effect that repeats itself
212
+ # over time. The type determines the shape of the wave and the parameters
213
+ # determine the dimensions of the wave.
214
+ #
215
+ # Phase is given by hundredth of a cycle meaning that giving the phase a value
216
+ # of 9000 will displace it 25% of its period. Here are sample values:
217
+ # - 0: No phase displacement.
218
+ # - 9000: Displaced 25% of its period.
219
+ # - 18000: Displaced 50% of its period.
220
+ # - 27000: Displaced 75% of its period.
221
+ # - 36000: Displaced 100% of its period, same as 0, but 0 is preferred.
222
+ #
223
+ # Examples:
224
+ #
225
+ # SDL_HAPTIC_SINE
226
+ # __ __ __ __
227
+ # / \ / \ / \ /
228
+ # / \__/ \__/ \__/
229
+ #
230
+ # SDL_HAPTIC_SQUARE
231
+ # __ __ __ __ __
232
+ # | | | | | | | | | |
233
+ # | |__| |__| |__| |__| |
234
+ #
235
+ # SDL_HAPTIC_TRIANGLE
236
+ # /\ /\ /\ /\ /\
237
+ # / \ / \ / \ / \ /
238
+ # / \/ \/ \/ \/
239
+ #
240
+ # SDL_HAPTIC_SAWTOOTHUP
241
+ # /| /| /| /| /| /| /|
242
+ # / | / | / | / | / | / | / |
243
+ # / |/ |/ |/ |/ |/ |/ |
244
+ #
245
+ # SDL_HAPTIC_SAWTOOTHDOWN
246
+ # \ |\ |\ |\ |\ |\ |\ |
247
+ # \ | \ | \ | \ | \ | \ | \ |
248
+ # \| \| \| \| \| \| \|
249
+ #
250
+ # @sa SDL_HAPTIC_SINE
251
+ # @sa SDL_HAPTIC_LEFTRIGHT
252
+ # @sa SDL_HAPTIC_TRIANGLE
253
+ # @sa SDL_HAPTIC_SAWTOOTHUP
254
+ # @sa SDL_HAPTIC_SAWTOOTHDOWN
255
+ # @sa SDL_HapticEffect
66
256
  class Periodic < Struct
67
- layout *COMMON_EVENT_LAYOUT+[
257
+ layout *COMMON_LAYOUT+[
68
258
  :period, :uint16,
69
259
  :magnitude, :int16,
70
260
  :offset, :int16,
71
261
  :phase, :uint16,
72
262
  ]+COMMON_ENVELOPE
73
263
  end
74
-
264
+ # \brief A structure containing a template for a Condition effect.
265
+ #
266
+ # The struct handles the following effects:
267
+ # - SDL2::Haptic::SPRING: Effect based on axes position.
268
+ # - SDL2::Haptic::DAMPER: Effect based on axes velocity.
269
+ # - SDL2::Haptic::INERTIA: Effect based on axes acceleration.
270
+ # - SDL2::Haptic::FRICTION: Effect based on axes movement.
271
+ #
272
+ # Direction is handled by condition internals instead of a direction member.
273
+ # The condition effect specific members have three parameters. The first
274
+ # refers to the X axis, the second refers to the Y axis and the third
275
+ # refers to the Z axis. The right terms refer to the positive side of the
276
+ # axis and the left terms refer to the negative side of the axis. Please
277
+ # refer to the ::SDL2::Haptic::Direction diagram for which side is positive and
278
+ # which is negative.
279
+ #
280
+ # \sa SDL2::Haptic::Direction
281
+ # \sa SDL_HAPTIC_SPRING
282
+ # \sa SDL_HAPTIC_DAMPER
283
+ # \sa SDL_HAPTIC_INERTIA
284
+ # \sa SDL_HAPTIC_FRICTION
285
+ # \sa SDL_HapticEffect
75
286
  class Condition < Struct
76
- layout *COMMON_EVENT_LAYOUT+[
287
+ layout *COMMON_LAYOUT+[
77
288
  :right_sat, [:uint16, 3],
78
289
  :left_sat, [:uint16, 3],
79
290
  :right_coeff, [:int16, 3],
@@ -82,49 +293,149 @@ module SDL2
82
293
  :center, [:int16, 3]
83
294
  ]
84
295
  end
85
-
296
+
297
+ # \brief A structure containing a template for a Ramp effect.
298
+ #
299
+ # This struct is exclusively for the ::SDL_HAPTIC_RAMP effect.
300
+ #
301
+ # The ramp effect starts at start strength and ends at end strength.
302
+ # It augments in linear fashion. If you use attack and fade with a ramp
303
+ # the effects get added to the ramp effect making the effect become
304
+ # quadratic instead of linear.
305
+ #
306
+ # \sa SDL_HAPTIC_RAMP
307
+ # \sa SDL_HapticEffect
86
308
  class Ramp < Struct
87
- layout *COMMON_EVENT_LAYOUT+[
309
+ layout *COMMON_LAYOUT+[
88
310
  :start, :int16,
89
- :'end', :int16,
311
+ :'end', :int16,
90
312
  ]+COMMON_ENVELOPE
91
313
  end
92
-
314
+
315
+ # \brief A structure containing a template for a Left/Right effect.
316
+ #
317
+ # This struct is exclusively for the ::SDL_HAPTIC_LEFTRIGHT effect.
318
+ #
319
+ # The Left/Right effect is used to explicitly control the large and small
320
+ # motors, commonly found in modern game controllers. One motor is high
321
+ # frequency, the other is low frequency.
322
+ #
323
+ # \sa SDL_HAPTIC_LEFTRIGHT
324
+ # \sa SDL_HapticEffect
93
325
  class LeftRight < Struct
94
326
  layout :type, :uint16,
95
- :length, :uint32,
96
- :large_magnitude, :uint16,
97
- :small_magnitude, :uint16
327
+ :length, :uint32,
328
+ :large_magnitude, :uint16,
329
+ :small_magnitude, :uint16
98
330
  end
99
-
331
+ # \brief A structure containing a template for the ::SDL_HAPTIC_CUSTOM effect.
332
+ #
333
+ # A custom force feedback effect is much like a periodic effect, where the
334
+ # application can define its exact shape. You will have to allocate the
335
+ # data yourself. Data should consist of channels # samples Uint16 samples.
336
+ #
337
+ # If channels is one, the effect is rotated using the defined direction.
338
+ # Otherwise it uses the samples in data for the different axes.
339
+ #
340
+ # \sa SDL_HAPTIC_CUSTOM
341
+ # \sa SDL_HapticEffect
100
342
  class Custom < Struct
101
- layout *COMMON_EVENT_LAYOUT+[
343
+ layout *COMMON_LAYOUT+[
102
344
  :channels, :uint8,
103
345
  :period, :uint16,
104
346
  :samples, :uint16,
105
- :data, :pointer,
347
+ :data, :pointer,
106
348
  ]+COMMON_ENVELOPE
107
349
  end
108
-
350
+
351
+ # \brief The generic template for any haptic effect.
352
+ #
353
+ # All values max at 32767 (0x7FFF). Signed values also can be negative.
354
+ # Time values unless specified otherwise are in milliseconds.
355
+ #
356
+ # You can also pass ::SDL_HAPTIC_INFINITY to length instead of a 0-32767
357
+ # value. Neither delay, interval, attack_length nor fade_length support
358
+ # ::SDL_HAPTIC_INFINITY. Fade will also not be used since effect never ends.
359
+ #
360
+ # Additionally, the ::SDL_HAPTIC_RAMP effect does not support a duration of
361
+ # ::SDL_HAPTIC_INFINITY.
362
+ #
363
+ # Button triggers may not be supported on all devices, it is advised to not
364
+ # use them if possible. Buttons start at index 1 instead of index 0 like
365
+ # the joystick.
366
+ #
367
+ # If both attack_length and fade_level are 0, the envelope is not used,
368
+ # otherwise both values are used.
369
+ #
370
+ # Common parts:
371
+ # \code
372
+ # // Replay - All effects have this
373
+ # Uint32 length; // Duration of effect (ms).
374
+ # Uint16 delay; // Delay before starting effect.
375
+ #
376
+ # // Trigger - All effects have this
377
+ # Uint16 button; // Button that triggers effect.
378
+ # Uint16 interval; // How soon before effect can be triggered again.
379
+ #
380
+ # // Envelope - All effects except condition effects have this
381
+ # Uint16 attack_length; // Duration of the attack (ms).
382
+ # Uint16 attack_level; // Level at the start of the attack.
383
+ # Uint16 fade_length; // Duration of the fade out (ms).
384
+ # Uint16 fade_level; // Level at the end of the fade.
385
+ # \endcode
386
+ #
387
+ #
388
+ # Here we have an example of a constant effect evolution in time:
389
+ # \verbatim
390
+ # Strength
391
+ # ^
392
+ # |
393
+ # | effect level --> _________________
394
+ # | / \
395
+ # | / \
396
+ # | / \
397
+ # | / \
398
+ # | attack_level --> | \
399
+ # | | | <--- fade_level
400
+ # |
401
+ # +--------------------------------------------------> Time
402
+ # [--] [---]
403
+ # attack_length fade_length
404
+ #
405
+ # [------------------][-----------------------]
406
+ # delay length
407
+ # \endverbatim
408
+ #
409
+ # Note either the attack_level or the fade_level may be above the actual
410
+ # effect level.
411
+ #
412
+ # \sa SDL_HapticConstant
413
+ # \sa SDL_HapticPeriodic
414
+ # \sa SDL_HapticCondition
415
+ # \sa SDL_HapticRamp
416
+ # \sa SDL_HapticLeftRight
417
+ # \sa SDL_HapticCustom
109
418
  class Effect < FFI::Union
110
419
  layout :type, :uint16,
111
- :constant, Constant,
112
- :periodic, Periodic,
113
- :condition, Condition,
114
- :ramp, Ramp,
115
- :leftright, LeftRight,
116
- :custom, Custom
420
+ :constant, Constant,
421
+ :periodic, Periodic,
422
+ :condition, Condition,
423
+ :ramp, Ramp,
424
+ :leftright, LeftRight,
425
+ :custom, Custom
117
426
  end
118
-
427
+
119
428
  # Haptic
429
+
430
+ # Free pointer associated with a haptic structure
120
431
  def self.release(pointer)
121
432
  SDL2.haptic_close(pointer)
122
433
  end
123
434
 
124
435
  end
125
-
126
-
127
436
 
437
+ ##
438
+ # Get the number of haptics?
128
439
  api :SDL_NumHaptics, [], :int
129
440
  api :SDL_HapticName, [:int], :string
130
441
  api :SDL_HapticOpen, [:int], Haptic.auto_ptr
@@ -155,5 +466,5 @@ module SDL2
155
466
  api :SDL_HapticRumbleInit, [Haptic.by_ref], :int
156
467
  api :SDL_HapticRumblePlay, [Haptic.by_ref], :int
157
468
  api :SDL_HapticRumbleStop, [Haptic.by_ref], :int
158
-
469
+
159
470
  end