1 (* Copyright (C) Doom 2D: Forever Developers
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.
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.
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/>.
15 {$INCLUDE ../shared/a_modes.inc}
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
;
29 function sys_HandleInput (): Boolean;
30 procedure sys_RequestQuit
;
33 function sys_IsTextInputActive (): Boolean;
34 procedure sys_ShowKeyboard (yes
: Boolean);
42 sys_CharPress
: procedure (ch
: AnsiChar) = nil;
43 sys_ScreenResize
: procedure (w
, h
: Integer) = nil;
48 {$IFDEF ENABLE_HOLMES}
51 {$IFDEF ENABLE_RENDER}
57 SysUtils
, SDL2
, Math
, ctypes
,
58 e_log
, e_input
, e_sound
,
59 g_options
, g_console
, g_game
, g_basic
63 GameTitle
= 'Doom 2D: Forever (SDL 2, %s)';
67 context
: TSDL_GLContext
;
68 display
, wx
, wy
: Integer;
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;
77 keyFinger
: array [VK_FIRSTKEY
..VK_LASTKEY
] of Integer;
80 (* --------- Graphics --------- *)
82 function GetTitle (): PChar;
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
]))
91 function InitWindow (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
92 var flags
: UInt32
; x
, y
: cint
;
94 // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
95 e_LogWritefln('InitWindow %s %s %s %s', [w
, h
, bpp
, fullScreen
]);
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
);
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...
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
;
118 x
:= SDL_WINDOWPOS_CENTERED
;
119 y
:= SDL_WINDOWPOS_CENTERED
126 window
:= SDL_CreateWindow(GetTitle(), x
, y
, w
, h
, flags
);
127 if window
<> nil then
129 context
:= SDL_GL_CreateContext(window
);
130 if context
<> nil then
132 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
134 SDL_GetWindowPosition(window
, @x
, @y
);
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
);
147 // SDL_DestroyWindow(window);
148 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError
])
153 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError
])
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
165 x
:= SDL_WINDOWPOS_CENTERED
;
166 y
:= SDL_WINDOWPOS_CENTERED
173 SDL_SetWindowPosition(window
, x
, y
);
174 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
176 SDL_GetWindowPosition(window
, @x
, @y
);
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
);
189 procedure sys_Repaint
;
191 SDL_GL_SwapWindow(window
)
194 procedure sys_EnableVSync (yes
: Boolean);
197 SDL_GL_SetSwapInterval(1)
199 SDL_GL_SetSwapInterval(0)
202 function sys_GetDisplayModes (bpp
: Integer): SSArray
;
203 var i
, count
, num
, pw
, ph
: Integer; m
: TSDL_DisplayMode
;
206 num
:= SDL_GetNumDisplayModes(display
);
208 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError
]);
211 e_LogWritefln('Video modes for display %s:', [display
]);
212 SetLength(result
, num
);
213 i
:= 0; count
:= 0; pw
:= 0; ph
:= 0;
216 SDL_GetDisplayMode(display
, i
, @m
);
217 if ((pw
<> m
.w
) or (ph
<> m
.h
)) then
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
);
226 e_LogWritefln('- %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
230 SetLength(result
, count
)
234 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
236 result
:= InitWindow(w
, h
, bpp
, fullScreen
, maximized
)
239 (* --------- Joystick --------- *)
241 procedure HandleJoyButton (var ev
: TSDL_JoyButtonEvent
);
242 var down
: Boolean; key
: Integer;
244 if (ev
.which
< e_MaxJoys
) and (ev
.button
< e_MaxJoyBtns
) then
246 key
:= e_JoyButtonToKey(ev
.which
, ev
.button
);
247 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
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
)
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
])
263 procedure HandleJoyAxis (var ev
: TSDL_JoyAxisEvent
);
264 var key
, minuskey
: Integer;
266 if (ev
.which
< e_MaxJoys
) and (ev
.axis
< e_MaxJoyAxes
) then
268 key
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_PLUS
);
269 minuskey
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_MINUS
);
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
276 if (e_KeyPressed(key
)) then
278 e_KeyUpDown(key
, False);
279 g_Console_ProcessBind(key
, False)
281 e_KeyUpDown(minuskey
, True);
282 g_Console_ProcessBind(minuskey
, True)
284 else if ev
.value
> JoystickZeroAxes
[ev
.which
, ev
.axis
] + e_JoystickDeadzones
[ev
.which
] then
286 if (e_KeyPressed(minuskey
)) then
288 e_KeyUpDown(minuskey
, False);
289 g_Console_ProcessBind(minuskey
, False)
291 e_KeyUpDown(key
, True);
292 g_Console_ProcessBind(key
, True)
296 if (e_KeyPressed(minuskey
)) then
298 e_KeyUpDown(minuskey
, False);
299 g_Console_ProcessBind(minuskey
, False)
301 if (e_KeyPressed(key
)) then
303 e_KeyUpDown(key
, False);
304 g_Console_ProcessBind(key
, False)
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
]])
315 procedure HandleJoyHat (var ev
: TSDL_JoyHatEvent
);
319 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
321 if (ev
.which
< e_MaxJoys
) and (ev
.hat
< e_MaxJoyHats
) 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
331 if JoystickHatState
[ev
.which
, ev
.hat
, i
] <> hat
[i
] then
334 key
:= e_JoyHatToKey(ev
.which
, ev
.hat
, i
);
335 e_KeyUpDown(key
, down
);
336 g_Console_ProcessBind(key
, down
)
339 JoystickHatState
[ev
.which
, ev
.hat
] := hat
344 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
])
348 procedure HandleJoyAdd (var ev
: TSDL_JoyDeviceEvent
);
351 if (ev
.which
< e_MaxJoys
) then
353 JoystickHandle
[ev
.which
] := SDL_JoystickOpen(ev
.which
);
354 if JoystickHandle
[ev
.which
] <> nil then
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
)
363 e_LogWritefln('Warning! Failed to open Joystick %s', [ev
.which
])
368 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev
.which
, e_MaxJoys
])
372 procedure HandleJoyRemove (var ev
: TSDL_JoyDeviceEvent
);
374 e_LogWritefln('Removed Joystick %s', [ev
.which
]);
375 if (ev
.which
< e_MaxJoys
) then
377 e_JoystickAvailable
[ev
.which
] := False;
378 if JoystickHandle
[ev
.which
] <> nil then
379 SDL_JoystickClose(JoystickHandle
[ev
.which
]);
380 JoystickHandle
[ev
.which
] := nil
384 (* --------- Touch --------- *)
386 {$IFDEF ENABLE_TOUCH}
387 function sys_IsTextInputActive (): Boolean;
389 Result
:= SDL_IsTextInputActive() = SDL_True
392 procedure sys_ShowKeyboard (yes
: Boolean);
396 e_LogWritefln('g_Touch_ShowKeyboard(%s)', [yes
]);
397 (* on desktop we always receive text (needed for cheats) *)
398 if yes
or (SDL_HasScreenKeyboardSupport() = SDL_FALSE
) then
405 procedure HandleTouch (const ev
: TSDL_TouchFingerEvent
);
407 x
, y
, i
, finger
: Integer;
409 function IntersectControl(ctl
, xx
, yy
: Integer): Boolean;
410 {$IFDEF ENABLE_RENDER}
411 var x
, y
, w
, h
: Integer; founded
: Boolean;
414 {$IFDEF ENABLE_RENDER}
415 r_Render_GetKeyRect(ctl
, x
, y
, w
, h
, founded
);
416 Result
:= founded
and (xx
>= x
) and (yy
>= y
) and (xx
<= x
+ w
) and (yy
<= y
+ h
);
422 procedure KeyUp (finger
, i
: Integer);
425 e_LogWritefln('Input Debug: g_touch.KeyUp, finger=%s, key=%s', [finger
, i
]);
428 e_KeyUpDown(i
, False);
429 g_Console_ProcessBind(i
, False);
431 (* up/down + fire hack *)
433 if g_touch_fire
and (gGameSettings
.GameType
<> GT_NONE
) and (g_ActiveWindow
= nil) and angleFire
then
435 if g_touch_fire
and (gGameSettings
.GameType
<> GT_NONE
) and angleFire
then
438 if (i
= VK_UP
) or (i
= VK_DOWN
) then
441 keyFinger
[VK_FIRE
] := 0;
442 e_KeyUpDown(VK_FIRE
, False);
443 g_Console_ProcessBind(VK_FIRE
, False)
448 procedure KeyDown (finger
, i
: Integer);
451 e_LogWritefln('Input Debug: g_touch.KeyDown, finger=%s, key=%s', [finger
, i
]);
453 keyFinger
[i
] := finger
;
454 e_KeyUpDown(i
, True);
455 g_Console_ProcessBind(i
, True);
457 (* up/down + fire hack *)
459 if g_touch_fire
and (gGameSettings
.GameType
<> GT_NONE
) and (g_ActiveWindow
= nil) then
461 if g_touch_fire
and (gGameSettings
.GameType
<> GT_NONE
) then
467 keyFinger
[VK_FIRE
] := -1;
468 e_KeyUpDown(VK_FIRE
, True);
469 g_Console_ProcessBind(VK_FIRE
, True)
471 else if i
= VK_DOWN
then
474 keyFinger
[VK_FIRE
] := -1;
475 e_KeyUpDown(VK_FIRE
, True);
476 g_Console_ProcessBind(VK_FIRE
, True)
481 procedure KeyMotion (finger
, i
: Integer);
483 if keyFinger
[i
] <> finger
then
491 if not g_touch_enabled
then
494 finger
:= ev
.fingerId
+ 2;
495 x
:= Trunc(ev
.x
* gWinSizeX
);
496 y
:= Trunc(ev
.y
* gWinSizeY
);
498 for i
:= VK_FIRSTKEY
to VK_LASTKEY
do
500 if IntersectControl(i
, x
, y
) then
502 if ev
.type_
= SDL_FINGERUP
then
504 else if ev
.type_
= SDL_FINGERMOTION
then
506 else if ev
.type_
= SDL_FINGERDOWN
then
509 else if keyFinger
[i
] = finger
then
511 if ev
.type_
= SDL_FINGERUP
then
513 else if ev
.type_
= SDL_FINGERMOTION
then
520 (* --------- Input --------- *)
522 function HandleWindow (var ev
: TSDL_WindowEvent
): Boolean;
526 e_LogWritefln('Window Event: event = %s, data1 = %s, data2 = %s', [ev
.event
, ev
.data1
, ev
.data2
]);
528 SDL_WINDOWEVENT_RESIZED
:
529 if @sys_ScreenResize
<> nil then
530 sys_ScreenResize(ev
.data1
, ev
.data2
);
531 SDL_WINDOWEVENT_EXPOSED
: sys_Repaint
;
532 SDL_WINDOWEVENT_CLOSE
: result
:= true;
533 SDL_WINDOWEVENT_MOVED
:
538 SDL_WINDOWEVENT_FOCUS_LOST
, SDL_WINDOWEVENT_MINIMIZED
:
541 if gMuteWhenInactive
then
542 e_MuteChannels(true);
543 {$IFDEF ENABLE_HOLMES}
544 if assigned(winBlurCB
) then winBlurCB
;
547 SDL_WINDOWEVENT_FOCUS_GAINED
, SDL_WINDOWEVENT_MAXIMIZED
, SDL_WINDOWEVENT_RESTORED
:
549 if ev
.event
= SDL_WINDOWEVENT_MAXIMIZED
then
551 gWinMaximized
:= true;
552 gRC_Maximized
:= true
554 else if ev
.event
= SDL_WINDOWEVENT_RESTORED
then
556 gWinMaximized
:= false;
557 gRC_Maximized
:= false
559 e_MuteChannels(false);
560 {$IFDEF ENABLE_HOLMES}
561 if assigned(winFocusCB
) then winFocusCB
;
567 procedure HandleKeyboard (var ev
: TSDL_KeyboardEvent
);
568 var down
: Boolean; key
: Integer;
570 key
:= ev
.keysym
.scancode
;
571 down
:= (ev
.type_
= SDL_KEYDOWN
);
572 if key
= SDL_SCANCODE_AC_BACK
then
573 key
:= SDL_SCANCODE_ESCAPE
;
574 {$IFDEF ENABLE_HOLMES}
575 if fuiOnSDLEvent(PSDL_Event(@ev
)^) then
577 // event eaten, but...
578 if not down
then e_KeyUpDown(key
, false);
582 if ev
._repeat
= 0 then
585 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down
, key
]);
586 e_KeyUpDown(key
, down
);
587 g_Console_ProcessBind(key
, down
);
592 e_LogWritefln('Input Debug: keyrep, scancode=%s', [key
]);
593 g_Console_ProcessBindRepeat(key
);
597 procedure HandleTextInput (var ev
: TSDL_TextInputEvent
);
598 var ch
: UnicodeChar
; sch
: AnsiChar;
600 Utf8ToUnicode(@ch
, PChar(ev
.text), 1);
601 sch
:= AnsiChar(wchar2win(ch
));
603 e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev
.text, Ord(ch
), Ord(sch
)]);
604 if @sys_CharPress
<> nil then
605 if IsValid1251(Word(ch
)) and IsPrintable1251(ch
) then
609 function sys_HandleInput (): Boolean;
613 ZeroMemory(@ev
, sizeof(ev
));
614 while SDL_PollEvent(@ev
) <> 0 do
617 SDL_QUITEV
: result
:= true;
618 SDL_WINDOWEVENT
: result
:= HandleWindow(ev
.window
);
619 SDL_KEYUP
, SDL_KEYDOWN
: HandleKeyboard(ev
.key
);
620 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
: HandleJoyButton(ev
.jbutton
);
621 SDL_JOYAXISMOTION
: HandleJoyAxis(ev
.jaxis
);
622 SDL_JOYHATMOTION
: HandleJoyHat(ev
.jhat
);
623 SDL_JOYDEVICEADDED
: HandleJoyAdd(ev
.jdevice
);
624 SDL_JOYDEVICEREMOVED
: HandleJoyRemove(ev
.jdevice
);
625 SDL_TEXTINPUT
: HandleTextInput(ev
.text);
626 {$IFDEF ENABLE_TOUCH}
627 SDL_FINGERMOTION
, SDL_FINGERDOWN
, SDL_FINGERUP
: HandleTouch(ev
.tfinger
);
629 {$IFDEF ENABLE_HOLMES}
630 SDL_MOUSEBUTTONDOWN
, SDL_MOUSEBUTTONUP
, SDL_MOUSEWHEEL
, SDL_MOUSEMOTION
: fuiOnSDLEvent(ev
);
636 procedure sys_RequestQuit
;
639 ev
.type_
:= SDL_QUITEV
;
643 (* --------- Init --------- *)
648 e_WriteLog('Init SDL2', TMsgType
.Notify
);
650 {$IFDEF USE_SDLMIXER}
651 flags
:= SDL_INIT_TIMER
or SDL_INIT_AUDIO
or $00004000;
653 flags
:= SDL_INIT_TIMER
or $00004000;
656 flags
:= SDL_INIT_TIMER
or SDL_INIT_VIDEO
;
658 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK
, '0');
659 if SDL_Init(flags
) <> 0 then
660 raise Exception
.Create('SDL: Init failed: ' + SDL_GetError
);
662 if SDL_InitSubSystem(SDL_INIT_JOYSTICK
) <> 0 then
663 e_LogWritefln('SDL: Init subsystem failed: %s', [SDL_GetError()]);
665 SDL_ShowCursor(SDL_DISABLE
);
666 {$IFDEF ENABLE_TOUCH}
667 sys_ShowKeyboard(FALSE);
668 g_touch_enabled
:= SDL_GetNumTouchDevices() > 0;
670 g_touch_enabled
:= False;
676 e_WriteLog('Releasing SDL2', TMsgType
.Notify
);
677 if context
<> nil then
679 SDL_GL_DeleteContext(context
);
682 if window
<> nil then
684 SDL_DestroyWindow(window
);
691 conRegVar('sdl2_display_index', @display
, 'use display index as base', '');
692 conRegVar('sdl2_window_x', @wx
, 'window position x', '');
693 conRegVar('sdl2_window_y', @wy
, 'window position y', '');
694 conRegVar('sdl2_window_center', @wc
, 'force window creation at center', '');
696 wx
:= SDL_WINDOWPOS_CENTERED
;
697 wy
:= SDL_WINDOWPOS_CENTERED
;