DEADSOFTWARE

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