1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
16 #include <SDL_image.h>
17 #include <SDL_mixer.h>
19 #include <SDL_thread.h>
20 #if defined(PLATFORM_WIN32)
21 #include <SDL_syswm.h>
24 /* definitions needed for "system.c" */
26 #if defined(TARGET_SDL2)
27 #define SURFACE_FLAGS (0)
29 #define SURFACE_FLAGS (SDL_SWSURFACE)
32 #if defined(TARGET_SDL2)
33 #define SET_TRANSPARENT_PIXEL (SDL_TRUE)
34 #define UNSET_TRANSPARENT_PIXEL (SDL_FALSE)
36 #define SET_TRANSPARENT_PIXEL (SDL_SRCCOLORKEY)
37 #define UNSET_TRANSPARENT_PIXEL (0)
40 /* system dependent definitions */
42 #if defined(TARGET_SDL2)
43 #define TARGET_STRING "SDL2"
45 #define TARGET_STRING "SDL"
48 #if defined(PLATFORM_ANDROID)
49 #define WINDOW_SCALING_STATUS WINDOW_SCALING_NOT_AVAILABLE
50 #define FULLSCREEN_STATUS FULLSCREEN_AVAILABLE
51 #elif defined(TARGET_SDL2)
52 #define WINDOW_SCALING_STATUS WINDOW_SCALING_AVAILABLE
53 #define FULLSCREEN_STATUS FULLSCREEN_AVAILABLE
55 #define WINDOW_SCALING_STATUS WINDOW_SCALING_NOT_AVAILABLE
56 #define FULLSCREEN_STATUS FULLSCREEN_AVAILABLE
59 #define CURSOR_MAX_WIDTH 32
60 #define CURSOR_MAX_HEIGHT 32
63 /* SDL type definitions */
65 typedef struct SDLSurfaceInfo Bitmap;
66 typedef struct SDLSurfaceInfo DrawBuffer;
67 typedef struct SDLSurfaceInfo DrawWindow;
69 typedef SDL_Cursor *Cursor;
71 #if defined(TARGET_SDL2)
72 typedef SDL_Keycode Key;
73 typedef SDL_Keymod KeyMod;
76 typedef unsigned int KeyMod;
79 typedef SDL_Event Event;
80 typedef SDL_MouseButtonEvent ButtonEvent;
81 typedef SDL_MouseMotionEvent MotionEvent;
82 #if defined(TARGET_SDL2)
83 typedef SDL_MouseWheelEvent WheelEvent;
84 typedef SDL_TouchFingerEvent FingerEvent;
85 typedef SDL_TextInputEvent TextEvent;
86 typedef SDL_Event PauseResumeEvent;
87 typedef SDL_WindowEvent WindowEvent;
89 typedef SDL_KeyboardEvent KeyEvent;
90 typedef SDL_Event ExposeEvent;
91 typedef SDL_Event FocusChangeEvent;
92 typedef SDL_Event ClientMessageEvent;
95 /* structure definitions */
99 char *source_filename;
102 SDL_Surface *surface;
103 SDL_Surface *surface_masked;
104 #if defined(TARGET_SDL2)
105 SDL_Texture *texture;
106 SDL_Texture *texture_masked;
110 struct MouseCursorInfo
115 byte data[CURSOR_MAX_WIDTH * CURSOR_MAX_HEIGHT / 8];
116 byte mask[CURSOR_MAX_WIDTH * CURSOR_MAX_HEIGHT / 8];
120 /* SDL symbol definitions */
124 #define BLACK_PIXEL 0x000000
125 #define WHITE_PIXEL 0xffffff
127 #define EVENT_BUTTONPRESS SDL_MOUSEBUTTONDOWN
128 #define EVENT_BUTTONRELEASE SDL_MOUSEBUTTONUP
129 #define EVENT_MOTIONNOTIFY SDL_MOUSEMOTION
130 #if defined(TARGET_SDL2)
131 #define EVENT_WHEELMOTION SDL_MOUSEWHEEL
132 #define EVENT_FINGERPRESS SDL_FINGERDOWN
133 #define EVENT_FINGERRELEASE SDL_FINGERUP
134 #define EVENT_FINGERMOTION SDL_FINGERMOTION
135 #define EVENT_TEXTINPUT SDL_TEXTINPUT
137 #define EVENT_KEYPRESS SDL_KEYDOWN
138 #define EVENT_KEYRELEASE SDL_KEYUP
139 #define EVENT_EXPOSE SDL_USEREVENT + 0
140 #define EVENT_FOCUSIN SDL_USEREVENT + 1
141 #define EVENT_FOCUSOUT SDL_USEREVENT + 2
142 #define EVENT_CLIENTMESSAGE SDL_QUIT
143 #define EVENT_MAPNOTIFY SDL_USEREVENT + 4
144 #define EVENT_UNMAPNOTIFY SDL_USEREVENT + 5
146 #define KSYM_UNDEFINED SDLK_UNKNOWN
148 #define KSYM_Return SDLK_RETURN
149 #define KSYM_Escape SDLK_ESCAPE
151 #define KSYM_Left SDLK_LEFT
152 #define KSYM_Right SDLK_RIGHT
153 #define KSYM_Up SDLK_UP
154 #define KSYM_Down SDLK_DOWN
157 #define KSYM_KP_Left SDLK_KP_LEFT
158 #define KSYM_KP_Right SDLK_KP_RIGHT
159 #define KSYM_KP_Up SDLK_KP_UP
160 #define KSYM_KP_Down SDLK_KP_DOWN
163 #define KSYM_KP_Enter SDLK_KP_ENTER
164 #define KSYM_KP_Add SDLK_KP_PLUS
165 #define KSYM_KP_Subtract SDLK_KP_MINUS
166 #define KSYM_KP_Multiply SDLK_KP_MULTIPLY
167 #define KSYM_KP_Divide SDLK_KP_DIVIDE
168 #define KSYM_KP_Separator SDLK_KP_PERIOD
170 #define KSYM_Shift_L SDLK_LSHIFT
171 #define KSYM_Shift_R SDLK_RSHIFT
172 #define KSYM_Control_L SDLK_LCTRL
173 #define KSYM_Control_R SDLK_RCTRL
175 #if defined(TARGET_SDL2)
176 #define KSYM_Meta_L SDLK_LGUI
177 #define KSYM_Meta_R SDLK_RGUI
179 #define KSYM_Meta_L SDLK_LMETA
180 #define KSYM_Meta_R SDLK_RMETA
183 #define KSYM_Alt_L SDLK_LALT
184 #define KSYM_Alt_R SDLK_RALT
185 #if !defined(TARGET_SDL2)
186 #define KSYM_Super_L SDLK_LSUPER
187 #define KSYM_Super_R SDLK_RSUPER
189 #define KSYM_Mode_switch SDLK_MODE
190 #define KSYM_Multi_key SDLK_RCTRL
192 #define KSYM_BackSpace SDLK_BACKSPACE
193 #define KSYM_Delete SDLK_DELETE
194 #define KSYM_Insert SDLK_INSERT
195 #define KSYM_Tab SDLK_TAB
196 #define KSYM_Home SDLK_HOME
197 #define KSYM_End SDLK_END
198 #define KSYM_Page_Up SDLK_PAGEUP
199 #define KSYM_Page_Down SDLK_PAGEDOWN
201 #if defined(TARGET_SDL2)
202 #define KSYM_Menu SDLK_MENU
203 #define KSYM_Back SDLK_AC_BACK
206 #define KSYM_space SDLK_SPACE
207 #define KSYM_exclam SDLK_EXCLAIM
208 #define KSYM_quotedbl SDLK_QUOTEDBL
209 #define KSYM_numbersign SDLK_HASH
210 #define KSYM_dollar SDLK_DOLLAR
212 #if defined(TARGET_SDL2)
213 #define KSYM_percent SDLK_PERCENT
215 #define KSYM_percent 37 /* undefined in SDL */
218 #define KSYM_ampersand SDLK_AMPERSAND
219 #define KSYM_apostrophe SDLK_QUOTE
220 #define KSYM_parenleft SDLK_LEFTPAREN
221 #define KSYM_parenright SDLK_RIGHTPAREN
222 #define KSYM_asterisk SDLK_ASTERISK
223 #define KSYM_plus SDLK_PLUS
224 #define KSYM_comma SDLK_COMMA
225 #define KSYM_minus SDLK_MINUS
226 #define KSYM_period SDLK_PERIOD
227 #define KSYM_slash SDLK_SLASH
229 #define KSYM_colon SDLK_COLON
230 #define KSYM_semicolon SDLK_SEMICOLON
231 #define KSYM_less SDLK_LESS
232 #define KSYM_equal SDLK_EQUALS
233 #define KSYM_greater SDLK_GREATER
234 #define KSYM_question SDLK_QUESTION
235 #define KSYM_at SDLK_AT
237 #define KSYM_bracketleft SDLK_LEFTBRACKET
238 #define KSYM_backslash SDLK_BACKSLASH
239 #define KSYM_bracketright SDLK_RIGHTBRACKET
240 #define KSYM_asciicircum SDLK_CARET
241 #define KSYM_underscore SDLK_UNDERSCORE
242 #define KSYM_grave SDLK_BACKQUOTE
244 #define KSYM_quoteleft KSYM_UNDEFINED /* undefined */
245 #define KSYM_braceleft KSYM_UNDEFINED /* undefined */
246 #define KSYM_bar KSYM_UNDEFINED /* undefined */
247 #define KSYM_braceright KSYM_UNDEFINED /* undefined */
248 #define KSYM_asciitilde KSYM_UNDEFINED /* undefined */
250 #if defined(TARGET_SDL2)
251 #define KSYM_degree 176
252 #define KSYM_Adiaeresis 196
253 #define KSYM_Odiaeresis 214
254 #define KSYM_Udiaeresis 220
255 #define KSYM_adiaeresis 228
256 #define KSYM_odiaeresis 246
257 #define KSYM_udiaeresis 252
258 #define KSYM_ssharp 223
260 #define KSYM_degree SDLK_WORLD_16
261 #define KSYM_Adiaeresis SDLK_WORLD_36
262 #define KSYM_Odiaeresis SDLK_WORLD_54
263 #define KSYM_Udiaeresis SDLK_WORLD_60
264 #define KSYM_adiaeresis SDLK_WORLD_68
265 #define KSYM_odiaeresis SDLK_WORLD_86
266 #define KSYM_udiaeresis SDLK_WORLD_92
267 #define KSYM_ssharp SDLK_WORLD_63
299 #define KSYM_A SDLK_A
300 #define KSYM_B SDLK_B
301 #define KSYM_C SDLK_C
302 #define KSYM_D SDLK_D
303 #define KSYM_E SDLK_E
304 #define KSYM_F SDLK_F
305 #define KSYM_G SDLK_G
306 #define KSYM_H SDLK_H
307 #define KSYM_I SDLK_I
308 #define KSYM_J SDLK_J
309 #define KSYM_K SDLK_K
310 #define KSYM_L SDLK_L
311 #define KSYM_M SDLK_M
312 #define KSYM_N SDLK_N
313 #define KSYM_O SDLK_O
314 #define KSYM_P SDLK_P
315 #define KSYM_Q SDLK_Q
316 #define KSYM_R SDLK_R
317 #define KSYM_S SDLK_S
318 #define KSYM_T SDLK_T
319 #define KSYM_U SDLK_U
320 #define KSYM_V SDLK_V
321 #define KSYM_W SDLK_W
322 #define KSYM_X SDLK_X
323 #define KSYM_Y SDLK_Y
324 #define KSYM_Z SDLK_Z
326 #define KSYM_a SDLK_a
327 #define KSYM_b SDLK_b
328 #define KSYM_c SDLK_c
329 #define KSYM_d SDLK_d
330 #define KSYM_e SDLK_e
331 #define KSYM_f SDLK_f
332 #define KSYM_g SDLK_g
333 #define KSYM_h SDLK_h
334 #define KSYM_i SDLK_i
335 #define KSYM_j SDLK_j
336 #define KSYM_k SDLK_k
337 #define KSYM_l SDLK_l
338 #define KSYM_m SDLK_m
339 #define KSYM_n SDLK_n
340 #define KSYM_o SDLK_o
341 #define KSYM_p SDLK_p
342 #define KSYM_q SDLK_q
343 #define KSYM_r SDLK_r
344 #define KSYM_s SDLK_s
345 #define KSYM_t SDLK_t
346 #define KSYM_u SDLK_u
347 #define KSYM_v SDLK_v
348 #define KSYM_w SDLK_w
349 #define KSYM_x SDLK_x
350 #define KSYM_y SDLK_y
351 #define KSYM_z SDLK_z
353 #define KSYM_0 SDLK_0
354 #define KSYM_1 SDLK_1
355 #define KSYM_2 SDLK_2
356 #define KSYM_3 SDLK_3
357 #define KSYM_4 SDLK_4
358 #define KSYM_5 SDLK_5
359 #define KSYM_6 SDLK_6
360 #define KSYM_7 SDLK_7
361 #define KSYM_8 SDLK_8
362 #define KSYM_9 SDLK_9
364 #if defined(TARGET_SDL2)
365 #define KSYM_KP_0 SDLK_KP_0
366 #define KSYM_KP_1 SDLK_KP_1
367 #define KSYM_KP_2 SDLK_KP_2
368 #define KSYM_KP_3 SDLK_KP_3
369 #define KSYM_KP_4 SDLK_KP_4
370 #define KSYM_KP_5 SDLK_KP_5
371 #define KSYM_KP_6 SDLK_KP_6
372 #define KSYM_KP_7 SDLK_KP_7
373 #define KSYM_KP_8 SDLK_KP_8
374 #define KSYM_KP_9 SDLK_KP_9
376 #define KSYM_KP_0 SDLK_KP0
377 #define KSYM_KP_1 SDLK_KP1
378 #define KSYM_KP_2 SDLK_KP2
379 #define KSYM_KP_3 SDLK_KP3
380 #define KSYM_KP_4 SDLK_KP4
381 #define KSYM_KP_5 SDLK_KP5
382 #define KSYM_KP_6 SDLK_KP6
383 #define KSYM_KP_7 SDLK_KP7
384 #define KSYM_KP_8 SDLK_KP8
385 #define KSYM_KP_9 SDLK_KP9
388 #define KSYM_F1 SDLK_F1
389 #define KSYM_F2 SDLK_F2
390 #define KSYM_F3 SDLK_F3
391 #define KSYM_F4 SDLK_F4
392 #define KSYM_F5 SDLK_F5
393 #define KSYM_F6 SDLK_F6
394 #define KSYM_F7 SDLK_F7
395 #define KSYM_F8 SDLK_F8
396 #define KSYM_F9 SDLK_F9
397 #define KSYM_F10 SDLK_F10
398 #define KSYM_F11 SDLK_F11
399 #define KSYM_F12 SDLK_F12
401 #define KSYM_FKEY_FIRST KSYM_F1
402 #define KSYM_FKEY_LAST KSYM_F12
403 #define KSYM_NUM_FKEYS (KSYM_FKEY_LAST - KSYM_FKEY_FIRST + 1)
405 #define KMOD_None KMOD_NONE
406 #define KMOD_Shift_L KMOD_LSHIFT
407 #define KMOD_Shift_R KMOD_RSHIFT
408 #define KMOD_Control_L KMOD_LCTRL
409 #define KMOD_Control_R KMOD_RCTRL
411 #if defined(TARGET_SDL2)
412 #define KMOD_Meta_L KMOD_LGUI
413 #define KMOD_Meta_R KMOD_RGUI
415 #define KMOD_Meta_L KMOD_LMETA
416 #define KMOD_Meta_R KMOD_RMETA
419 #define KMOD_Alt_L KMOD_LALT
420 #define KMOD_Alt_R KMOD_RALT
422 #define KMOD_Shift (KMOD_Shift_L | KMOD_Shift_R)
423 #define KMOD_Control (KMOD_Control_L | KMOD_Control_R)
424 #define KMOD_Meta (KMOD_Meta_L | KMOD_Meta_R)
425 #define KMOD_Alt (KMOD_Alt_L | KMOD_Alt_R)
427 #if defined(TARGET_SDL2)
428 #define KMOD_TextInput (KMOD_Shift | KMOD_Alt_R)
431 /* SDL function definitions */
433 boolean SDLSetNativeSurface(SDL_Surface **);
434 SDL_Surface *SDLGetNativeSurface(SDL_Surface *);
435 void SDLCreateBitmapTextures(Bitmap *);
436 void SDLFreeBitmapTextures(Bitmap *);
438 #if defined(TARGET_SDL2)
439 SDL_Surface *SDL_DisplayFormat(SDL_Surface *);
440 void SDLSetWindowScaling(int);
441 void SDLSetWindowScalingQuality(char *);
442 void SDLSetWindowFullscreen(boolean);
443 void SDLSetDisplaySize(void);
444 void SDLSetScreenSizeAndOffsets(int, int);
445 void SDLSetScreenSizeForRenderer(int, int);
446 void SDLSetScreenProperties(void);
449 void SDLSetScreenRenderingMode(char *);
450 void SDLRedrawWindow();
451 void SDLSetWindowTitle(void);
453 void SDLLimitScreenUpdates(boolean);
454 void SDLInitVideoDisplay(void);
455 void SDLInitVideoBuffer(boolean);
456 boolean SDLSetVideoMode(boolean);
457 void SDLCreateBitmapContent(Bitmap *, int, int, int);
458 void SDLFreeBitmapPointers(Bitmap *);
459 void SDLCopyArea(Bitmap *, Bitmap *, int, int, int, int, int, int, int);
460 void SDLBlitTexture(Bitmap *, int, int, int, int, int, int, int);
461 void SDLFillRectangle(Bitmap *, int, int, int, int, Uint32);
462 void SDLFadeRectangle(int, int, int, int, int, int, int,
463 void (*draw_border_function)(void));
464 void SDLDrawSimpleLine(Bitmap *, int, int, int, int, Uint32);
465 void SDLDrawLine(Bitmap *, int, int, int, int, Uint32);
466 Pixel SDLGetPixel(Bitmap *, int, int);
467 void SDLPutPixel(Bitmap *, int, int, Pixel);
469 void SDLInvertArea(Bitmap *, int, int, int, int, Uint32);
470 void SDLCopyInverseMasked(Bitmap *, Bitmap *, int, int, int, int, int, int);
472 Bitmap *SDLZoomBitmap(Bitmap *, int, int);
474 Bitmap *SDLLoadImage(char *);
476 void SDLSetMouseCursor(struct MouseCursorInfo *);
478 void SDLOpenAudio(void);
479 void SDLCloseAudio(void);
481 void SDLNextEvent(Event *);
482 void SDLHandleWindowManagerEvent(Event *);
484 void HandleJoystickEvent(Event *);
485 void SDLInitJoysticks(void);
486 boolean SDLReadJoystick(int, int *, int *, boolean *, boolean *);
488 void PrepareFadeBitmap(int);