DEADSOFTWARE

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