DEADSOFTWARE

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