DEADSOFTWARE

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