ruby-sdl2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,53 @@
1
+ require 'mkmf'
2
+
3
+ def add_cflags(str)
4
+ $CFLAGS += " " + str
5
+ end
6
+
7
+ def add_libs(str)
8
+ $LOCAL_LIBS += " " + str
9
+ end
10
+
11
+ def run_config_program(*args)
12
+ IO.popen([*args], "r") do |io|
13
+ io.gets.chomp
14
+ end
15
+ end
16
+
17
+ def config(pkg_config, header, libnames)
18
+ if system("pkg-config", pkg_config, "--exists")
19
+ puts("Use pkg-config #{pkg_config}")
20
+ add_cflags(run_config_program("pkg-config", pkg_config, "--cflags"))
21
+ add_libs(run_config_program("pkg-config", pkg_config, "--libs"))
22
+ else
23
+ libnames.each{|libname| break if have_library(libname) }
24
+ end
25
+
26
+ have_header(header)
27
+ end
28
+
29
+ def sdl2config_with_command
30
+ sdl2_config = with_config('sdl2-config', 'sdl2-config')
31
+ add_cflags(run_config_program(sdl2_config, "--cflags"))
32
+ add_libs(run_config_program(sdl2_config, "--libs"))
33
+ end
34
+
35
+ def sdl2config_on_mingw
36
+ have_library("mingw32")
37
+ have_library("SDL2")
38
+ add_libs("-mwindows")
39
+ end
40
+
41
+ case RbConfig::CONFIG["arch"]
42
+ when /mingw/
43
+ sdl2config_on_mingw
44
+ else
45
+ sdl2config_with_command
46
+ end
47
+
48
+ config("SDL2_image", "SDL_image.h", ["SDL2_image", "SDL_image"])
49
+ config("SDL2_mixer", "SDL_mixer.h", ["SDL2_mixer", "SDL_mixer"])
50
+ config("SDL2_ttf", "SDL_ttf.h", ["SDL2_ttf", "SDL_ttf"])
51
+ have_header("SDL_filesystem.h")
52
+
53
+ create_makefile('sdl2_ext')
@@ -0,0 +1,36 @@
1
+ #ifdef HAVE_SDL_FILESYSTEM_H
2
+ #include "rubysdl2_internal.h"
3
+ #include <SDL_filesystem.h>
4
+
5
+ static VALUE SDL2_s_base_path(VALUE self)
6
+ {
7
+ char* path = SDL_GetBasePath();
8
+ VALUE str;
9
+ if (!path)
10
+ SDL_ERROR();
11
+ str = utf8str_new_cstr(path);
12
+ SDL_free(path);
13
+ return str;
14
+ }
15
+
16
+ static VALUE SDL2_s_preference_path(VALUE self, VALUE org, VALUE app)
17
+ {
18
+ char* path = SDL_GetPrefPath(StringValueCStr(org), StringValueCStr(app));
19
+ VALUE str;
20
+ if (!path)
21
+ SDL_ERROR();
22
+ str = utf8str_new_cstr(path);
23
+ SDL_free(path);
24
+ return str;
25
+ }
26
+
27
+ void rubysdl2_init_filesystem(void)
28
+ {
29
+ rb_define_module_function(mSDL2, "base_path", SDL2_s_base_path, 0);
30
+ rb_define_module_function(mSDL2, "preference_path", SDL2_s_preference_path, 2);
31
+ }
32
+ #else
33
+ void rubysdl2_init_filesystem(void)
34
+ {
35
+ }
36
+ #endif
@@ -0,0 +1,194 @@
1
+ #include "rubysdl2_internal.h"
2
+ #include <SDL_gamecontroller.h>
3
+
4
+ static VALUE cGameController;
5
+
6
+ typedef struct GameController {
7
+ SDL_GameController* controller;
8
+ } GameController;
9
+
10
+ static void GameController_free(GameController* g)
11
+ {
12
+ if (rubysdl2_is_active() && g->controller)
13
+ SDL_GameControllerClose(g->controller);
14
+ free(g);
15
+ }
16
+
17
+ static VALUE GameController_new(SDL_GameController* controller)
18
+ {
19
+ GameController* g = ALLOC(GameController);
20
+ g->controller = controller;
21
+ return Data_Wrap_Struct(cGameController, 0, GameController_free, g);
22
+ }
23
+
24
+ DEFINE_WRAPPER(SDL_GameController, GameController, controller, cGameController,
25
+ "SDL2::GameController");
26
+
27
+
28
+ static VALUE GameController_s_add_mapping(VALUE self, VALUE string)
29
+ {
30
+ int ret = HANDLE_ERROR(SDL_GameControllerAddMapping(StringValueCStr(string)));
31
+ return INT2NUM(ret);
32
+ }
33
+
34
+ static VALUE GameController_s_axis_name_of(VALUE self, VALUE axis)
35
+ {
36
+ const char* name = SDL_GameControllerGetStringForAxis(NUM2INT(axis));
37
+ if (!name) {
38
+ SDL_SetError("Unknown axis %d", NUM2INT(axis));
39
+ SDL_ERROR();
40
+ }
41
+ return utf8str_new_cstr(name);
42
+ }
43
+
44
+ static VALUE GameController_s_button_name_of(VALUE self, VALUE button)
45
+ {
46
+ const char* name = SDL_GameControllerGetStringForButton(NUM2INT(button));
47
+ if (!name) {
48
+ SDL_SetError("Unknown axis %d", NUM2INT(button));
49
+ SDL_ERROR();
50
+ }
51
+ return utf8str_new_cstr(name);
52
+ }
53
+
54
+ static VALUE GameController_s_axis_from_name(VALUE self, VALUE name)
55
+ {
56
+ int axis = SDL_GameControllerGetAxisFromString(StringValueCStr(name));
57
+ if (axis < 0) {
58
+ SDL_SetError("Unknown axis name \"%s\"", StringValueCStr(name));
59
+ SDL_ERROR();
60
+ }
61
+ return INT2FIX(axis);
62
+ }
63
+
64
+ static VALUE GameController_s_button_from_name(VALUE self, VALUE name)
65
+ {
66
+ int button = SDL_GameControllerGetButtonFromString(StringValueCStr(name));
67
+ if (button < 0) {
68
+ SDL_SetError("Unknown button name \"%s\"", StringValueCStr(name));
69
+ SDL_ERROR();
70
+ }
71
+ return INT2FIX(button);
72
+ }
73
+
74
+ static VALUE GameController_s_device_names(VALUE self)
75
+ {
76
+ int num_joysticks = SDL_NumJoysticks();
77
+ int i;
78
+ VALUE device_names = rb_ary_new2(num_joysticks);
79
+ for (i=0; i<num_joysticks; ++i) {
80
+ const char* name = SDL_GameControllerNameForIndex(i);
81
+ if (name)
82
+ rb_ary_push(device_names, utf8str_new_cstr(name));
83
+ else
84
+ rb_ary_push(device_names, Qnil);
85
+ }
86
+ return device_names;
87
+ }
88
+
89
+ static VALUE GameController_s_mapping_for(VALUE self, VALUE guid_string)
90
+ {
91
+ SDL_JoystickGUID guid = SDL_JoystickGetGUIDFromString(StringValueCStr(guid_string));
92
+ return utf8str_new_cstr(SDL_GameControllerMappingForGUID(guid));
93
+ }
94
+
95
+ static VALUE GameController_s_open(VALUE self, VALUE index)
96
+ {
97
+ SDL_GameController* controller = SDL_GameControllerOpen(NUM2INT(index));
98
+ if (!controller)
99
+ SDL_ERROR();
100
+ return GameController_new(controller);
101
+ }
102
+
103
+ static VALUE GameController_name(VALUE self)
104
+ {
105
+ return utf8str_new_cstr(SDL_GameControllerName(Get_SDL_GameController(self)));
106
+ }
107
+
108
+ static VALUE GameController_attached_p(VALUE self)
109
+ {
110
+ return INT2BOOL(SDL_GameControllerGetAttached(Get_SDL_GameController(self)));
111
+ }
112
+
113
+ static VALUE GameController_destroy(VALUE self)
114
+ {
115
+ GameController* g = Get_GameController(self);
116
+ if (g->controller)
117
+ SDL_GameControllerClose(g->controller);
118
+ g->controller = NULL;
119
+ return Qnil;
120
+ }
121
+
122
+ static VALUE GameController_mapping(VALUE self)
123
+ {
124
+ return utf8str_new_cstr(SDL_GameControllerMapping(Get_SDL_GameController(self)));
125
+ }
126
+
127
+ static VALUE GameController_axis(VALUE self, VALUE axis)
128
+ {
129
+ return INT2FIX(SDL_GameControllerGetAxis(Get_SDL_GameController(self),
130
+ NUM2INT(axis)));
131
+ }
132
+
133
+ static VALUE GameController_button_pressed_p(VALUE self, VALUE button)
134
+ {
135
+ return INT2BOOL(SDL_GameControllerGetButton(Get_SDL_GameController(self),
136
+ NUM2INT(button)));
137
+ }
138
+
139
+ void rubysdl2_init_gamecontorller(void)
140
+ {
141
+ cGameController = rb_define_class_under(mSDL2, "GameController", rb_cObject);
142
+
143
+ rb_define_singleton_method(cGameController, "add_mapping",
144
+ GameController_s_add_mapping, 1);
145
+ rb_define_singleton_method(cGameController, "device_names",
146
+ GameController_s_device_names, 0);
147
+ rb_define_singleton_method(cGameController, "axis_name_of",
148
+ GameController_s_axis_name_of, 1);
149
+ rb_define_singleton_method(cGameController, "button_name_of",
150
+ GameController_s_button_name_of, 1);
151
+ rb_define_singleton_method(cGameController, "mapping_for",
152
+ GameController_s_mapping_for, 1);
153
+ rb_define_singleton_method(cGameController, "button_from_name",
154
+ GameController_s_button_from_name, 1);
155
+ rb_define_singleton_method(cGameController, "axis_from_name",
156
+ GameController_s_axis_from_name, 1);
157
+ rb_define_singleton_method(cGameController, "open", GameController_s_open, 1);
158
+ rb_define_method(cGameController, "destroy?", GameController_destroy_p, 0);
159
+ rb_define_method(cGameController, "destroy", GameController_destroy, 0);
160
+ rb_define_method(cGameController, "name", GameController_name, 0);
161
+ rb_define_method(cGameController, "attached?", GameController_attached_p, 0);
162
+ rb_define_method(cGameController, "mapping", GameController_mapping, 0);
163
+ rb_define_method(cGameController, "axis", GameController_axis, 1);
164
+ rb_define_method(cGameController, "button_pressed?", GameController_button_pressed_p, 1);
165
+ #define DEFINE_CONTROLLER_AXIS_CONST(type) \
166
+ rb_define_const(cGameController, "AXIS_" #type, INT2NUM(SDL_CONTROLLER_AXIS_##type))
167
+ DEFINE_CONTROLLER_AXIS_CONST(INVALID);
168
+ DEFINE_CONTROLLER_AXIS_CONST(LEFTX);
169
+ DEFINE_CONTROLLER_AXIS_CONST(LEFTY);
170
+ DEFINE_CONTROLLER_AXIS_CONST(RIGHTX);
171
+ DEFINE_CONTROLLER_AXIS_CONST(RIGHTY);
172
+ DEFINE_CONTROLLER_AXIS_CONST(TRIGGERLEFT);
173
+ DEFINE_CONTROLLER_AXIS_CONST(TRIGGERRIGHT);
174
+ DEFINE_CONTROLLER_AXIS_CONST(MAX);
175
+ #define DEFINE_CONTROLLER_BUTTON_CONST(type) \
176
+ rb_define_const(cGameController, "BUTTON_" #type, INT2NUM(SDL_CONTROLLER_BUTTON_##type))
177
+ DEFINE_CONTROLLER_BUTTON_CONST(INVALID);
178
+ DEFINE_CONTROLLER_BUTTON_CONST(A);
179
+ DEFINE_CONTROLLER_BUTTON_CONST(B);
180
+ DEFINE_CONTROLLER_BUTTON_CONST(X);
181
+ DEFINE_CONTROLLER_BUTTON_CONST(Y);
182
+ DEFINE_CONTROLLER_BUTTON_CONST(BACK);
183
+ DEFINE_CONTROLLER_BUTTON_CONST(GUIDE);
184
+ DEFINE_CONTROLLER_BUTTON_CONST(START);
185
+ DEFINE_CONTROLLER_BUTTON_CONST(LEFTSTICK);
186
+ DEFINE_CONTROLLER_BUTTON_CONST(RIGHTSTICK);
187
+ DEFINE_CONTROLLER_BUTTON_CONST(LEFTSHOULDER);
188
+ DEFINE_CONTROLLER_BUTTON_CONST(RIGHTSHOULDER);
189
+ DEFINE_CONTROLLER_BUTTON_CONST(DPAD_UP);
190
+ DEFINE_CONTROLLER_BUTTON_CONST(DPAD_DOWN);
191
+ DEFINE_CONTROLLER_BUTTON_CONST(DPAD_LEFT);
192
+ DEFINE_CONTROLLER_BUTTON_CONST(DPAD_RIGHT);
193
+ DEFINE_CONTROLLER_BUTTON_CONST(MAX);
194
+ }
data/gl.c.m4 ADDED
@@ -0,0 +1,201 @@
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
+ * @overload create(window)
32
+ * Create an OpenGL context for use with an OpenGL window, and make it
33
+ * current.
34
+ *
35
+ * @param window [SDL2::Window] the window corresponding with a new context
36
+ * @return [SDL2::GL::Context]
37
+ *
38
+ * @see #delete
39
+ */
40
+ static VALUE GLContext_s_create(VALUE self, VALUE window)
41
+ {
42
+ SDL_GLContext context = SDL_GL_CreateContext(Get_SDL_Window(window));
43
+ if (!context)
44
+ SDL_ERROR();
45
+
46
+ return (current_context = GLContext_new(context));
47
+ }
48
+
49
+ static VALUE GLContext_destroy(VALUE self)
50
+ {
51
+ GLContext* c = Get_GLContext(self);
52
+ if (c->context)
53
+ SDL_GL_DeleteContext(c->context);
54
+ c->context = NULL;
55
+ return Qnil;
56
+ }
57
+
58
+ static VALUE GLContext_make_current(VALUE self, VALUE window)
59
+ {
60
+ HANDLE_ERROR(SDL_GL_MakeCurrent(Get_SDL_Window(window), Get_SDL_GLContext(self)));
61
+ current_context = self;
62
+ return Qnil;
63
+ }
64
+
65
+ static VALUE GLContext_s_current(VALUE self)
66
+ {
67
+ return current_context;
68
+ }
69
+
70
+ static VALUE GL_s_extension_supported_p(VALUE self, VALUE extension)
71
+ {
72
+ return INT2BOOL(SDL_GL_ExtensionSupported(StringValueCStr(extension)));
73
+ }
74
+
75
+ static VALUE GL_s_swap_interval(VALUE self)
76
+ {
77
+ return INT2NUM(SDL_GL_GetSwapInterval());
78
+ }
79
+
80
+ static VALUE GL_s_set_swap_interval(VALUE self, VALUE interval)
81
+ {
82
+ HANDLE_ERROR(SDL_GL_SetSwapInterval(NUM2INT(interval)));
83
+ return Qnil;
84
+ }
85
+
86
+ static VALUE GL_s_get_attribute(VALUE self, VALUE attr)
87
+ {
88
+ int value;
89
+ HANDLE_ERROR(SDL_GL_GetAttribute(NUM2INT(attr), &value));
90
+ return INT2NUM(value);
91
+ }
92
+
93
+ static VALUE GL_s_set_attribute(VALUE self, VALUE attr, VALUE value)
94
+ {
95
+ HANDLE_ERROR(SDL_GL_SetAttribute(NUM2INT(attr), NUM2INT(value)));
96
+ return value;
97
+ }
98
+
99
+ void rubysdl2_init_gl(void)
100
+ {
101
+ mGL = rb_define_module_under(mSDL2, "GL");
102
+ cGLContext = rb_define_class_under(mGL, "Context", rb_cObject);
103
+
104
+ rb_define_singleton_method(cGLContext, "create", GLContext_s_create, 1);
105
+ rb_define_singleton_method(cGLContext, "current", GLContext_s_current, 0);
106
+
107
+ rb_define_method(cGLContext, "destroy?", GLContext_destroy_p, 0);
108
+ rb_define_method(cGLContext, "destroy", GLContext_destroy, 0);
109
+ rb_define_method(cGLContext, "make_current", GLContext_make_current, 1);
110
+
111
+ rb_define_module_function(mGL, "extension_supported?", GL_s_extension_supported_p, 1);
112
+ rb_define_module_function(mGL, "swap_interval", GL_s_swap_interval, 0);
113
+ rb_define_module_function(mGL, "swap_interval=", GL_s_set_swap_interval, 1);
114
+ rb_define_module_function(mGL, "get_attribute", GL_s_get_attribute, 1);
115
+ rb_define_module_function(mGL, "set_attribute", GL_s_set_attribute, 2);
116
+
117
+ /* define(`DEFINE_GL_ATTR_CONST',`rb_define_const(mGL, "$1", INT2NUM(SDL_GL_$1))') */
118
+ /* OpenGL attribute - minimal bits of red channel in color buffer, default is 3 */
119
+ DEFINE_GL_ATTR_CONST(RED_SIZE);
120
+ /* OpenGL attribute - minimal bits of green channel in color buffer, default is 3 */
121
+ DEFINE_GL_ATTR_CONST(GREEN_SIZE);
122
+ /* OpenGL attribute - minimal bits of blue channel in color buffer, default is 2 */
123
+ DEFINE_GL_ATTR_CONST(BLUE_SIZE);
124
+ /* OpenGL attribute - minimal bits of alpha channel in color buffer, default is 0 */
125
+ DEFINE_GL_ATTR_CONST(ALPHA_SIZE);
126
+ /* OpenGL attribute - minimal bits of framebufer, default is 0 */
127
+ DEFINE_GL_ATTR_CONST(BUFFER_SIZE);
128
+ /* OpenGL attribute - whether the single buffer (0) or double buffer (1), default
129
+ is double buffer */
130
+ DEFINE_GL_ATTR_CONST(DOUBLEBUFFER);
131
+ /* OpenGL attribute - bits of depth buffer, default is 16 */
132
+ DEFINE_GL_ATTR_CONST(DEPTH_SIZE);
133
+ /* OpenGL attribute - bits of stencil buffer, default is 0 */
134
+ DEFINE_GL_ATTR_CONST(STENCIL_SIZE);
135
+ /* OpenGL attribute - minimal bits of red channel in accumlation buffer,
136
+ default is 0 */
137
+ DEFINE_GL_ATTR_CONST(ACCUM_RED_SIZE);
138
+ /* OpenGL attribute - minimal bits of green channel in accumlation buffer,
139
+ default is 0 */
140
+ DEFINE_GL_ATTR_CONST(ACCUM_GREEN_SIZE);
141
+ /* OpenGL attribute - minimal bits of blue channel in accumlation buffer,
142
+ default is 0 */
143
+ DEFINE_GL_ATTR_CONST(ACCUM_BLUE_SIZE);
144
+ /* OpenGL attribute - minimal bits of alpha channel in accumlation buffer,
145
+ default is 0 */
146
+ DEFINE_GL_ATTR_CONST(ACCUM_ALPHA_SIZE);
147
+ /* OpenGL attribute - whether output is stereo (1) or not (0), default is 0 */
148
+ DEFINE_GL_ATTR_CONST(STEREO);
149
+ /* OpenGL attribuite - the number of buffers used for multisampe anti-aliasing,
150
+ default is 0 */
151
+ DEFINE_GL_ATTR_CONST(MULTISAMPLEBUFFERS);
152
+ /* OpenGL attribute - the number of samples used around the current pixel
153
+ use for multisample anti-aliasing, default is 0 */
154
+ DEFINE_GL_ATTR_CONST(MULTISAMPLESAMPLES);
155
+ /* OpenGL attribute - 1 for requiring hardware acceleration, 0 for software rendering,
156
+ default is allowing either */
157
+ DEFINE_GL_ATTR_CONST(ACCELERATED_VISUAL);
158
+ /* OpenGL attribute - not used (deprecated) */
159
+ DEFINE_GL_ATTR_CONST(RETAINED_BACKING);
160
+ /* OpenGL attribute - OpenGL context major version */
161
+ DEFINE_GL_ATTR_CONST(CONTEXT_MAJOR_VERSION);
162
+ /* OpenGL attribute - OpenGL context minor version */
163
+ DEFINE_GL_ATTR_CONST(CONTEXT_MINOR_VERSION);
164
+ /* OpenGL attribute - the bit combination of following constants, or 0.
165
+ * default is 0
166
+ *
167
+ * * {SDL2::GL::CONTEXT_DEBUG_FLAG}
168
+ * * {SDL2::GL::CONTEXT_FORWARD_COMPATIBLE_FLAG}
169
+ * * {SDL2::GL::CONTEXT_ROBUST_ACCESS_FLAG}
170
+ * * {SDL2::GL::CONTEXT_RESET_ISOLATION_FLAG}
171
+ */
172
+ DEFINE_GL_ATTR_CONST(CONTEXT_FLAGS);
173
+ /* OpenGL attribute - type of GL context, one of the following constants,
174
+ * defaults depends on platform
175
+ *
176
+ * * {SDL2::GL::CONTEXT::PROFILE_CORE}
177
+ * * {SDL2::GL::CONTEXT::PROFILE_COMPATIBLITY}
178
+ * * {SDL2::GL::CONTEXT::PROFILE_ES}
179
+ */
180
+ DEFINE_GL_ATTR_CONST(CONTEXT_PROFILE_MASK);
181
+ /* OpenGL attribute - OpenGL context sharing, default is 0 */
182
+ DEFINE_GL_ATTR_CONST(SHARE_WITH_CURRENT_CONTEXT);
183
+ #if SDL_VERSION_ATLEAST(2,0,1)
184
+ /* OpenGL attribute - 1 for requesting sRGB capable visual, default to 0 */
185
+ DEFINE_GL_ATTR_CONST(FRAMEBUFFER_SRGB_CAPABLE);
186
+ #endif
187
+ /* OpenGL attribute - not used (deprecated) */
188
+ DEFINE_GL_ATTR_CONST(CONTEXT_EGL);
189
+
190
+ /* define(`DEFINE_GL_CONTEXT_CONST',`rb_define_const(mGL, "CONTEXT_$1", INT2NUM(SDL_GL_CONTEXT_$1))') */
191
+ DEFINE_GL_CONTEXT_CONST(DEBUG_FLAG);
192
+ DEFINE_GL_CONTEXT_CONST(FORWARD_COMPATIBLE_FLAG);
193
+ DEFINE_GL_CONTEXT_CONST(ROBUST_ACCESS_FLAG);
194
+ DEFINE_GL_CONTEXT_CONST(RESET_ISOLATION_FLAG);
195
+
196
+ DEFINE_GL_CONTEXT_CONST(PROFILE_CORE);
197
+ DEFINE_GL_CONTEXT_CONST(PROFILE_COMPATIBILITY);
198
+ DEFINE_GL_CONTEXT_CONST(PROFILE_ES);
199
+
200
+ rb_gc_register_address(&current_context);
201
+ }