DEADSOFTWARE

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