DEADSOFTWARE

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