forked from pete-gordon/oricutron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_sdl.c
487 lines (432 loc) · 10.5 KB
/
system_sdl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
** Oricutron
** Copyright (C) 2009-2014 Peter Gordon
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation, version 2
** of the License.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
** SDL System specific stuff
*/
#ifndef __APPLE__
#define WANT_WMINFO
#else
#define SDL_SysWMinfo void
#endif
#include "system.h"
#if SDL_MAJOR_VERSION == 1
#ifdef __SPECIFY_SDL_DIR__
#include <SDL/SDL_endian.h>
#else
#include <SDL_endian.h>
#endif
#else
#ifdef __SPECIFY_SDL_DIR__
#include <SDL2/SDL_endian.h>
#else
#include <SDL_endian.h>
#endif
#endif
#ifdef __OPENGL_AVAILABLE__
#ifndef __APPLE__
#include <GL/gl.h>
#else
#include <OpenGL/gl.h>
#endif
#endif
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
# define RMASK 0xFF000000
# define GMASK 0x00FF0000
# define BMASK 0x0000FF00
# define AMASK 0x000000FF
#else
# define RMASK 0x000000FF
# define GMASK 0x0000FF00
# define BMASK 0x00FF0000
# define AMASK 0xFF000000
#endif
#if SDL_MAJOR_VERSION == 1
/* NOP */
#else
static SDL_bool g_fullscreen = SDL_FALSE;
static SDL_GLContext g_glcontext = NULL;
static SDL_Renderer* g_renderer = NULL;
static SDL_Texture *g_texture = NULL;
static SDL_Surface* g_screen = NULL;
static SDL_Window* g_window = NULL;
static SDL_Surface* g_icon = NULL;
static int g_bpp = 0;
static int g_width = 0;
static int g_height = 0;
static int g_lastx = SDL_WINDOWPOS_CENTERED;
static int g_lasty = SDL_WINDOWPOS_CENTERED;
static void FreeResources(void)
{
if(g_glcontext)
{
SDL_GL_DeleteContext(g_glcontext);
g_glcontext = NULL;
}
if(g_renderer)
{
SDL_DestroyRenderer(g_renderer);
g_renderer = NULL;
}
if(g_texture)
{
SDL_DestroyTexture(g_texture);
g_texture = NULL;
}
if(g_screen)
{
SDL_FreeSurface(g_screen);
g_screen = NULL;
}
if(g_window)
{
SDL_GetWindowPosition(g_window, &g_lastx, &g_lasty);
SDL_DestroyWindow(g_window);
g_window = NULL;
}
}
#endif
#if SDL_MAJOR_VERSION == 1
int SDL_COMPAT_GetWMInfo(SDL_SysWMinfo *info)
{
#if defined(__MORPHOS__)|defined(__APPLE__)
return 0;
#else
return SDL_GetWMInfo(info);
#endif
}
#else
int SDL_COMPAT_GetWMInfo(SDL_SysWMinfo *info)
{
if(g_window)
return SDL_GetWindowWMInfo(g_window, info);
return -1;
}
#endif
#if SDL_MAJOR_VERSION == 1
void SDL_COMPAT_WM_SetIcon(SDL_Surface *icon, Uint8 *mask)
{
SDL_WM_SetIcon(icon, mask);
}
void SDL_COMPAT_WM_SetCaption(const char *title, const char *icon)
{
SDL_WM_SetCaption(title, icon);
}
#else
void SDL_COMPAT_WM_SetIcon(SDL_Surface *icon, Uint8 *mask)
{
if(g_icon)
{
SDL_FreeSurface(g_icon);
g_icon = NULL;
}
if(icon)
{
g_icon = icon;
g_icon->refcount++;
}
}
void SDL_COMPAT_WM_SetCaption(const char *title, const char *icon)
{
if(g_window)
SDL_SetWindowTitle(g_window, title);
}
#endif
#if SDL_MAJOR_VERSION == 1
SDL_bool SDL_COMPAT_IsAppActive(SDL_Event* event)
{
SDL_ActiveEvent *act_ev = (SDL_ActiveEvent*)event;
return (act_ev->state == SDL_APPACTIVE && act_ev->gain == 1)? SDL_TRUE : SDL_FALSE;
}
SDL_bool SDL_COMPAT_IsAppFocused(SDL_Event* event)
{
switch( event->active.type )
{
case SDL_APPINPUTFOCUS:
case SDL_APPACTIVE:
return SDL_TRUE;
break;
}
return SDL_FALSE;
}
#else
SDL_bool SDL_COMPAT_IsAppActive(SDL_Event* event)
{
/* NOTE: not needed wit SDL 2.0
* return (event->window.event == SDL_WINDOWEVENT_FOCUS_GAINED)? SDL_TRUE : SDL_FALSE; */
return SDL_FALSE;
}
SDL_bool SDL_COMPAT_IsAppFocused(SDL_Event* event)
{
/* NOTE: not needed wit SDL 2.0
* switch( event->window.event )
* {
* case SDL_WINDOWEVENT_FOCUS_GAINED:
* case SDL_WINDOWEVENT_ENTER:
* return SDL_TRUE;
* break;
} */
return SDL_FALSE;
}
#endif
#if SDL_MAJOR_VERSION == 1
int SDL_COMPAT_EnableKeyRepeat(int delay, int interval)
{
return SDL_EnableKeyRepeat(delay, interval);
}
#else
int SDL_COMPAT_EnableKeyRepeat(int delay, int interval)
{
/* This is gone in SDL2 */
return 0;
}
#endif
#if SDL_MAJOR_VERSION == 1
int SDL_COMPAT_EnableUNICODE(int enable)
{
return SDL_EnableUNICODE(enable);
}
#else
int SDL_COMPAT_EnableUNICODE(int enable)
{
/* It's always enabled */
return 0;
}
#endif
#if SDL_MAJOR_VERSION == 1
SDL_COMPAT_KEY SDL_COMPAT_GetKeysymUnicode(SDL_KEYSYM keysym)
{
return keysym.unicode;
}
#else
SDL_COMPAT_KEY SDL_COMPAT_GetKeysymUnicode(SDL_KEYSYM keysym)
{
return keysym.sym;
}
#endif
#if SDL_MAJOR_VERSION == 1
int SDL_COMPAT_Flip(SDL_Surface* screen)
{
return SDL_Flip(screen);
}
#else
int SDL_COMPAT_Flip(SDL_Surface* screen)
{
if(g_renderer)
{
SDL_UpdateTexture(g_texture, NULL, g_screen->pixels, g_screen->pitch);
SDL_RenderClear(g_renderer);
SDL_RenderCopy(g_renderer, g_texture, NULL, NULL);
SDL_RenderPresent(g_renderer);
}
return 0;
}
#endif
#if SDL_MAJOR_VERSION == 1
int SDL_COMPAT_GetBitsPerPixel(void)
{
const SDL_VideoInfo* info = SDL_GetVideoInfo();
return (info)? info->vfmt->BitsPerPixel : 0;
}
#else
int SDL_COMPAT_GetBitsPerPixel(void)
{
int bpp;
SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(0, &mode);
bpp = SDL_BITSPERPIXEL(mode.format);
return (bpp==24)? 32 : bpp;
}
#endif
#if SDL_MAJOR_VERSION == 1
int SDL_COMPAT_WM_ToggleFullScreen(SDL_Surface *surface)
{
return SDL_WM_ToggleFullScreen(surface);
}
#else
int SDL_COMPAT_WM_ToggleFullScreen(SDL_Surface *surface)
{
if(g_window)
{
g_fullscreen = (g_fullscreen)? SDL_FALSE : SDL_TRUE;
if(g_fullscreen)
SDL_SetWindowFullscreen(g_window, SDL_WINDOW_FULLSCREEN);
else
SDL_SetWindowSize(g_window, g_width, g_height);
}
return 0;
}
#endif
#if SDL_MAJOR_VERSION == 1
SDL_Surface* SDL_COMPAT_SetVideoMode(int width, int height, int bitsperpixel, Uint32 flags)
{
return SDL_SetVideoMode(width, height, bitsperpixel, flags);
}
#else
SDL_Surface* SDL_COMPAT_SetVideoMode(int width, int height, int bitsperpixel, Uint32 flags)
{
g_width = width;
g_height = height;
g_bpp = bitsperpixel;
FreeResources();
g_window = SDL_CreateWindow("oricutron", g_lastx, g_lasty,
g_width, g_height, flags);
if(g_icon)
SDL_SetWindowIcon(g_window, g_icon);
if(flags & SDL_WINDOW_OPENGL)
{
g_screen = SDL_GetWindowSurface(g_window);
g_glcontext = SDL_GL_CreateContext(g_window);
}
else
{
g_screen = SDL_CreateRGBSurface(0, g_width, g_height, g_bpp,
RMASK, GMASK, BMASK, AMASK);
g_renderer = SDL_CreateRenderer(g_window, -1, 0);
g_texture = SDL_CreateTexture(g_renderer,
SDL_PIXELFORMAT_ABGR8888,
SDL_TEXTUREACCESS_STREAMING,
g_width, g_height);
}
return g_screen;
}
#endif
#if SDL_MAJOR_VERSION == 1
int SDL_COMPAT_SetPalette(SDL_Surface *surface, int flags, SDL_Color *colors, int firstcolor, int ncolors)
{
return SDL_SetPalette(surface, flags, colors, firstcolor, ncolors);
}
#else
int SDL_COMPAT_SetPalette(SDL_Surface *surface, int flags, SDL_Color *colors, int firstcolor, int ncolors)
{
/* FIXME */
return 0;
}
#endif
#if SDL_MAJOR_VERSION == 1
void SDL_COMPAT_SetEventFilter(SDL_EventFilter filter)
{
SDL_SetEventFilter(filter);
}
#else
void SDL_COMPAT_SetEventFilter(SDL_EventFilter filter)
{
SDL_SetEventFilter(filter, NULL);
}
#endif
#if SDL_MAJOR_VERSION == 1
void SDL_COMPAT_Quit(void)
{
SDL_Quit();
}
#else
void SDL_COMPAT_Quit(void)
{
FreeResources();
SDL_Quit();
}
#endif
#ifdef __OPENGL_AVAILABLE__
#if SDL_MAJOR_VERSION == 1
void SDL_COMPAT_GL_SwapBuffers(void)
{
SDL_GL_SwapBuffers();
}
#else
void SDL_COMPAT_GL_SwapBuffers(void)
{
SDL_GL_SwapWindow(g_window);
}
#endif
#endif
#ifdef __OPENGL_AVAILABLE__
SDL_Surface * flipVert(SDL_Surface* sfc)
{
int line;
SDL_Surface* result = SDL_CreateRGBSurface(sfc->flags, sfc->w, sfc->h,
sfc->format->BytesPerPixel * 8, sfc->format->Rmask, sfc->format->Gmask,
sfc->format->Bmask, sfc->format->Amask);
if (result)
{
Uint8* pixels = (Uint8*) sfc->pixels;
Uint8* rpixels = (Uint8*) result->pixels;
Uint32 pitch = sfc->pitch;
Uint32 pxlength = pitch*sfc->h;
for(line = 0; line < sfc->h; ++line)
{
Uint32 pos = line * pitch;
memcpy(&rpixels[pos], &pixels[(pxlength-pos)-pitch], pitch);
}
}
return result;
}
#if SDL_MAJOR_VERSION == 1
void SDL_COMPAT_TakeScreenshot(char *fname)
{
SDL_Surface *g_screen = SDL_GetVideoSurface();
if(g_screen->flags & SDL_COMPAT_OPENGL)
{
// Juste pour recuperer les dimensions :/
int g_width = g_screen->w;
int g_height = g_screen->h;
SDL_Surface *flip;
// 24 Bits
SDL_Surface *surf = SDL_CreateRGBSurface(SDL_SWSURFACE, g_width, g_height, 24, RMASK, GMASK, BMASK, 0);
glReadPixels(0, 0, g_width, g_height, GL_RGB, GL_UNSIGNED_BYTE, surf->pixels);
// 32 Bits
//SDL_Surface *surf = SDL_CreateRGBSurface(SDL_SWSURFACE, g_width, g_height, 32, RMASK, GMASK, BMASK, AMASK);
//glReadPixels(0, 0, g_width, g_height, GL_RGBA, GL_UNSIGNED_BYTE, surf->pixels);
flip = flipVert(surf);
SDL_SaveBMP(flip, fname);
SDL_FreeSurface(flip);
SDL_FreeSurface(surf);
}
else
SDL_SaveBMP(g_screen, fname);
}
#else
void SDL_COMPAT_TakeScreenshot(char *fname)
{
// 24 Bits
// SDL_Surface *surf = SDL_CreateRGBSurface(SDL_SWSURFACE, g_width, g_height, 24, RMASK, BMASK, GMASK, 0);
// 32 Bits
SDL_Surface *surf = SDL_CreateRGBSurface(SDL_SWSURFACE, g_width, g_height, g_bpp, RMASK, GMASK, BMASK, AMASK);
if (surf == NULL)
return;
// 24 Bits
// glReadPixels(0,0,g_width, g_height, GL_RGB, GL_UNSIGNED_BYTE, surf->pixels);
// 32 Bits
glReadPixels(0,0,g_width, g_height, GL_RGBA, GL_UNSIGNED_BYTE, surf->pixels);
SDL_Surface *flip = flipVert(surf);
SDL_SaveBMP(flip, fname);
SDL_FreeSurface(flip);
SDL_FreeSurface(surf);
}
#endif
#else
#if SDL_MAJOR_VERSION == 1
void SDL_COMPAT_TakeScreenshot(char *fname)
{
SDL_SaveBMP(SDL_GetVideoSurface(), fname);
}
#else
void SDL_COMPAT_TakeScreenshot(char *fname)
{
SDL_SaveBMP(g_screen, fname);
}
#endif
#endif