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