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 function sys_GetTicks (): Int64;
24 procedure sys_Delay (ms
: Integer);
26 (* --- Graphics --- *)
27 function sys_GetDisplayModes (bpp
: Integer): SSArray
;
28 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullscreen
, maximized
: Boolean): Boolean;
29 procedure sys_EnableVSync (yes
: Boolean);
30 procedure sys_Repaint
;
33 function sys_HandleInput (): Boolean;
34 procedure sys_RequestQuit
;
43 SysUtils
, SDL2
, Math
, ctypes
,
44 e_log
, e_graphics
, e_input
, e_sound
,
45 {$INCLUDE ../nogl/noGLuses.inc}
46 {$IFDEF ENABLE_HOLMES}
47 g_holmes
, sdlcarcass
, fui_ctls
,
49 g_touch
, g_options
, g_window
, g_console
, g_game
, g_menu
, g_gui
, g_main
, 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 (* --------- Utils --------- *)
65 function sys_GetTicks (): Int64;
67 result
:= SDL_GetTicks()
70 procedure sys_Delay (ms
: Integer);
75 (* --------- Graphics --------- *)
77 function LoadGL
: Boolean;
83 if glRenderToFBO
and (not nogl_ExtensionSupported('GL_OES_framebuffer_object')) then
85 e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
86 glRenderToFBO
:= false;
89 if glRenderToFBO
and (not Load_GL_ARB_framebuffer_object
) then
91 e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
92 glRenderToFBO
:= false;
95 if SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE
, @ltmp
) = 0 then
97 e_LogWritefln('stencil buffer size: %s', [ltmp
]);
98 gwin_has_stencil
:= (ltmp
> 0);
109 procedure UpdateSize (w
, h
: Integer);
115 if glRenderToFBO
then
117 // store real window size in gWinSize, downscale resolution now
118 w
:= round(w
/ r_pixel_scale
);
119 h
:= round(h
/ r_pixel_scale
);
120 if not e_ResizeFramebuffer(w
, h
) then
122 e_LogWriteln('GL: could not create framebuffer, falling back to --no-fbo');
123 glRenderToFBO
:= False;
130 {$IFDEF ENABLE_HOLMES}
134 e_ResizeWindow(w
, h
);
136 g_Game_SetupScreenSize
;
138 (* This will fix menu reset on keyboard showing *)
142 {$IFDEF ENABLE_HOLMES}
143 if assigned(oglInitCB
) then oglInitCB
;
147 function GetTitle (): AnsiString;
148 var info
: AnsiString;
150 info
:= g_GetBuildHash(false);
151 if info
= 'custom build' then
152 info
:= info
+ ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE
+ ' ' + GAME_BUILDTIME
;
153 result
:= Format(GameTitle
, [info
]);
156 function InitWindow (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
157 var flags
: UInt32
; x
, y
: cint
; title
: AnsiString;
159 // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
160 e_LogWritefln('InitWindow %s %s %s %s', [w
, h
, bpp
, fullScreen
]);
165 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 1);
166 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
167 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK
, SDL_GL_CONTEXT_PROFILE_ES
);
169 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
170 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
171 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
172 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
173 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
174 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
175 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
176 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
178 flags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
179 if fullScreen
then flags
:= flags
or SDL_WINDOW_FULLSCREEN
;
180 if maximized
then flags
:= flags
or SDL_WINDOW_MAXIMIZED
;
183 x
:= SDL_WINDOWPOS_CENTERED
;
184 y
:= SDL_WINDOWPOS_CENTERED
192 window
:= SDL_CreateWindow(PChar(title
), x
, y
, w
, h
, flags
);
193 if window
<> nil then
195 context
:= SDL_GL_CreateContext(window
);
196 if context
<> nil then
200 e_LogWriteln('GL: unable to load OpenGL functions', TMsgType
.Fatal
);
201 SDL_GL_DeleteContext(context
); context
:= nil;
204 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
206 SDL_GetWindowPosition(window
, @x
, @y
);
209 gFullScreen
:= fullscreen
;
210 gWinMaximized
:= maximized
;
211 gRC_FullScreen
:= fullscreen
;
212 gRC_Maximized
:= maximized
;
218 // SDL_DestroyWindow(window);
219 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError
])
224 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError
])
229 if fullScreen
then flags
:= SDL_WINDOW_FULLSCREEN
else flags
:= 0;
230 SDL_SetWindowFullscreen(window
, flags
);
231 SDL_SetWindowSize(window
, w
, h
);
232 if maximized
then SDL_MaximizeWindow(window
);
233 // always reset to center when changing fullscreen->windowed for safety purposes
234 if wc
or (gFullscreen
and not fullscreen
) or (gWinMaximized
and not maximized
) then
236 x
:= SDL_WINDOWPOS_CENTERED
;
237 y
:= SDL_WINDOWPOS_CENTERED
244 SDL_SetWindowPosition(window
, x
, y
);
245 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
247 SDL_GetWindowPosition(window
, @x
, @y
);
250 gFullScreen
:= fullscreen
;
251 gWinMaximized
:= maximized
;
252 gRC_FullScreen
:= fullscreen
;
253 gRC_Maximized
:= maximized
;
259 procedure sys_Repaint
;
261 SDL_GL_SwapWindow(window
)
264 procedure sys_EnableVSync (yes
: Boolean);
267 SDL_GL_SetSwapInterval(1)
269 SDL_GL_SetSwapInterval(0)
272 function sys_GetDisplayModes (bpp
: Integer): SSArray
;
273 var i
, count
, num
, pw
, ph
: Integer; m
: TSDL_DisplayMode
;
276 num
:= SDL_GetNumDisplayModes(display
);
278 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError
]);
281 e_LogWritefln('Video modes for display %s:', [display
]);
282 SetLength(result
, num
);
283 i
:= 0; count
:= 0; pw
:= 0; ph
:= 0;
286 SDL_GetDisplayMode(display
, i
, @m
);
287 if ((pw
<> m
.w
) or (ph
<> m
.h
)) then
289 e_LogWritefln('* %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
290 pw
:= m
.w
; ph
:= m
.h
;
291 result
[count
] := IntToStr(m
.w
) + 'x' + IntToStr(m
.h
);
296 e_LogWritefln('- %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
300 SetLength(result
, count
)
304 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
306 result
:= InitWindow(w
, h
, bpp
, fullScreen
, maximized
)
309 (* --------- Joystick --------- *)
311 procedure HandleJoyButton (var ev
: TSDL_JoyButtonEvent
);
312 var down
: Boolean; key
: Integer;
314 if (ev
.which
< e_MaxJoys
) and (ev
.button
< e_MaxJoyBtns
) then
316 key
:= e_JoyButtonToKey(ev
.which
, ev
.button
);
317 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
319 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev
.which
, ev
.button
, key
, down
]);
320 e_KeyUpDown(key
, down
);
321 g_Console_ProcessBind(key
, down
)
327 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
328 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev
.which
, ev
.button
, down
])
333 procedure HandleJoyAxis (var ev
: TSDL_JoyAxisEvent
);
334 var key
, minuskey
: Integer;
336 if (ev
.which
< e_MaxJoys
) and (ev
.axis
< e_MaxJoyAxes
) then
338 key
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_PLUS
);
339 minuskey
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_MINUS
);
342 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
]]);
344 if ev
.value
< JoystickZeroAxes
[ev
.which
, ev
.axis
] - e_JoystickDeadzones
[ev
.which
] then
346 if (e_KeyPressed(key
)) then
348 e_KeyUpDown(key
, False);
349 g_Console_ProcessBind(key
, False)
351 e_KeyUpDown(minuskey
, True);
352 g_Console_ProcessBind(minuskey
, True)
354 else if ev
.value
> JoystickZeroAxes
[ev
.which
, ev
.axis
] + e_JoystickDeadzones
[ev
.which
] then
356 if (e_KeyPressed(minuskey
)) then
358 e_KeyUpDown(minuskey
, False);
359 g_Console_ProcessBind(minuskey
, False)
361 e_KeyUpDown(key
, True);
362 g_Console_ProcessBind(key
, True)
366 if (e_KeyPressed(minuskey
)) then
368 e_KeyUpDown(minuskey
, False);
369 g_Console_ProcessBind(minuskey
, False)
371 if (e_KeyPressed(key
)) then
373 e_KeyUpDown(key
, False);
374 g_Console_ProcessBind(key
, False)
381 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
]])
385 procedure HandleJoyHat (var ev
: TSDL_JoyHatEvent
);
389 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
391 if (ev
.which
< e_MaxJoys
) and (ev
.hat
< e_MaxJoyHats
) then
394 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
]);
395 hat
[HAT_UP
] := LongBool(ev
.value
and SDL_HAT_UP
);
396 hat
[HAT_DOWN
] := LongBool(ev
.value
and SDL_HAT_DOWN
);
397 hat
[HAT_LEFT
] := LongBool(ev
.value
and SDL_HAT_LEFT
);
398 hat
[HAT_RIGHT
] := LongBool(ev
.value
and SDL_HAT_RIGHT
);
399 for i
:= HAT_LEFT
to HAT_DOWN
do
401 if JoystickHatState
[ev
.which
, ev
.hat
, i
] <> hat
[i
] then
404 key
:= e_JoyHatToKey(ev
.which
, ev
.hat
, i
);
405 e_KeyUpDown(key
, down
);
406 g_Console_ProcessBind(key
, down
)
409 JoystickHatState
[ev
.which
, ev
.hat
] := hat
414 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
])
418 procedure HandleJoyAdd (var ev
: TSDL_JoyDeviceEvent
);
421 if (ev
.which
< e_MaxJoys
) then
423 JoystickHandle
[ev
.which
] := SDL_JoystickOpen(ev
.which
);
424 if JoystickHandle
[ev
.which
] <> nil then
426 e_LogWritefln('Added Joystick %s', [ev
.which
]);
427 e_JoystickAvailable
[ev
.which
] := True;
428 for i
:= 0 to Min(SDL_JoystickNumAxes(JoystickHandle
[ev
.which
]), e_MaxJoyAxes
) - 1 do
429 JoystickZeroAxes
[ev
.which
, i
] := SDL_JoystickGetAxis(JoystickHandle
[ev
.which
], i
)
433 e_LogWritefln('Warning! Failed to open Joystick %s', [ev
.which
])
438 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev
.which
, e_MaxJoys
])
442 procedure HandleJoyRemove (var ev
: TSDL_JoyDeviceEvent
);
444 e_LogWritefln('Removed Joystick %s', [ev
.which
]);
445 if (ev
.which
< e_MaxJoys
) then
447 e_JoystickAvailable
[ev
.which
] := False;
448 if JoystickHandle
[ev
.which
] <> nil then
449 SDL_JoystickClose(JoystickHandle
[ev
.which
]);
450 JoystickHandle
[ev
.which
] := nil
454 (* --------- Input --------- *)
456 function HandleWindow (var ev
: TSDL_WindowEvent
): Boolean;
460 e_LogWritefln('Window Event: event = %s, data1 = %s, data2 = %s', [ev
.event
, ev
.data1
, ev
.data2
]);
462 SDL_WINDOWEVENT_RESIZED
: UpdateSize(ev
.data1
, ev
.data2
);
463 SDL_WINDOWEVENT_EXPOSED
: sys_Repaint
;
464 SDL_WINDOWEVENT_CLOSE
: result
:= true;
465 SDL_WINDOWEVENT_MOVED
:
470 SDL_WINDOWEVENT_FOCUS_LOST
, SDL_WINDOWEVENT_MINIMIZED
:
473 if gMuteWhenInactive
then
474 e_MuteChannels(true);
475 {$IFDEF ENABLE_HOLMES}
476 if assigned(winBlurCB
) then winBlurCB
;
479 SDL_WINDOWEVENT_FOCUS_GAINED
, SDL_WINDOWEVENT_MAXIMIZED
, SDL_WINDOWEVENT_RESTORED
:
481 if ev
.event
= SDL_WINDOWEVENT_MAXIMIZED
then
483 gWinMaximized
:= true;
484 gRC_Maximized
:= true
486 else if ev
.event
= SDL_WINDOWEVENT_RESTORED
then
488 gWinMaximized
:= false;
489 gRC_Maximized
:= false
491 e_MuteChannels(false);
492 {$IFDEF ENABLE_HOLMES}
493 if assigned(winFocusCB
) then winFocusCB
;
499 procedure HandleKeyboard (var ev
: TSDL_KeyboardEvent
);
500 var down
: Boolean; key
: Integer;
502 key
:= ev
.keysym
.scancode
;
503 down
:= (ev
.type_
= SDL_KEYDOWN
);
504 if key
= SDL_SCANCODE_AC_BACK
then
505 key
:= SDL_SCANCODE_ESCAPE
;
506 {$IFDEF ENABLE_HOLMES}
507 if fuiOnSDLEvent(PSDL_Event(@ev
)^) then
509 // event eaten, but...
510 if not down
then e_KeyUpDown(key
, false);
514 if ev
._repeat
= 0 then
517 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down
, key
]);
518 e_KeyUpDown(key
, down
);
519 g_Console_ProcessBind(key
, down
);
524 e_LogWritefln('Input Debug: keyrep, scancode=%s', [key
]);
525 g_Console_ProcessBindRepeat(key
);
529 procedure HandleTextInput (var ev
: TSDL_TextInputEvent
);
530 var ch
: UnicodeChar
; sch
: AnsiChar;
532 Utf8ToUnicode(@ch
, PChar(ev
.text), 1);
533 sch
:= AnsiChar(wchar2win(ch
));
535 e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev
.text, Ord(ch
), Ord(sch
)]);
536 if IsValid1251(Word(ch
)) and IsPrintable1251(ch
) then
540 function sys_HandleInput (): Boolean;
544 ZeroMemory(@ev
, sizeof(ev
));
545 while SDL_PollEvent(@ev
) <> 0 do
548 SDL_QUITEV
: result
:= true;
549 SDL_WINDOWEVENT
: result
:= HandleWindow(ev
.window
);
550 SDL_KEYUP
, SDL_KEYDOWN
: HandleKeyboard(ev
.key
);
551 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
: HandleJoyButton(ev
.jbutton
);
552 SDL_JOYAXISMOTION
: HandleJoyAxis(ev
.jaxis
);
553 SDL_JOYHATMOTION
: HandleJoyHat(ev
.jhat
);
554 SDL_JOYDEVICEADDED
: HandleJoyAdd(ev
.jdevice
);
555 SDL_JOYDEVICEREMOVED
: HandleJoyRemove(ev
.jdevice
);
556 SDL_TEXTINPUT
: HandleTextInput(ev
.text);
557 SDL_FINGERMOTION
, SDL_FINGERDOWN
, SDL_FINGERUP
: g_Touch_HandleEvent(ev
.tfinger
);
558 {$IFDEF ENABLE_HOLMES}
559 SDL_MOUSEBUTTONDOWN
, SDL_MOUSEBUTTONUP
, SDL_MOUSEWHEEL
, SDL_MOUSEMOTION
: fuiOnSDLEvent(ev
);
565 procedure sys_RequestQuit
;
568 ev
.type_
:= SDL_QUITEV
;
572 (* --------- Init --------- *)
577 e_WriteLog('Init SDL2', TMsgType
.Notify
);
579 {$IFDEF USE_SDLMIXER}
580 flags
:= SDL_INIT_TIMER
or SDL_INIT_AUDIO
or $00004000;
582 flags
:= SDL_INIT_TIMER
or $00004000;
585 flags
:= SDL_INIT_TIMER
or SDL_INIT_VIDEO
;
587 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK
, '0');
588 if SDL_Init(flags
) <> 0 then
589 raise Exception
.Create('SDL: Init failed: ' + SDL_GetError
);
591 if SDL_InitSubSystem(SDL_INIT_JOYSTICK
) <> 0 then
592 e_LogWritefln('SDL: Init subsystem failed: %s', [SDL_GetError()]);
594 SDL_ShowCursor(SDL_DISABLE
);
599 e_WriteLog('Releasing SDL2', TMsgType
.Notify
);
600 if context
<> nil then
603 SDL_GL_DeleteContext(context
);
606 if window
<> nil then
608 SDL_DestroyWindow(window
);
615 conRegVar('sdl2_display_index', @display
, 'use display index as base', '');
616 conRegVar('sdl2_window_x', @wx
, 'window position x', '');
617 conRegVar('sdl2_window_y', @wy
, 'window position y', '');
618 conRegVar('sdl2_window_center', @wc
, 'force window creation at center', '');
620 wx
:= SDL_WINDOWPOS_CENTERED
;
621 wy
:= SDL_WINDOWPOS_CENTERED
;