DEADSOFTWARE

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