ruby2d 0.9.0 → 0.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
data/ext/ruby2d/window.c
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
// window.c
|
|
2
|
+
|
|
3
|
+
#include "ruby2d.h"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Create a window
|
|
8
|
+
*/
|
|
9
|
+
R2D_Window *R2D_CreateWindow(const char *title, int width, int height,
|
|
10
|
+
R2D_Update update, R2D_Render render, int flags) {
|
|
11
|
+
|
|
12
|
+
R2D_Init();
|
|
13
|
+
|
|
14
|
+
SDL_DisplayMode dm;
|
|
15
|
+
SDL_GetCurrentDisplayMode(0, &dm);
|
|
16
|
+
R2D_Log(R2D_INFO, "Current display mode is %dx%dpx @ %dhz", dm.w, dm.h, dm.refresh_rate);
|
|
17
|
+
|
|
18
|
+
width = width == R2D_DISPLAY_WIDTH ? dm.w : width;
|
|
19
|
+
height = height == R2D_DISPLAY_HEIGHT ? dm.h : height;
|
|
20
|
+
|
|
21
|
+
// Allocate window and set default values
|
|
22
|
+
R2D_Window *window = (R2D_Window *) malloc(sizeof(R2D_Window));
|
|
23
|
+
window->sdl = NULL;
|
|
24
|
+
window->glcontext = NULL;
|
|
25
|
+
window->title = title;
|
|
26
|
+
window->width = width;
|
|
27
|
+
window->height = height;
|
|
28
|
+
window->orig_width = width;
|
|
29
|
+
window->orig_height = height;
|
|
30
|
+
window->viewport.width = width;
|
|
31
|
+
window->viewport.height = height;
|
|
32
|
+
window->viewport.mode = R2D_SCALE;
|
|
33
|
+
window->update = update;
|
|
34
|
+
window->render = render;
|
|
35
|
+
window->flags = flags;
|
|
36
|
+
window->on_key = NULL;
|
|
37
|
+
window->on_mouse = NULL;
|
|
38
|
+
window->on_controller = NULL;
|
|
39
|
+
window->vsync = true;
|
|
40
|
+
window->fps_cap = 60;
|
|
41
|
+
window->background.r = 0.0;
|
|
42
|
+
window->background.g = 0.0;
|
|
43
|
+
window->background.b = 0.0;
|
|
44
|
+
window->background.a = 1.0;
|
|
45
|
+
window->icon = NULL;
|
|
46
|
+
window->close = true;
|
|
47
|
+
|
|
48
|
+
// Return the window structure
|
|
49
|
+
return window;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
/*
|
|
54
|
+
* Show the window
|
|
55
|
+
*/
|
|
56
|
+
int R2D_Show(R2D_Window *window) {
|
|
57
|
+
|
|
58
|
+
if (!window) {
|
|
59
|
+
R2D_Error("R2D_Show", "Window cannot be shown (because it's NULL)");
|
|
60
|
+
return 1;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Create SDL window
|
|
64
|
+
window->sdl = SDL_CreateWindow(
|
|
65
|
+
window->title, // title
|
|
66
|
+
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, // window position
|
|
67
|
+
window->width, window->height, // window size
|
|
68
|
+
SDL_WINDOW_OPENGL | window->flags // flags
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
if (!window->sdl) R2D_Error("SDL_CreateWindow", SDL_GetError());
|
|
72
|
+
if (window->icon) R2D_SetIcon(window, window->icon);
|
|
73
|
+
|
|
74
|
+
// The window created by SDL might not actually be the requested size.
|
|
75
|
+
// If it's not the same, retrieve and store the actual window size.
|
|
76
|
+
int actual_width, actual_height;
|
|
77
|
+
SDL_GetWindowSize(window->sdl, &actual_width, &actual_height);
|
|
78
|
+
|
|
79
|
+
if ((window->width != actual_width) || (window->height != actual_height)) {
|
|
80
|
+
R2D_Log(R2D_INFO,
|
|
81
|
+
"Scaling window to %ix%i (requested size was %ix%i)",
|
|
82
|
+
actual_width, actual_height, window->width, window->height
|
|
83
|
+
);
|
|
84
|
+
window->width = actual_width;
|
|
85
|
+
window->height = actual_height;
|
|
86
|
+
window->orig_width = actual_width;
|
|
87
|
+
window->orig_height = actual_height;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Set Up OpenGL /////////////////////////////////////////////////////////////
|
|
91
|
+
|
|
92
|
+
R2D_GL_Init(window);
|
|
93
|
+
|
|
94
|
+
// SDL 2.0.10 and macOS 10.15 fix ////////////////////////////////////////////
|
|
95
|
+
|
|
96
|
+
#if MACOS
|
|
97
|
+
SDL_SetWindowSize(window->sdl, window->width, window->height);
|
|
98
|
+
#endif
|
|
99
|
+
|
|
100
|
+
// Set Main Loop Data ////////////////////////////////////////////////////////
|
|
101
|
+
|
|
102
|
+
const Uint8 *key_state;
|
|
103
|
+
|
|
104
|
+
Uint32 frames = 0; // Total frames since start
|
|
105
|
+
Uint32 frames_last_sec = 0; // Frames in the last second
|
|
106
|
+
Uint32 start_ms = SDL_GetTicks(); // Elapsed time since start
|
|
107
|
+
Uint32 next_second_ms = SDL_GetTicks(); // The last time plus a second
|
|
108
|
+
Uint32 begin_ms = start_ms; // Time at beginning of loop
|
|
109
|
+
Uint32 end_ms; // Time at end of loop
|
|
110
|
+
Uint32 elapsed_ms; // Total elapsed time
|
|
111
|
+
Uint32 loop_ms; // Elapsed time of loop
|
|
112
|
+
int delay_ms; // Amount of delay to achieve desired frame rate
|
|
113
|
+
const double decay_rate = 0.5; // Determines how fast an average decays over time
|
|
114
|
+
double fps = window->fps_cap; // Moving average of actual FPS, initial value a guess
|
|
115
|
+
|
|
116
|
+
// Enable VSync
|
|
117
|
+
if (window->vsync) {
|
|
118
|
+
if (!SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1")) {
|
|
119
|
+
R2D_Log(R2D_WARN, "VSync cannot be enabled");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
window->close = false;
|
|
124
|
+
|
|
125
|
+
// Main Loop /////////////////////////////////////////////////////////////////
|
|
126
|
+
|
|
127
|
+
while (!window->close) {
|
|
128
|
+
|
|
129
|
+
// Clear Frame /////////////////////////////////////////////////////////////
|
|
130
|
+
|
|
131
|
+
R2D_GL_Clear(window->background);
|
|
132
|
+
|
|
133
|
+
// Set FPS /////////////////////////////////////////////////////////////////
|
|
134
|
+
|
|
135
|
+
frames++;
|
|
136
|
+
frames_last_sec++;
|
|
137
|
+
end_ms = SDL_GetTicks();
|
|
138
|
+
elapsed_ms = end_ms - start_ms;
|
|
139
|
+
|
|
140
|
+
// Calculate the frame rate using an exponential moving average
|
|
141
|
+
if (next_second_ms < end_ms) {
|
|
142
|
+
fps = decay_rate * fps + (1.0 - decay_rate) * frames_last_sec;
|
|
143
|
+
frames_last_sec = 0;
|
|
144
|
+
next_second_ms = SDL_GetTicks() + 1000;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
loop_ms = end_ms - begin_ms;
|
|
148
|
+
delay_ms = (1000 / window->fps_cap) - loop_ms;
|
|
149
|
+
|
|
150
|
+
if (delay_ms < 0) delay_ms = 0;
|
|
151
|
+
|
|
152
|
+
// Note: `loop_ms + delay_ms` should equal `1000 / fps_cap`
|
|
153
|
+
|
|
154
|
+
SDL_Delay(delay_ms);
|
|
155
|
+
begin_ms = SDL_GetTicks();
|
|
156
|
+
|
|
157
|
+
// Handle Input and Window Events //////////////////////////////////////////
|
|
158
|
+
|
|
159
|
+
int mx, my; // mouse x, y coordinates
|
|
160
|
+
|
|
161
|
+
SDL_Event e;
|
|
162
|
+
while (SDL_PollEvent(&e)) {
|
|
163
|
+
switch (e.type) {
|
|
164
|
+
|
|
165
|
+
case SDL_KEYDOWN:
|
|
166
|
+
if (window->on_key && e.key.repeat == 0) {
|
|
167
|
+
R2D_Event event = {
|
|
168
|
+
.type = R2D_KEY_DOWN, .key = SDL_GetScancodeName(e.key.keysym.scancode)
|
|
169
|
+
};
|
|
170
|
+
window->on_key(event);
|
|
171
|
+
}
|
|
172
|
+
break;
|
|
173
|
+
|
|
174
|
+
case SDL_KEYUP:
|
|
175
|
+
if (window->on_key) {
|
|
176
|
+
R2D_Event event = {
|
|
177
|
+
.type = R2D_KEY_UP, .key = SDL_GetScancodeName(e.key.keysym.scancode)
|
|
178
|
+
};
|
|
179
|
+
window->on_key(event);
|
|
180
|
+
}
|
|
181
|
+
break;
|
|
182
|
+
|
|
183
|
+
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP:
|
|
184
|
+
if (window->on_mouse) {
|
|
185
|
+
R2D_GetMouseOnViewport(window, e.button.x, e.button.y, &mx, &my);
|
|
186
|
+
R2D_Event event = {
|
|
187
|
+
.button = e.button.button, .x = mx, .y = my
|
|
188
|
+
};
|
|
189
|
+
event.type = e.type == SDL_MOUSEBUTTONDOWN ? R2D_MOUSE_DOWN : R2D_MOUSE_UP;
|
|
190
|
+
event.dblclick = e.button.clicks == 2 ? true : false;
|
|
191
|
+
window->on_mouse(event);
|
|
192
|
+
}
|
|
193
|
+
break;
|
|
194
|
+
|
|
195
|
+
case SDL_MOUSEWHEEL:
|
|
196
|
+
if (window->on_mouse) {
|
|
197
|
+
R2D_Event event = {
|
|
198
|
+
.type = R2D_MOUSE_SCROLL, .direction = e.wheel.direction,
|
|
199
|
+
.delta_x = e.wheel.x, .delta_y = -e.wheel.y
|
|
200
|
+
};
|
|
201
|
+
window->on_mouse(event);
|
|
202
|
+
}
|
|
203
|
+
break;
|
|
204
|
+
|
|
205
|
+
case SDL_MOUSEMOTION:
|
|
206
|
+
if (window->on_mouse) {
|
|
207
|
+
R2D_GetMouseOnViewport(window, e.motion.x, e.motion.y, &mx, &my);
|
|
208
|
+
R2D_Event event = {
|
|
209
|
+
.type = R2D_MOUSE_MOVE,
|
|
210
|
+
.x = mx, .y = my, .delta_x = e.motion.xrel, .delta_y = e.motion.yrel
|
|
211
|
+
};
|
|
212
|
+
window->on_mouse(event);
|
|
213
|
+
}
|
|
214
|
+
break;
|
|
215
|
+
|
|
216
|
+
case SDL_CONTROLLERAXISMOTION:
|
|
217
|
+
if (window->on_controller) {
|
|
218
|
+
R2D_Event event = {
|
|
219
|
+
.which = e.caxis.which, .type = R2D_AXIS,
|
|
220
|
+
.axis = e.caxis.axis, .value = e.caxis.value
|
|
221
|
+
};
|
|
222
|
+
window->on_controller(event);
|
|
223
|
+
}
|
|
224
|
+
break;
|
|
225
|
+
|
|
226
|
+
case SDL_JOYAXISMOTION:
|
|
227
|
+
if (window->on_controller && !R2D_IsController(e.jbutton.which)) {
|
|
228
|
+
R2D_Event event = {
|
|
229
|
+
.which = e.jaxis.which, .type = R2D_AXIS,
|
|
230
|
+
.axis = e.jaxis.axis, .value = e.jaxis.value
|
|
231
|
+
};
|
|
232
|
+
window->on_controller(event);
|
|
233
|
+
}
|
|
234
|
+
break;
|
|
235
|
+
|
|
236
|
+
case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONUP:
|
|
237
|
+
if (window->on_controller) {
|
|
238
|
+
R2D_Event event = {
|
|
239
|
+
.which = e.cbutton.which, .button = e.cbutton.button
|
|
240
|
+
};
|
|
241
|
+
event.type = e.type == SDL_CONTROLLERBUTTONDOWN ? R2D_BUTTON_DOWN : R2D_BUTTON_UP;
|
|
242
|
+
window->on_controller(event);
|
|
243
|
+
}
|
|
244
|
+
break;
|
|
245
|
+
|
|
246
|
+
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP:
|
|
247
|
+
if (window->on_controller && !R2D_IsController(e.jbutton.which)) {
|
|
248
|
+
R2D_Event event = {
|
|
249
|
+
.which = e.jbutton.which, .button = e.jbutton.button
|
|
250
|
+
};
|
|
251
|
+
event.type = e.type == SDL_JOYBUTTONDOWN ? R2D_BUTTON_DOWN : R2D_BUTTON_UP;
|
|
252
|
+
window->on_controller(event);
|
|
253
|
+
}
|
|
254
|
+
break;
|
|
255
|
+
|
|
256
|
+
case SDL_JOYDEVICEADDED:
|
|
257
|
+
R2D_Log(R2D_INFO, "Controller connected (%i total)", SDL_NumJoysticks());
|
|
258
|
+
R2D_OpenControllers();
|
|
259
|
+
break;
|
|
260
|
+
|
|
261
|
+
case SDL_JOYDEVICEREMOVED:
|
|
262
|
+
if (R2D_IsController(e.jdevice.which)) {
|
|
263
|
+
R2D_Log(R2D_INFO, "Controller #%i: %s removed (%i remaining)", e.jdevice.which, SDL_GameControllerName(SDL_GameControllerFromInstanceID(e.jdevice.which)), SDL_NumJoysticks());
|
|
264
|
+
SDL_GameControllerClose(SDL_GameControllerFromInstanceID(e.jdevice.which));
|
|
265
|
+
} else {
|
|
266
|
+
R2D_Log(R2D_INFO, "Controller #%i: %s removed (%i remaining)", e.jdevice.which, SDL_JoystickName(SDL_JoystickFromInstanceID(e.jdevice.which)), SDL_NumJoysticks());
|
|
267
|
+
SDL_JoystickClose(SDL_JoystickFromInstanceID(e.jdevice.which));
|
|
268
|
+
}
|
|
269
|
+
break;
|
|
270
|
+
|
|
271
|
+
case SDL_WINDOWEVENT:
|
|
272
|
+
switch (e.window.event) {
|
|
273
|
+
case SDL_WINDOWEVENT_RESIZED:
|
|
274
|
+
// Store new window size, set viewport
|
|
275
|
+
window->width = e.window.data1;
|
|
276
|
+
window->height = e.window.data2;
|
|
277
|
+
R2D_GL_SetViewport(window);
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
break;
|
|
281
|
+
|
|
282
|
+
case SDL_QUIT:
|
|
283
|
+
R2D_Close(window);
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Detect keys held down
|
|
289
|
+
int num_keys;
|
|
290
|
+
key_state = SDL_GetKeyboardState(&num_keys);
|
|
291
|
+
|
|
292
|
+
for (int i = 0; i < num_keys; i++) {
|
|
293
|
+
if (window->on_key) {
|
|
294
|
+
if (key_state[i] == 1) {
|
|
295
|
+
R2D_Event event = {
|
|
296
|
+
.type = R2D_KEY_HELD, .key = SDL_GetScancodeName(i)
|
|
297
|
+
};
|
|
298
|
+
window->on_key(event);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Get and store mouse position relative to the viewport
|
|
304
|
+
int wx, wy; // mouse x, y coordinates relative to the window
|
|
305
|
+
SDL_GetMouseState(&wx, &wy);
|
|
306
|
+
R2D_GetMouseOnViewport(window, wx, wy, &window->mouse.x, &window->mouse.y);
|
|
307
|
+
|
|
308
|
+
// Update Window State /////////////////////////////////////////////////////
|
|
309
|
+
|
|
310
|
+
// Store new values in the window
|
|
311
|
+
window->frames = frames;
|
|
312
|
+
window->elapsed_ms = elapsed_ms;
|
|
313
|
+
window->loop_ms = loop_ms;
|
|
314
|
+
window->delay_ms = delay_ms;
|
|
315
|
+
window->fps = fps;
|
|
316
|
+
|
|
317
|
+
// Call update and render callbacks
|
|
318
|
+
if (window->update) window->update();
|
|
319
|
+
if (window->render) window->render();
|
|
320
|
+
|
|
321
|
+
// Draw Frame //////////////////////////////////////////////////////////////
|
|
322
|
+
|
|
323
|
+
// Render and flush all OpenGL buffers
|
|
324
|
+
R2D_GL_FlushBuffers();
|
|
325
|
+
|
|
326
|
+
// Swap buffers to display drawn contents in the window
|
|
327
|
+
SDL_GL_SwapWindow(window->sdl);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return 0;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
/*
|
|
335
|
+
* Set the icon for the window
|
|
336
|
+
*/
|
|
337
|
+
void R2D_SetIcon(R2D_Window *window, const char *icon) {
|
|
338
|
+
R2D_Image *img = R2D_CreateImage(icon);
|
|
339
|
+
if (img) {
|
|
340
|
+
window->icon = icon;
|
|
341
|
+
SDL_SetWindowIcon(window->sdl, img->surface);
|
|
342
|
+
R2D_FreeImage(img);
|
|
343
|
+
} else {
|
|
344
|
+
R2D_Log(R2D_WARN, "Could not set window icon");
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
/*
|
|
350
|
+
* Take a screenshot of the window
|
|
351
|
+
*/
|
|
352
|
+
void R2D_Screenshot(R2D_Window *window, const char *path) {
|
|
353
|
+
|
|
354
|
+
#if GLES
|
|
355
|
+
R2D_Error("R2D_Screenshot", "Not supported in OpenGL ES");
|
|
356
|
+
#else
|
|
357
|
+
// Create a surface the size of the window
|
|
358
|
+
SDL_Surface *surface = SDL_CreateRGBSurface(
|
|
359
|
+
SDL_SWSURFACE, window->width, window->height, 24,
|
|
360
|
+
0x000000FF, 0x0000FF00, 0x00FF0000, 0
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
// Grab the pixels from the front buffer, save to surface
|
|
364
|
+
glReadBuffer(GL_FRONT);
|
|
365
|
+
glReadPixels(0, 0, window->width, window->height, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
|
|
366
|
+
|
|
367
|
+
// Flip image vertically
|
|
368
|
+
|
|
369
|
+
void *temp_row = (void *)malloc(surface->pitch);
|
|
370
|
+
if (!temp_row) {
|
|
371
|
+
R2D_Error("R2D_Screenshot", "Out of memory!");
|
|
372
|
+
SDL_FreeSurface(surface);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
int height_div_2 = (int) (surface->h * 0.5);
|
|
377
|
+
|
|
378
|
+
for (int index = 0; index < height_div_2; index++) {
|
|
379
|
+
memcpy((Uint8 *)temp_row,(Uint8 *)(surface->pixels) + surface->pitch * index, surface->pitch);
|
|
380
|
+
memcpy((Uint8 *)(surface->pixels) + surface->pitch * index, (Uint8 *)(surface->pixels) + surface->pitch * (surface->h - index-1), surface->pitch);
|
|
381
|
+
memcpy((Uint8 *)(surface->pixels) + surface->pitch * (surface->h - index-1), temp_row, surface->pitch);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
free(temp_row);
|
|
385
|
+
|
|
386
|
+
// Save image to disk
|
|
387
|
+
IMG_SavePNG(surface, path);
|
|
388
|
+
SDL_FreeSurface(surface);
|
|
389
|
+
#endif
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
/*
|
|
394
|
+
* Close the window
|
|
395
|
+
*/
|
|
396
|
+
int R2D_Close(R2D_Window *window) {
|
|
397
|
+
if (!window->close) {
|
|
398
|
+
R2D_Log(R2D_INFO, "Closing window");
|
|
399
|
+
window->close = true;
|
|
400
|
+
}
|
|
401
|
+
return 0;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
/*
|
|
406
|
+
* Free all resources
|
|
407
|
+
*/
|
|
408
|
+
int R2D_FreeWindow(R2D_Window *window) {
|
|
409
|
+
R2D_Close(window);
|
|
410
|
+
SDL_GL_DeleteContext(window->glcontext);
|
|
411
|
+
SDL_DestroyWindow(window->sdl);
|
|
412
|
+
free(window);
|
|
413
|
+
return 0;
|
|
414
|
+
}
|
data/lib/ruby2d.rb
CHANGED
data/lib/ruby2d/cli/build.rb
CHANGED
|
@@ -7,7 +7,7 @@ require 'fileutils'
|
|
|
7
7
|
|
|
8
8
|
# The Ruby 2D library files
|
|
9
9
|
@lib_files = [
|
|
10
|
-
'colorize',
|
|
10
|
+
'cli/colorize',
|
|
11
11
|
'exceptions',
|
|
12
12
|
'renderable',
|
|
13
13
|
'color',
|
|
@@ -96,7 +96,7 @@ def build_native(rb_file)
|
|
|
96
96
|
end
|
|
97
97
|
|
|
98
98
|
# Compile to a native executable
|
|
99
|
-
`cc build/app.c -lmruby
|
|
99
|
+
`cc build/app.c -lmruby -o build/app`
|
|
100
100
|
|
|
101
101
|
# Clean up
|
|
102
102
|
clean_up unless @debug
|
|
@@ -162,12 +162,6 @@ end
|
|
|
162
162
|
def build_ios_tvos(rb_file, device)
|
|
163
163
|
check_build_src_file(rb_file)
|
|
164
164
|
|
|
165
|
-
# Check for Simple 2D framework,
|
|
166
|
-
unless File.exist?('/usr/local/Frameworks/Simple2D/iOS/Simple2D.framework') && File.exist?('/usr/local/Frameworks/Simple2D/tvOS/Simple2D.framework')
|
|
167
|
-
puts "#{'Error:'.error} Simple 2D iOS and tvOS frameworks not found. Install them and try again.\n"
|
|
168
|
-
exit
|
|
169
|
-
end
|
|
170
|
-
|
|
171
165
|
# Check if MRuby exists; if not, quit
|
|
172
166
|
if `which mruby`.empty?
|
|
173
167
|
puts "#{'Error:'.error} Can't find MRuby, which is needed to build native Ruby 2D applications.\n"
|
|
@@ -197,6 +191,7 @@ def build_ios_tvos(rb_file, device)
|
|
|
197
191
|
f << File.read("#{@gem_dir}/ext/ruby2d/ruby2d.c")
|
|
198
192
|
end
|
|
199
193
|
|
|
194
|
+
# TODO: Need add this functionality to the gem
|
|
200
195
|
# Build the Xcode project
|
|
201
196
|
`simple2d build --#{device} build/#{device}/MyApp.xcodeproj`
|
|
202
197
|
|