DEADSOFTWARE

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