DEADSOFTWARE

game: less references to HEADLESS
[d2df-sdl.git] / src / game / sdl2 / 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 {$IFDEF ENABLE_TOUCH}
33 function sys_IsTextInputActive (): Boolean;
34 procedure sys_ShowKeyboard (yes: Boolean);
35 {$ENDIF}
37 (* --- Init --- *)
38 procedure sys_Init;
39 procedure sys_Final;
41 var (* hooks *)
42 sys_CharPress: procedure (ch: AnsiChar) = nil;
43 sys_ScreenResize: procedure (w, h: Integer) = nil;
45 implementation
47 uses
48 {$IFDEF ENABLE_HOLMES}
49 sdlcarcass,
50 {$ENDIF}
51 {$IFDEF ENABLE_RENDER}
52 r_render,
53 {$ENDIF}
54 {$IFDEF ENABLE_MENU}
55 g_gui,
56 {$ENDIF}
57 SysUtils, SDL2, Math, ctypes,
58 e_log, e_input, e_sound,
59 g_options, g_console, g_game, g_basic
60 ;
62 const
63 GameTitle = 'Doom 2D: Forever (SDL 2, %s)';
65 var
66 window: PSDL_Window;
67 context: TSDL_GLContext;
68 display, wx, wy: Integer;
69 wc: Boolean;
70 JoystickHandle: array [0..e_MaxJoys - 1] of PSDL_Joystick;
71 JoystickHatState: array [0..e_MaxJoys - 1, 0..e_MaxJoyHats - 1, HAT_LEFT..HAT_DOWN] of Boolean;
72 JoystickZeroAxes: array [0..e_MaxJoys - 1, 0..e_MaxJoyAxes - 1] of Integer;
74 {$IFDEF ENABLE_TOUCH}
75 var (* touch *)
76 angleFire: Boolean;
77 keyFinger: array [VK_FIRSTKEY..VK_LASTKEY] of Integer;
78 {$ENDIF}
80 (* --------- Graphics --------- *)
82 function GetTitle (): PChar;
83 var info: AnsiString;
84 begin
85 info := g_GetBuildHash(false);
86 if info = 'custom build' then
87 info := info + ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME;
88 result := PChar(Format(GameTitle, [info]))
89 end;
91 function InitWindow (w, h, bpp: Integer; fullScreen, maximized: Boolean): Boolean;
92 var flags: UInt32; x, y: cint;
93 begin
94 // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
95 e_LogWritefln('InitWindow %s %s %s %s', [w, h, bpp, fullScreen]);
96 result := false;
97 if window = nil then
98 begin
99 {$IFDEF USE_GLES1}
100 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
101 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
102 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
103 {$ELSE}
104 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
105 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
106 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
107 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
108 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
109 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
110 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
111 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
112 {$ENDIF}
113 flags := SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE;
114 if fullScreen then flags := flags or SDL_WINDOW_FULLSCREEN;
115 if maximized then flags := flags or SDL_WINDOW_MAXIMIZED;
116 if wc then
117 begin
118 x := SDL_WINDOWPOS_CENTERED;
119 y := SDL_WINDOWPOS_CENTERED
120 end
121 else
122 begin
123 x := wx;
124 y := wy
125 end;
126 window := SDL_CreateWindow(GetTitle(), x, y, w, h, flags);
127 if window <> nil then
128 begin
129 context := SDL_GL_CreateContext(window);
130 if context <> nil then
131 begin
132 if (fullscreen = false) and (maximized = false) and (wc = false) then
133 begin
134 SDL_GetWindowPosition(window, @x, @y);
135 wx := x; wy := y
136 end;
137 gFullScreen := fullscreen;
138 gWinMaximized := maximized;
139 gRC_FullScreen := fullscreen;
140 gRC_Maximized := maximized;
141 if @sys_ScreenResize <> nil then
142 sys_ScreenResize(w, h);
143 result := true
144 end
145 else
146 begin
147 // SDL_DestroyWindow(window);
148 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError])
149 end
150 end
151 else
152 begin
153 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError])
154 end
155 end
156 else
157 begin
158 if fullScreen then flags := SDL_WINDOW_FULLSCREEN else flags := 0;
159 SDL_SetWindowFullscreen(window, flags);
160 SDL_SetWindowSize(window, w, h);
161 if maximized then SDL_MaximizeWindow(window);
162 // always reset to center when changing fullscreen->windowed for safety purposes
163 if wc or (gFullscreen and not fullscreen) or (gWinMaximized and not maximized) then
164 begin
165 x := SDL_WINDOWPOS_CENTERED;
166 y := SDL_WINDOWPOS_CENTERED
167 end
168 else
169 begin
170 x := wx;
171 y := wy
172 end;
173 SDL_SetWindowPosition(window, x, y);
174 if (fullscreen = false) and (maximized = false) and (wc = false) then
175 begin
176 SDL_GetWindowPosition(window, @x, @y);
177 wx := x; wy := y
178 end;
179 gFullScreen := fullscreen;
180 gWinMaximized := maximized;
181 gRC_FullScreen := fullscreen;
182 gRC_Maximized := maximized;
183 if @sys_ScreenResize <> nil then
184 sys_ScreenResize(w, h);
185 result := true
186 end
187 end;
189 procedure sys_Repaint;
190 begin
191 SDL_GL_SwapWindow(window)
192 end;
194 procedure sys_EnableVSync (yes: Boolean);
195 begin
196 if yes then
197 SDL_GL_SetSwapInterval(1)
198 else
199 SDL_GL_SetSwapInterval(0)
200 end;
202 function sys_GetDisplayModes (bpp: Integer): SSArray;
203 var i, count, num, pw, ph: Integer; m: TSDL_DisplayMode;
204 begin
205 result := nil;
206 num := SDL_GetNumDisplayModes(display);
207 if num < 0 then
208 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError]);
209 if num > 0 then
210 begin
211 e_LogWritefln('Video modes for display %s:', [display]);
212 SetLength(result, num);
213 i := 0; count := 0; pw := 0; ph := 0;
214 while i < num do
215 begin
216 SDL_GetDisplayMode(display, i, @m);
217 if ((pw <> m.w) or (ph <> m.h)) then
218 begin
219 e_LogWritefln('* %sx%sx%s@%s', [m.w, m.h, SDL_BITSPERPIXEL(m.format), m.refresh_rate]);
220 pw := m.w; ph := m.h;
221 result[count] := IntToStr(m.w) + 'x' + IntToStr(m.h);
222 Inc(count);
223 end
224 else
225 begin
226 e_LogWritefln('- %sx%sx%s@%s', [m.w, m.h, SDL_BITSPERPIXEL(m.format), m.refresh_rate]);
227 end;
228 Inc(i)
229 end;
230 SetLength(result, count)
231 end
232 end;
234 function sys_SetDisplayMode (w, h, bpp: Integer; fullScreen, maximized: Boolean): Boolean;
235 begin
236 result := InitWindow(w, h, bpp, fullScreen, maximized)
237 end;
239 (* --------- Joystick --------- *)
241 procedure HandleJoyButton (var ev: TSDL_JoyButtonEvent);
242 var down: Boolean; key: Integer;
243 begin
244 if (ev.which < e_MaxJoys) and (ev.button < e_MaxJoyBtns) then
245 begin
246 key := e_JoyButtonToKey(ev.which, ev.button);
247 down := ev.type_ = SDL_JOYBUTTONDOWN;
248 if g_dbg_input then
249 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev.which, ev.button, key, down]);
250 e_KeyUpDown(key, down);
251 g_Console_ProcessBind(key, down)
252 end
253 else
254 begin
255 if g_dbg_input then
256 begin
257 down := ev.type_ = SDL_JOYBUTTONDOWN;
258 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev.which, ev.button, down])
259 end
260 end
261 end;
263 procedure HandleJoyAxis (var ev: TSDL_JoyAxisEvent);
264 var key, minuskey: Integer;
265 begin
266 if (ev.which < e_MaxJoys) and (ev.axis < e_MaxJoyAxes) then
267 begin
268 key := e_JoyAxisToKey(ev.which, ev.axis, AX_PLUS);
269 minuskey := e_JoyAxisToKey(ev.which, ev.axis, AX_MINUS);
271 if g_dbg_input then
272 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]]);
274 if ev.value < JoystickZeroAxes[ev.which, ev.axis] - e_JoystickDeadzones[ev.which] then
275 begin
276 if (e_KeyPressed(key)) then
277 begin
278 e_KeyUpDown(key, False);
279 g_Console_ProcessBind(key, False)
280 end;
281 e_KeyUpDown(minuskey, True);
282 g_Console_ProcessBind(minuskey, True)
283 end
284 else if ev.value > JoystickZeroAxes[ev.which, ev.axis] + e_JoystickDeadzones[ev.which] then
285 begin
286 if (e_KeyPressed(minuskey)) then
287 begin
288 e_KeyUpDown(minuskey, False);
289 g_Console_ProcessBind(minuskey, False)
290 end;
291 e_KeyUpDown(key, True);
292 g_Console_ProcessBind(key, True)
293 end
294 else
295 begin
296 if (e_KeyPressed(minuskey)) then
297 begin
298 e_KeyUpDown(minuskey, False);
299 g_Console_ProcessBind(minuskey, False)
300 end;
301 if (e_KeyPressed(key)) then
302 begin
303 e_KeyUpDown(key, False);
304 g_Console_ProcessBind(key, False)
305 end
306 end
307 end
308 else
309 begin
310 if g_dbg_input then
311 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]])
312 end
313 end;
315 procedure HandleJoyHat (var ev: TSDL_JoyHatEvent);
316 var
317 down: Boolean;
318 i, key: Integer;
319 hat: array [HAT_LEFT..HAT_DOWN] of Boolean;
320 begin
321 if (ev.which < e_MaxJoys) and (ev.hat < e_MaxJoyHats) then
322 begin
323 if g_dbg_input then
324 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev.which, ev.hat, ev.value]);
325 hat[HAT_UP] := LongBool(ev.value and SDL_HAT_UP);
326 hat[HAT_DOWN] := LongBool(ev.value and SDL_HAT_DOWN);
327 hat[HAT_LEFT] := LongBool(ev.value and SDL_HAT_LEFT);
328 hat[HAT_RIGHT] := LongBool(ev.value and SDL_HAT_RIGHT);
329 for i := HAT_LEFT to HAT_DOWN do
330 begin
331 if JoystickHatState[ev.which, ev.hat, i] <> hat[i] then
332 begin
333 down := hat[i];
334 key := e_JoyHatToKey(ev.which, ev.hat, i);
335 e_KeyUpDown(key, down);
336 g_Console_ProcessBind(key, down)
337 end
338 end;
339 JoystickHatState[ev.which, ev.hat] := hat
340 end
341 else
342 begin
343 if g_dbg_input then
344 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev.which, ev.hat, ev.value])
345 end
346 end;
348 procedure HandleJoyAdd (var ev: TSDL_JoyDeviceEvent);
349 var i: Integer;
350 begin
351 if (ev.which < e_MaxJoys) then
352 begin
353 JoystickHandle[ev.which] := SDL_JoystickOpen(ev.which);
354 if JoystickHandle[ev.which] <> nil then
355 begin
356 e_LogWritefln('Added Joystick %s', [ev.which]);
357 e_JoystickAvailable[ev.which] := True;
358 for i := 0 to Min(SDL_JoystickNumAxes(JoystickHandle[ev.which]), e_MaxJoyAxes) - 1 do
359 JoystickZeroAxes[ev.which, i] := SDL_JoystickGetAxis(JoystickHandle[ev.which], i)
360 end
361 else
362 begin
363 e_LogWritefln('Warning! Failed to open Joystick %s', [ev.which])
364 end
365 end
366 else
367 begin
368 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev.which, e_MaxJoys])
369 end
370 end;
372 procedure HandleJoyRemove (var ev: TSDL_JoyDeviceEvent);
373 begin
374 e_LogWritefln('Removed Joystick %s', [ev.which]);
375 if (ev.which < e_MaxJoys) then
376 begin
377 e_JoystickAvailable[ev.which] := False;
378 if JoystickHandle[ev.which] <> nil then
379 SDL_JoystickClose(JoystickHandle[ev.which]);
380 JoystickHandle[ev.which] := nil
381 end
382 end;
384 (* --------- Touch --------- *)
386 {$IFDEF ENABLE_TOUCH}
387 function sys_IsTextInputActive (): Boolean;
388 begin
389 Result := SDL_IsTextInputActive() = SDL_True
390 end;
392 procedure sys_ShowKeyboard (yes: Boolean);
393 begin
394 if g_dbg_input then
395 e_LogWritefln('g_Touch_ShowKeyboard(%s)', [yes]);
396 (* on desktop we always receive text (needed for cheats) *)
397 if yes or (SDL_HasScreenKeyboardSupport() = SDL_FALSE) then
398 SDL_StartTextInput
399 else
400 SDL_StopTextInput
401 end;
403 procedure HandleTouch (const ev: TSDL_TouchFingerEvent);
404 var
405 x, y, i, finger: Integer;
407 function IntersectControl(ctl, xx, yy: Integer): Boolean;
408 {$IFDEF ENABLE_RENDER}
409 var x, y, w, h: Integer; founded: Boolean;
410 {$ENDIF}
411 begin
412 {$IFDEF ENABLE_RENDER}
413 r_Render_GetKeyRect(ctl, x, y, w, h, founded);
414 Result := founded and (xx >= x) and (yy >= y) and (xx <= x + w) and (yy <= y + h);
415 {$ELSE}
416 Result := False
417 {$ENDIF}
418 end;
420 procedure KeyUp (finger, i: Integer);
421 begin
422 if g_dbg_input then
423 e_LogWritefln('Input Debug: g_touch.KeyUp, finger=%s, key=%s', [finger, i]);
425 keyFinger[i] := 0;
426 e_KeyUpDown(i, False);
427 g_Console_ProcessBind(i, False);
429 (* up/down + fire hack *)
430 {$IFDEF ENABLE_MENU}
431 if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) and angleFire then
432 {$ELSE}
433 if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and angleFire then
434 {$ENDIF}
435 begin
436 if (i = VK_UP) or (i = VK_DOWN) then
437 begin
438 angleFire := False;
439 keyFinger[VK_FIRE] := 0;
440 e_KeyUpDown(VK_FIRE, False);
441 g_Console_ProcessBind(VK_FIRE, False)
442 end
443 end
444 end;
446 procedure KeyDown (finger, i: Integer);
447 begin
448 if g_dbg_input then
449 e_LogWritefln('Input Debug: g_touch.KeyDown, finger=%s, key=%s', [finger, i]);
451 keyFinger[i] := finger;
452 e_KeyUpDown(i, True);
453 g_Console_ProcessBind(i, True);
455 (* up/down + fire hack *)
456 {$IFDEF ENABLE_MENU}
457 if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) then
458 {$ELSE}
459 if g_touch_fire and (gGameSettings.GameType <> GT_NONE) then
460 {$ENDIF}
461 begin
462 if i = VK_UP then
463 begin
464 angleFire := True;
465 keyFinger[VK_FIRE] := -1;
466 e_KeyUpDown(VK_FIRE, True);
467 g_Console_ProcessBind(VK_FIRE, True)
468 end
469 else if i = VK_DOWN then
470 begin
471 angleFire := True;
472 keyFinger[VK_FIRE] := -1;
473 e_KeyUpDown(VK_FIRE, True);
474 g_Console_ProcessBind(VK_FIRE, True)
475 end
476 end
477 end;
479 procedure KeyMotion (finger, i: Integer);
480 begin
481 if keyFinger[i] <> finger then
482 begin
483 KeyUp(finger, i);
484 KeyDown(finger, i)
485 end
486 end;
488 begin
489 if not g_touch_enabled then
490 Exit;
492 finger := ev.fingerId + 2;
493 x := Trunc(ev.x * gWinSizeX);
494 y := Trunc(ev.y * gWinSizeY);
496 for i := VK_FIRSTKEY to VK_LASTKEY do
497 begin
498 if IntersectControl(i, x, y) then
499 begin
500 if ev.type_ = SDL_FINGERUP then
501 KeyUp(finger, i)
502 else if ev.type_ = SDL_FINGERMOTION then
503 KeyMotion(finger, i)
504 else if ev.type_ = SDL_FINGERDOWN then
505 keyDown(finger, i)
506 end
507 else if keyFinger[i] = finger then
508 begin
509 if ev.type_ = SDL_FINGERUP then
510 KeyUp(finger, i)
511 else if ev.type_ = SDL_FINGERMOTION then
512 KeyUp(finger, i)
513 end
514 end
515 end;
516 {$ENDIF} // TOUCH
518 (* --------- Input --------- *)
520 function HandleWindow (var ev: TSDL_WindowEvent): Boolean;
521 begin
522 result := false;
523 if g_dbg_input then
524 e_LogWritefln('Window Event: event = %s, data1 = %s, data2 = %s', [ev.event, ev.data1, ev.data2]);
525 case ev.event of
526 SDL_WINDOWEVENT_RESIZED:
527 if @sys_ScreenResize <> nil then
528 sys_ScreenResize(ev.data1, ev.data2);
529 SDL_WINDOWEVENT_EXPOSED: sys_Repaint;
530 SDL_WINDOWEVENT_CLOSE: result := true;
531 SDL_WINDOWEVENT_MOVED:
532 begin
533 wx := ev.data1;
534 wy := ev.data2
535 end;
536 SDL_WINDOWEVENT_FOCUS_LOST, SDL_WINDOWEVENT_MINIMIZED:
537 begin
538 e_UnpressAllKeys;
539 if gMuteWhenInactive then
540 e_MuteChannels(true);
541 {$IFDEF ENABLE_HOLMES}
542 if assigned(winBlurCB) then winBlurCB;
543 {$ENDIF}
544 end;
545 SDL_WINDOWEVENT_FOCUS_GAINED, SDL_WINDOWEVENT_MAXIMIZED, SDL_WINDOWEVENT_RESTORED:
546 begin
547 if ev.event = SDL_WINDOWEVENT_MAXIMIZED then
548 begin
549 gWinMaximized := true;
550 gRC_Maximized := true
551 end
552 else if ev.event = SDL_WINDOWEVENT_RESTORED then
553 begin
554 gWinMaximized := false;
555 gRC_Maximized := false
556 end;
557 e_MuteChannels(false);
558 {$IFDEF ENABLE_HOLMES}
559 if assigned(winFocusCB) then winFocusCB;
560 {$ENDIF}
561 end;
562 end
563 end;
565 procedure HandleKeyboard (var ev: TSDL_KeyboardEvent);
566 var down: Boolean; key: Integer;
567 begin
568 key := ev.keysym.scancode;
569 down := (ev.type_ = SDL_KEYDOWN);
570 if key = SDL_SCANCODE_AC_BACK then
571 key := SDL_SCANCODE_ESCAPE;
572 {$IFDEF ENABLE_HOLMES}
573 if fuiOnSDLEvent(PSDL_Event(@ev)^) then
574 begin
575 // event eaten, but...
576 if not down then e_KeyUpDown(key, false);
577 exit;
578 end;
579 {$ENDIF}
580 if ev._repeat = 0 then
581 begin
582 if g_dbg_input then
583 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down, key]);
584 e_KeyUpDown(key, down);
585 g_Console_ProcessBind(key, down);
586 end
587 else
588 begin
589 if g_dbg_input then
590 e_LogWritefln('Input Debug: keyrep, scancode=%s', [key]);
591 g_Console_ProcessBindRepeat(key);
592 end
593 end;
595 procedure HandleTextInput (var ev: TSDL_TextInputEvent);
596 var ch: UnicodeChar; sch: AnsiChar;
597 begin
598 Utf8ToUnicode(@ch, PChar(ev.text), 1);
599 sch := AnsiChar(wchar2win(ch));
600 if g_dbg_input then
601 e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev.text, Ord(ch), Ord(sch)]);
602 if @sys_CharPress <> nil then
603 if IsValid1251(Word(ch)) and IsPrintable1251(ch) then
604 sys_CharPress(sch)
605 end;
607 function sys_HandleInput (): Boolean;
608 var ev: TSDL_Event;
609 begin
610 result := false;
611 ZeroMemory(@ev, sizeof(ev));
612 while SDL_PollEvent(@ev) <> 0 do
613 begin
614 case ev.type_ of
615 SDL_QUITEV: result := true;
616 SDL_WINDOWEVENT: result := HandleWindow(ev.window);
617 SDL_KEYUP, SDL_KEYDOWN: HandleKeyboard(ev.key);
618 SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP: HandleJoyButton(ev.jbutton);
619 SDL_JOYAXISMOTION: HandleJoyAxis(ev.jaxis);
620 SDL_JOYHATMOTION: HandleJoyHat(ev.jhat);
621 SDL_JOYDEVICEADDED: HandleJoyAdd(ev.jdevice);
622 SDL_JOYDEVICEREMOVED: HandleJoyRemove(ev.jdevice);
623 SDL_TEXTINPUT: HandleTextInput(ev.text);
624 {$IFDEF ENABLE_TOUCH}
625 SDL_FINGERMOTION, SDL_FINGERDOWN, SDL_FINGERUP: HandleTouch(ev.tfinger);
626 {$ENDIF}
627 {$IFDEF ENABLE_HOLMES}
628 SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION: fuiOnSDLEvent(ev);
629 {$ENDIF}
630 end
631 end
632 end;
634 procedure sys_RequestQuit;
635 var ev: TSDL_Event;
636 begin
637 ev.type_ := SDL_QUITEV;
638 SDL_PushEvent(@ev)
639 end;
641 (* --------- Init --------- *)
643 procedure sys_Init;
644 var flags: UInt32;
645 begin
646 e_WriteLog('Init SDL2', TMsgType.Notify);
647 {$IFDEF HEADLESS}
648 {$IFDEF USE_SDLMIXER}
649 flags := SDL_INIT_TIMER or SDL_INIT_AUDIO or $00004000;
650 {$ELSE}
651 flags := SDL_INIT_TIMER or $00004000;
652 {$ENDIF}
653 {$ELSE}
654 flags := SDL_INIT_TIMER or SDL_INIT_VIDEO;
655 {$ENDIF}
656 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, '0');
657 if SDL_Init(flags) <> 0 then
658 raise Exception.Create('SDL: Init failed: ' + SDL_GetError);
659 {$IFNDEF HEADLESS}
660 if SDL_InitSubSystem(SDL_INIT_JOYSTICK) <> 0 then
661 e_LogWritefln('SDL: Init subsystem failed: %s', [SDL_GetError()]);
662 {$ENDIF}
663 SDL_ShowCursor(SDL_DISABLE);
664 {$IFDEF ENABLE_TOUCH}
665 sys_ShowKeyboard(FALSE);
666 g_touch_enabled := SDL_GetNumTouchDevices() > 0;
667 {$ELSE}
668 g_touch_enabled := False;
669 {$ENDIF}
670 end;
672 procedure sys_Final;
673 begin
674 e_WriteLog('Releasing SDL2', TMsgType.Notify);
675 if context <> nil then
676 begin
677 SDL_GL_DeleteContext(context);
678 context := nil;
679 end;
680 if window <> nil then
681 begin
682 SDL_DestroyWindow(window);
683 window := nil;
684 end;
685 SDL_Quit
686 end;
688 initialization
689 conRegVar('sdl2_display_index', @display, 'use display index as base', '');
690 conRegVar('sdl2_window_x', @wx, 'window position x', '');
691 conRegVar('sdl2_window_y', @wy, 'window position y', '');
692 conRegVar('sdl2_window_center', @wc, 'force window creation at center', '');
693 display := 0;
694 wx := SDL_WINDOWPOS_CENTERED;
695 wy := SDL_WINDOWPOS_CENTERED;
696 wc := false
697 end.