DEADSOFTWARE

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