DEADSOFTWARE

sdl: fix sdl 1.2 for new render
[d2df-sdl.git] / src / game / sdl / g_system.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_system;
18 interface
20 uses Utils;
22 type
23 TGLProfile = (Core, Compat, Common, CommonLite);
25 TGLDisplayInfo = record
26 w, h, bpp: Integer;
27 fullscreen: Boolean;
28 maximized: Boolean;
29 major, minor: Integer;
30 profile: TGLProfile;
31 end;
33 (* --- Graphics --- *)
35 function sys_GetDisplayModes (bpp: Integer): SSArray;
36 function sys_SetDisplayMode (w, h, bpp: Integer; fullscreen, maximized: Boolean): Boolean;
37 procedure sys_EnableVSync (yes: Boolean);
38 procedure sys_Repaint;
40 function sys_SetDisplayModeGL (const info: TGLDisplayInfo): Boolean;
42 (* --- Input --- *)
44 function sys_HandleInput (): Boolean;
45 procedure sys_RequestQuit;
47 {$IFDEF ENABLE_TOUCH}
48 function sys_IsTextInputActive (): Boolean;
49 procedure sys_ShowKeyboard (yes: Boolean);
50 {$ENDIF}
52 (* --- Init --- *)
53 procedure sys_Init;
54 procedure sys_Final;
56 var (* hooks *)
57 sys_CharPress: procedure (ch: AnsiChar) = nil;
58 sys_ScreenResize: procedure (w, h: Integer) = nil;
60 implementation
62 uses
63 {$IF DEFINED(DARWIN) OR DEFINED(MACOS)}
64 Keyboards, Events,
65 {$IFDEF CPU64}
66 {$linkframework Carbon}
67 {$ENDIF}
68 {$ENDIF}
69 SysUtils, SDL, Math,
70 e_log, e_input, e_sound,
71 g_options, g_console, g_game, g_basic;
73 const
74 GameTitle = 'Doom 2D: Forever (SDL 1.2, %s, %s)';
76 var
77 userResize: Boolean;
78 modeResize: Integer;
79 useScancodes: Boolean;
80 screen: PSDL_Surface;
81 JoystickHandle: array [0..e_MaxJoys - 1] of PSDL_Joystick;
82 JoystickHatState: array [0..e_MaxJoys - 1, 0..e_MaxJoyHats - 1, HAT_LEFT..HAT_DOWN] of Boolean;
83 JoystickZeroAxes: array [0..e_MaxJoys - 1, 0..e_MaxJoyAxes - 1] of Integer;
85 (* --------- Graphics --------- *)
87 function GetDriver (): AnsiString;
88 var buf: array [0..31] of AnsiChar;
89 begin
90 buf := '';
91 SDL_VideoDriverName(buf, Length(buf));
92 result := AnsiString(buf);
93 end;
95 function GetTitle (): AnsiString;
96 var info: AnsiString;
97 begin
98 info := g_GetBuildHash(false);
99 if info = 'custom build' then
100 info := info + ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME;
101 result := Format(GameTitle, [GetDriver(), info]);
102 end;
104 function InitWindow (w, h, bpp: Integer; fullScreen: Boolean): Boolean;
105 var flags: Uint32; title: AnsiString;
106 begin
107 e_LogWritefln('InitWindow %s %s %s %s', [w, h, bpp, fullScreen]);
108 result := false;
109 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
110 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
111 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
112 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
113 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
114 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
115 flags := SDL_OPENGL;
116 if fullScreen then flags := flags or SDL_FULLSCREEN;
117 if userResize then flags := flags or SDL_VIDEORESIZE;
118 if (screen = nil) or (SDL_VideoModeOk(w, h, bpp, flags) <> 0) then
119 begin
120 SDL_FreeSurface(screen);
121 screen := SDL_SetVideoMode(w, h, bpp, flags);
122 if screen <> nil then
123 begin
124 title := GetTitle();
125 SDL_WM_SetCaption(PChar(title), nil);
126 gFullScreen := fullscreen;
127 gRC_FullScreen := fullscreen;
128 if @sys_ScreenResize <> nil then
129 sys_ScreenResize(w, h);
130 result := True
131 end
132 end
133 else
134 begin
135 e_LogWritefln('SDL: video mode not supported', [])
136 end
137 end;
139 procedure sys_Repaint;
140 begin
141 SDL_GL_SwapBuffers
142 end;
144 procedure sys_EnableVSync (yes: Boolean);
145 begin
146 (* ??? *)
147 end;
149 function sys_GetDisplayModes (bpp: Integer): SSArray;
150 var m: PPSDL_Rect; f: TSDL_PixelFormat; i, count: Integer;
151 begin
152 result := nil;
153 FillChar(f, sizeof(f), 0);
154 f.palette := nil;
155 f.BitsPerPixel := bpp;
156 f.BytesPerPixel := (bpp + 7) div 8;
157 m := SDL_ListModes(@f, SDL_OPENGL or SDL_FULLSCREEN);
158 if (m <> NIL) and (UIntPtr(m) <> UIntPtr(-1)) then
159 begin
160 count := 0;
161 while m[count] <> nil do inc(count);
162 SetLength(result, count);
163 for i := 0 to count - 1 do
164 result[i] := IntToStr(m[i].w) + 'x' + IntToStr(m[i].h);
165 end
166 end;
168 function sys_SetDisplayMode (w, h, bpp: Integer; fullscreen, maximized: Boolean): Boolean;
169 begin
170 result := InitWindow(w, h, bpp, fullscreen)
171 end;
173 function sys_SetDisplayModeGL (const info: TGLDisplayInfo): Boolean;
174 begin
175 result := false;
176 case info.profile of
177 TGLProfile.Compat: result := InitWindow(info.w, info.h, info.bpp, info.fullscreen);
178 end;
179 end;
181 (* --------- Joystick --------- *)
183 procedure HandleJoyButton (var ev: TSDL_JoyButtonEvent);
184 var down: Boolean; key: Integer;
185 begin
186 if (ev.which < e_MaxJoys) and (ev.button < e_MaxJoyBtns) then
187 begin
188 key := e_JoyButtonToKey(ev.which, ev.button);
189 down := ev.type_ = SDL_JOYBUTTONDOWN;
190 if g_dbg_input then
191 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev.which, ev.button, key, down]);
192 e_KeyUpDown(key, down);
193 g_Console_ProcessBind(key, down)
194 end
195 else
196 begin
197 if g_dbg_input then
198 begin
199 down := ev.type_ = SDL_JOYBUTTONDOWN;
200 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev.which, ev.button, down])
201 end
202 end
203 end;
205 procedure HandleJoyAxis (var ev: TSDL_JoyAxisEvent);
206 var key, minuskey: Integer;
207 begin
208 if (ev.which < e_MaxJoys) and (ev.axis < e_MaxJoyAxes) then
209 begin
210 key := e_JoyAxisToKey(ev.which, ev.axis, AX_PLUS);
211 minuskey := e_JoyAxisToKey(ev.which, ev.axis, AX_MINUS);
213 if g_dbg_input then
214 e_LogWritefln('Input Debug: jaxis, joy=%s, axis=%s, value=%s, zeroaxes=%s, deadzone=%s', [ev.which, ev.axis, ev.value, JoystickZeroAxes[ev.which, ev.axis], e_JoystickDeadzones[ev.which]]);
216 if ev.value < JoystickZeroAxes[ev.which, ev.axis] - e_JoystickDeadzones[ev.which] then
217 begin
218 if (e_KeyPressed(key)) then
219 begin
220 e_KeyUpDown(key, False);
221 g_Console_ProcessBind(key, False)
222 end;
223 e_KeyUpDown(minuskey, True);
224 g_Console_ProcessBind(minuskey, True)
225 end
226 else if ev.value > JoystickZeroAxes[ev.which, ev.axis] + e_JoystickDeadzones[ev.which] then
227 begin
228 if (e_KeyPressed(minuskey)) then
229 begin
230 e_KeyUpDown(minuskey, False);
231 g_Console_ProcessBind(minuskey, False)
232 end;
233 e_KeyUpDown(key, True);
234 g_Console_ProcessBind(key, True)
235 end
236 else
237 begin
238 if (e_KeyPressed(minuskey)) then
239 begin
240 e_KeyUpDown(minuskey, False);
241 g_Console_ProcessBind(minuskey, False)
242 end;
243 if (e_KeyPressed(key)) then
244 begin
245 e_KeyUpDown(key, False);
246 g_Console_ProcessBind(key, False)
247 end
248 end
249 end
250 else
251 begin
252 if g_dbg_input then
253 e_LogWritefln('Input Debug: NOT IN RANGE! jaxis, joy=%s, axis=%s, value=%s, zeroaxes=%s, deadzone=%s', [ev.which, ev.axis, ev.value, JoystickZeroAxes[ev.which, ev.axis], e_JoystickDeadzones[ev.which]])
254 end
255 end;
257 procedure HandleJoyHat (var ev: TSDL_JoyHatEvent);
258 var
259 down: Boolean;
260 i, key: Integer;
261 hat: array [HAT_LEFT..HAT_DOWN] of Boolean;
262 begin
263 if (ev.which < e_MaxJoys) and (ev.hat < e_MaxJoyHats) then
264 begin
265 if g_dbg_input then
266 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev.which, ev.hat, ev.value]);
267 hat[HAT_UP] := LongBool(ev.value and SDL_HAT_UP);
268 hat[HAT_DOWN] := LongBool(ev.value and SDL_HAT_DOWN);
269 hat[HAT_LEFT] := LongBool(ev.value and SDL_HAT_LEFT);
270 hat[HAT_RIGHT] := LongBool(ev.value and SDL_HAT_RIGHT);
271 for i := HAT_LEFT to HAT_DOWN do
272 begin
273 if JoystickHatState[ev.which, ev.hat, i] <> hat[i] then
274 begin
275 down := hat[i];
276 key := e_JoyHatToKey(ev.which, ev.hat, i);
277 e_KeyUpDown(key, down);
278 g_Console_ProcessBind(key, down)
279 end
280 end;
281 JoystickHatState[ev.which, ev.hat] := hat
282 end
283 else
284 begin
285 if g_dbg_input then
286 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev.which, ev.hat, ev.value])
287 end
288 end;
290 procedure AddJoystick (which: Integer);
291 var i: Integer;
292 begin
293 assert(which < e_MaxJoys);
294 JoystickHandle[which] := SDL_JoystickOpen(which);
295 if JoystickHandle[which] <> nil then
296 begin
297 e_LogWritefln('Added Joystick %s', [which]);
298 e_JoystickAvailable[which] := True;
299 for i := 0 to Min(SDL_JoystickNumAxes(JoystickHandle[which]), e_MaxJoyAxes) - 1 do
300 JoystickZeroAxes[which, i] := SDL_JoystickGetAxis(JoystickHandle[which], i)
301 end
302 else
303 begin
304 e_LogWritefln('Failed to open Joystick %s', [which])
305 end
306 end;
308 procedure RemoveJoystick (which: Integer);
309 begin
310 assert(which < e_MaxJoys);
311 e_LogWritefln('Remove Joystick %s', [which]);
312 e_JoystickAvailable[which] := False;
313 if JoystickHandle[which] <> nil then
314 SDL_JoystickClose(JoystickHandle[which]);
315 JoystickHandle[which] := nil
316 end;
318 (* --------- Input --------- *)
320 {$IF DEFINED(DARWIN) OR DEFINED(MACOS)}
321 var
322 IsMacPlatform: Boolean;
324 function IsMacModSym (sym: Integer): Boolean;
325 begin
326 case sym of
327 SDLK_NUMLOCK,
328 SDLK_CAPSLOCK,
329 SDLK_RCTRL, SDLK_LCTRL,
330 SDLK_RSHIFT, SDLK_LSHIFT,
331 SDLK_RALT, SDLK_LALT,
332 SDLK_RSUPER, SDLK_LSUPER,
333 SDLK_RMETA, SDLK_LMETA:
334 result := true;
335 otherwise
336 result := false;
337 end;
338 end;
340 function Mac2Stub (scancode: Integer): Integer;
341 begin
342 (* SDL 2 swap tilda/grave and paragraph/plus-minus buttons on ISO keyboards *)
343 if ((scancode = 10) or (scancode = 50)) and (KBGetLayoutType(LMGetKbdType()) = kKeyboardISO) then
344 scancode := 60 - scancode;
345 case scancode of
346 0: result := IK_A;
347 1: result := IK_S;
348 2: result := IK_D;
349 3: result := IK_F;
350 4: result := IK_H;
351 5: result := IK_G;
352 6: result := IK_Z;
353 7: result := IK_X;
354 8: result := IK_C;
355 9: result := IK_V;
356 10: result := IK_NONUSBACKSLASH;
357 11: result := IK_B;
358 12: result := IK_Q;
359 13: result := IK_W;
360 14: result := IK_E;
361 15: result := IK_R;
362 16: result := IK_Y;
363 17: result := IK_T;
364 18: result := IK_1;
365 19: result := IK_2;
366 20: result := IK_3;
367 21: result := IK_4;
368 22: result := IK_6;
369 23: result := IK_5;
370 24: result := IK_EQUALS;
371 25: result := IK_9;
372 26: result := IK_7;
373 27: result := IK_MINUS;
374 28: result := IK_8;
375 29: result := IK_0;
376 30: result := {IK_RIGHTBRACKET} IK_RBRACKET;
377 31: result := IK_O;
378 32: result := IK_U;
379 33: result := {IK_LEFTBRACKET} IK_LBRACKET;
380 34: result := IK_I;
381 35: result := IK_P;
382 36: result := IK_RETURN;
383 37: result := IK_L;
384 38: result := IK_J;
385 39: result := {IK_APOSTROPHE} IK_QUOTE;
386 40: result := IK_K;
387 41: result := IK_SEMICOLON;
388 42: result := IK_BACKSLASH;
389 43: result := IK_COMMA;
390 44: result := IK_SLASH;
391 45: result := IK_N;
392 46: result := IK_M;
393 47: result := {IK_PERIOD} IK_DOT;
394 48: result := IK_TAB;
395 49: result := IK_SPACE;
396 50: result := IK_GRAVE;
397 51: result := IK_BACKSPACE;
398 52: result := {IK_KP_ENTER} IK_KPRETURN;
399 53: result := IK_ESCAPE;
400 54: result := {IK_RGUI} IK_RWIN;
401 55: result := {IK_LGUI} IK_WIN;
402 56: result := {IK_LSHIFT} IK_SHIFT;
403 57: result := IK_CAPSLOCK;
404 58: result := {IK_LALT} IK_ALT;
405 59: result := {IK_LCTRL} IK_CTRL;
406 60: result := IK_RSHIFT;
407 61: result := IK_RALT;
408 62: result := IK_RCTRL;
409 63: result := {IK_RGUI} IK_RWIN;
410 {64: result := IK_F17;}
411 65: result := {IK_KP_PERIOD} IK_KPDOT;
412 66: result := IK_INVALID; (* unused? *)
413 67: result := {IK_KP_MULTIPLY} IK_KPMULTIPLE;
414 68: result := IK_INVALID; (* unused? *)
415 69: result := {IK_KP_PLUS} IK_KPPLUS;
416 70: result := IK_INVALID; (* unused? *)
417 71: result := {IK_NUMLOCKCLEAR} IK_NUMLOCK;
418 {72: result := IK_VOLUMEUP;}
419 {73: result := IK_VOLUMEDOWN;}
420 {74: result := IK_MUTE;}
421 75: result := {IK_KP_DIVIDE} IK_KPDIVIDE;
422 76: result := {IK_KP_ENTER} IK_KPRETURN;
423 77: result := IK_INVALID; (* unused? *)
424 78: result := {IK_KP_MINUS} IK_KPMINUS;
425 {79: result := IK_F18;}
426 {80: result := IK_F19;}
427 {81: result := IK_KP_EQUALS;}
428 82: result := {IK_KP_0} IK_KPINSERT;
429 83: result := {IK_KP_1} IK_KPEND;
430 84: result := {IK_KP_2} IK_KPDOWN;
431 85: result := {IK_KP_3} IK_KPPAGEDN;
432 86: result := {IK_KP_4} IK_KPLEFT;
433 87: result := {IK_KP_5} IK_KP5;
434 88: result := {IK_KP_6} IK_KPRIGHT;
435 89: result := {IK_KP_7} IK_KPHOME;
436 90: result := IK_INVALID; (* unused? *)
437 91: result := {IK_KP_8} IK_KPUP;
438 92: result := {IK_KP_9} IK_KPPAGEUP;
439 {93: result := IK_INTERNATIONAL3;}
440 {94: result := IK_INTERNATIONAL1;}
441 {95: result := IK_KP_COMMA;}
442 96: result := IK_F5;
443 97: result := IK_F6;
444 98: result := IK_F7;
445 99: result := IK_F3;
446 100: result := IK_F8;
447 101: result := IK_F9;
448 {102: result := IK_LANG2;}
449 103: result := IK_F11;
450 {104: result := IK_LANG1;}
451 105: result := {IK_PRINTSCREEN} IK_PRINTSCR;
452 {106: result := IK_F16;}
453 107: result := IK_SCROLLLOCK;
454 108: result := IK_INVALID; (* unused? *)
455 109: result := IK_F10;
456 {110: result := IK_APPLICATION;}
457 111: result := IK_F12;
458 112: result := IK_INVALID; (* unused? *)
459 113: result := IK_PAUSE;
460 114: result := IK_INSERT;
461 115: result := IK_HOME;
462 116: result := IK_PAGEUP;
463 117: result := IK_DELETE;
464 118: result := IK_F4;
465 119: result := IK_END;
466 120: result := IK_F2;
467 121: result := {IK_PAGEDOWN} IK_PAGEDN;
468 122: result := IK_F1;
469 123: result := IK_LEFT;
470 124: result := IK_RIGHT;
471 125: result := IK_DOWN;
472 126: result := IK_UP;
473 {127: result := IK_POWER;}
474 otherwise result := IK_INVALID
475 end
476 end;
477 {$ENDIF}
479 {$IFDEF ENABLE_TOUCH}
480 procedure sys_ShowKeyboard (yes: Boolean);
481 begin
482 // stub
483 end;
485 function sys_IsTextInputActive (): Boolean;
486 begin
487 Result := false
488 end;
489 {$ENDIF}
491 function Key2Stub (key: Integer): Integer;
492 var x: Integer;
493 begin
494 case key of
495 SDLK_ESCAPE: x := IK_ESCAPE;
496 SDLK_RETURN: x := IK_RETURN;
497 SDLK_KP_ENTER: x := IK_KPRETURN;
498 SDLK_KP0: x := IK_KPINSERT;
499 SDLK_UP: x := IK_UP;
500 SDLK_KP8: x := IK_KPUP;
501 SDLK_DOWN: x := IK_DOWN;
502 SDLK_KP2: x := IK_KPDOWN;
503 SDLK_LEFT: x := IK_LEFT;
504 SDLK_KP4: x := IK_KPLEFT;
505 SDLK_RIGHT: x := IK_RIGHT;
506 SDLK_KP6: x := IK_KPRIGHT;
507 SDLK_DELETE: x := IK_DELETE;
508 SDLK_HOME: x := IK_HOME;
509 SDLK_KP7: x := IK_KPHOME;
510 SDLK_INSERT: x := IK_INSERT;
511 SDLK_SPACE: x := IK_SPACE;
512 SDLK_LSHIFT: x := IK_SHIFT;
513 SDLK_LALT: x := IK_ALT;
514 SDLK_TAB: x := IK_TAB;
515 SDLK_PAGEUP: x := IK_PAGEUP;
516 SDLK_KP9: x := IK_KPPAGEUP;
517 SDLK_PAGEDOWN: x := IK_PAGEDN;
518 SDLK_KP3: x := IK_KPPAGEDN;
519 SDLK_KP5: x := IK_KP5;
520 SDLK_NUMLOCK: x := IK_NUMLOCK;
521 SDLK_KP_DIVIDE: x := IK_KPDIVIDE;
522 SDLK_KP_MULTIPLY: x := IK_KPMULTIPLE;
523 SDLK_KP_MINUS: x := IK_KPMINUS;
524 SDLK_KP_PLUS: x := IK_KPPLUS;
525 SDLK_KP_PERIOD: x := IK_KPDOT;
526 SDLK_CAPSLOCK: x := IK_CAPSLOCK;
527 SDLK_RSHIFT: x := IK_RSHIFT;
528 SDLK_LCTRL: x := IK_CTRL;
529 SDLK_RCTRL: x := IK_RCTRL;
530 SDLK_RALT: x := IK_RALT;
531 SDLK_LSUPER: x := IK_WIN;
532 SDLK_RSUPER: x := IK_RWIN;
533 SDLK_MENU: x := IK_MENU;
534 SDLK_PRINT: x := IK_PRINTSCR;
535 SDLK_SCROLLOCK: x := IK_SCROLLLOCK;
536 SDLK_LEFTBRACKET: x := IK_LBRACKET;
537 SDLK_RIGHTBRACKET: x := IK_RBRACKET;
538 SDLK_SEMICOLON: x := IK_SEMICOLON;
539 SDLK_QUOTE: x := IK_QUOTE;
540 SDLK_BACKSLASH: x := IK_BACKSLASH;
541 SDLK_SLASH: x := IK_SLASH;
542 SDLK_COMMA: x := IK_COMMA;
543 SDLK_PERIOD: x := IK_DOT;
544 SDLK_EQUALS: x := IK_EQUALS;
545 SDLK_0: x := IK_0;
546 SDLK_1: x := IK_1;
547 SDLK_2: x := IK_2;
548 SDLK_3: x := IK_3;
549 SDLK_4: x := IK_4;
550 SDLK_5: x := IK_5;
551 SDLK_6: x := IK_6;
552 SDLK_7: x := IK_7;
553 SDLK_8: x := IK_8;
554 SDLK_9: x := IK_9;
555 SDLK_F1: x := IK_F1;
556 SDLK_F2: x := IK_F2;
557 SDLK_F3: x := IK_F3;
558 SDLK_F4: x := IK_F4;
559 SDLK_F5: x := IK_F5;
560 SDLK_F6: x := IK_F6;
561 SDLK_F7: x := IK_F7;
562 SDLK_F8: x := IK_F8;
563 SDLK_F9: x := IK_F9;
564 SDLK_F10: x := IK_F10;
565 SDLK_F11: x := IK_F11;
566 SDLK_F12: x := IK_F12;
567 SDLK_END: x := IK_END;
568 SDLK_KP1: x := IK_KPEND;
569 SDLK_BACKSPACE: x := IK_BACKSPACE;
570 SDLK_BACKQUOTE: x := IK_BACKQUOTE;
571 SDLK_PAUSE: x := IK_PAUSE;
572 SDLK_A..SDLK_Z: x := IK_A + (key - SDLK_A);
573 SDLK_MINUS: x := IK_MINUS;
574 else
575 x := IK_INVALID
576 end;
577 result := x
578 end;
580 procedure HandleKeyboard (var ev: TSDL_KeyboardEvent);
581 var down, repeated: Boolean; key: Integer; ch: Char;
582 begin
583 key := IK_INVALID;
584 if useScancodes then
585 begin
586 {$IF DEFINED(DARWIN) OR DEFINED(MACOS)}
587 if IsMacPlatform then
588 if IsMacModSym(ev.keysym.sym) = false then
589 key := Mac2Stub(ev.keysym.scancode);
590 {$ENDIF}
591 end;
592 if key = IK_INVALID then
593 key := Key2Stub(ev.keysym.sym);
594 down := (ev.type_ = SDL_KEYDOWN);
595 repeated := down and e_KeyPressed(key);
596 ch := wchar2win(WideChar(ev.keysym.unicode));
597 if g_dbg_input then
598 e_LogWritefln('Input Debug: keysym, scancode=%s, down=%s, sym=%s, state=%s, unicode=%s, stubsym=%s, cp1251=%s', [ev.keysym.scancode, down, ev.keysym.sym, ev.state, ev.keysym.unicode, key, Ord(ch)]);
599 if not repeated then
600 begin
601 e_KeyUpDown(key, down);
602 g_Console_ProcessBind(key, down);
603 end
604 else
605 begin
606 g_Console_ProcessBindRepeat(key)
607 end;
608 if @sys_CharPress <> nil then
609 if down and IsValid1251(ev.keysym.unicode) and IsPrintable1251(ch) then
610 sys_CharPress(ch)
611 end;
613 procedure HandleResize (var ev: TSDL_ResizeEvent);
614 begin
615 if g_dbg_input then
616 e_LogWritefln('Input Debug: SDL_VIDEORESIZE %s %s', [ev.w, ev.h]);
617 if (modeResize = 1) and (@sys_ScreenResize <> nil) then
618 sys_ScreenResize(ev.w, ev.h)
619 else if modeResize > 1 then
620 InitWindow(ev.w, ev.h, gBPP, gFullscreen)
621 end;
623 function sys_HandleInput (): Boolean;
624 var ev: TSDL_Event;
625 begin
626 result := false;
627 while SDL_PollEvent(@ev) <> 0 do
628 begin
629 case ev.type_ of
630 SDL_QUITEV: result := true;
631 SDL_VIDEORESIZE: HandleResize(ev.resize);
632 SDL_KEYUP, SDL_KEYDOWN: HandleKeyboard(ev.key);
633 SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP: HandleJoyButton(ev.jbutton);
634 SDL_JOYAXISMOTION: HandleJoyAxis(ev.jaxis);
635 SDL_JOYHATMOTION: HandleJoyHat(ev.jhat);
636 SDL_VIDEOEXPOSE: sys_Repaint;
637 SDL_ACTIVEEVENT: e_MuteChannels((ev.active.gain = 0) and gMuteWhenInactive);
638 end
639 end
640 end;
642 procedure sys_RequestQuit;
643 var ev: TSDL_Event;
644 begin
645 ev.quit.type_ := SDL_QUITEV;
646 SDL_PushEvent(@ev)
647 end;
649 (* --------- Init --------- *)
651 procedure sys_Init;
652 var flags: Uint32; i: Integer; name: AnsiString;
653 begin
654 e_WriteLog('Init SDL', TMsgType.Notify);
655 flags := SDL_INIT_VIDEO or SDL_INIT_AUDIO or
656 SDL_INIT_TIMER or SDL_INIT_JOYSTICK
657 (*or SDL_INIT_NOPARACHUTE*);
658 if SDL_Init(flags) <> 0 then
659 raise Exception.Create('SDL: Init failed: ' + SDL_GetError);
660 name := GetDriver();
661 e_LogWritefln('SDL: Video Driver "%s"', [name]);
662 {$IF DEFINED(DARWIN) OR DEFINED(MACOS)}
663 IsMacPlatform := (name = 'Quartz') or (name = 'toolbox') or (name = 'DSp');
664 {$ENDIF}
665 SDL_EnableUNICODE(1);
666 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
667 for i := 0 to e_MaxJoys - 1 do
668 AddJoystick(i)
669 end;
671 procedure sys_Final;
672 var i: Integer;
673 begin
674 e_WriteLog('Releasing SDL', TMsgType.Notify);
675 for i := 0 to e_MaxJoys - 1 do
676 RemoveJoystick(i);
677 if screen <> nil then
678 SDL_FreeSurface(screen);
679 SDL_Quit
680 end;
682 initialization
683 (* window resize are broken both on linux and osx, so disabled by default *)
684 conRegVar('sdl_allow_resize', @userResize, 'allow to resize window by user', 'allow to resize window by user');
685 conRegVar('sdl_resize_action', @modeResize, 'set window resize mode (0: ignore, 1: change, 2: reset)', '');
686 conRegVar('sdl_use_scancodes', @useScancodes, 'use platform-specific scancodes when possible', '');
687 userResize := false;
688 modeResize := 0;
689 useScancodes := true;
690 end.