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;
82 if glRenderToFBO
and (not nogl_ExtensionSupported('GL_OES_framebuffer_object')) then
84 if glRenderToFBO
and (not Load_GL_ARB_framebuffer_object
) then
87 e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
88 glRenderToFBO
:= false;
99 procedure UpdateSize (w
, h
: Integer);
105 if glRenderToFBO
then
107 // store real window size in gWinSize, downscale resolution now
108 w
:= round(w
/ r_pixel_scale
);
109 h
:= round(h
/ r_pixel_scale
);
110 if not e_ResizeFramebuffer(w
, h
) then
112 e_LogWriteln('GL: could not create framebuffer, falling back to --no-fbo');
113 glRenderToFBO
:= False;
120 {$IFDEF ENABLE_HOLMES}
124 e_ResizeWindow(w
, h
);
126 g_Game_SetupScreenSize
;
128 (* This will fix menu reset on keyboard showing *)
132 {$IFDEF ENABLE_HOLMES}
133 if assigned(oglInitCB
) then oglInitCB
;
137 function GetTitle (): PChar;
138 var info
: AnsiString;
140 info
:= g_GetBuildHash(false);
141 if info
= 'custom build' then
142 info
:= info
+ ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE
+ ' ' + GAME_BUILDTIME
;
143 result
:= PChar(Format(GameTitle
, [info
]))
146 function InitWindow (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
147 var flags
: UInt32
; x
, y
: cint
;
149 // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
150 e_LogWritefln('InitWindow %s %s %s %s', [w
, h
, bpp
, fullScreen
]);
155 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 1);
156 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
157 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK
, SDL_GL_CONTEXT_PROFILE_ES
);
159 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
160 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
161 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
162 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
163 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
164 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
165 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
166 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
168 flags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
169 if fullScreen
then flags
:= flags
or SDL_WINDOW_FULLSCREEN
;
170 if maximized
then flags
:= flags
or SDL_WINDOW_MAXIMIZED
;
173 x
:= SDL_WINDOWPOS_CENTERED
;
174 y
:= SDL_WINDOWPOS_CENTERED
181 window
:= SDL_CreateWindow(GetTitle(), x
, y
, w
, h
, flags
);
182 if window
<> nil then
184 context
:= SDL_GL_CreateContext(window
);
185 if context
<> nil then
189 e_LogWriteln('GL: unable to load OpenGL functions', TMsgType
.Fatal
);
190 SDL_GL_DeleteContext(context
); context
:= nil;
193 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
195 SDL_GetWindowPosition(window
, @x
, @y
);
198 gFullScreen
:= fullscreen
;
199 gWinMaximized
:= maximized
;
200 gRC_FullScreen
:= fullscreen
;
201 gRC_Maximized
:= maximized
;
207 // SDL_DestroyWindow(window);
208 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError
])
213 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError
])
218 if fullScreen
then flags
:= SDL_WINDOW_FULLSCREEN
else flags
:= 0;
219 SDL_SetWindowFullscreen(window
, flags
);
220 SDL_SetWindowSize(window
, w
, h
);
221 if maximized
then SDL_MaximizeWindow(window
);
222 // always reset to center when changing fullscreen->windowed for safety purposes
223 if wc
or (gFullscreen
and not fullscreen
) or (gWinMaximized
and not maximized
) then
225 x
:= SDL_WINDOWPOS_CENTERED
;
226 y
:= SDL_WINDOWPOS_CENTERED
233 SDL_SetWindowPosition(window
, x
, y
);
234 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
236 SDL_GetWindowPosition(window
, @x
, @y
);
239 gFullScreen
:= fullscreen
;
240 gWinMaximized
:= maximized
;
241 gRC_FullScreen
:= fullscreen
;
242 gRC_Maximized
:= maximized
;
248 procedure sys_Repaint
;
250 SDL_GL_SwapWindow(window
)
253 procedure sys_EnableVSync (yes
: Boolean);
256 SDL_GL_SetSwapInterval(1)
258 SDL_GL_SetSwapInterval(0)
261 function sys_GetDisplayModes (bpp
: Integer): SSArray
;
262 var i
, count
, num
, pw
, ph
: Integer; m
: TSDL_DisplayMode
;
265 num
:= SDL_GetNumDisplayModes(display
);
267 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError
]);
270 e_LogWritefln('Video modes for display %s:', [display
]);
271 SetLength(result
, num
);
272 i
:= 0; count
:= 0; pw
:= 0; ph
:= 0;
275 SDL_GetDisplayMode(display
, i
, @m
);
276 if ((pw
<> m
.w
) or (ph
<> m
.h
)) then
278 e_LogWritefln('* %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
279 pw
:= m
.w
; ph
:= m
.h
;
280 result
[count
] := IntToStr(m
.w
) + 'x' + IntToStr(m
.h
);
285 e_LogWritefln('- %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
289 SetLength(result
, count
)
293 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
295 result
:= InitWindow(w
, h
, bpp
, fullScreen
, maximized
)
298 (* --------- Joystick --------- *)
300 procedure HandleJoyButton (var ev
: TSDL_JoyButtonEvent
);
301 var down
: Boolean; key
: Integer;
303 if (ev
.which
< e_MaxJoys
) and (ev
.button
< e_MaxJoyBtns
) then
305 key
:= e_JoyButtonToKey(ev
.which
, ev
.button
);
306 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
308 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev
.which
, ev
.button
, key
, down
]);
309 e_KeyUpDown(key
, down
);
310 g_Console_ProcessBind(key
, down
)
316 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
317 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev
.which
, ev
.button
, down
])
322 procedure HandleJoyAxis (var ev
: TSDL_JoyAxisEvent
);
323 var key
, minuskey
: Integer;
325 if (ev
.which
< e_MaxJoys
) and (ev
.axis
< e_MaxJoyAxes
) then
327 key
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_PLUS
);
328 minuskey
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_MINUS
);
331 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
]]);
333 if ev
.value
< JoystickZeroAxes
[ev
.which
, ev
.axis
] - e_JoystickDeadzones
[ev
.which
] then
335 if (e_KeyPressed(key
)) then
337 e_KeyUpDown(key
, False);
338 g_Console_ProcessBind(key
, False)
340 e_KeyUpDown(minuskey
, True);
341 g_Console_ProcessBind(minuskey
, True)
343 else if ev
.value
> JoystickZeroAxes
[ev
.which
, ev
.axis
] + e_JoystickDeadzones
[ev
.which
] then
345 if (e_KeyPressed(minuskey
)) then
347 e_KeyUpDown(minuskey
, False);
348 g_Console_ProcessBind(minuskey
, False)
350 e_KeyUpDown(key
, True);
351 g_Console_ProcessBind(key
, True)
355 if (e_KeyPressed(minuskey
)) then
357 e_KeyUpDown(minuskey
, False);
358 g_Console_ProcessBind(minuskey
, False)
360 if (e_KeyPressed(key
)) then
362 e_KeyUpDown(key
, False);
363 g_Console_ProcessBind(key
, False)
370 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
]])
374 procedure HandleJoyHat (var ev
: TSDL_JoyHatEvent
);
378 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
380 if (ev
.which
< e_MaxJoys
) and (ev
.hat
< e_MaxJoyHats
) then
383 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
]);
384 hat
[HAT_UP
] := LongBool(ev
.value
and SDL_HAT_UP
);
385 hat
[HAT_DOWN
] := LongBool(ev
.value
and SDL_HAT_DOWN
);
386 hat
[HAT_LEFT
] := LongBool(ev
.value
and SDL_HAT_LEFT
);
387 hat
[HAT_RIGHT
] := LongBool(ev
.value
and SDL_HAT_RIGHT
);
388 for i
:= HAT_LEFT
to HAT_DOWN
do
390 if JoystickHatState
[ev
.which
, ev
.hat
, i
] <> hat
[i
] then
393 key
:= e_JoyHatToKey(ev
.which
, ev
.hat
, i
);
394 e_KeyUpDown(key
, down
);
395 g_Console_ProcessBind(key
, down
)
398 JoystickHatState
[ev
.which
, ev
.hat
] := hat
403 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
])
407 procedure HandleJoyAdd (var ev
: TSDL_JoyDeviceEvent
);
410 if (ev
.which
< e_MaxJoys
) then
412 JoystickHandle
[ev
.which
] := SDL_JoystickOpen(ev
.which
);
413 if JoystickHandle
[ev
.which
] <> nil then
415 e_LogWritefln('Added Joystick %s', [ev
.which
]);
416 e_JoystickAvailable
[ev
.which
] := True;
417 for i
:= 0 to Min(SDL_JoystickNumAxes(JoystickHandle
[ev
.which
]), e_MaxJoyAxes
) - 1 do
418 JoystickZeroAxes
[ev
.which
, i
] := SDL_JoystickGetAxis(JoystickHandle
[ev
.which
], i
)
422 e_LogWritefln('Warning! Failed to open Joystick %s', [ev
.which
])
427 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev
.which
, e_MaxJoys
])
431 procedure HandleJoyRemove (var ev
: TSDL_JoyDeviceEvent
);
433 e_LogWritefln('Removed Joystick %s', [ev
.which
]);
434 if (ev
.which
< e_MaxJoys
) then
436 e_JoystickAvailable
[ev
.which
] := False;
437 if JoystickHandle
[ev
.which
] <> nil then
438 SDL_JoystickClose(JoystickHandle
[ev
.which
]);
439 JoystickHandle
[ev
.which
] := nil
443 (* --------- Input --------- *)
445 function HandleWindow (var ev
: TSDL_WindowEvent
): Boolean;
449 e_LogWritefln('Window Event: event = %s, data1 = %s, data2 = %s', [ev
.event
, ev
.data1
, ev
.data2
]);
451 SDL_WINDOWEVENT_RESIZED
: UpdateSize(ev
.data1
, ev
.data2
);
452 SDL_WINDOWEVENT_EXPOSED
: sys_Repaint
;
453 SDL_WINDOWEVENT_CLOSE
: result
:= true;
454 SDL_WINDOWEVENT_MOVED
:
459 SDL_WINDOWEVENT_FOCUS_LOST
, SDL_WINDOWEVENT_MINIMIZED
:
462 if gMuteWhenInactive
then
463 e_MuteChannels(true);
464 {$IFDEF ENABLE_HOLMES}
465 if assigned(winBlurCB
) then winBlurCB
;
468 SDL_WINDOWEVENT_FOCUS_GAINED
, SDL_WINDOWEVENT_MAXIMIZED
, SDL_WINDOWEVENT_RESTORED
:
470 if ev
.event
= SDL_WINDOWEVENT_MAXIMIZED
then
472 gWinMaximized
:= true;
473 gRC_Maximized
:= true
475 else if ev
.event
= SDL_WINDOWEVENT_RESTORED
then
477 gWinMaximized
:= false;
478 gRC_Maximized
:= false
480 e_MuteChannels(false);
481 {$IFDEF ENABLE_HOLMES}
482 if assigned(winFocusCB
) then winFocusCB
;
488 procedure HandleKeyboard (var ev
: TSDL_KeyboardEvent
);
489 var down
: Boolean; key
: Integer;
491 key
:= ev
.keysym
.scancode
;
492 down
:= (ev
.type_
= SDL_KEYDOWN
);
493 if key
= SDL_SCANCODE_AC_BACK
then
494 key
:= SDL_SCANCODE_ESCAPE
;
495 {$IFDEF ENABLE_HOLMES}
496 if fuiOnSDLEvent(PSDL_Event(@ev
)^) then
498 // event eaten, but...
499 if not down
then e_KeyUpDown(key
, false);
503 if ev
._repeat
= 0 then
506 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down
, key
]);
507 e_KeyUpDown(key
, down
);
508 g_Console_ProcessBind(key
, down
);
510 else if gConsoleShow
or gChatShow
or (g_ActiveWindow
<> nil) then
512 KeyPress(key
) // key repeat in menus and shit
516 procedure HandleTextInput (var ev
: TSDL_TextInputEvent
);
517 var ch
: UnicodeChar
; sch
: AnsiChar;
519 Utf8ToUnicode(@ch
, PChar(ev
.text), 1);
520 sch
:= AnsiChar(wchar2win(ch
));
522 e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev
.text, Ord(ch
), Ord(sch
)]);
523 if IsValid1251(Word(ch
)) and IsPrintable1251(ch
) then
527 function sys_HandleInput (): Boolean;
531 ZeroMemory(@ev
, sizeof(ev
));
532 while SDL_PollEvent(@ev
) <> 0 do
535 SDL_QUITEV
: result
:= true;
536 SDL_WINDOWEVENT
: result
:= HandleWindow(ev
.window
);
537 SDL_KEYUP
, SDL_KEYDOWN
: HandleKeyboard(ev
.key
);
538 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
: HandleJoyButton(ev
.jbutton
);
539 SDL_JOYAXISMOTION
: HandleJoyAxis(ev
.jaxis
);
540 SDL_JOYHATMOTION
: HandleJoyHat(ev
.jhat
);
541 SDL_JOYDEVICEADDED
: HandleJoyAdd(ev
.jdevice
);
542 SDL_JOYDEVICEREMOVED
: HandleJoyRemove(ev
.jdevice
);
543 SDL_TEXTINPUT
: HandleTextInput(ev
.text);
544 SDL_FINGERMOTION
, SDL_FINGERDOWN
, SDL_FINGERUP
: g_Touch_HandleEvent(ev
.tfinger
);
545 {$IFDEF ENABLE_HOLMES}
546 SDL_MOUSEBUTTONDOWN
, SDL_MOUSEBUTTONUP
, SDL_MOUSEWHEEL
, SDL_MOUSEMOTION
: fuiOnSDLEvent(ev
);
552 procedure sys_RequestQuit
;
555 ev
.type_
:= SDL_QUITEV
;
559 (* --------- Init --------- *)
564 e_WriteLog('Init SDL2', TMsgType
.Notify
);
566 {$IFDEF USE_SDLMIXER}
567 flags
:= SDL_INIT_TIMER
or SDL_INIT_AUDIO
or $00004000;
569 flags
:= SDL_INIT_TIMER
or $00004000;
572 flags
:= SDL_INIT_TIMER
or SDL_INIT_VIDEO
;
574 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK
, '0');
575 if SDL_Init(flags
) <> 0 then
576 raise Exception
.Create('SDL: Init failed: ' + SDL_GetError
);
578 if SDL_InitSubSystem(SDL_INIT_JOYSTICK
) <> 0 then
579 e_LogWritefln('SDL: Init subsystem failed: %s', [SDL_GetError()]);
581 SDL_ShowCursor(SDL_DISABLE
);
586 e_WriteLog('Releasing SDL2', TMsgType
.Notify
);
587 if context
<> nil then
590 SDL_GL_DeleteContext(context
);
593 if window
<> nil then
595 SDL_DestroyWindow(window
);
602 conRegVar('sdl2_display_index', @display
, 'use display index as base', '');
603 conRegVar('sdl2_window_x', @wx
, 'window position x', '');
604 conRegVar('sdl2_window_y', @wy
, 'window position y', '');
605 conRegVar('sdl2_window_center', @wc
, 'force window creation at center', '');
607 wx
:= SDL_WINDOWPOS_CENTERED
;
608 wy
:= SDL_WINDOWPOS_CENTERED
;