DEADSOFTWARE

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