DEADSOFTWARE

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