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
;
37 sys_CharPress
: procedure (ch
: AnsiChar) = nil;
38 sys_ScreenResize
: procedure (w
, h
: Integer) = nil;
43 SysUtils
, SDL2
, Math
, ctypes
,
44 e_log
, e_input
, e_sound
,
45 {$IFDEF ENABLE_HOLMES}
48 g_touch
, g_options
, g_console
, g_game
, g_basic
52 GameTitle
= 'Doom 2D: Forever (SDL 2, %s)';
56 context
: TSDL_GLContext
;
57 display
, wx
, wy
: Integer;
59 JoystickHandle
: array [0..e_MaxJoys
- 1] of PSDL_Joystick
;
60 JoystickHatState
: array [0..e_MaxJoys
- 1, 0..e_MaxJoyHats
- 1, HAT_LEFT
..HAT_DOWN
] of Boolean;
61 JoystickZeroAxes
: array [0..e_MaxJoys
- 1, 0..e_MaxJoyAxes
- 1] of Integer;
63 (* --------- Graphics --------- *)
65 function GetTitle (): PChar;
68 info
:= g_GetBuildHash(false);
69 if info
= 'custom build' then
70 info
:= info
+ ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE
+ ' ' + GAME_BUILDTIME
;
71 result
:= PChar(Format(GameTitle
, [info
]))
74 function InitWindow (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
75 var flags
: UInt32
; x
, y
: cint
;
77 // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
78 e_LogWritefln('InitWindow %s %s %s %s', [w
, h
, bpp
, fullScreen
]);
83 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 1);
84 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
85 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK
, SDL_GL_CONTEXT_PROFILE_ES
);
87 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
88 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
89 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
90 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
91 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
92 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
93 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
94 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
96 flags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
97 if fullScreen
then flags
:= flags
or SDL_WINDOW_FULLSCREEN
;
98 if maximized
then flags
:= flags
or SDL_WINDOW_MAXIMIZED
;
101 x
:= SDL_WINDOWPOS_CENTERED
;
102 y
:= SDL_WINDOWPOS_CENTERED
109 window
:= SDL_CreateWindow(GetTitle(), x
, y
, w
, h
, flags
);
110 if window
<> nil then
112 context
:= SDL_GL_CreateContext(window
);
113 if context
<> nil then
115 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
117 SDL_GetWindowPosition(window
, @x
, @y
);
120 gFullScreen
:= fullscreen
;
121 gWinMaximized
:= maximized
;
122 gRC_FullScreen
:= fullscreen
;
123 gRC_Maximized
:= maximized
;
124 if @sys_ScreenResize
<> nil then
125 sys_ScreenResize(w
, h
);
130 // SDL_DestroyWindow(window);
131 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError
])
136 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError
])
141 if fullScreen
then flags
:= SDL_WINDOW_FULLSCREEN
else flags
:= 0;
142 SDL_SetWindowFullscreen(window
, flags
);
143 SDL_SetWindowSize(window
, w
, h
);
144 if maximized
then SDL_MaximizeWindow(window
);
145 // always reset to center when changing fullscreen->windowed for safety purposes
146 if wc
or (gFullscreen
and not fullscreen
) or (gWinMaximized
and not maximized
) then
148 x
:= SDL_WINDOWPOS_CENTERED
;
149 y
:= SDL_WINDOWPOS_CENTERED
156 SDL_SetWindowPosition(window
, x
, y
);
157 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
159 SDL_GetWindowPosition(window
, @x
, @y
);
162 gFullScreen
:= fullscreen
;
163 gWinMaximized
:= maximized
;
164 gRC_FullScreen
:= fullscreen
;
165 gRC_Maximized
:= maximized
;
166 if @sys_ScreenResize
<> nil then
167 sys_ScreenResize(w
, h
);
172 procedure sys_Repaint
;
174 SDL_GL_SwapWindow(window
)
177 procedure sys_EnableVSync (yes
: Boolean);
180 SDL_GL_SetSwapInterval(1)
182 SDL_GL_SetSwapInterval(0)
185 function sys_GetDisplayModes (bpp
: Integer): SSArray
;
186 var i
, count
, num
, pw
, ph
: Integer; m
: TSDL_DisplayMode
;
189 num
:= SDL_GetNumDisplayModes(display
);
191 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError
]);
194 e_LogWritefln('Video modes for display %s:', [display
]);
195 SetLength(result
, num
);
196 i
:= 0; count
:= 0; pw
:= 0; ph
:= 0;
199 SDL_GetDisplayMode(display
, i
, @m
);
200 if ((pw
<> m
.w
) or (ph
<> m
.h
)) then
202 e_LogWritefln('* %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
203 pw
:= m
.w
; ph
:= m
.h
;
204 result
[count
] := IntToStr(m
.w
) + 'x' + IntToStr(m
.h
);
209 e_LogWritefln('- %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
213 SetLength(result
, count
)
217 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
219 result
:= InitWindow(w
, h
, bpp
, fullScreen
, maximized
)
222 (* --------- Joystick --------- *)
224 procedure HandleJoyButton (var ev
: TSDL_JoyButtonEvent
);
225 var down
: Boolean; key
: Integer;
227 if (ev
.which
< e_MaxJoys
) and (ev
.button
< e_MaxJoyBtns
) then
229 key
:= e_JoyButtonToKey(ev
.which
, ev
.button
);
230 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
232 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev
.which
, ev
.button
, key
, down
]);
233 e_KeyUpDown(key
, down
);
234 g_Console_ProcessBind(key
, down
)
240 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
241 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev
.which
, ev
.button
, down
])
246 procedure HandleJoyAxis (var ev
: TSDL_JoyAxisEvent
);
247 var key
, minuskey
: Integer;
249 if (ev
.which
< e_MaxJoys
) and (ev
.axis
< e_MaxJoyAxes
) then
251 key
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_PLUS
);
252 minuskey
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_MINUS
);
255 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
]]);
257 if ev
.value
< JoystickZeroAxes
[ev
.which
, ev
.axis
] - e_JoystickDeadzones
[ev
.which
] then
259 if (e_KeyPressed(key
)) then
261 e_KeyUpDown(key
, False);
262 g_Console_ProcessBind(key
, False)
264 e_KeyUpDown(minuskey
, True);
265 g_Console_ProcessBind(minuskey
, True)
267 else if ev
.value
> JoystickZeroAxes
[ev
.which
, ev
.axis
] + e_JoystickDeadzones
[ev
.which
] then
269 if (e_KeyPressed(minuskey
)) then
271 e_KeyUpDown(minuskey
, False);
272 g_Console_ProcessBind(minuskey
, False)
274 e_KeyUpDown(key
, True);
275 g_Console_ProcessBind(key
, True)
279 if (e_KeyPressed(minuskey
)) then
281 e_KeyUpDown(minuskey
, False);
282 g_Console_ProcessBind(minuskey
, False)
284 if (e_KeyPressed(key
)) then
286 e_KeyUpDown(key
, False);
287 g_Console_ProcessBind(key
, False)
294 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
]])
298 procedure HandleJoyHat (var ev
: TSDL_JoyHatEvent
);
302 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
304 if (ev
.which
< e_MaxJoys
) and (ev
.hat
< e_MaxJoyHats
) then
307 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
]);
308 hat
[HAT_UP
] := LongBool(ev
.value
and SDL_HAT_UP
);
309 hat
[HAT_DOWN
] := LongBool(ev
.value
and SDL_HAT_DOWN
);
310 hat
[HAT_LEFT
] := LongBool(ev
.value
and SDL_HAT_LEFT
);
311 hat
[HAT_RIGHT
] := LongBool(ev
.value
and SDL_HAT_RIGHT
);
312 for i
:= HAT_LEFT
to HAT_DOWN
do
314 if JoystickHatState
[ev
.which
, ev
.hat
, i
] <> hat
[i
] then
317 key
:= e_JoyHatToKey(ev
.which
, ev
.hat
, i
);
318 e_KeyUpDown(key
, down
);
319 g_Console_ProcessBind(key
, down
)
322 JoystickHatState
[ev
.which
, ev
.hat
] := hat
327 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
])
331 procedure HandleJoyAdd (var ev
: TSDL_JoyDeviceEvent
);
334 if (ev
.which
< e_MaxJoys
) then
336 JoystickHandle
[ev
.which
] := SDL_JoystickOpen(ev
.which
);
337 if JoystickHandle
[ev
.which
] <> nil then
339 e_LogWritefln('Added Joystick %s', [ev
.which
]);
340 e_JoystickAvailable
[ev
.which
] := True;
341 for i
:= 0 to Min(SDL_JoystickNumAxes(JoystickHandle
[ev
.which
]), e_MaxJoyAxes
) - 1 do
342 JoystickZeroAxes
[ev
.which
, i
] := SDL_JoystickGetAxis(JoystickHandle
[ev
.which
], i
)
346 e_LogWritefln('Warning! Failed to open Joystick %s', [ev
.which
])
351 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev
.which
, e_MaxJoys
])
355 procedure HandleJoyRemove (var ev
: TSDL_JoyDeviceEvent
);
357 e_LogWritefln('Removed Joystick %s', [ev
.which
]);
358 if (ev
.which
< e_MaxJoys
) then
360 e_JoystickAvailable
[ev
.which
] := False;
361 if JoystickHandle
[ev
.which
] <> nil then
362 SDL_JoystickClose(JoystickHandle
[ev
.which
]);
363 JoystickHandle
[ev
.which
] := nil
367 (* --------- Input --------- *)
369 function HandleWindow (var ev
: TSDL_WindowEvent
): Boolean;
373 e_LogWritefln('Window Event: event = %s, data1 = %s, data2 = %s', [ev
.event
, ev
.data1
, ev
.data2
]);
375 SDL_WINDOWEVENT_RESIZED
:
376 if @sys_ScreenResize
<> nil then
377 sys_ScreenResize(ev
.data1
, ev
.data2
);
378 SDL_WINDOWEVENT_EXPOSED
: sys_Repaint
;
379 SDL_WINDOWEVENT_CLOSE
: result
:= true;
380 SDL_WINDOWEVENT_MOVED
:
385 SDL_WINDOWEVENT_FOCUS_LOST
, SDL_WINDOWEVENT_MINIMIZED
:
388 if gMuteWhenInactive
then
389 e_MuteChannels(true);
390 {$IFDEF ENABLE_HOLMES}
391 if assigned(winBlurCB
) then winBlurCB
;
394 SDL_WINDOWEVENT_FOCUS_GAINED
, SDL_WINDOWEVENT_MAXIMIZED
, SDL_WINDOWEVENT_RESTORED
:
396 if ev
.event
= SDL_WINDOWEVENT_MAXIMIZED
then
398 gWinMaximized
:= true;
399 gRC_Maximized
:= true
401 else if ev
.event
= SDL_WINDOWEVENT_RESTORED
then
403 gWinMaximized
:= false;
404 gRC_Maximized
:= false
406 e_MuteChannels(false);
407 {$IFDEF ENABLE_HOLMES}
408 if assigned(winFocusCB
) then winFocusCB
;
414 procedure HandleKeyboard (var ev
: TSDL_KeyboardEvent
);
415 var down
: Boolean; key
: Integer;
417 key
:= ev
.keysym
.scancode
;
418 down
:= (ev
.type_
= SDL_KEYDOWN
);
419 if key
= SDL_SCANCODE_AC_BACK
then
420 key
:= SDL_SCANCODE_ESCAPE
;
421 {$IFDEF ENABLE_HOLMES}
422 if fuiOnSDLEvent(PSDL_Event(@ev
)^) then
424 // event eaten, but...
425 if not down
then e_KeyUpDown(key
, false);
429 if ev
._repeat
= 0 then
432 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down
, key
]);
433 e_KeyUpDown(key
, down
);
434 g_Console_ProcessBind(key
, down
);
439 e_LogWritefln('Input Debug: keyrep, scancode=%s', [key
]);
440 g_Console_ProcessBindRepeat(key
);
444 procedure HandleTextInput (var ev
: TSDL_TextInputEvent
);
445 var ch
: UnicodeChar
; sch
: AnsiChar;
447 Utf8ToUnicode(@ch
, PChar(ev
.text), 1);
448 sch
:= AnsiChar(wchar2win(ch
));
450 e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev
.text, Ord(ch
), Ord(sch
)]);
451 if @sys_CharPress
<> nil then
452 if IsValid1251(Word(ch
)) and IsPrintable1251(ch
) then
456 function sys_HandleInput (): Boolean;
460 ZeroMemory(@ev
, sizeof(ev
));
461 while SDL_PollEvent(@ev
) <> 0 do
464 SDL_QUITEV
: result
:= true;
465 SDL_WINDOWEVENT
: result
:= HandleWindow(ev
.window
);
466 SDL_KEYUP
, SDL_KEYDOWN
: HandleKeyboard(ev
.key
);
467 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
: HandleJoyButton(ev
.jbutton
);
468 SDL_JOYAXISMOTION
: HandleJoyAxis(ev
.jaxis
);
469 SDL_JOYHATMOTION
: HandleJoyHat(ev
.jhat
);
470 SDL_JOYDEVICEADDED
: HandleJoyAdd(ev
.jdevice
);
471 SDL_JOYDEVICEREMOVED
: HandleJoyRemove(ev
.jdevice
);
472 SDL_TEXTINPUT
: HandleTextInput(ev
.text);
473 SDL_FINGERMOTION
, SDL_FINGERDOWN
, SDL_FINGERUP
: g_Touch_HandleEvent(ev
.tfinger
);
474 {$IFDEF ENABLE_HOLMES}
475 SDL_MOUSEBUTTONDOWN
, SDL_MOUSEBUTTONUP
, SDL_MOUSEWHEEL
, SDL_MOUSEMOTION
: fuiOnSDLEvent(ev
);
481 procedure sys_RequestQuit
;
484 ev
.type_
:= SDL_QUITEV
;
488 (* --------- Init --------- *)
493 e_WriteLog('Init SDL2', TMsgType
.Notify
);
495 {$IFDEF USE_SDLMIXER}
496 flags
:= SDL_INIT_TIMER
or SDL_INIT_AUDIO
or $00004000;
498 flags
:= SDL_INIT_TIMER
or $00004000;
501 flags
:= SDL_INIT_TIMER
or SDL_INIT_VIDEO
;
503 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK
, '0');
504 if SDL_Init(flags
) <> 0 then
505 raise Exception
.Create('SDL: Init failed: ' + SDL_GetError
);
507 if SDL_InitSubSystem(SDL_INIT_JOYSTICK
) <> 0 then
508 e_LogWritefln('SDL: Init subsystem failed: %s', [SDL_GetError()]);
510 SDL_ShowCursor(SDL_DISABLE
);
515 e_WriteLog('Releasing SDL2', TMsgType
.Notify
);
516 if context
<> nil then
518 SDL_GL_DeleteContext(context
);
521 if window
<> nil then
523 SDL_DestroyWindow(window
);
530 conRegVar('sdl2_display_index', @display
, 'use display index as base', '');
531 conRegVar('sdl2_window_x', @wx
, 'window position x', '');
532 conRegVar('sdl2_window_y', @wy
, 'window position y', '');
533 conRegVar('sdl2_window_center', @wc
, 'force window creation at center', '');
535 wx
:= SDL_WINDOWPOS_CENTERED
;
536 wy
:= SDL_WINDOWPOS_CENTERED
;