ruby2d 0.9.0 → 0.9.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/assets/README.md +8 -8
- data/assets/Rakefile +85 -0
- data/assets/include/SDL2/SDL.h +1 -1
- data/assets/include/SDL2/SDL_assert.h +1 -1
- data/assets/include/SDL2/SDL_atomic.h +20 -2
- data/assets/include/SDL2/SDL_audio.h +47 -14
- data/assets/include/SDL2/SDL_bits.h +10 -1
- data/assets/include/SDL2/SDL_blendmode.h +7 -7
- data/assets/include/SDL2/SDL_clipboard.h +1 -1
- data/assets/include/SDL2/SDL_config.h +24 -390
- data/assets/include/SDL2/SDL_config_android.h +178 -0
- data/assets/include/SDL2/SDL_config_iphoneos.h +201 -0
- data/assets/include/SDL2/SDL_config_macosx.h +240 -0
- data/assets/include/SDL2/SDL_config_minimal.h +85 -0
- data/assets/include/SDL2/SDL_config_os2.h +170 -0
- data/assets/include/SDL2/SDL_config_pandora.h +133 -0
- data/assets/include/SDL2/SDL_config_psp.h +164 -0
- data/assets/include/SDL2/SDL_config_windows.h +257 -0
- data/assets/include/SDL2/SDL_config_winrt.h +240 -0
- data/assets/include/SDL2/SDL_config_wiz.h +146 -0
- data/assets/include/SDL2/SDL_copying.h +20 -0
- data/assets/include/SDL2/SDL_cpuinfo.h +90 -8
- data/assets/include/SDL2/SDL_egl.h +1 -1
- data/assets/include/SDL2/SDL_endian.h +1 -1
- data/assets/include/SDL2/SDL_error.h +1 -1
- data/assets/include/SDL2/SDL_events.h +4 -1
- data/assets/include/SDL2/SDL_filesystem.h +1 -1
- data/assets/include/SDL2/SDL_gamecontroller.h +1 -1
- data/assets/include/SDL2/SDL_gesture.h +1 -1
- data/assets/include/SDL2/SDL_haptic.h +1 -1
- data/assets/include/SDL2/SDL_hints.h +145 -14
- data/assets/include/SDL2/SDL_image.h +2 -2
- data/assets/include/SDL2/SDL_joystick.h +1 -1
- data/assets/include/SDL2/SDL_keyboard.h +1 -1
- data/assets/include/SDL2/SDL_keycode.h +1 -1
- data/assets/include/SDL2/SDL_loadso.h +1 -1
- data/assets/include/SDL2/SDL_log.h +1 -1
- data/assets/include/SDL2/SDL_main.h +28 -16
- data/assets/include/SDL2/SDL_messagebox.h +1 -1
- data/assets/include/SDL2/SDL_mouse.h +1 -1
- data/assets/include/SDL2/SDL_mutex.h +1 -1
- data/assets/include/SDL2/SDL_name.h +1 -1
- data/assets/include/SDL2/SDL_opengl.h +1 -1
- data/assets/include/SDL2/SDL_opengles.h +1 -1
- data/assets/include/SDL2/SDL_opengles2.h +1 -1
- data/assets/include/SDL2/SDL_pixels.h +3 -3
- data/assets/include/SDL2/SDL_platform.h +1 -1
- data/assets/include/SDL2/SDL_power.h +1 -1
- data/assets/include/SDL2/SDL_quit.h +1 -1
- data/assets/include/SDL2/SDL_rect.h +29 -3
- data/assets/include/SDL2/SDL_render.h +168 -1
- data/assets/include/SDL2/SDL_revision.h +2 -2
- data/assets/include/SDL2/SDL_rwops.h +50 -13
- data/assets/include/SDL2/SDL_scancode.h +2 -2
- data/assets/include/SDL2/SDL_sensor.h +4 -4
- data/assets/include/SDL2/SDL_shape.h +1 -1
- data/assets/include/SDL2/SDL_stdinc.h +1 -1
- data/assets/include/SDL2/SDL_surface.h +2 -1
- data/assets/include/SDL2/SDL_system.h +1 -1
- data/assets/include/SDL2/SDL_syswm.h +11 -8
- data/assets/include/SDL2/SDL_test.h +1 -1
- data/assets/include/SDL2/SDL_test_assert.h +1 -1
- data/assets/include/SDL2/SDL_test_common.h +22 -5
- data/assets/include/SDL2/SDL_test_compare.h +1 -1
- data/assets/include/SDL2/SDL_test_crc32.h +1 -1
- data/assets/include/SDL2/SDL_test_font.h +1 -1
- data/assets/include/SDL2/SDL_test_fuzzer.h +1 -1
- data/assets/include/SDL2/SDL_test_harness.h +1 -1
- data/assets/include/SDL2/SDL_test_images.h +1 -1
- data/assets/include/SDL2/SDL_test_log.h +1 -1
- data/assets/include/SDL2/SDL_test_md5.h +1 -1
- data/assets/include/SDL2/SDL_test_memory.h +3 -3
- data/assets/include/SDL2/SDL_test_random.h +1 -1
- data/assets/include/SDL2/SDL_thread.h +1 -1
- data/assets/include/SDL2/SDL_timer.h +1 -1
- data/assets/include/SDL2/SDL_touch.h +17 -1
- data/assets/include/SDL2/SDL_types.h +1 -1
- data/assets/include/SDL2/SDL_version.h +2 -2
- data/assets/include/SDL2/SDL_video.h +1 -1
- data/assets/include/SDL2/SDL_vulkan.h +3 -3
- data/assets/include/SDL2/begin_code.h +4 -1
- data/assets/include/SDL2/close_code.h +4 -1
- data/assets/macos/lib/libFLAC.a +0 -0
- data/assets/macos/lib/libSDL2.a +0 -0
- data/assets/macos/lib/libSDL2_image.a +0 -0
- data/assets/macos/lib/libSDL2_mixer.a +0 -0
- data/assets/macos/lib/libSDL2_ttf.a +0 -0
- data/assets/macos/lib/libfreetype.a +0 -0
- data/assets/macos/lib/libjpeg.a +0 -0
- data/assets/macos/lib/libmpg123.a +0 -0
- data/assets/macos/lib/libogg.a +0 -0
- data/assets/macos/lib/libpng16.a +0 -0
- data/assets/macos/lib/libtiff.a +0 -0
- data/assets/macos/lib/libwebp.a +0 -0
- data/assets/mingw/bin/SDL2.dll +0 -0
- data/assets/mingw/bin/SDL2_image.dll +0 -0
- data/assets/mingw/bin/libpng16-16.dll +0 -0
- data/assets/mingw/bin/libtiff-5.dll +0 -0
- data/assets/mingw/bin/libwebp-7.dll +0 -0
- data/assets/mingw/lib/libSDL2.a +0 -0
- data/assets/mingw/lib/libSDL2.dll.a +0 -0
- data/assets/mingw/lib/libSDL2_image.a +0 -0
- data/assets/mingw/lib/libSDL2_image.dll.a +0 -0
- data/assets/mingw/lib/libSDL2_test.a +0 -0
- data/assets/mingw/lib/libSDL2main.a +0 -0
- data/bin/ruby2d +2 -1
- data/ext/ruby2d/common.c +185 -0
- data/ext/ruby2d/controllers.c +110 -0
- data/ext/ruby2d/extconf.rb +97 -46
- data/ext/ruby2d/gl.c +430 -0
- data/ext/ruby2d/gl2.c +146 -0
- data/ext/ruby2d/gl3.c +348 -0
- data/ext/ruby2d/gles.c +308 -0
- data/ext/ruby2d/image.c +138 -0
- data/ext/ruby2d/input.c +48 -0
- data/ext/ruby2d/music.c +114 -0
- data/ext/ruby2d/ruby2d.c +125 -126
- data/{assets/include/simple2d.h → ext/ruby2d/ruby2d.h} +223 -201
- data/ext/ruby2d/shapes.c +154 -0
- data/ext/ruby2d/sound.c +93 -0
- data/ext/ruby2d/sprite.c +147 -0
- data/ext/ruby2d/text.c +129 -0
- data/ext/ruby2d/window.c +414 -0
- data/lib/ruby2d.rb +1 -1
- data/lib/ruby2d/cli/build.rb +3 -8
- data/lib/ruby2d/cli/colorize.rb +10 -0
- data/lib/ruby2d/version.rb +1 -1
- metadata +34 -10
- data/assets/macos/lib/libsimple2d.a +0 -0
- data/assets/mingw/lib/libsimple2d.a +0 -0
- data/lib/ruby2d/colorize.rb +0 -10
@@ -1,6 +1,6 @@
|
|
1
1
|
/*
|
2
2
|
Simple DirectMedia Layer
|
3
|
-
Copyright (C) 1997-
|
3
|
+
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
|
4
4
|
|
5
5
|
This software is provided 'as-is', without any express or implied
|
6
6
|
warranty. In no event will the authors be held liable for any damages
|
@@ -1,6 +1,6 @@
|
|
1
1
|
/*
|
2
2
|
Simple DirectMedia Layer
|
3
|
-
Copyright (C) 1997-
|
3
|
+
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
|
4
4
|
|
5
5
|
This software is provided 'as-is', without any express or implied
|
6
6
|
warranty. In no event will the authors be held liable for any damages
|
@@ -59,7 +59,7 @@ typedef struct SDL_version
|
|
59
59
|
*/
|
60
60
|
#define SDL_MAJOR_VERSION 2
|
61
61
|
#define SDL_MINOR_VERSION 0
|
62
|
-
#define SDL_PATCHLEVEL
|
62
|
+
#define SDL_PATCHLEVEL 10
|
63
63
|
|
64
64
|
/**
|
65
65
|
* \brief Macro to determine SDL version program was compiled against.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
/*
|
2
2
|
Simple DirectMedia Layer
|
3
|
-
Copyright (C) 1997-
|
3
|
+
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
|
4
4
|
|
5
5
|
This software is provided 'as-is', without any express or implied
|
6
6
|
warranty. In no event will the authors be held liable for any damages
|
@@ -98,8 +98,8 @@ typedef VkSurfaceKHR SDL_vulkanSurface; /* for compatibility with Tizen */
|
|
98
98
|
* applications to link with libvulkan (and historically MoltenVK was
|
99
99
|
* provided as a static library). If it is not found then, on macOS, SDL
|
100
100
|
* will attempt to load \c vulkan.framework/vulkan, \c libvulkan.1.dylib,
|
101
|
-
*
|
102
|
-
* On iOS SDL will attempt to load \c
|
101
|
+
* followed by \c libvulkan.dylib, in that order.
|
102
|
+
* On iOS SDL will attempt to load \c libvulkan.dylib only. Applications
|
103
103
|
* using a dynamic framework or .dylib must ensure it is included in its
|
104
104
|
* application bundle.
|
105
105
|
*
|
@@ -153,7 +153,7 @@ extern DECLSPEC void SDLCALL SDL_Vulkan_UnloadLibrary(void);
|
|
153
153
|
* is smaller than the number of required extensions, \c SDL_FALSE will be
|
154
154
|
* returned instead of \c SDL_TRUE, to indicate that not all the required
|
155
155
|
* extensions were returned.
|
156
|
-
*
|
156
|
+
*
|
157
157
|
* \note If \c window is not NULL, it will be checked against its creation
|
158
158
|
* flags to ensure that the Vulkan flag is present. This parameter
|
159
159
|
* will be removed in a future major release.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
/*
|
2
2
|
Simple DirectMedia Layer
|
3
|
-
Copyright (C) 1997-
|
3
|
+
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
|
4
4
|
|
5
5
|
This software is provided 'as-is', without any express or implied
|
6
6
|
warranty. In no event will the authors be held liable for any damages
|
@@ -105,6 +105,9 @@
|
|
105
105
|
#ifdef _MSC_VER
|
106
106
|
#pragma warning(disable: 4103)
|
107
107
|
#endif
|
108
|
+
#ifdef __clang__
|
109
|
+
#pragma clang diagnostic ignored "-Wpragma-pack"
|
110
|
+
#endif
|
108
111
|
#ifdef __BORLANDC__
|
109
112
|
#pragma nopackwarning
|
110
113
|
#endif
|
@@ -1,6 +1,6 @@
|
|
1
1
|
/*
|
2
2
|
Simple DirectMedia Layer
|
3
|
-
Copyright (C) 1997-
|
3
|
+
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
|
4
4
|
|
5
5
|
This software is provided 'as-is', without any express or implied
|
6
6
|
warranty. In no event will the authors be held liable for any damages
|
@@ -26,6 +26,9 @@
|
|
26
26
|
* after you finish any function and structure declarations in your headers
|
27
27
|
*/
|
28
28
|
|
29
|
+
#ifndef _begin_code_h
|
30
|
+
#error close_code.h included without matching begin_code.h
|
31
|
+
#endif
|
29
32
|
#undef _begin_code_h
|
30
33
|
|
31
34
|
/* Reset structure packing at previous byte alignment */
|
data/assets/macos/lib/libFLAC.a
CHANGED
Binary file
|
data/assets/macos/lib/libSDL2.a
CHANGED
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/assets/macos/lib/libjpeg.a
CHANGED
Binary file
|
Binary file
|
data/assets/macos/lib/libogg.a
CHANGED
Binary file
|
data/assets/macos/lib/libpng16.a
CHANGED
Binary file
|
data/assets/macos/lib/libtiff.a
CHANGED
Binary file
|
data/assets/macos/lib/libwebp.a
CHANGED
Binary file
|
data/assets/mingw/bin/SDL2.dll
CHANGED
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/assets/mingw/lib/libSDL2.a
CHANGED
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/bin/ruby2d
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require 'ruby2d/colorize'
|
3
|
+
require 'ruby2d/cli/colorize'
|
4
4
|
require 'ruby2d/version'
|
5
5
|
|
6
6
|
# Debugging command-line flag
|
@@ -115,6 +115,7 @@ when 'launch'
|
|
115
115
|
else
|
116
116
|
puts usage_launch
|
117
117
|
end
|
118
|
+
# TODO: Need add this functionality to the gem
|
118
119
|
when 'simulator'
|
119
120
|
case ARGV[1]
|
120
121
|
when '--list'
|
data/ext/ruby2d/common.c
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
// Ruby 2D Shared functions and data
|
2
|
+
|
3
|
+
#include "ruby2d.h"
|
4
|
+
|
5
|
+
// Initalize shared data
|
6
|
+
bool R2D_diagnostics = false;
|
7
|
+
|
8
|
+
// Initialization status
|
9
|
+
static bool initted = false;
|
10
|
+
|
11
|
+
|
12
|
+
/*
|
13
|
+
* Provide a `vasprintf()` implementation for Windows
|
14
|
+
*/
|
15
|
+
#if WINDOWS && !MINGW
|
16
|
+
int vasprintf(char **strp, const char *fmt, va_list ap) {
|
17
|
+
int r = -1, size = _vscprintf(fmt, ap);
|
18
|
+
if ((size >= 0) && (size < INT_MAX)) {
|
19
|
+
*strp = (char *)malloc(size + 1);
|
20
|
+
if (*strp) {
|
21
|
+
r = vsnprintf(*strp, size + 1, fmt, ap);
|
22
|
+
if (r == -1) free(*strp);
|
23
|
+
}
|
24
|
+
} else { *strp = 0; }
|
25
|
+
return(r);
|
26
|
+
}
|
27
|
+
#endif
|
28
|
+
|
29
|
+
|
30
|
+
/*
|
31
|
+
* Checks if a file exists and can be accessed
|
32
|
+
*/
|
33
|
+
bool R2D_FileExists(const char *path) {
|
34
|
+
if (!path) return false;
|
35
|
+
|
36
|
+
if (access(path, F_OK) != -1) {
|
37
|
+
return true;
|
38
|
+
} else {
|
39
|
+
return false;
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
|
44
|
+
/*
|
45
|
+
* Logs standard messages to the console
|
46
|
+
*/
|
47
|
+
void R2D_Log(int type, const char *msg, ...) {
|
48
|
+
|
49
|
+
// Always log if diagnostics set, or if a warning or error message
|
50
|
+
if (R2D_diagnostics || type != R2D_INFO) {
|
51
|
+
|
52
|
+
va_list args;
|
53
|
+
va_start(args, msg);
|
54
|
+
|
55
|
+
switch (type) {
|
56
|
+
case R2D_INFO:
|
57
|
+
printf("\033[1;36mInfo:\033[0m ");
|
58
|
+
break;
|
59
|
+
case R2D_WARN:
|
60
|
+
printf("\033[1;33mWarning:\033[0m ");
|
61
|
+
break;
|
62
|
+
case R2D_ERROR:
|
63
|
+
printf("\033[1;31mError:\033[0m ");
|
64
|
+
break;
|
65
|
+
}
|
66
|
+
|
67
|
+
vprintf(msg, args);
|
68
|
+
printf("\n");
|
69
|
+
va_end(args);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
/*
|
75
|
+
* Logs Ruby 2D errors to the console, with caller and message body
|
76
|
+
*/
|
77
|
+
void R2D_Error(const char *caller, const char *msg, ...) {
|
78
|
+
va_list args;
|
79
|
+
va_start(args, msg);
|
80
|
+
char *fmsg;
|
81
|
+
vasprintf(&fmsg, msg, args);
|
82
|
+
R2D_Log(R2D_ERROR, "(%s) %s", caller, fmsg);
|
83
|
+
free(fmsg);
|
84
|
+
va_end(args);
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
/*
|
89
|
+
* Enable/disable logging of diagnostics
|
90
|
+
*/
|
91
|
+
void R2D_Diagnostics(bool status) {
|
92
|
+
R2D_diagnostics = status;
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
/*
|
97
|
+
* Enable terminal colors in Windows
|
98
|
+
*/
|
99
|
+
void R2D_Windows_EnableTerminalColors() {
|
100
|
+
#if WINDOWS
|
101
|
+
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
102
|
+
DWORD dwMode = 0;
|
103
|
+
GetConsoleMode(hOut, &dwMode);
|
104
|
+
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
105
|
+
SetConsoleMode(hOut, dwMode);
|
106
|
+
#endif
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
/*
|
111
|
+
* Initialize Ruby 2D subsystems
|
112
|
+
*/
|
113
|
+
bool R2D_Init() {
|
114
|
+
if (initted) return true;
|
115
|
+
|
116
|
+
// Enable terminal colors in Windows
|
117
|
+
R2D_Windows_EnableTerminalColors();
|
118
|
+
|
119
|
+
R2D_Log(R2D_INFO, "Initializing Ruby 2D");
|
120
|
+
|
121
|
+
// Initialize SDL
|
122
|
+
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
123
|
+
R2D_Error("SDL_Init", SDL_GetError());
|
124
|
+
return false;
|
125
|
+
}
|
126
|
+
|
127
|
+
// Initialize SDL_ttf
|
128
|
+
if (TTF_Init() != 0) {
|
129
|
+
R2D_Error("TTF_Init", TTF_GetError());
|
130
|
+
return false;
|
131
|
+
}
|
132
|
+
|
133
|
+
// Initialize SDL_mixer
|
134
|
+
int mix_flags = MIX_INIT_FLAC | MIX_INIT_OGG | MIX_INIT_MP3;
|
135
|
+
int mix_initted = Mix_Init(mix_flags);
|
136
|
+
|
137
|
+
// Bug in SDL2_mixer 2.0.2:
|
138
|
+
// Mix_Init should return OR'ed flags if successful, but returns 0 instead.
|
139
|
+
// Fixed in: https://hg.libsdl.org/SDL_mixer/rev/7fa15b556953
|
140
|
+
const SDL_version *linked_version = Mix_Linked_Version();
|
141
|
+
if (linked_version->major == 2 && linked_version->minor == 0 && linked_version->patch == 2) {
|
142
|
+
// It's version 2.0.2, don't check for Mix_Init errors
|
143
|
+
} else {
|
144
|
+
if ((mix_initted&mix_flags) != mix_flags) {
|
145
|
+
R2D_Error("Mix_Init", Mix_GetError());
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) != 0) {
|
150
|
+
R2D_Error("Mix_OpenAudio", Mix_GetError());
|
151
|
+
return false;
|
152
|
+
}
|
153
|
+
|
154
|
+
// Call `R2D_Quit` at program exit
|
155
|
+
atexit(R2D_Quit);
|
156
|
+
|
157
|
+
// All subsystems initted
|
158
|
+
initted = true;
|
159
|
+
return true;
|
160
|
+
}
|
161
|
+
|
162
|
+
|
163
|
+
/*
|
164
|
+
* Gets the primary display's dimensions
|
165
|
+
*/
|
166
|
+
void R2D_GetDisplayDimensions(int *w, int *h) {
|
167
|
+
R2D_Init();
|
168
|
+
SDL_DisplayMode dm;
|
169
|
+
SDL_GetCurrentDisplayMode(0, &dm);
|
170
|
+
*w = dm.w;
|
171
|
+
*h = dm.h;
|
172
|
+
}
|
173
|
+
|
174
|
+
|
175
|
+
/*
|
176
|
+
* Quits Ruby 2D subsystems
|
177
|
+
*/
|
178
|
+
void R2D_Quit() {
|
179
|
+
IMG_Quit();
|
180
|
+
Mix_CloseAudio();
|
181
|
+
Mix_Quit();
|
182
|
+
TTF_Quit();
|
183
|
+
SDL_Quit();
|
184
|
+
initted = false;
|
185
|
+
}
|
@@ -0,0 +1,110 @@
|
|
1
|
+
// controllers.c
|
2
|
+
|
3
|
+
#include "ruby2d.h"
|
4
|
+
|
5
|
+
// Stores the last joystick instance ID seen by the system. These instance IDs
|
6
|
+
// are unique and increment with each new joystick connected.
|
7
|
+
static int last_intance_id = -1;
|
8
|
+
|
9
|
+
|
10
|
+
/*
|
11
|
+
* Add controller mapping from string
|
12
|
+
*/
|
13
|
+
void R2D_AddControllerMapping(const char *map) {
|
14
|
+
int result = SDL_GameControllerAddMapping(map);
|
15
|
+
|
16
|
+
char guid[33];
|
17
|
+
strncpy(guid, map, 32);
|
18
|
+
|
19
|
+
switch (result) {
|
20
|
+
case 1:
|
21
|
+
R2D_Log(R2D_INFO, "Mapping added for GUID: %s", guid);
|
22
|
+
break;
|
23
|
+
case 0:
|
24
|
+
R2D_Log(R2D_INFO, "Mapping updated for GUID: %s", guid);
|
25
|
+
break;
|
26
|
+
case -1:
|
27
|
+
R2D_Error("SDL_GameControllerAddMapping", SDL_GetError());
|
28
|
+
break;
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
|
33
|
+
/*
|
34
|
+
* Add controller mappings from the specified file
|
35
|
+
*/
|
36
|
+
void R2D_AddControllerMappingsFromFile(const char *path) {
|
37
|
+
if (!R2D_FileExists(path)) {
|
38
|
+
R2D_Log(R2D_WARN, "Controller mappings file not found: %s", path);
|
39
|
+
return;
|
40
|
+
}
|
41
|
+
|
42
|
+
int mappings_added = SDL_GameControllerAddMappingsFromFile(path);
|
43
|
+
if (mappings_added == -1) {
|
44
|
+
R2D_Error("SDL_GameControllerAddMappingsFromFile", SDL_GetError());
|
45
|
+
} else {
|
46
|
+
R2D_Log(R2D_INFO, "Added %i controller mapping(s)", mappings_added);
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
|
51
|
+
/*
|
52
|
+
* Check if joystick is a controller
|
53
|
+
*/
|
54
|
+
bool R2D_IsController(SDL_JoystickID id) {
|
55
|
+
return SDL_GameControllerFromInstanceID(id) == NULL ? false : true;
|
56
|
+
}
|
57
|
+
|
58
|
+
|
59
|
+
/*
|
60
|
+
* Open controllers and joysticks
|
61
|
+
*/
|
62
|
+
void R2D_OpenControllers() {
|
63
|
+
|
64
|
+
char guid_str[33];
|
65
|
+
|
66
|
+
// Enumerate joysticks
|
67
|
+
for (int device_index = 0; device_index < SDL_NumJoysticks(); ++device_index) {
|
68
|
+
|
69
|
+
// Check if joystick supports SDL's game controller interface (a mapping is available)
|
70
|
+
if (SDL_IsGameController(device_index)) {
|
71
|
+
SDL_GameController *controller = SDL_GameControllerOpen(device_index);
|
72
|
+
SDL_JoystickID intance_id = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller));
|
73
|
+
|
74
|
+
SDL_JoystickGetGUIDString(
|
75
|
+
SDL_JoystickGetGUID(SDL_GameControllerGetJoystick(controller)),
|
76
|
+
guid_str, 33
|
77
|
+
);
|
78
|
+
|
79
|
+
if (intance_id > last_intance_id) {
|
80
|
+
if (controller) {
|
81
|
+
R2D_Log(R2D_INFO, "Controller #%i: %s\n GUID: %s", intance_id, SDL_GameControllerName(controller), guid_str);
|
82
|
+
} else {
|
83
|
+
R2D_Log(R2D_ERROR, "Could not open controller #%i: %s", intance_id, SDL_GetError());
|
84
|
+
}
|
85
|
+
last_intance_id = intance_id;
|
86
|
+
}
|
87
|
+
|
88
|
+
// Controller interface not supported, try to open as joystick
|
89
|
+
} else {
|
90
|
+
SDL_Joystick *joy = SDL_JoystickOpen(device_index);
|
91
|
+
SDL_JoystickID intance_id = SDL_JoystickInstanceID(joy);
|
92
|
+
|
93
|
+
if (!joy) {
|
94
|
+
R2D_Log(R2D_ERROR, "Could not open controller");
|
95
|
+
} else if(intance_id > last_intance_id) {
|
96
|
+
SDL_JoystickGetGUIDString(
|
97
|
+
SDL_JoystickGetGUID(joy),
|
98
|
+
guid_str, 33
|
99
|
+
);
|
100
|
+
R2D_Log(R2D_INFO,
|
101
|
+
"Controller #%i: %s\n GUID: %s\n Axes: %d\n Buttons: %d\n Balls: %d",
|
102
|
+
intance_id, SDL_JoystickName(joy), guid_str, SDL_JoystickNumAxes(joy),
|
103
|
+
SDL_JoystickNumButtons(joy), SDL_JoystickNumBalls(joy)
|
104
|
+
);
|
105
|
+
R2D_Log(R2D_WARN, "Controller #%i does not have a mapping available", intance_id);
|
106
|
+
last_intance_id = intance_id;
|
107
|
+
}
|
108
|
+
}
|
109
|
+
}
|
110
|
+
}
|