sdl2-win93 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/.dir-locals.el +2 -0
  3. data/.github/workflows/gempush.yml +29 -0
  4. data/.gitignore +14 -0
  5. data/COPYING.txt +165 -0
  6. data/Gemfile +4 -0
  7. data/Gemfile.lock +24 -0
  8. data/Makefile +4 -0
  9. data/README.md +36 -0
  10. data/Rakefile +51 -0
  11. data/doc/po/ja.po +10357 -0
  12. data/ext/sdl2_ext/clipboard.c +61 -0
  13. data/ext/sdl2_ext/color.c +103 -0
  14. data/ext/sdl2_ext/color.h +4 -0
  15. data/ext/sdl2_ext/event.c +1298 -0
  16. data/ext/sdl2_ext/extconf.rb +22 -0
  17. data/ext/sdl2_ext/filesystem.c +63 -0
  18. data/ext/sdl2_ext/gamecontroller.c +408 -0
  19. data/ext/sdl2_ext/gamecontroller.c.m4 +408 -0
  20. data/ext/sdl2_ext/gl.c +351 -0
  21. data/ext/sdl2_ext/gl.c.m4 +351 -0
  22. data/ext/sdl2_ext/hint.c +99 -0
  23. data/ext/sdl2_ext/joystick.c +339 -0
  24. data/ext/sdl2_ext/joystick.c.m4 +339 -0
  25. data/ext/sdl2_ext/key.c +1302 -0
  26. data/ext/sdl2_ext/key.c.m4 +833 -0
  27. data/ext/sdl2_ext/main.c +258 -0
  28. data/ext/sdl2_ext/messagebox.c +233 -0
  29. data/ext/sdl2_ext/mixer.c +1205 -0
  30. data/ext/sdl2_ext/mixer.c.m4 +1205 -0
  31. data/ext/sdl2_ext/mouse.c +286 -0
  32. data/ext/sdl2_ext/rubysdl2_internal.h +127 -0
  33. data/ext/sdl2_ext/timer.c +63 -0
  34. data/ext/sdl2_ext/ttf.c +376 -0
  35. data/ext/sdl2_ext/ttf.c.m4 +376 -0
  36. data/ext/sdl2_ext/video.c +4093 -0
  37. data/ext/sdl2_ext/video.c.m4 +3867 -0
  38. data/lib/sdl2.rb +3 -0
  39. data/lib/sdl2/event.rb +55 -0
  40. data/lib/sdl2/version.rb +8 -0
  41. data/sample/chunk_destroy.rb +16 -0
  42. data/sample/gfxprimitives.rb +54 -0
  43. data/sample/icon.bmp +0 -0
  44. data/sample/memory_test/m1.rb +28 -0
  45. data/sample/memory_test/m2.rb +18 -0
  46. data/sample/memory_test/m3.rb +12 -0
  47. data/sample/message_box.rb +33 -0
  48. data/sample/music_player.rb +137 -0
  49. data/sample/playwave.rb +19 -0
  50. data/sample/primitives.rb +32 -0
  51. data/sample/test_clipboard.rb +16 -0
  52. data/sample/test_controller.rb +62 -0
  53. data/sample/test_joystick.rb +53 -0
  54. data/sample/test_keyboard.rb +52 -0
  55. data/sample/test_mouse.rb +50 -0
  56. data/sample/test_surface.rb +13 -0
  57. data/sample/test_ttf.rb +82 -0
  58. data/sample/test_video.rb +59 -0
  59. data/sample/testgl.rb +175 -0
  60. data/sample/testsprite.rb +296 -0
  61. data/sample/testspriteminimal.rb +75 -0
  62. data/sample/timer.rb +11 -0
  63. data/sample/version.rb +12 -0
  64. data/sample/video_info.rb +64 -0
  65. data/sdl2-win93.gemspec +31 -0
  66. metadata +158 -0
@@ -0,0 +1,351 @@
1
+ /* -*- mode: C -*- */
2
+ #include "rubysdl2_internal.h"
3
+ #include <SDL_video.h>
4
+ #include <SDL_version.h>
5
+
6
+ static VALUE mGL;
7
+ static VALUE cGLContext;
8
+
9
+ static VALUE current_context = Qnil;
10
+
11
+ typedef struct GLContext {
12
+ SDL_GLContext context;
13
+ } GLContext;
14
+
15
+ DEFINE_WRAPPER(SDL_GLContext, GLContext, context, cGLContext, "SDL2::GL::Context");
16
+
17
+ static void GLContext_free(GLContext* c)
18
+ {
19
+ if (c->context)
20
+ SDL_GL_DeleteContext(c->context);
21
+ free(c);
22
+ }
23
+
24
+ static VALUE GLContext_new(SDL_GLContext context)
25
+ {
26
+ GLContext* c = ALLOC(GLContext);
27
+ c->context = context;
28
+ return Data_Wrap_Struct(cGLContext, 0, GLContext_free, c);
29
+ }
30
+
31
+ /*
32
+ * Document-module: SDL2::GL
33
+ *
34
+ * This module provides the initialize/shutdown functions of OpenGL.
35
+ *
36
+ */
37
+
38
+ /*
39
+ * Document-class: SDL2::GL::Context
40
+ *
41
+ * This class represents an OpenGL context.
42
+ *
43
+ * You must create a new OpenGL context before using
44
+ * OpenGL functions.
45
+ *
46
+ * @!method destroy?
47
+ * Return true if the context is {#destroy destroyed}.
48
+ */
49
+
50
+ /*
51
+ * @overload create(window)
52
+ * Create an OpenGL context for use with an OpenGL window, and make it
53
+ * current.
54
+ *
55
+ * @param window [SDL2::Window] the window associate with a new context
56
+ * @return [SDL2::GL::Context]
57
+ *
58
+ * @see #delete
59
+ *
60
+ * @example
61
+ *
62
+ * SDL2.init(SDL2::INIT_EVERYTHING)
63
+ * # You need to create a window with `OPENGL' flag
64
+ * window = SDL2::Window.create("testgl", 0, 0, WINDOW_W, WINDOW_H,
65
+ * SDL2::Window::Flags::OPENGL)
66
+ *
67
+ * # Create a OpenGL context attached to the window
68
+ * context = SDL2::GL::Context.create(window)
69
+ *
70
+ * # You can use OpenGL functions
71
+ * :
72
+ *
73
+ * # Delete the context after using OpenGL functions
74
+ * context.destroy
75
+ */
76
+ static VALUE GLContext_s_create(VALUE self, VALUE window)
77
+ {
78
+ SDL_GLContext context = SDL_GL_CreateContext(Get_SDL_Window(window));
79
+ if (!context)
80
+ SDL_ERROR();
81
+
82
+ return (current_context = GLContext_new(context));
83
+ }
84
+
85
+ /*
86
+ * Delete the OpenGL context.
87
+ *
88
+ * @return [nil]
89
+ *
90
+ * @see #destroy?
91
+ */
92
+ static VALUE GLContext_destroy(VALUE self)
93
+ {
94
+ GLContext* c = Get_GLContext(self);
95
+ if (c->context)
96
+ SDL_GL_DeleteContext(c->context);
97
+ c->context = NULL;
98
+ return Qnil;
99
+ }
100
+
101
+ /*
102
+ * @overload make_current(window)
103
+ * Set the OpenGL context for rendering into an OpenGL window.
104
+ *
105
+ * @param window [SDL2::Window] the window to associate with the context
106
+ * @return [nil]
107
+ */
108
+ static VALUE GLContext_make_current(VALUE self, VALUE window)
109
+ {
110
+ HANDLE_ERROR(SDL_GL_MakeCurrent(Get_SDL_Window(window), Get_SDL_GLContext(self)));
111
+ current_context = self;
112
+ return Qnil;
113
+ }
114
+
115
+ /*
116
+ * Get the current OpenGL context.
117
+ *
118
+ * @return [SDL2::GL::Context] the curren context
119
+ * @return [nil] if there is no current context
120
+ *
121
+ * @see #make_current
122
+ */
123
+ static VALUE GLContext_s_current(VALUE self)
124
+ {
125
+ return current_context;
126
+ }
127
+
128
+ /*
129
+ * @overload extension_supported?(extension)
130
+ * Return true if the current context supports **extension**
131
+ *
132
+ * @param extension [String] the name of an extension
133
+ * @example
134
+ * SDL2::GL.extension_supported?("GL_EXT_framebuffer_blit")
135
+ */
136
+ static VALUE GL_s_extension_supported_p(VALUE self, VALUE extension)
137
+ {
138
+ return INT2BOOL(SDL_GL_ExtensionSupported(StringValueCStr(extension)));
139
+ }
140
+
141
+ /*
142
+ * Get the state of swap interval of the current context.
143
+ *
144
+ * @return [Integer]
145
+ * return 0 when vsync is not used,
146
+ * return 1 when vsync is used,
147
+ * and return -1 when vsync is not supported by the system (OS).
148
+ *
149
+ */
150
+ static VALUE GL_s_swap_interval(VALUE self)
151
+ {
152
+ return INT2NUM(SDL_GL_GetSwapInterval());
153
+ }
154
+
155
+
156
+ /*
157
+ * @overload swap_interval=(interval)
158
+ * Set the state of swap interval of the current context.
159
+ *
160
+ * @param interval [Integer]
161
+ * 0 if you don't want to wait for vsync,
162
+ * 1 if you want to wait for vsync,
163
+ * -1 if you want to use late swap tearing
164
+ * @return [nil]
165
+ *
166
+ */
167
+ static VALUE GL_s_set_swap_interval(VALUE self, VALUE interval)
168
+ {
169
+ HANDLE_ERROR(SDL_GL_SetSwapInterval(NUM2INT(interval)));
170
+ return Qnil;
171
+ }
172
+
173
+ /*
174
+ * @overload get_attribute(attr)
175
+ * Get the acutal value for an attribute from current context.
176
+ *
177
+ * @param attr [Integer] the OpenGL attribute to query
178
+ * @return [Integer]
179
+ */
180
+ static VALUE GL_s_get_attribute(VALUE self, VALUE attr)
181
+ {
182
+ int value;
183
+ HANDLE_ERROR(SDL_GL_GetAttribute(NUM2INT(attr), &value));
184
+ return INT2NUM(value);
185
+ }
186
+
187
+ /*
188
+ * @overload set_attribute(attr, value)
189
+ * Set an OpenGL window attribute before window creation.
190
+ *
191
+ * @param attr [Integer] the OpenGL attribute to set
192
+ * @param value [Integer] the desired value for the attribute
193
+ * @return [value]
194
+ */
195
+ static VALUE GL_s_set_attribute(VALUE self, VALUE attr, VALUE value)
196
+ {
197
+ HANDLE_ERROR(SDL_GL_SetAttribute(NUM2INT(attr), NUM2INT(value)));
198
+ return value;
199
+ }
200
+
201
+ void rubysdl2_init_gl(void)
202
+ {
203
+ mGL = rb_define_module_under(mSDL2, "GL");
204
+ cGLContext = rb_define_class_under(mGL, "Context", rb_cObject);
205
+
206
+ rb_define_singleton_method(cGLContext, "create", GLContext_s_create, 1);
207
+ rb_define_singleton_method(cGLContext, "current", GLContext_s_current, 0);
208
+
209
+ rb_define_method(cGLContext, "destroy?", GLContext_destroy_p, 0);
210
+ rb_define_method(cGLContext, "destroy", GLContext_destroy, 0);
211
+ rb_define_method(cGLContext, "make_current", GLContext_make_current, 1);
212
+
213
+ rb_define_module_function(mGL, "extension_supported?", GL_s_extension_supported_p, 1);
214
+ rb_define_module_function(mGL, "swap_interval", GL_s_swap_interval, 0);
215
+ rb_define_module_function(mGL, "swap_interval=", GL_s_set_swap_interval, 1);
216
+ rb_define_module_function(mGL, "get_attribute", GL_s_get_attribute, 1);
217
+ rb_define_module_function(mGL, "set_attribute", GL_s_set_attribute, 2);
218
+
219
+ /* define(`DEFINE_GL_ATTR_CONST',`rb_define_const(mGL, "$1", INT2NUM(SDL_GL_$1))') */
220
+ /* OpenGL attribute - minimal bits of red channel in color buffer, default is 3 */
221
+ DEFINE_GL_ATTR_CONST(RED_SIZE);
222
+ /* OpenGL attribute - minimal bits of green channel in color buffer, default is 3 */
223
+ DEFINE_GL_ATTR_CONST(GREEN_SIZE);
224
+ /* OpenGL attribute - minimal bits of blue channel in color buffer, default is 2 */
225
+ DEFINE_GL_ATTR_CONST(BLUE_SIZE);
226
+ /* OpenGL attribute - minimal bits of alpha channel in color buffer, default is 0 */
227
+ DEFINE_GL_ATTR_CONST(ALPHA_SIZE);
228
+ /* OpenGL attribute - minimal bits of framebufer, default is 0 */
229
+ DEFINE_GL_ATTR_CONST(BUFFER_SIZE);
230
+ /* OpenGL attribute - whether the single buffer (0) or double buffer (1), default
231
+ is double buffer */
232
+ DEFINE_GL_ATTR_CONST(DOUBLEBUFFER);
233
+ /* OpenGL attribute - bits of depth buffer, default is 16 */
234
+ DEFINE_GL_ATTR_CONST(DEPTH_SIZE);
235
+ /* OpenGL attribute - bits of stencil buffer, default is 0 */
236
+ DEFINE_GL_ATTR_CONST(STENCIL_SIZE);
237
+ /* OpenGL attribute - minimal bits of red channel in accumlation buffer,
238
+ default is 0 */
239
+ DEFINE_GL_ATTR_CONST(ACCUM_RED_SIZE);
240
+ /* OpenGL attribute - minimal bits of green channel in accumlation buffer,
241
+ default is 0 */
242
+ DEFINE_GL_ATTR_CONST(ACCUM_GREEN_SIZE);
243
+ /* OpenGL attribute - minimal bits of blue channel in accumlation buffer,
244
+ default is 0 */
245
+ DEFINE_GL_ATTR_CONST(ACCUM_BLUE_SIZE);
246
+ /* OpenGL attribute - minimal bits of alpha channel in accumlation buffer,
247
+ default is 0 */
248
+ DEFINE_GL_ATTR_CONST(ACCUM_ALPHA_SIZE);
249
+ /* OpenGL attribute - whether output is stereo (1) or not (0), default is 0 */
250
+ DEFINE_GL_ATTR_CONST(STEREO);
251
+ /* OpenGL attribuite - the number of buffers used for multisampe anti-aliasing,
252
+ default is 0 */
253
+ DEFINE_GL_ATTR_CONST(MULTISAMPLEBUFFERS);
254
+ /* OpenGL attribute - the number of samples used around the current pixel
255
+ use for multisample anti-aliasing, default is 0 */
256
+ DEFINE_GL_ATTR_CONST(MULTISAMPLESAMPLES);
257
+ /* OpenGL attribute - 1 for requiring hardware acceleration, 0 for software rendering,
258
+ default is allowing either */
259
+ DEFINE_GL_ATTR_CONST(ACCELERATED_VISUAL);
260
+ /* OpenGL attribute - not used (deprecated) */
261
+ DEFINE_GL_ATTR_CONST(RETAINED_BACKING);
262
+ /* OpenGL attribute - OpenGL context major version */
263
+ DEFINE_GL_ATTR_CONST(CONTEXT_MAJOR_VERSION);
264
+ /* OpenGL attribute - OpenGL context minor version */
265
+ DEFINE_GL_ATTR_CONST(CONTEXT_MINOR_VERSION);
266
+ /*
267
+ * INT2NUM(SDL_GL_CONTEXT_FLAGS):
268
+ *
269
+ * OpenGL attribute - the bit combination of following constants, or 0.
270
+ * default is 0
271
+ *
272
+ * * {SDL2::GL::CONTEXT_DEBUG_FLAG}
273
+ * * {SDL2::GL::CONTEXT_FORWARD_COMPATIBLE_FLAG}
274
+ * * {SDL2::GL::CONTEXT_ROBUST_ACCESS_FLAG}
275
+ * * {SDL2::GL::CONTEXT_RESET_ISOLATION_FLAG}
276
+ *
277
+ * These flags are mapped to some OpenGL extensions. Please see
278
+ * the documentation of each constant for more details.
279
+ *
280
+ * https://wiki.libsdl.org/SDL_GLcontextFlag
281
+ */
282
+ DEFINE_GL_ATTR_CONST(CONTEXT_FLAGS);
283
+ /* INT2NUM(SDL_GL_CONTEXT_PROFILE_MASK):
284
+ *
285
+ * OpenGL attribute - type of GL context, one of the following constants,
286
+ * defaults depends on platform
287
+ *
288
+ * * {CONTEXT_PROFILE_CORE}
289
+ * * {CONTEXT_PROFILE_COMPATIBILITY}
290
+ * * {CONTEXT_PROFILE_ES}
291
+ *
292
+ * https://wiki.libsdl.org/SDL_GLprofile
293
+ */
294
+ DEFINE_GL_ATTR_CONST(CONTEXT_PROFILE_MASK);
295
+ /* OpenGL attribute - OpenGL context sharing, default is 0 */
296
+ DEFINE_GL_ATTR_CONST(SHARE_WITH_CURRENT_CONTEXT);
297
+ #if SDL_VERSION_ATLEAST(2,0,1)
298
+ /* OpenGL attribute - 1 for requesting sRGB capable visual, default to 0 */
299
+ DEFINE_GL_ATTR_CONST(FRAMEBUFFER_SRGB_CAPABLE);
300
+ #endif
301
+ /* OpenGL attribute - not used (deprecated) */
302
+ DEFINE_GL_ATTR_CONST(CONTEXT_EGL);
303
+
304
+ /* define(`DEFINE_GL_CONTEXT_CONST',`rb_define_const(mGL, "CONTEXT_$1", INT2NUM(SDL_GL_CONTEXT_$1))') */
305
+
306
+ /* This flag maps to GLX_CONTEXT_DEBUG_BIT_ARB in
307
+ * the GLX_ARB_create_context extension for X11
308
+ * and WGL_CONTEXT_DEBUG_BIT_ARB in the WGL_ARB_create_context
309
+ * extension for Windows.
310
+ */
311
+ DEFINE_GL_CONTEXT_CONST(DEBUG_FLAG);
312
+ /*
313
+ * This flag maps to GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB in the
314
+ * GLX_ARB_create_context extension for X11 and
315
+ * WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB in the WGL_ARB_create_context
316
+ * extension for Windows.
317
+ */
318
+ DEFINE_GL_CONTEXT_CONST(FORWARD_COMPATIBLE_FLAG);
319
+ /*
320
+ * This flag maps to GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB in the
321
+ * GLX_ARB_create_context_robustness extension for X11 and
322
+ * WGL_CONTEXT_ROBUST_ACCESS_BIT_ARB in the WGL_ARB_create_context_robustness
323
+ * extension for Windows.
324
+ */
325
+ DEFINE_GL_CONTEXT_CONST(ROBUST_ACCESS_FLAG);
326
+ /*
327
+ * This flag maps to GLX_CONTEXT_RESET_ISOLATION_BIT_ARB in the
328
+ * GLX_ARB_robustness_isolation extension for X11 and
329
+ * WGL_CONTEXT_RESET_ISOLATION_BIT_ARB in the WGL_ARB_create_context_robustness
330
+ * extension for Windows.
331
+ */
332
+ DEFINE_GL_CONTEXT_CONST(RESET_ISOLATION_FLAG);
333
+
334
+ /*
335
+ * OpenGL core profile - deprecated
336
+ * functions are disabled
337
+ */
338
+ DEFINE_GL_CONTEXT_CONST(PROFILE_CORE);
339
+ /*
340
+ * OpenGL compatibility profile -
341
+ * deprecated functions are allowed
342
+ */
343
+ DEFINE_GL_CONTEXT_CONST(PROFILE_COMPATIBILITY);
344
+ /*
345
+ * OpenGL ES profile - only a subset of the
346
+ * base OpenGL functionality is available
347
+ */
348
+ DEFINE_GL_CONTEXT_CONST(PROFILE_ES);
349
+
350
+ rb_gc_register_address(&current_context);
351
+ }
@@ -0,0 +1,99 @@
1
+ #include "rubysdl2_internal.h"
2
+ #include <SDL_hints.h>
3
+
4
+ static VALUE sym_priority;
5
+
6
+ /*
7
+ * Document-module: SDL2::Hints
8
+ *
9
+ * This module enables you to get and set configuration hints.
10
+ *
11
+ */
12
+
13
+ /*
14
+ * Clear all hints set by {.[]=}.
15
+ *
16
+ * @return [nil]
17
+ */
18
+ static VALUE Hints_s_clear(VALUE self)
19
+ {
20
+ SDL_ClearHints();
21
+ return Qnil;
22
+ }
23
+
24
+ /*
25
+ * @overload [](hint)
26
+ * Get the value of a hint.
27
+ *
28
+ * @param hint [String] the name of the hint to query
29
+ *
30
+ * @return [String] the string value of a hint
31
+ * @return [nil] if the hint isn't set
32
+ */
33
+ static VALUE Hints_s_aref(VALUE self, VALUE name)
34
+ {
35
+ const char* value = SDL_GetHint(StringValueCStr(name));
36
+ if (value)
37
+ return utf8str_new_cstr(value);
38
+ else
39
+ return Qnil;
40
+ }
41
+
42
+ /*
43
+ * Set the value of a hint.
44
+ *
45
+ * @overload []=(hint, value)
46
+ * Set a hint with normal priority.
47
+ *
48
+ * @param hint [String] the name of the hint to query
49
+ * @param value [String] the value of the hint varaible
50
+ *
51
+ * @overload []=(hint, priority: , value)
52
+ * Set a hint with given priority.
53
+ *
54
+ * @param hint [String] the name of the hint to query
55
+ * @param priority [Integer] the priority, one of the
56
+ * {DEFAULT}, {NORMAL}, or {OVERRIDE}.
57
+ * @param value [String] the value of the hint varaible
58
+ *
59
+ * @return [Boolean] return true if the hint was set
60
+ *
61
+ * @example
62
+ * SDL2::Hints["SDL_HINT_XINPUT_ENABLED", priority: SDL2::Hints::OVERRIDE] = "0"
63
+ *
64
+ */
65
+ static VALUE Hints_s_aset(int argc, VALUE* argv, VALUE self)
66
+ {
67
+ VALUE name, pri, value;
68
+ rb_scan_args(argc, argv, "21", &name, &pri, &value);
69
+
70
+ if (argc == 2) {
71
+ value = pri;
72
+ return INT2BOOL(SDL_SetHint(StringValueCStr(name), StringValueCStr(value)));
73
+ } else {
74
+ Check_Type(pri, T_HASH);
75
+ return INT2BOOL(SDL_SetHintWithPriority(StringValueCStr(name),
76
+ StringValueCStr(value),
77
+ NUM2INT(rb_hash_aref(pri, sym_priority))));
78
+ }
79
+
80
+ return Qnil;
81
+ }
82
+
83
+ void rubysdl2_init_hints(void)
84
+ {
85
+ VALUE mHints = rb_define_module_under(mSDL2, "Hints");
86
+
87
+ rb_define_singleton_method(mHints, "clear", Hints_s_clear, 0);
88
+ rb_define_singleton_method(mHints, "get", Hints_s_aref, 1);
89
+ rb_define_singleton_method(mHints, "[]=", Hints_s_aset, -1);
90
+
91
+ /* low priority, used fro default values */
92
+ rb_define_const(mHints, "DEFAULT", INT2NUM(SDL_HINT_DEFAULT));
93
+ /* medium priority, overrided by an environment variable */
94
+ rb_define_const(mHints, "NORMAL", INT2NUM(SDL_HINT_NORMAL));
95
+ /* high priority, this priority overrides the value by environment variables */
96
+ rb_define_const(mHints, "OVERRIDE", INT2NUM(SDL_HINT_OVERRIDE));
97
+
98
+ sym_priority = ID2SYM(rb_intern("priority"));
99
+ }
@@ -0,0 +1,339 @@
1
+ /* -*- mode: C -*- */
2
+ #include "rubysdl2_internal.h"
3
+ #include <SDL_joystick.h>
4
+ #include <SDL_gamecontroller.h>
5
+
6
+ static VALUE cJoystick;
7
+ static VALUE cDeviceInfo;
8
+ static VALUE mHat;
9
+
10
+ typedef struct Joystick {
11
+ SDL_Joystick* joystick;
12
+ } Joystick;
13
+
14
+ static void Joystick_free(Joystick* j)
15
+ {
16
+ if (rubysdl2_is_active() && j->joystick)
17
+ SDL_JoystickClose(j->joystick);
18
+ free(j);
19
+ }
20
+
21
+ static VALUE Joystick_new(SDL_Joystick* joystick)
22
+ {
23
+ Joystick* j = ALLOC(Joystick);
24
+ j->joystick = joystick;
25
+ return Data_Wrap_Struct(cJoystick, 0, Joystick_free, j);
26
+ }
27
+
28
+ DEFINE_WRAPPER(SDL_Joystick, Joystick, joystick, cJoystick, "SDL2::Joystick");
29
+
30
+ /*
31
+ * Document-class: SDL2::Joystick
32
+ *
33
+ * This class represents a joystick connected to the machine.
34
+ *
35
+ * In order to use joystick subsystem, {SDL2.init} must have been called
36
+ * with the SDL2::INIT_JOYSTICK flag.
37
+ *
38
+ * @!method destroy?
39
+ * Return true if the device is alread closed.
40
+ * @see #destroy
41
+ */
42
+
43
+ /*
44
+ * Get the number of connected joysticks.
45
+ *
46
+ * @return [Integer]
47
+ */
48
+ static VALUE Joystick_s_num_connected_joysticks(VALUE self)
49
+ {
50
+ return INT2FIX(HANDLE_ERROR(SDL_NumJoysticks()));
51
+ }
52
+
53
+ static VALUE GUID_to_String(SDL_JoystickGUID guid)
54
+ {
55
+ char buf[128];
56
+ SDL_JoystickGetGUIDString(guid, buf, sizeof(buf));
57
+ return rb_usascii_str_new_cstr(buf);
58
+ }
59
+
60
+ /*
61
+ * Get the information of connected joysticks
62
+ *
63
+ * @return [Array<SDL2::Joystick::DeviceInfo>] information of connected devices
64
+ */
65
+ static VALUE Joystick_s_devices(VALUE self)
66
+ {
67
+ int num_joysticks = SDL_NumJoysticks();
68
+ int i;
69
+ VALUE devices = rb_ary_new2(num_joysticks);
70
+ for (i=0; i<num_joysticks; ++i) {
71
+ VALUE device = rb_obj_alloc(cDeviceInfo);
72
+ rb_iv_set(device, "@GUID", GUID_to_String(SDL_JoystickGetDeviceGUID(i)));
73
+ rb_iv_set(device, "@name", utf8str_new_cstr(SDL_JoystickNameForIndex(i)));
74
+ rb_ary_push(devices, device);
75
+ }
76
+ return devices;
77
+ }
78
+
79
+ /*
80
+ * @overload open(device_index)
81
+ * Open a joystick for use.
82
+ *
83
+ * @param [Integer] device_index device index
84
+ * @return [SDL2::Joystick] opended joystick object
85
+ * @raise [SDL2::Error] raised when device open is failed.
86
+ * for exmaple, device_index is out of range.
87
+ */
88
+ static VALUE Joystick_s_open(VALUE self, VALUE device_index)
89
+ {
90
+ SDL_Joystick* joystick = SDL_JoystickOpen(NUM2INT(device_index));
91
+ if (!joystick)
92
+ SDL_ERROR();
93
+ return Joystick_new(joystick);
94
+ }
95
+
96
+ /*
97
+ * @overload game_controller?(index)
98
+ * Return true if the joystick of given index supports the game controller
99
+ * interface.
100
+ *
101
+ * @param [Integer] index the joystick device index
102
+ * @see SDL2::GameController
103
+ *
104
+ */
105
+ static VALUE Joystick_s_game_controller_p(VALUE self, VALUE index)
106
+ {
107
+ return INT2BOOL(SDL_IsGameController(NUM2INT(index)));
108
+ }
109
+
110
+ /*
111
+ * Return true a joystick has been opened and currently connected.
112
+ */
113
+ static VALUE Joystick_attached_p(VALUE self)
114
+ {
115
+ Joystick* j = Get_Joystick(self);
116
+ if (!j->joystick)
117
+ return Qfalse;
118
+ return INT2BOOL(SDL_JoystickGetAttached(j->joystick));
119
+ }
120
+
121
+ /*
122
+ * Get the joystick GUID
123
+ *
124
+ * @return [String] GUID string
125
+ */
126
+ static VALUE Joystick_GUID(VALUE self)
127
+ {
128
+ SDL_JoystickGUID guid;
129
+ char buf[128];
130
+ guid = SDL_JoystickGetGUID(Get_SDL_Joystick(self));
131
+ SDL_JoystickGetGUIDString(guid, buf, sizeof(buf));
132
+ return rb_usascii_str_new_cstr(buf);
133
+ }
134
+
135
+ /*
136
+ * Get the index of a joystick
137
+ *
138
+ * @return [Integer] index
139
+ */
140
+ static VALUE Joystick_index(VALUE self)
141
+ {
142
+ return INT2NUM(HANDLE_ERROR(SDL_JoystickInstanceID(Get_SDL_Joystick(self))));
143
+ }
144
+
145
+ /*
146
+ * Close a joystick device.
147
+ *
148
+ * @return [nil]
149
+ * @see #destroy?
150
+ */
151
+ static VALUE Joystick_destroy(VALUE self)
152
+ {
153
+ Joystick* j = Get_Joystick(self);
154
+ if (j->joystick)
155
+ SDL_JoystickClose(j->joystick);
156
+ j->joystick = NULL;
157
+ return Qnil;
158
+ }
159
+
160
+ /*
161
+ * Get the name of a joystick
162
+ *
163
+ * @return [String] name
164
+ */
165
+ static VALUE Joystick_name(VALUE self)
166
+ {
167
+ return utf8str_new_cstr(SDL_JoystickName(Get_SDL_Joystick(self)));
168
+ }
169
+
170
+ /*
171
+ * Get the number of general axis controls on a joystick.
172
+ * @return [Integer]
173
+ * @see #axis
174
+ */
175
+ static VALUE Joystick_num_axes(VALUE self)
176
+ {
177
+ return INT2FIX(SDL_JoystickNumAxes(Get_SDL_Joystick(self)));
178
+ }
179
+
180
+ /*
181
+ * Get the number of trackball on a joystick
182
+ * @return [Integer]
183
+ * @see #ball
184
+ */
185
+ static VALUE Joystick_num_balls(VALUE self)
186
+ {
187
+ return INT2FIX(SDL_JoystickNumBalls(Get_SDL_Joystick(self)));
188
+ }
189
+
190
+ /*
191
+ * Get the number of button on a joystick
192
+ * @return [Integer]
193
+ * @see #button
194
+ */
195
+ static VALUE Joystick_num_buttons(VALUE self)
196
+ {
197
+ return INT2FIX(SDL_JoystickNumButtons(Get_SDL_Joystick(self)));
198
+ }
199
+
200
+ /*
201
+ * Get the number of POV hats on a joystick
202
+ * @return [Integer]
203
+ * @see #hat
204
+ */
205
+ static VALUE Joystick_num_hats(VALUE self)
206
+ {
207
+ return INT2FIX(SDL_JoystickNumHats(Get_SDL_Joystick(self)));
208
+ }
209
+
210
+ /*
211
+ * @overload axis(which)
212
+ * Get the current state of an axis control on a joystick.
213
+ *
214
+ * @param [Integer] which an index of an axis, started at index 0
215
+ * @return [Integer] state value, ranging from -32768 to 32767.
216
+ * @see #num_axes
217
+ */
218
+ static VALUE Joystick_axis(VALUE self, VALUE which)
219
+ {
220
+ return INT2FIX(SDL_JoystickGetAxis(Get_SDL_Joystick(self), NUM2INT(which)));
221
+ }
222
+
223
+ /*
224
+ * @overload ball(which)
225
+ * Get the current state of a trackball on a joystick.
226
+ *
227
+ * @param [Integer] which an index of a trackball, started at index 0
228
+ * @return [[Integer,Integer]] dx and dy
229
+ * @see #num_balls
230
+ */
231
+ static VALUE Joystick_ball(VALUE self, VALUE which)
232
+ {
233
+ int dx, dy;
234
+ HANDLE_ERROR(SDL_JoystickGetBall(Get_SDL_Joystick(self), NUM2INT(which), &dx, &dy));
235
+ return rb_ary_new3(2, INT2NUM(dx), INT2NUM(dy));
236
+ }
237
+
238
+ /*
239
+ * @overload button(which)
240
+ * Get the current state of a button on a joystick.
241
+ *
242
+ * @param [Integer] which an index of a button, started at index 0
243
+ * @return [Boolean] true if the button is pressed
244
+ * @see #num_buttons
245
+ */
246
+ static VALUE Joystick_button(VALUE self, VALUE which)
247
+ {
248
+ return INT2BOOL(SDL_JoystickGetButton(Get_SDL_Joystick(self), NUM2INT(which)));
249
+ }
250
+
251
+ /*
252
+ * @overload hat(which)
253
+ * Get the current state of a POV hat on a joystick.
254
+ *
255
+ * @param [Integer] which an index of a hat, started at index 0
256
+ * @return [Integer] hat state
257
+ * @see #num_hats
258
+ */
259
+ static VALUE Joystick_hat(VALUE self, VALUE which)
260
+ {
261
+ return UINT2NUM(SDL_JoystickGetHat(Get_SDL_Joystick(self), NUM2INT(which)));
262
+ }
263
+
264
+ /*
265
+ * Document-class: SDL2::Joystick::DeviceInfo
266
+ *
267
+ * This class represents joystick device information, its name and GUID.
268
+ *
269
+ * You can get the information with {SDL2::Joystick.devices}.
270
+ */
271
+
272
+ /*
273
+ * Document-module: SDL2::Joystick::Hat
274
+ *
275
+ * This module provides constants of joysticks's hat positions used by {SDL2::Joystick} class.
276
+ * The position of the hat is represented by OR'd bits of {RIGHT}, {LEFT}, {UP}, and {DOWN}.
277
+ * This means the center position ({CENTERED}) is represeted by 0 and
278
+ * the left up position {LEFTUP} is represeted by ({LEFT}|{UP}).
279
+ */
280
+
281
+ void rubysdl2_init_joystick(void)
282
+ {
283
+ cJoystick = rb_define_class_under(mSDL2, "Joystick", rb_cObject);
284
+ cDeviceInfo = rb_define_class_under(cJoystick, "DeviceInfo", rb_cObject);
285
+
286
+ rb_define_singleton_method(cJoystick, "num_connected_joysticks",
287
+ Joystick_s_num_connected_joysticks, 0);
288
+ rb_define_singleton_method(cJoystick, "devices", Joystick_s_devices, 0);
289
+ rb_define_singleton_method(cJoystick, "open", Joystick_s_open, 1);
290
+ rb_define_singleton_method(cJoystick, "game_controller?",
291
+ Joystick_s_game_controller_p, 1);
292
+ rb_define_method(cJoystick, "destroy?", Joystick_destroy_p, 0);
293
+ rb_define_alias(cJoystick, "close?", "destroy?");
294
+ rb_define_method(cJoystick, "attached?", Joystick_attached_p, 0);
295
+ rb_define_method(cJoystick, "GUID", Joystick_GUID, 0);
296
+ rb_define_method(cJoystick, "index", Joystick_index, 0);
297
+ rb_define_method(cJoystick, "destroy", Joystick_destroy, 0);
298
+ rb_define_alias(cJoystick, "close", "destroy");
299
+ rb_define_method(cJoystick, "name", Joystick_name, 0);
300
+ rb_define_method(cJoystick, "num_axes", Joystick_num_axes, 0);
301
+ rb_define_method(cJoystick, "num_balls", Joystick_num_balls, 0);
302
+ rb_define_method(cJoystick, "num_buttons", Joystick_num_buttons, 0);
303
+ rb_define_method(cJoystick, "num_hats", Joystick_num_hats, 0);
304
+ rb_define_method(cJoystick, "axis", Joystick_axis, 1);
305
+ rb_define_method(cJoystick, "ball", Joystick_ball, 1);
306
+ rb_define_method(cJoystick, "button", Joystick_button, 1);
307
+ rb_define_method(cJoystick, "hat", Joystick_hat, 1);
308
+
309
+ mHat = rb_define_module_under(cJoystick, "Hat");
310
+
311
+ /* */
312
+ /* Center position. Equal to 0. */
313
+ rb_define_const(mHat, "CENTERED", INT2NUM(SDL_HAT_CENTERED));
314
+ /* Up position. */
315
+ rb_define_const(mHat, "UP", INT2NUM(SDL_HAT_UP));
316
+ /* Right position. */
317
+ rb_define_const(mHat, "RIGHT", INT2NUM(SDL_HAT_RIGHT));
318
+ /* Down position. */
319
+ rb_define_const(mHat, "DOWN", INT2NUM(SDL_HAT_DOWN));
320
+ /* Left position. */
321
+ rb_define_const(mHat, "LEFT", INT2NUM(SDL_HAT_LEFT));
322
+ /* Right Up position. Equal to ({RIGHT} | {UP}) */
323
+ rb_define_const(mHat, "RIGHTUP", INT2NUM(SDL_HAT_RIGHTUP));
324
+ /* Right Down position. Equal to ({RIGHT} | {DOWN}) */
325
+ rb_define_const(mHat, "RIGHTDOWN", INT2NUM(SDL_HAT_RIGHTDOWN));
326
+ /* Left Up position. Equal to ({LEFT} | {UP}) */
327
+ rb_define_const(mHat, "LEFTUP", INT2NUM(SDL_HAT_LEFTUP));
328
+ /* Left Down position. Equal to ({LEFT} | {DOWN}) */
329
+ rb_define_const(mHat, "LEFTDOWN", INT2NUM(SDL_HAT_LEFTDOWN));
330
+
331
+ /* Device GUID
332
+ * @return [String] */
333
+ rb_define_attr(cDeviceInfo, "GUID", 1, 0);
334
+ /* Device name
335
+ * @return [String] */
336
+ rb_define_attr(cDeviceInfo, "name", 1, 0);
337
+
338
+
339
+ }