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 (): AnsiString;
68 info
:= g_GetBuildHash(false);
69 if info
= 'custom build' then
70 info
:= info
+ ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE
+ ' ' + GAME_BUILDTIME
;
71 result
:= Format(GameTitle
, [info
]);
74 function InitWindow (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
75 var flags
: UInt32
; x
, y
: cint
; title
: AnsiString;
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
110 window
:= SDL_CreateWindow(PChar(title
), x
, y
, w
, h
, flags
);
111 if window
<> nil then
113 context
:= SDL_GL_CreateContext(window
);
114 if context
<> nil then
116 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
118 SDL_GetWindowPosition(window
, @x
, @y
);
121 gFullScreen
:= fullscreen
;
122 gWinMaximized
:= maximized
;
123 gRC_FullScreen
:= fullscreen
;
124 gRC_Maximized
:= maximized
;
125 if @sys_ScreenResize
<> nil then
126 sys_ScreenResize(w
, h
);
131 // SDL_DestroyWindow(window);
132 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError
])
137 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError
])
142 if fullScreen
then flags
:= SDL_WINDOW_FULLSCREEN
else flags
:= 0;
143 SDL_SetWindowFullscreen(window
, flags
);
144 SDL_SetWindowSize(window
, w
, h
);
145 if maximized
then SDL_MaximizeWindow(window
);
146 // always reset to center when changing fullscreen->windowed for safety purposes
147 if wc
or (gFullscreen
and not fullscreen
) or (gWinMaximized
and not maximized
) then
149 x
:= SDL_WINDOWPOS_CENTERED
;
150 y
:= SDL_WINDOWPOS_CENTERED
157 SDL_SetWindowPosition(window
, x
, y
);
158 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
160 SDL_GetWindowPosition(window
, @x
, @y
);
163 gFullScreen
:= fullscreen
;
164 gWinMaximized
:= maximized
;
165 gRC_FullScreen
:= fullscreen
;
166 gRC_Maximized
:= maximized
;
167 if @sys_ScreenResize
<> nil then
168 sys_ScreenResize(w
, h
);
173 procedure sys_Repaint
;
175 SDL_GL_SwapWindow(window
)
178 procedure sys_EnableVSync (yes
: Boolean);
181 SDL_GL_SetSwapInterval(1)
183 SDL_GL_SetSwapInterval(0)
186 function sys_GetDisplayModes (bpp
: Integer): SSArray
;
187 var i
, count
, num
, pw
, ph
: Integer; m
: TSDL_DisplayMode
;
190 num
:= SDL_GetNumDisplayModes(display
);
192 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError
]);
195 e_LogWritefln('Video modes for display %s:', [display
]);
196 SetLength(result
, num
);
197 i
:= 0; count
:= 0; pw
:= 0; ph
:= 0;
200 SDL_GetDisplayMode(display
, i
, @m
);
201 if ((pw
<> m
.w
) or (ph
<> m
.h
)) then
203 e_LogWritefln('* %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
204 pw
:= m
.w
; ph
:= m
.h
;
205 result
[count
] := IntToStr(m
.w
) + 'x' + IntToStr(m
.h
);
210 e_LogWritefln('- %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
214 SetLength(result
, count
)
218 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
220 result
:= InitWindow(w
, h
, bpp
, fullScreen
, maximized
)
223 (* --------- Joystick --------- *)
225 procedure HandleJoyButton (var ev
: TSDL_JoyButtonEvent
);
226 var down
: Boolean; key
: Integer;
228 if (ev
.which
< e_MaxJoys
) and (ev
.button
< e_MaxJoyBtns
) then
230 key
:= e_JoyButtonToKey(ev
.which
, ev
.button
);
231 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
233 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev
.which
, ev
.button
, key
, down
]);
234 e_KeyUpDown(key
, down
);
235 g_Console_ProcessBind(key
, down
)
241 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
242 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev
.which
, ev
.button
, down
])
247 procedure HandleJoyAxis (var ev
: TSDL_JoyAxisEvent
);
248 var key
, minuskey
: Integer;
250 if (ev
.which
< e_MaxJoys
) and (ev
.axis
< e_MaxJoyAxes
) then
252 key
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_PLUS
);
253 minuskey
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_MINUS
);
256 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
]]);
258 if ev
.value
< JoystickZeroAxes
[ev
.which
, ev
.axis
] - e_JoystickDeadzones
[ev
.which
] then
260 if (e_KeyPressed(key
)) then
262 e_KeyUpDown(key
, False);
263 g_Console_ProcessBind(key
, False)
265 e_KeyUpDown(minuskey
, True);
266 g_Console_ProcessBind(minuskey
, True)
268 else if ev
.value
> JoystickZeroAxes
[ev
.which
, ev
.axis
] + e_JoystickDeadzones
[ev
.which
] then
270 if (e_KeyPressed(minuskey
)) then
272 e_KeyUpDown(minuskey
, False);
273 g_Console_ProcessBind(minuskey
, False)
275 e_KeyUpDown(key
, True);
276 g_Console_ProcessBind(key
, True)
280 if (e_KeyPressed(minuskey
)) then
282 e_KeyUpDown(minuskey
, False);
283 g_Console_ProcessBind(minuskey
, False)
285 if (e_KeyPressed(key
)) then
287 e_KeyUpDown(key
, False);
288 g_Console_ProcessBind(key
, False)
295 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
]])
299 procedure HandleJoyHat (var ev
: TSDL_JoyHatEvent
);
303 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
305 if (ev
.which
< e_MaxJoys
) and (ev
.hat
< e_MaxJoyHats
) then
308 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
]);
309 hat
[HAT_UP
] := LongBool(ev
.value
and SDL_HAT_UP
);
310 hat
[HAT_DOWN
] := LongBool(ev
.value
and SDL_HAT_DOWN
);
311 hat
[HAT_LEFT
] := LongBool(ev
.value
and SDL_HAT_LEFT
);
312 hat
[HAT_RIGHT
] := LongBool(ev
.value
and SDL_HAT_RIGHT
);
313 for i
:= HAT_LEFT
to HAT_DOWN
do
315 if JoystickHatState
[ev
.which
, ev
.hat
, i
] <> hat
[i
] then
318 key
:= e_JoyHatToKey(ev
.which
, ev
.hat
, i
);
319 e_KeyUpDown(key
, down
);
320 g_Console_ProcessBind(key
, down
)
323 JoystickHatState
[ev
.which
, ev
.hat
] := hat
328 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
])
332 procedure HandleJoyAdd (var ev
: TSDL_JoyDeviceEvent
);
335 if (ev
.which
< e_MaxJoys
) then
337 JoystickHandle
[ev
.which
] := SDL_JoystickOpen(ev
.which
);
338 if JoystickHandle
[ev
.which
] <> nil then
340 e_LogWritefln('Added Joystick %s', [ev
.which
]);
341 e_JoystickAvailable
[ev
.which
] := True;
342 for i
:= 0 to Min(SDL_JoystickNumAxes(JoystickHandle
[ev
.which
]), e_MaxJoyAxes
) - 1 do
343 JoystickZeroAxes
[ev
.which
, i
] := SDL_JoystickGetAxis(JoystickHandle
[ev
.which
], i
)
347 e_LogWritefln('Warning! Failed to open Joystick %s', [ev
.which
])
352 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev
.which
, e_MaxJoys
])
356 procedure HandleJoyRemove (var ev
: TSDL_JoyDeviceEvent
);
358 e_LogWritefln('Removed Joystick %s', [ev
.which
]);
359 if (ev
.which
< e_MaxJoys
) then
361 e_JoystickAvailable
[ev
.which
] := False;
362 if JoystickHandle
[ev
.which
] <> nil then
363 SDL_JoystickClose(JoystickHandle
[ev
.which
]);
364 JoystickHandle
[ev
.which
] := nil
368 (* --------- Input --------- *)
370 function HandleWindow (var ev
: TSDL_WindowEvent
): Boolean;
374 e_LogWritefln('Window Event: event = %s, data1 = %s, data2 = %s', [ev
.event
, ev
.data1
, ev
.data2
]);
376 SDL_WINDOWEVENT_RESIZED
:
377 if @sys_ScreenResize
<> nil then
378 sys_ScreenResize(ev
.data1
, ev
.data2
);
379 SDL_WINDOWEVENT_EXPOSED
: sys_Repaint
;
380 SDL_WINDOWEVENT_CLOSE
: result
:= true;
381 SDL_WINDOWEVENT_MOVED
:
386 SDL_WINDOWEVENT_FOCUS_LOST
, SDL_WINDOWEVENT_MINIMIZED
:
389 if gMuteWhenInactive
then
390 e_MuteChannels(true);
391 {$IFDEF ENABLE_HOLMES}
392 if assigned(winBlurCB
) then winBlurCB
;
395 SDL_WINDOWEVENT_FOCUS_GAINED
, SDL_WINDOWEVENT_MAXIMIZED
, SDL_WINDOWEVENT_RESTORED
:
397 if ev
.event
= SDL_WINDOWEVENT_MAXIMIZED
then
399 gWinMaximized
:= true;
400 gRC_Maximized
:= true
402 else if ev
.event
= SDL_WINDOWEVENT_RESTORED
then
404 gWinMaximized
:= false;
405 gRC_Maximized
:= false
407 e_MuteChannels(false);
408 {$IFDEF ENABLE_HOLMES}
409 if assigned(winFocusCB
) then winFocusCB
;
415 procedure HandleKeyboard (var ev
: TSDL_KeyboardEvent
);
416 var down
: Boolean; key
: Integer;
418 key
:= ev
.keysym
.scancode
;
419 down
:= (ev
.type_
= SDL_KEYDOWN
);
420 if key
= SDL_SCANCODE_AC_BACK
then
421 key
:= SDL_SCANCODE_ESCAPE
;
422 {$IFDEF ENABLE_HOLMES}
423 if fuiOnSDLEvent(PSDL_Event(@ev
)^) then
425 // event eaten, but...
426 if not down
then e_KeyUpDown(key
, false);
430 if ev
._repeat
= 0 then
433 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down
, key
]);
434 e_KeyUpDown(key
, down
);
435 g_Console_ProcessBind(key
, down
);
440 e_LogWritefln('Input Debug: keyrep, scancode=%s', [key
]);
441 g_Console_ProcessBindRepeat(key
);
445 procedure HandleTextInput (var ev
: TSDL_TextInputEvent
);
446 var ch
: UnicodeChar
; sch
: AnsiChar;
448 Utf8ToUnicode(@ch
, PChar(ev
.text), 1);
449 sch
:= AnsiChar(wchar2win(ch
));
451 e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev
.text, Ord(ch
), Ord(sch
)]);
452 if @sys_CharPress
<> nil then
453 if IsValid1251(Word(ch
)) and IsPrintable1251(ch
) then
457 function sys_HandleInput (): Boolean;
461 ZeroMemory(@ev
, sizeof(ev
));
462 while SDL_PollEvent(@ev
) <> 0 do
465 SDL_QUITEV
: result
:= true;
466 SDL_WINDOWEVENT
: result
:= HandleWindow(ev
.window
);
467 SDL_KEYUP
, SDL_KEYDOWN
: HandleKeyboard(ev
.key
);
468 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
: HandleJoyButton(ev
.jbutton
);
469 SDL_JOYAXISMOTION
: HandleJoyAxis(ev
.jaxis
);
470 SDL_JOYHATMOTION
: HandleJoyHat(ev
.jhat
);
471 SDL_JOYDEVICEADDED
: HandleJoyAdd(ev
.jdevice
);
472 SDL_JOYDEVICEREMOVED
: HandleJoyRemove(ev
.jdevice
);
473 SDL_TEXTINPUT
: HandleTextInput(ev
.text);
474 SDL_FINGERMOTION
, SDL_FINGERDOWN
, SDL_FINGERUP
: g_Touch_HandleEvent(ev
.tfinger
);
475 {$IFDEF ENABLE_HOLMES}
476 SDL_MOUSEBUTTONDOWN
, SDL_MOUSEBUTTONUP
, SDL_MOUSEWHEEL
, SDL_MOUSEMOTION
: fuiOnSDLEvent(ev
);
482 procedure sys_RequestQuit
;
485 ev
.type_
:= SDL_QUITEV
;
489 (* --------- Init --------- *)
494 e_WriteLog('Init SDL2', TMsgType
.Notify
);
496 {$IFDEF USE_SDLMIXER}
497 flags
:= SDL_INIT_TIMER
or SDL_INIT_AUDIO
or $00004000;
499 flags
:= SDL_INIT_TIMER
or $00004000;
502 flags
:= SDL_INIT_TIMER
or SDL_INIT_VIDEO
;
504 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK
, '0');
505 if SDL_Init(flags
) <> 0 then
506 raise Exception
.Create('SDL: Init failed: ' + SDL_GetError
);
508 if SDL_InitSubSystem(SDL_INIT_JOYSTICK
) <> 0 then
509 e_LogWritefln('SDL: Init subsystem failed: %s', [SDL_GetError()]);
511 SDL_ShowCursor(SDL_DISABLE
);
516 e_WriteLog('Releasing SDL2', TMsgType
.Notify
);
517 if context
<> nil then
519 SDL_GL_DeleteContext(context
);
522 if window
<> nil then
524 SDL_DestroyWindow(window
);
531 conRegVar('sdl2_display_index', @display
, 'use display index as base', '');
532 conRegVar('sdl2_window_x', @wx
, 'window position x', '');
533 conRegVar('sdl2_window_y', @wy
, 'window position y', '');
534 conRegVar('sdl2_window_center', @wc
, 'force window creation at center', '');
536 wx
:= SDL_WINDOWPOS_CENTERED
;
537 wy
:= SDL_WINDOWPOS_CENTERED
;