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}
23 procedure sys_Delay (ms
: Integer);
25 (* --- Graphics --- *)
26 function sys_GetDisplayModes (bpp
: Integer): SSArray
;
27 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullscreen
, maximized
: Boolean): Boolean;
28 procedure sys_EnableVSync (yes
: Boolean);
29 procedure sys_Repaint
;
32 function sys_HandleInput (): Boolean;
33 procedure sys_RequestQuit
;
40 sys_CharPress
: procedure (ch
: AnsiChar) = nil;
41 sys_ScreenResize
: procedure (w
, h
: Integer) = nil;
46 SysUtils
, SDL2
, Math
, ctypes
,
47 e_log
, e_input
, e_sound
,
48 {$IFDEF ENABLE_HOLMES}
51 g_touch
, g_options
, g_console
, g_game
, g_basic
55 GameTitle
= 'Doom 2D: Forever (SDL 2, %s)';
59 context
: TSDL_GLContext
;
60 display
, wx
, wy
: Integer;
62 JoystickHandle
: array [0..e_MaxJoys
- 1] of PSDL_Joystick
;
63 JoystickHatState
: array [0..e_MaxJoys
- 1, 0..e_MaxJoyHats
- 1, HAT_LEFT
..HAT_DOWN
] of Boolean;
64 JoystickZeroAxes
: array [0..e_MaxJoys
- 1, 0..e_MaxJoyAxes
- 1] of Integer;
66 (* --------- Utils --------- *)
68 procedure sys_Delay (ms
: Integer);
73 (* --------- Graphics --------- *)
75 function GetTitle (): AnsiString;
78 info
:= g_GetBuildHash(false);
79 if info
= 'custom build' then
80 info
:= info
+ ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE
+ ' ' + GAME_BUILDTIME
;
81 result
:= Format(GameTitle
, [info
]);
84 function InitWindow (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
85 var flags
: UInt32
; x
, y
: cint
; title
: AnsiString;
87 // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
88 e_LogWritefln('InitWindow %s %s %s %s', [w
, h
, bpp
, fullScreen
]);
93 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 1);
94 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
95 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK
, SDL_GL_CONTEXT_PROFILE_ES
);
97 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
98 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
99 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
100 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
101 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
102 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
103 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
104 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
106 flags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
107 if fullScreen
then flags
:= flags
or SDL_WINDOW_FULLSCREEN
;
108 if maximized
then flags
:= flags
or SDL_WINDOW_MAXIMIZED
;
111 x
:= SDL_WINDOWPOS_CENTERED
;
112 y
:= SDL_WINDOWPOS_CENTERED
120 window
:= SDL_CreateWindow(PChar(title
), x
, y
, w
, h
, flags
);
121 if window
<> nil then
123 context
:= SDL_GL_CreateContext(window
);
124 if context
<> nil then
126 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
128 SDL_GetWindowPosition(window
, @x
, @y
);
131 gFullScreen
:= fullscreen
;
132 gWinMaximized
:= maximized
;
133 gRC_FullScreen
:= fullscreen
;
134 gRC_Maximized
:= maximized
;
135 if @sys_ScreenResize
<> nil then
136 sys_ScreenResize(w
, h
);
141 // SDL_DestroyWindow(window);
142 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError
])
147 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError
])
152 if fullScreen
then flags
:= SDL_WINDOW_FULLSCREEN
else flags
:= 0;
153 SDL_SetWindowFullscreen(window
, flags
);
154 SDL_SetWindowSize(window
, w
, h
);
155 if maximized
then SDL_MaximizeWindow(window
);
156 // always reset to center when changing fullscreen->windowed for safety purposes
157 if wc
or (gFullscreen
and not fullscreen
) or (gWinMaximized
and not maximized
) then
159 x
:= SDL_WINDOWPOS_CENTERED
;
160 y
:= SDL_WINDOWPOS_CENTERED
167 SDL_SetWindowPosition(window
, x
, y
);
168 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
170 SDL_GetWindowPosition(window
, @x
, @y
);
173 gFullScreen
:= fullscreen
;
174 gWinMaximized
:= maximized
;
175 gRC_FullScreen
:= fullscreen
;
176 gRC_Maximized
:= maximized
;
177 if @sys_ScreenResize
<> nil then
178 sys_ScreenResize(w
, h
);
183 procedure sys_Repaint
;
185 SDL_GL_SwapWindow(window
)
188 procedure sys_EnableVSync (yes
: Boolean);
191 SDL_GL_SetSwapInterval(1)
193 SDL_GL_SetSwapInterval(0)
196 function sys_GetDisplayModes (bpp
: Integer): SSArray
;
197 var i
, count
, num
, pw
, ph
: Integer; m
: TSDL_DisplayMode
;
200 num
:= SDL_GetNumDisplayModes(display
);
202 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError
]);
205 e_LogWritefln('Video modes for display %s:', [display
]);
206 SetLength(result
, num
);
207 i
:= 0; count
:= 0; pw
:= 0; ph
:= 0;
210 SDL_GetDisplayMode(display
, i
, @m
);
211 if ((pw
<> m
.w
) or (ph
<> m
.h
)) then
213 e_LogWritefln('* %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
214 pw
:= m
.w
; ph
:= m
.h
;
215 result
[count
] := IntToStr(m
.w
) + 'x' + IntToStr(m
.h
);
220 e_LogWritefln('- %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
224 SetLength(result
, count
)
228 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
230 result
:= InitWindow(w
, h
, bpp
, fullScreen
, maximized
)
233 (* --------- Joystick --------- *)
235 procedure HandleJoyButton (var ev
: TSDL_JoyButtonEvent
);
236 var down
: Boolean; key
: Integer;
238 if (ev
.which
< e_MaxJoys
) and (ev
.button
< e_MaxJoyBtns
) then
240 key
:= e_JoyButtonToKey(ev
.which
, ev
.button
);
241 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
243 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev
.which
, ev
.button
, key
, down
]);
244 e_KeyUpDown(key
, down
);
245 g_Console_ProcessBind(key
, down
)
251 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
252 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev
.which
, ev
.button
, down
])
257 procedure HandleJoyAxis (var ev
: TSDL_JoyAxisEvent
);
258 var key
, minuskey
: Integer;
260 if (ev
.which
< e_MaxJoys
) and (ev
.axis
< e_MaxJoyAxes
) then
262 key
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_PLUS
);
263 minuskey
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_MINUS
);
266 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
]]);
268 if ev
.value
< JoystickZeroAxes
[ev
.which
, ev
.axis
] - e_JoystickDeadzones
[ev
.which
] then
270 if (e_KeyPressed(key
)) then
272 e_KeyUpDown(key
, False);
273 g_Console_ProcessBind(key
, False)
275 e_KeyUpDown(minuskey
, True);
276 g_Console_ProcessBind(minuskey
, True)
278 else if ev
.value
> JoystickZeroAxes
[ev
.which
, ev
.axis
] + e_JoystickDeadzones
[ev
.which
] then
280 if (e_KeyPressed(minuskey
)) then
282 e_KeyUpDown(minuskey
, False);
283 g_Console_ProcessBind(minuskey
, False)
285 e_KeyUpDown(key
, True);
286 g_Console_ProcessBind(key
, True)
290 if (e_KeyPressed(minuskey
)) then
292 e_KeyUpDown(minuskey
, False);
293 g_Console_ProcessBind(minuskey
, False)
295 if (e_KeyPressed(key
)) then
297 e_KeyUpDown(key
, False);
298 g_Console_ProcessBind(key
, False)
305 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
]])
309 procedure HandleJoyHat (var ev
: TSDL_JoyHatEvent
);
313 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
315 if (ev
.which
< e_MaxJoys
) and (ev
.hat
< e_MaxJoyHats
) then
318 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
]);
319 hat
[HAT_UP
] := LongBool(ev
.value
and SDL_HAT_UP
);
320 hat
[HAT_DOWN
] := LongBool(ev
.value
and SDL_HAT_DOWN
);
321 hat
[HAT_LEFT
] := LongBool(ev
.value
and SDL_HAT_LEFT
);
322 hat
[HAT_RIGHT
] := LongBool(ev
.value
and SDL_HAT_RIGHT
);
323 for i
:= HAT_LEFT
to HAT_DOWN
do
325 if JoystickHatState
[ev
.which
, ev
.hat
, i
] <> hat
[i
] then
328 key
:= e_JoyHatToKey(ev
.which
, ev
.hat
, i
);
329 e_KeyUpDown(key
, down
);
330 g_Console_ProcessBind(key
, down
)
333 JoystickHatState
[ev
.which
, ev
.hat
] := hat
338 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
])
342 procedure HandleJoyAdd (var ev
: TSDL_JoyDeviceEvent
);
345 if (ev
.which
< e_MaxJoys
) then
347 JoystickHandle
[ev
.which
] := SDL_JoystickOpen(ev
.which
);
348 if JoystickHandle
[ev
.which
] <> nil then
350 e_LogWritefln('Added Joystick %s', [ev
.which
]);
351 e_JoystickAvailable
[ev
.which
] := True;
352 for i
:= 0 to Min(SDL_JoystickNumAxes(JoystickHandle
[ev
.which
]), e_MaxJoyAxes
) - 1 do
353 JoystickZeroAxes
[ev
.which
, i
] := SDL_JoystickGetAxis(JoystickHandle
[ev
.which
], i
)
357 e_LogWritefln('Warning! Failed to open Joystick %s', [ev
.which
])
362 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev
.which
, e_MaxJoys
])
366 procedure HandleJoyRemove (var ev
: TSDL_JoyDeviceEvent
);
368 e_LogWritefln('Removed Joystick %s', [ev
.which
]);
369 if (ev
.which
< e_MaxJoys
) then
371 e_JoystickAvailable
[ev
.which
] := False;
372 if JoystickHandle
[ev
.which
] <> nil then
373 SDL_JoystickClose(JoystickHandle
[ev
.which
]);
374 JoystickHandle
[ev
.which
] := nil
378 (* --------- Input --------- *)
380 function HandleWindow (var ev
: TSDL_WindowEvent
): Boolean;
384 e_LogWritefln('Window Event: event = %s, data1 = %s, data2 = %s', [ev
.event
, ev
.data1
, ev
.data2
]);
386 SDL_WINDOWEVENT_RESIZED
:
387 if @sys_ScreenResize
<> nil then
388 sys_ScreenResize(ev
.data1
, ev
.data2
);
389 SDL_WINDOWEVENT_EXPOSED
: sys_Repaint
;
390 SDL_WINDOWEVENT_CLOSE
: result
:= true;
391 SDL_WINDOWEVENT_MOVED
:
396 SDL_WINDOWEVENT_FOCUS_LOST
, SDL_WINDOWEVENT_MINIMIZED
:
399 if gMuteWhenInactive
then
400 e_MuteChannels(true);
401 {$IFDEF ENABLE_HOLMES}
402 if assigned(winBlurCB
) then winBlurCB
;
405 SDL_WINDOWEVENT_FOCUS_GAINED
, SDL_WINDOWEVENT_MAXIMIZED
, SDL_WINDOWEVENT_RESTORED
:
407 if ev
.event
= SDL_WINDOWEVENT_MAXIMIZED
then
409 gWinMaximized
:= true;
410 gRC_Maximized
:= true
412 else if ev
.event
= SDL_WINDOWEVENT_RESTORED
then
414 gWinMaximized
:= false;
415 gRC_Maximized
:= false
417 e_MuteChannels(false);
418 {$IFDEF ENABLE_HOLMES}
419 if assigned(winFocusCB
) then winFocusCB
;
425 procedure HandleKeyboard (var ev
: TSDL_KeyboardEvent
);
426 var down
: Boolean; key
: Integer;
428 key
:= ev
.keysym
.scancode
;
429 down
:= (ev
.type_
= SDL_KEYDOWN
);
430 if key
= SDL_SCANCODE_AC_BACK
then
431 key
:= SDL_SCANCODE_ESCAPE
;
432 {$IFDEF ENABLE_HOLMES}
433 if fuiOnSDLEvent(PSDL_Event(@ev
)^) then
435 // event eaten, but...
436 if not down
then e_KeyUpDown(key
, false);
440 if ev
._repeat
= 0 then
443 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down
, key
]);
444 e_KeyUpDown(key
, down
);
445 g_Console_ProcessBind(key
, down
);
450 e_LogWritefln('Input Debug: keyrep, scancode=%s', [key
]);
451 g_Console_ProcessBindRepeat(key
);
455 procedure HandleTextInput (var ev
: TSDL_TextInputEvent
);
456 var ch
: UnicodeChar
; sch
: AnsiChar;
458 Utf8ToUnicode(@ch
, PChar(ev
.text), 1);
459 sch
:= AnsiChar(wchar2win(ch
));
461 e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev
.text, Ord(ch
), Ord(sch
)]);
462 if @sys_CharPress
<> nil then
463 if IsValid1251(Word(ch
)) and IsPrintable1251(ch
) then
467 function sys_HandleInput (): Boolean;
471 ZeroMemory(@ev
, sizeof(ev
));
472 while SDL_PollEvent(@ev
) <> 0 do
475 SDL_QUITEV
: result
:= true;
476 SDL_WINDOWEVENT
: result
:= HandleWindow(ev
.window
);
477 SDL_KEYUP
, SDL_KEYDOWN
: HandleKeyboard(ev
.key
);
478 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
: HandleJoyButton(ev
.jbutton
);
479 SDL_JOYAXISMOTION
: HandleJoyAxis(ev
.jaxis
);
480 SDL_JOYHATMOTION
: HandleJoyHat(ev
.jhat
);
481 SDL_JOYDEVICEADDED
: HandleJoyAdd(ev
.jdevice
);
482 SDL_JOYDEVICEREMOVED
: HandleJoyRemove(ev
.jdevice
);
483 SDL_TEXTINPUT
: HandleTextInput(ev
.text);
484 SDL_FINGERMOTION
, SDL_FINGERDOWN
, SDL_FINGERUP
: g_Touch_HandleEvent(ev
.tfinger
);
485 {$IFDEF ENABLE_HOLMES}
486 SDL_MOUSEBUTTONDOWN
, SDL_MOUSEBUTTONUP
, SDL_MOUSEWHEEL
, SDL_MOUSEMOTION
: fuiOnSDLEvent(ev
);
492 procedure sys_RequestQuit
;
495 ev
.type_
:= SDL_QUITEV
;
499 (* --------- Init --------- *)
504 e_WriteLog('Init SDL2', TMsgType
.Notify
);
506 {$IFDEF USE_SDLMIXER}
507 flags
:= SDL_INIT_TIMER
or SDL_INIT_AUDIO
or $00004000;
509 flags
:= SDL_INIT_TIMER
or $00004000;
512 flags
:= SDL_INIT_TIMER
or SDL_INIT_VIDEO
;
514 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK
, '0');
515 if SDL_Init(flags
) <> 0 then
516 raise Exception
.Create('SDL: Init failed: ' + SDL_GetError
);
518 if SDL_InitSubSystem(SDL_INIT_JOYSTICK
) <> 0 then
519 e_LogWritefln('SDL: Init subsystem failed: %s', [SDL_GetError()]);
521 SDL_ShowCursor(SDL_DISABLE
);
526 e_WriteLog('Releasing SDL2', TMsgType
.Notify
);
527 if context
<> nil then
529 SDL_GL_DeleteContext(context
);
532 if window
<> nil then
534 SDL_DestroyWindow(window
);
541 conRegVar('sdl2_display_index', @display
, 'use display index as base', '');
542 conRegVar('sdl2_window_x', @wx
, 'window position x', '');
543 conRegVar('sdl2_window_y', @wy
, 'window position y', '');
544 conRegVar('sdl2_window_center', @wc
, 'force window creation at center', '');
546 wx
:= SDL_WINDOWPOS_CENTERED
;
547 wy
:= SDL_WINDOWPOS_CENTERED
;