sdl2-win93 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.dir-locals.el +2 -0
- data/.github/workflows/gempush.yml +29 -0
- data/.gitignore +14 -0
- data/COPYING.txt +165 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +24 -0
- data/Makefile +4 -0
- data/README.md +36 -0
- data/Rakefile +51 -0
- data/doc/po/ja.po +10357 -0
- data/ext/sdl2_ext/clipboard.c +61 -0
- data/ext/sdl2_ext/color.c +103 -0
- data/ext/sdl2_ext/color.h +4 -0
- data/ext/sdl2_ext/event.c +1298 -0
- data/ext/sdl2_ext/extconf.rb +22 -0
- data/ext/sdl2_ext/filesystem.c +63 -0
- data/ext/sdl2_ext/gamecontroller.c +408 -0
- data/ext/sdl2_ext/gamecontroller.c.m4 +408 -0
- data/ext/sdl2_ext/gl.c +351 -0
- data/ext/sdl2_ext/gl.c.m4 +351 -0
- data/ext/sdl2_ext/hint.c +99 -0
- data/ext/sdl2_ext/joystick.c +339 -0
- data/ext/sdl2_ext/joystick.c.m4 +339 -0
- data/ext/sdl2_ext/key.c +1302 -0
- data/ext/sdl2_ext/key.c.m4 +833 -0
- data/ext/sdl2_ext/main.c +258 -0
- data/ext/sdl2_ext/messagebox.c +233 -0
- data/ext/sdl2_ext/mixer.c +1205 -0
- data/ext/sdl2_ext/mixer.c.m4 +1205 -0
- data/ext/sdl2_ext/mouse.c +286 -0
- data/ext/sdl2_ext/rubysdl2_internal.h +127 -0
- data/ext/sdl2_ext/timer.c +63 -0
- data/ext/sdl2_ext/ttf.c +376 -0
- data/ext/sdl2_ext/ttf.c.m4 +376 -0
- data/ext/sdl2_ext/video.c +4093 -0
- data/ext/sdl2_ext/video.c.m4 +3867 -0
- data/lib/sdl2.rb +3 -0
- data/lib/sdl2/event.rb +55 -0
- data/lib/sdl2/version.rb +8 -0
- data/sample/chunk_destroy.rb +16 -0
- data/sample/gfxprimitives.rb +54 -0
- data/sample/icon.bmp +0 -0
- data/sample/memory_test/m1.rb +28 -0
- data/sample/memory_test/m2.rb +18 -0
- data/sample/memory_test/m3.rb +12 -0
- data/sample/message_box.rb +33 -0
- data/sample/music_player.rb +137 -0
- data/sample/playwave.rb +19 -0
- data/sample/primitives.rb +32 -0
- data/sample/test_clipboard.rb +16 -0
- data/sample/test_controller.rb +62 -0
- data/sample/test_joystick.rb +53 -0
- data/sample/test_keyboard.rb +52 -0
- data/sample/test_mouse.rb +50 -0
- data/sample/test_surface.rb +13 -0
- data/sample/test_ttf.rb +82 -0
- data/sample/test_video.rb +59 -0
- data/sample/testgl.rb +175 -0
- data/sample/testsprite.rb +296 -0
- data/sample/testspriteminimal.rb +75 -0
- data/sample/timer.rb +11 -0
- data/sample/version.rb +12 -0
- data/sample/video_info.rb +64 -0
- data/sdl2-win93.gemspec +31 -0
- metadata +158 -0
@@ -0,0 +1,408 @@
|
|
1
|
+
/* -*- mode: C -*- */
|
2
|
+
#include "rubysdl2_internal.h"
|
3
|
+
#include <SDL_gamecontroller.h>
|
4
|
+
|
5
|
+
static VALUE cGameController;
|
6
|
+
static VALUE mAxis;
|
7
|
+
static VALUE mButton;
|
8
|
+
|
9
|
+
typedef struct GameController {
|
10
|
+
SDL_GameController* controller;
|
11
|
+
} GameController;
|
12
|
+
|
13
|
+
static void GameController_free(GameController* g)
|
14
|
+
{
|
15
|
+
if (rubysdl2_is_active() && g->controller)
|
16
|
+
SDL_GameControllerClose(g->controller);
|
17
|
+
free(g);
|
18
|
+
}
|
19
|
+
|
20
|
+
/*
|
21
|
+
* Document-class: SDL2::GameController
|
22
|
+
*
|
23
|
+
* This class represents a "Game Controller".
|
24
|
+
*
|
25
|
+
* In SDL2, there is a gamecontroller framework to
|
26
|
+
* hide the details of joystick types. This framework
|
27
|
+
* is built on the existing joystick API.
|
28
|
+
*
|
29
|
+
* The framework consists of two parts:
|
30
|
+
*
|
31
|
+
* * Mapping joysticks to game controllers
|
32
|
+
* * Aquire input from game controllers
|
33
|
+
*
|
34
|
+
* Mapping information is a string, and described as:
|
35
|
+
*
|
36
|
+
* GUID,name,mapping
|
37
|
+
*
|
38
|
+
* GUID is a unique ID of a type of joystick and
|
39
|
+
* given by {Joystick#GUID}. name is the human readable
|
40
|
+
* string for the device, and mappings are contorller mappings
|
41
|
+
* to joystick ones.
|
42
|
+
*
|
43
|
+
* This mappings abstract the details of joysticks, for exmaple,
|
44
|
+
* the number of buttons, the number of axes, and the position
|
45
|
+
* of these buttons a on pad.
|
46
|
+
* You can use unified interface
|
47
|
+
* with this framework theoretically.
|
48
|
+
* Howerver, we need to prepare the
|
49
|
+
* database of many joysticks that users will use. A database
|
50
|
+
* is available at https://github.com/gabomdq/SDL_GameControllerDB,
|
51
|
+
* but perhaps this is not sufficient for your usage.
|
52
|
+
* In fact, Steam prepares its own database for Steam games,
|
53
|
+
* so if you will create a game for Steam, this framework is
|
54
|
+
* useful. Otherwise, it is a better way to use joystick API
|
55
|
+
* directly.
|
56
|
+
*
|
57
|
+
* @!method destroy?
|
58
|
+
* Return true if the gamecontroller is already closed.
|
59
|
+
*/
|
60
|
+
|
61
|
+
static VALUE GameController_new(SDL_GameController* controller)
|
62
|
+
{
|
63
|
+
GameController* g = ALLOC(GameController);
|
64
|
+
g->controller = controller;
|
65
|
+
return Data_Wrap_Struct(cGameController, 0, GameController_free, g);
|
66
|
+
}
|
67
|
+
|
68
|
+
DEFINE_WRAPPER(SDL_GameController, GameController, controller, cGameController,
|
69
|
+
"SDL2::GameController");
|
70
|
+
|
71
|
+
|
72
|
+
/*
|
73
|
+
* @overload add_mapping(string)
|
74
|
+
*
|
75
|
+
* Add suuport for controolers that SDL is unaware of or to cause an
|
76
|
+
* existing controller to have a different binding.
|
77
|
+
*
|
78
|
+
* "GUID,name,mapping", where GUID is
|
79
|
+
* the string value from SDL_JoystickGetGUIDString(), name is the human
|
80
|
+
* readable string for the device and mappings are controller mappings to
|
81
|
+
* joystick ones. Under Windows there is a reserved GUID of "xinput" that
|
82
|
+
* covers all XInput devices. The mapping format for joystick is:
|
83
|
+
*
|
84
|
+
* * bX: a joystick button, index X
|
85
|
+
* * hX.Y: hat X with value Y
|
86
|
+
* * aX: axis X of the joystick
|
87
|
+
*
|
88
|
+
* Buttons can be used as a controller axes and vice versa.
|
89
|
+
*
|
90
|
+
* This string shows an example of a valid mapping for a controller:
|
91
|
+
* "341a3608000000000000504944564944,Afterglow PS3 Controller,a:b1,b:b2,y:b3,x:b0,start:b9,guide:b12,back:b8,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftshoulder:b4,,rightshoulder:b5,leftstick:b10,rightstick:b11,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7"
|
92
|
+
*
|
93
|
+
* @param string [String] mapping string
|
94
|
+
* @return [Integer] 1 if a new mapping, 0 if an existing mapping is updated.
|
95
|
+
*/
|
96
|
+
static VALUE GameController_s_add_mapping(VALUE self, VALUE string)
|
97
|
+
{
|
98
|
+
int ret = HANDLE_ERROR(SDL_GameControllerAddMapping(StringValueCStr(string)));
|
99
|
+
return INT2NUM(ret);
|
100
|
+
}
|
101
|
+
|
102
|
+
/*
|
103
|
+
* @overload axis_name_of(axis)
|
104
|
+
*
|
105
|
+
* Get a string representing **axis**.
|
106
|
+
*
|
107
|
+
* @param axis [Integer] axis constant in {Axis}
|
108
|
+
* @return [String]
|
109
|
+
*/
|
110
|
+
static VALUE GameController_s_axis_name_of(VALUE self, VALUE axis)
|
111
|
+
{
|
112
|
+
const char* name = SDL_GameControllerGetStringForAxis(NUM2INT(axis));
|
113
|
+
if (!name) {
|
114
|
+
SDL_SetError("Unknown axis %d", NUM2INT(axis));
|
115
|
+
SDL_ERROR();
|
116
|
+
}
|
117
|
+
return utf8str_new_cstr(name);
|
118
|
+
}
|
119
|
+
|
120
|
+
/*
|
121
|
+
* @overload button_name_of(button)
|
122
|
+
*
|
123
|
+
* Get a string representing **button**.
|
124
|
+
*
|
125
|
+
* @param button [Integer] button constant in {Button}
|
126
|
+
* @return [String]
|
127
|
+
*/
|
128
|
+
static VALUE GameController_s_button_name_of(VALUE self, VALUE button)
|
129
|
+
{
|
130
|
+
const char* name = SDL_GameControllerGetStringForButton(NUM2INT(button));
|
131
|
+
if (!name) {
|
132
|
+
SDL_SetError("Unknown axis %d", NUM2INT(button));
|
133
|
+
SDL_ERROR();
|
134
|
+
}
|
135
|
+
return utf8str_new_cstr(name);
|
136
|
+
}
|
137
|
+
|
138
|
+
/*
|
139
|
+
* @overload axis_from_name(name)
|
140
|
+
*
|
141
|
+
* Get a integer representation of the axis
|
142
|
+
* whose string representation is **name**.
|
143
|
+
*
|
144
|
+
* The return value is the value of one of the constants in {Axis}
|
145
|
+
*
|
146
|
+
* @param name [String] a string representing an axis
|
147
|
+
* @return [Integer]
|
148
|
+
*/
|
149
|
+
static VALUE GameController_s_axis_from_name(VALUE self, VALUE name)
|
150
|
+
{
|
151
|
+
int axis = SDL_GameControllerGetAxisFromString(StringValueCStr(name));
|
152
|
+
if (axis < 0) {
|
153
|
+
SDL_SetError("Unknown axis name \"%s\"", StringValueCStr(name));
|
154
|
+
SDL_ERROR();
|
155
|
+
}
|
156
|
+
return INT2FIX(axis);
|
157
|
+
}
|
158
|
+
|
159
|
+
/*
|
160
|
+
* @overload button_from_name(name)
|
161
|
+
*
|
162
|
+
* Get a integer representation of the button
|
163
|
+
* whose string representation is **name**.
|
164
|
+
*
|
165
|
+
* The return value is the value of one of the constants in {Button}
|
166
|
+
*
|
167
|
+
* @param name [String] a string representing a button
|
168
|
+
* @return [Integer]
|
169
|
+
*/
|
170
|
+
static VALUE GameController_s_button_from_name(VALUE self, VALUE name)
|
171
|
+
{
|
172
|
+
int button = SDL_GameControllerGetButtonFromString(StringValueCStr(name));
|
173
|
+
if (button < 0) {
|
174
|
+
SDL_SetError("Unknown button name \"%s\"", StringValueCStr(name));
|
175
|
+
SDL_ERROR();
|
176
|
+
}
|
177
|
+
return INT2FIX(button);
|
178
|
+
}
|
179
|
+
|
180
|
+
/*
|
181
|
+
* Get the implementation dependent name for the game controllers
|
182
|
+
* connected to the machine.
|
183
|
+
*
|
184
|
+
* The number of elements of returning array is same as
|
185
|
+
* {Joystick.num_connected_joysticks}.
|
186
|
+
*
|
187
|
+
* @return [Array<String,nil>]
|
188
|
+
*/
|
189
|
+
static VALUE GameController_s_device_names(VALUE self)
|
190
|
+
{
|
191
|
+
int num_joysticks = SDL_NumJoysticks();
|
192
|
+
int i;
|
193
|
+
VALUE device_names = rb_ary_new2(num_joysticks);
|
194
|
+
for (i=0; i<num_joysticks; ++i) {
|
195
|
+
const char* name = SDL_GameControllerNameForIndex(i);
|
196
|
+
if (name)
|
197
|
+
rb_ary_push(device_names, utf8str_new_cstr(name));
|
198
|
+
else
|
199
|
+
rb_ary_push(device_names, Qnil);
|
200
|
+
}
|
201
|
+
return device_names;
|
202
|
+
}
|
203
|
+
|
204
|
+
/*
|
205
|
+
* @overload mapping_for(guid_string)
|
206
|
+
* Get the game controller mapping string for a given GUID.
|
207
|
+
*
|
208
|
+
* @param guid_string [String] GUID string
|
209
|
+
*
|
210
|
+
* @return [String]
|
211
|
+
*/
|
212
|
+
static VALUE GameController_s_mapping_for(VALUE self, VALUE guid_string)
|
213
|
+
{
|
214
|
+
SDL_JoystickGUID guid = SDL_JoystickGetGUIDFromString(StringValueCStr(guid_string));
|
215
|
+
return utf8str_new_cstr(SDL_GameControllerMappingForGUID(guid));
|
216
|
+
}
|
217
|
+
|
218
|
+
/*
|
219
|
+
* @overload open(index)
|
220
|
+
* Open a game controller and return the opened GameController object.
|
221
|
+
*
|
222
|
+
* @param index [Integer] device index, up to the return value of {Joystick.num_connected_joysticks}.
|
223
|
+
* @return [SDL2::GameController]
|
224
|
+
*
|
225
|
+
* @raise [SDL2::Error] raised when device open is failed.
|
226
|
+
* For exmaple, **index** is out of range.
|
227
|
+
*/
|
228
|
+
static VALUE GameController_s_open(VALUE self, VALUE index)
|
229
|
+
{
|
230
|
+
SDL_GameController* controller = SDL_GameControllerOpen(NUM2INT(index));
|
231
|
+
if (!controller)
|
232
|
+
SDL_ERROR();
|
233
|
+
return GameController_new(controller);
|
234
|
+
}
|
235
|
+
|
236
|
+
/*
|
237
|
+
* Get the name of an opened game controller.
|
238
|
+
*
|
239
|
+
* @return [String]
|
240
|
+
*/
|
241
|
+
static VALUE GameController_name(VALUE self)
|
242
|
+
{
|
243
|
+
const char* name = SDL_GameControllerName(Get_SDL_GameController(self));
|
244
|
+
if (!name)
|
245
|
+
SDL_ERROR();
|
246
|
+
|
247
|
+
return utf8str_new_cstr(name);
|
248
|
+
}
|
249
|
+
|
250
|
+
/*
|
251
|
+
* Return true if **self** is opened and attached.
|
252
|
+
*/
|
253
|
+
static VALUE GameController_attached_p(VALUE self)
|
254
|
+
{
|
255
|
+
return INT2BOOL(SDL_GameControllerGetAttached(Get_SDL_GameController(self)));
|
256
|
+
}
|
257
|
+
|
258
|
+
/*
|
259
|
+
* Close the game controller.
|
260
|
+
*
|
261
|
+
* @return nil
|
262
|
+
*/
|
263
|
+
static VALUE GameController_destroy(VALUE self)
|
264
|
+
{
|
265
|
+
GameController* g = Get_GameController(self);
|
266
|
+
if (g->controller)
|
267
|
+
SDL_GameControllerClose(g->controller);
|
268
|
+
g->controller = NULL;
|
269
|
+
return Qnil;
|
270
|
+
}
|
271
|
+
|
272
|
+
/*
|
273
|
+
* Get the mapping string of **self**.
|
274
|
+
*
|
275
|
+
* @return [String]
|
276
|
+
* @see .add_mapping
|
277
|
+
* @see .mapping_for
|
278
|
+
*/
|
279
|
+
static VALUE GameController_mapping(VALUE self)
|
280
|
+
{
|
281
|
+
return utf8str_new_cstr(SDL_GameControllerMapping(Get_SDL_GameController(self)));
|
282
|
+
}
|
283
|
+
|
284
|
+
/*
|
285
|
+
* @overload axis(axis)
|
286
|
+
* Get the state of an axis control.
|
287
|
+
*
|
288
|
+
* The state is an integer from -32768 to 32767.
|
289
|
+
* The state of trigger never returns negative value (from 0 to 32767).
|
290
|
+
*
|
291
|
+
* @param axis [Integer] the index of an axis, one of the constants in {Axis}
|
292
|
+
* @return [Integer]
|
293
|
+
*/
|
294
|
+
static VALUE GameController_axis(VALUE self, VALUE axis)
|
295
|
+
{
|
296
|
+
return INT2FIX(SDL_GameControllerGetAxis(Get_SDL_GameController(self),
|
297
|
+
NUM2INT(axis)));
|
298
|
+
}
|
299
|
+
|
300
|
+
/*
|
301
|
+
* @overload button_pressed?(button)
|
302
|
+
* Return true if a button is pressed.
|
303
|
+
*
|
304
|
+
* @param button [Integer] the index of a button, one of the constants in {Button}
|
305
|
+
*/
|
306
|
+
static VALUE GameController_button_pressed_p(VALUE self, VALUE button)
|
307
|
+
{
|
308
|
+
return INT2BOOL(SDL_GameControllerGetButton(Get_SDL_GameController(self),
|
309
|
+
NUM2INT(button)));
|
310
|
+
}
|
311
|
+
|
312
|
+
/*
|
313
|
+
* Document-module: SDL2::GameController::Axis
|
314
|
+
*
|
315
|
+
* This module provides constants of gamecontroller's axis indices used by
|
316
|
+
* {SDL2::GameController} class.
|
317
|
+
*/
|
318
|
+
|
319
|
+
/*
|
320
|
+
* Document-module: SDL2::GameController::Button
|
321
|
+
*
|
322
|
+
* This module provides constants of gamecontroller's button indices used by
|
323
|
+
* {SDL2::GameController} class.
|
324
|
+
*/
|
325
|
+
void rubysdl2_init_gamecontorller(void)
|
326
|
+
{
|
327
|
+
cGameController = rb_define_class_under(mSDL2, "GameController", rb_cObject);
|
328
|
+
|
329
|
+
rb_define_singleton_method(cGameController, "add_mapping",
|
330
|
+
GameController_s_add_mapping, 1);
|
331
|
+
rb_define_singleton_method(cGameController, "device_names",
|
332
|
+
GameController_s_device_names, 0);
|
333
|
+
rb_define_singleton_method(cGameController, "axis_name_of",
|
334
|
+
GameController_s_axis_name_of, 1);
|
335
|
+
rb_define_singleton_method(cGameController, "button_name_of",
|
336
|
+
GameController_s_button_name_of, 1);
|
337
|
+
rb_define_singleton_method(cGameController, "mapping_for",
|
338
|
+
GameController_s_mapping_for, 1);
|
339
|
+
rb_define_singleton_method(cGameController, "button_from_name",
|
340
|
+
GameController_s_button_from_name, 1);
|
341
|
+
rb_define_singleton_method(cGameController, "axis_from_name",
|
342
|
+
GameController_s_axis_from_name, 1);
|
343
|
+
rb_define_singleton_method(cGameController, "open", GameController_s_open, 1);
|
344
|
+
rb_define_method(cGameController, "destroy?", GameController_destroy_p, 0);
|
345
|
+
rb_define_method(cGameController, "destroy", GameController_destroy, 0);
|
346
|
+
rb_define_method(cGameController, "name", GameController_name, 0);
|
347
|
+
rb_define_method(cGameController, "attached?", GameController_attached_p, 0);
|
348
|
+
rb_define_method(cGameController, "mapping", GameController_mapping, 0);
|
349
|
+
rb_define_method(cGameController, "axis", GameController_axis, 1);
|
350
|
+
rb_define_method(cGameController, "button_pressed?", GameController_button_pressed_p, 1);
|
351
|
+
|
352
|
+
mAxis = rb_define_module_under(cGameController, "Axis");
|
353
|
+
mButton = rb_define_module_under(cGameController, "Button");
|
354
|
+
|
355
|
+
/* define(`DEFINE_CONTROLLER_AXIS_CONST',`rb_define_const(mAxis, "$1", INT2NUM(SDL_CONTROLLER_AXIS_$1))') */
|
356
|
+
/* Invalid axis index */
|
357
|
+
DEFINE_CONTROLLER_AXIS_CONST(INVALID);
|
358
|
+
/* Left X axis */
|
359
|
+
DEFINE_CONTROLLER_AXIS_CONST(LEFTX);
|
360
|
+
/* Left Y axis */
|
361
|
+
DEFINE_CONTROLLER_AXIS_CONST(LEFTY);
|
362
|
+
/* Right X axis */
|
363
|
+
DEFINE_CONTROLLER_AXIS_CONST(RIGHTX);
|
364
|
+
/* Right Y axis */
|
365
|
+
DEFINE_CONTROLLER_AXIS_CONST(RIGHTY);
|
366
|
+
/* Left trigger axis */
|
367
|
+
DEFINE_CONTROLLER_AXIS_CONST(TRIGGERLEFT);
|
368
|
+
/* Right trigger axis */
|
369
|
+
DEFINE_CONTROLLER_AXIS_CONST(TRIGGERRIGHT);
|
370
|
+
/* The max of an axis index */
|
371
|
+
DEFINE_CONTROLLER_AXIS_CONST(MAX);
|
372
|
+
|
373
|
+
/* define(`DEFINE_CONTROLLER_BUTTON_CONST',`rb_define_const(mButton, "$1", INT2NUM(SDL_CONTROLLER_BUTTON_$1))') */
|
374
|
+
/* Invalid button index */
|
375
|
+
DEFINE_CONTROLLER_BUTTON_CONST(INVALID);
|
376
|
+
/* Button A */
|
377
|
+
DEFINE_CONTROLLER_BUTTON_CONST(A);
|
378
|
+
/* Button B */
|
379
|
+
DEFINE_CONTROLLER_BUTTON_CONST(B);
|
380
|
+
/* Button X */
|
381
|
+
DEFINE_CONTROLLER_BUTTON_CONST(X);
|
382
|
+
/* Button Y */
|
383
|
+
DEFINE_CONTROLLER_BUTTON_CONST(Y);
|
384
|
+
/* Back Button */
|
385
|
+
DEFINE_CONTROLLER_BUTTON_CONST(BACK);
|
386
|
+
/* Guide Button */
|
387
|
+
DEFINE_CONTROLLER_BUTTON_CONST(GUIDE);
|
388
|
+
/* Start Button */
|
389
|
+
DEFINE_CONTROLLER_BUTTON_CONST(START);
|
390
|
+
/* Left stick Button */
|
391
|
+
DEFINE_CONTROLLER_BUTTON_CONST(LEFTSTICK);
|
392
|
+
/* Right stick Button */
|
393
|
+
DEFINE_CONTROLLER_BUTTON_CONST(RIGHTSTICK);
|
394
|
+
/* Left shoulder Button */
|
395
|
+
DEFINE_CONTROLLER_BUTTON_CONST(LEFTSHOULDER);
|
396
|
+
/* Right shoulder Button */
|
397
|
+
DEFINE_CONTROLLER_BUTTON_CONST(RIGHTSHOULDER);
|
398
|
+
/* D-pad UP Button */
|
399
|
+
DEFINE_CONTROLLER_BUTTON_CONST(DPAD_UP);
|
400
|
+
/* D-pad DOWN Button */
|
401
|
+
DEFINE_CONTROLLER_BUTTON_CONST(DPAD_DOWN);
|
402
|
+
/* D-pad LEFT Button */
|
403
|
+
DEFINE_CONTROLLER_BUTTON_CONST(DPAD_LEFT);
|
404
|
+
/* D-pad RIGHT Button */
|
405
|
+
DEFINE_CONTROLLER_BUTTON_CONST(DPAD_RIGHT);
|
406
|
+
/* The max of a button index */
|
407
|
+
DEFINE_CONTROLLER_BUTTON_CONST(MAX);
|
408
|
+
}
|
data/ext/sdl2_ext/gl.c
ADDED
@@ -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
|
+
/* */
|
220
|
+
/* OpenGL attribute - minimal bits of red channel in color buffer, default is 3 */
|
221
|
+
rb_define_const(mGL, "RED_SIZE", INT2NUM(SDL_GL_RED_SIZE));
|
222
|
+
/* OpenGL attribute - minimal bits of green channel in color buffer, default is 3 */
|
223
|
+
rb_define_const(mGL, "GREEN_SIZE", INT2NUM(SDL_GL_GREEN_SIZE));
|
224
|
+
/* OpenGL attribute - minimal bits of blue channel in color buffer, default is 2 */
|
225
|
+
rb_define_const(mGL, "BLUE_SIZE", INT2NUM(SDL_GL_BLUE_SIZE));
|
226
|
+
/* OpenGL attribute - minimal bits of alpha channel in color buffer, default is 0 */
|
227
|
+
rb_define_const(mGL, "ALPHA_SIZE", INT2NUM(SDL_GL_ALPHA_SIZE));
|
228
|
+
/* OpenGL attribute - minimal bits of framebufer, default is 0 */
|
229
|
+
rb_define_const(mGL, "BUFFER_SIZE", INT2NUM(SDL_GL_BUFFER_SIZE));
|
230
|
+
/* OpenGL attribute - whether the single buffer (0) or double buffer (1), default
|
231
|
+
is double buffer */
|
232
|
+
rb_define_const(mGL, "DOUBLEBUFFER", INT2NUM(SDL_GL_DOUBLEBUFFER));
|
233
|
+
/* OpenGL attribute - bits of depth buffer, default is 16 */
|
234
|
+
rb_define_const(mGL, "DEPTH_SIZE", INT2NUM(SDL_GL_DEPTH_SIZE));
|
235
|
+
/* OpenGL attribute - bits of stencil buffer, default is 0 */
|
236
|
+
rb_define_const(mGL, "STENCIL_SIZE", INT2NUM(SDL_GL_STENCIL_SIZE));
|
237
|
+
/* OpenGL attribute - minimal bits of red channel in accumlation buffer,
|
238
|
+
default is 0 */
|
239
|
+
rb_define_const(mGL, "ACCUM_RED_SIZE", INT2NUM(SDL_GL_ACCUM_RED_SIZE));
|
240
|
+
/* OpenGL attribute - minimal bits of green channel in accumlation buffer,
|
241
|
+
default is 0 */
|
242
|
+
rb_define_const(mGL, "ACCUM_GREEN_SIZE", INT2NUM(SDL_GL_ACCUM_GREEN_SIZE));
|
243
|
+
/* OpenGL attribute - minimal bits of blue channel in accumlation buffer,
|
244
|
+
default is 0 */
|
245
|
+
rb_define_const(mGL, "ACCUM_BLUE_SIZE", INT2NUM(SDL_GL_ACCUM_BLUE_SIZE));
|
246
|
+
/* OpenGL attribute - minimal bits of alpha channel in accumlation buffer,
|
247
|
+
default is 0 */
|
248
|
+
rb_define_const(mGL, "ACCUM_ALPHA_SIZE", INT2NUM(SDL_GL_ACCUM_ALPHA_SIZE));
|
249
|
+
/* OpenGL attribute - whether output is stereo (1) or not (0), default is 0 */
|
250
|
+
rb_define_const(mGL, "STEREO", INT2NUM(SDL_GL_STEREO));
|
251
|
+
/* OpenGL attribuite - the number of buffers used for multisampe anti-aliasing,
|
252
|
+
default is 0 */
|
253
|
+
rb_define_const(mGL, "MULTISAMPLEBUFFERS", INT2NUM(SDL_GL_MULTISAMPLEBUFFERS));
|
254
|
+
/* OpenGL attribute - the number of samples used around the current pixel
|
255
|
+
use for multisample anti-aliasing, default is 0 */
|
256
|
+
rb_define_const(mGL, "MULTISAMPLESAMPLES", INT2NUM(SDL_GL_MULTISAMPLESAMPLES));
|
257
|
+
/* OpenGL attribute - 1 for requiring hardware acceleration, 0 for software rendering,
|
258
|
+
default is allowing either */
|
259
|
+
rb_define_const(mGL, "ACCELERATED_VISUAL", INT2NUM(SDL_GL_ACCELERATED_VISUAL));
|
260
|
+
/* OpenGL attribute - not used (deprecated) */
|
261
|
+
rb_define_const(mGL, "RETAINED_BACKING", INT2NUM(SDL_GL_RETAINED_BACKING));
|
262
|
+
/* OpenGL attribute - OpenGL context major version */
|
263
|
+
rb_define_const(mGL, "CONTEXT_MAJOR_VERSION", INT2NUM(SDL_GL_CONTEXT_MAJOR_VERSION));
|
264
|
+
/* OpenGL attribute - OpenGL context minor version */
|
265
|
+
rb_define_const(mGL, "CONTEXT_MINOR_VERSION", INT2NUM(SDL_GL_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
|
+
rb_define_const(mGL, "CONTEXT_FLAGS", INT2NUM(SDL_GL_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
|
+
rb_define_const(mGL, "CONTEXT_PROFILE_MASK", INT2NUM(SDL_GL_CONTEXT_PROFILE_MASK));
|
295
|
+
/* OpenGL attribute - OpenGL context sharing, default is 0 */
|
296
|
+
rb_define_const(mGL, "SHARE_WITH_CURRENT_CONTEXT", INT2NUM(SDL_GL_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
|
+
rb_define_const(mGL, "FRAMEBUFFER_SRGB_CAPABLE", INT2NUM(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE));
|
300
|
+
#endif
|
301
|
+
/* OpenGL attribute - not used (deprecated) */
|
302
|
+
rb_define_const(mGL, "CONTEXT_EGL", INT2NUM(SDL_GL_CONTEXT_EGL));
|
303
|
+
|
304
|
+
/* */
|
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
|
+
rb_define_const(mGL, "CONTEXT_DEBUG_FLAG", INT2NUM(SDL_GL_CONTEXT_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
|
+
rb_define_const(mGL, "CONTEXT_FORWARD_COMPATIBLE_FLAG", INT2NUM(SDL_GL_CONTEXT_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
|
+
rb_define_const(mGL, "CONTEXT_ROBUST_ACCESS_FLAG", INT2NUM(SDL_GL_CONTEXT_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
|
+
rb_define_const(mGL, "CONTEXT_RESET_ISOLATION_FLAG", INT2NUM(SDL_GL_CONTEXT_RESET_ISOLATION_FLAG));
|
333
|
+
|
334
|
+
/*
|
335
|
+
* OpenGL core profile - deprecated
|
336
|
+
* functions are disabled
|
337
|
+
*/
|
338
|
+
rb_define_const(mGL, "CONTEXT_PROFILE_CORE", INT2NUM(SDL_GL_CONTEXT_PROFILE_CORE));
|
339
|
+
/*
|
340
|
+
* OpenGL compatibility profile -
|
341
|
+
* deprecated functions are allowed
|
342
|
+
*/
|
343
|
+
rb_define_const(mGL, "CONTEXT_PROFILE_COMPATIBILITY", INT2NUM(SDL_GL_CONTEXT_PROFILE_COMPATIBILITY));
|
344
|
+
/*
|
345
|
+
* OpenGL ES profile - only a subset of the
|
346
|
+
* base OpenGL functionality is available
|
347
|
+
*/
|
348
|
+
rb_define_const(mGL, "CONTEXT_PROFILE_ES", INT2NUM(SDL_GL_CONTEXT_PROFILE_ES));
|
349
|
+
|
350
|
+
rb_gc_register_address(¤t_context);
|
351
|
+
}
|