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 e_ResizeFramebuffer(w
, h
);
114 {$IFDEF ENABLE_HOLMES}
118 e_ResizeWindow(w
, h
);
120 g_Game_SetupScreenSize
;
122 (* This will fix menu reset on keyboard showing *)
126 {$IFDEF ENABLE_HOLMES}
127 if assigned(oglInitCB
) then oglInitCB
;
131 function GetTitle (): PChar;
132 var info
: AnsiString;
134 info
:= g_GetBuildHash(false);
135 if info
= 'custom build' then
136 info
:= info
+ ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE
+ ' ' + GAME_BUILDTIME
;
137 result
:= PChar(Format(GameTitle
, [info
]))
140 function InitWindow (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
141 var flags
: UInt32
; x
, y
: cint
;
143 // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
144 e_LogWritefln('InitWindow %s %s %s %s', [w
, h
, bpp
, fullScreen
]);
149 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 1);
150 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
151 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK
, SDL_GL_CONTEXT_PROFILE_ES
);
153 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
154 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
155 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
156 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
157 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
158 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
159 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
160 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
162 flags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
163 if fullScreen
then flags
:= flags
or SDL_WINDOW_FULLSCREEN
;
164 if maximized
then flags
:= flags
or SDL_WINDOW_MAXIMIZED
;
167 x
:= SDL_WINDOWPOS_CENTERED
;
168 y
:= SDL_WINDOWPOS_CENTERED
175 window
:= SDL_CreateWindow(GetTitle(), x
, y
, w
, h
, flags
);
176 if window
<> nil then
178 context
:= SDL_GL_CreateContext(window
);
179 if context
<> nil then
183 e_LogWriteln('GL: unable to load OpenGL functions', TMsgType
.Fatal
);
184 SDL_GL_DeleteContext(context
); context
:= nil;
187 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
189 SDL_GetWindowPosition(window
, @x
, @y
);
192 gFullScreen
:= fullscreen
;
193 gWinMaximized
:= maximized
;
194 gRC_FullScreen
:= fullscreen
;
195 gRC_Maximized
:= maximized
;
201 // SDL_DestroyWindow(window);
202 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError
])
207 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError
])
212 if fullScreen
then flags
:= SDL_WINDOW_FULLSCREEN
else flags
:= 0;
213 SDL_SetWindowFullscreen(window
, flags
);
214 SDL_SetWindowSize(window
, w
, h
);
215 if maximized
then SDL_MaximizeWindow(window
);
216 // always reset to center when changing fullscreen->windowed for safety purposes
217 if wc
or (gFullscreen
and not fullscreen
) or (gWinMaximized
and not maximized
) then
219 x
:= SDL_WINDOWPOS_CENTERED
;
220 y
:= SDL_WINDOWPOS_CENTERED
227 SDL_SetWindowPosition(window
, x
, y
);
228 if (fullscreen
= false) and (maximized
= false) and (wc
= false) then
230 SDL_GetWindowPosition(window
, @x
, @y
);
233 gFullScreen
:= fullscreen
;
234 gWinMaximized
:= maximized
;
235 gRC_FullScreen
:= fullscreen
;
236 gRC_Maximized
:= maximized
;
242 procedure sys_Repaint
;
244 if glRenderToFBO
then
245 e_BlitFramebuffer(gWinSizeX
, gWinSizeY
);
246 SDL_GL_SwapWindow(window
)
249 procedure sys_EnableVSync (yes
: Boolean);
252 SDL_GL_SetSwapInterval(1)
254 SDL_GL_SetSwapInterval(0)
257 function sys_GetDisplayModes (bpp
: Integer): SSArray
;
258 var i
, count
, num
, pw
, ph
: Integer; m
: TSDL_DisplayMode
;
261 num
:= SDL_GetNumDisplayModes(display
);
263 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError
]);
266 e_LogWritefln('Video modes for display %s:', [display
]);
267 SetLength(result
, num
);
268 i
:= 0; count
:= 0; pw
:= 0; ph
:= 0;
271 SDL_GetDisplayMode(display
, i
, @m
);
272 if ((pw
<> m
.w
) or (ph
<> m
.h
)) then
274 e_LogWritefln('* %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
275 pw
:= m
.w
; ph
:= m
.h
;
276 result
[count
] := IntToStr(m
.w
) + 'x' + IntToStr(m
.h
);
281 e_LogWritefln('- %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
285 SetLength(result
, count
)
289 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
291 result
:= InitWindow(w
, h
, bpp
, fullScreen
, maximized
)
294 (* --------- Joystick --------- *)
296 procedure HandleJoyButton (var ev
: TSDL_JoyButtonEvent
);
297 var down
: Boolean; key
: Integer;
299 if (ev
.which
< e_MaxJoys
) and (ev
.button
< e_MaxJoyBtns
) then
301 key
:= e_JoyButtonToKey(ev
.which
, ev
.button
);
302 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
304 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev
.which
, ev
.button
, key
, down
]);
305 e_KeyUpDown(key
, down
);
306 g_Console_ProcessBind(key
, down
)
312 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
313 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev
.which
, ev
.button
, down
])
318 procedure HandleJoyAxis (var ev
: TSDL_JoyAxisEvent
);
319 var key
, minuskey
: Integer;
321 if (ev
.which
< e_MaxJoys
) and (ev
.axis
< e_MaxJoyAxes
) then
323 key
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_PLUS
);
324 minuskey
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_MINUS
);
327 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
]]);
329 if ev
.value
< JoystickZeroAxes
[ev
.which
, ev
.axis
] - e_JoystickDeadzones
[ev
.which
] then
331 if (e_KeyPressed(key
)) then
333 e_KeyUpDown(key
, False);
334 g_Console_ProcessBind(key
, False)
336 e_KeyUpDown(minuskey
, True);
337 g_Console_ProcessBind(minuskey
, True)
339 else if ev
.value
> JoystickZeroAxes
[ev
.which
, ev
.axis
] + e_JoystickDeadzones
[ev
.which
] then
341 if (e_KeyPressed(minuskey
)) then
343 e_KeyUpDown(minuskey
, False);
344 g_Console_ProcessBind(minuskey
, False)
346 e_KeyUpDown(key
, True);
347 g_Console_ProcessBind(key
, True)
351 if (e_KeyPressed(minuskey
)) then
353 e_KeyUpDown(minuskey
, False);
354 g_Console_ProcessBind(minuskey
, False)
356 if (e_KeyPressed(key
)) then
358 e_KeyUpDown(key
, False);
359 g_Console_ProcessBind(key
, False)
366 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
]])
370 procedure HandleJoyHat (var ev
: TSDL_JoyHatEvent
);
374 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
376 if (ev
.which
< e_MaxJoys
) and (ev
.hat
< e_MaxJoyHats
) then
379 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
]);
380 hat
[HAT_UP
] := LongBool(ev
.value
and SDL_HAT_UP
);
381 hat
[HAT_DOWN
] := LongBool(ev
.value
and SDL_HAT_DOWN
);
382 hat
[HAT_LEFT
] := LongBool(ev
.value
and SDL_HAT_LEFT
);
383 hat
[HAT_RIGHT
] := LongBool(ev
.value
and SDL_HAT_RIGHT
);
384 for i
:= HAT_LEFT
to HAT_DOWN
do
386 if JoystickHatState
[ev
.which
, ev
.hat
, i
] <> hat
[i
] then
389 key
:= e_JoyHatToKey(ev
.which
, ev
.hat
, i
);
390 e_KeyUpDown(key
, down
);
391 g_Console_ProcessBind(key
, down
)
394 JoystickHatState
[ev
.which
, ev
.hat
] := hat
399 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
])
403 procedure HandleJoyAdd (var ev
: TSDL_JoyDeviceEvent
);
406 if (ev
.which
< e_MaxJoys
) then
408 JoystickHandle
[ev
.which
] := SDL_JoystickOpen(ev
.which
);
409 if JoystickHandle
[ev
.which
] <> nil then
411 e_LogWritefln('Added Joystick %s', [ev
.which
]);
412 e_JoystickAvailable
[ev
.which
] := True;
413 for i
:= 0 to Min(SDL_JoystickNumAxes(JoystickHandle
[ev
.which
]), e_MaxJoyAxes
) - 1 do
414 JoystickZeroAxes
[ev
.which
, i
] := SDL_JoystickGetAxis(JoystickHandle
[ev
.which
], i
)
418 e_LogWritefln('Warning! Failed to open Joystick %s', [ev
.which
])
423 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev
.which
, e_MaxJoys
])
427 procedure HandleJoyRemove (var ev
: TSDL_JoyDeviceEvent
);
429 e_LogWritefln('Removed Joystick %s', [ev
.which
]);
430 if (ev
.which
< e_MaxJoys
) then
432 e_JoystickAvailable
[ev
.which
] := False;
433 if JoystickHandle
[ev
.which
] <> nil then
434 SDL_JoystickClose(JoystickHandle
[ev
.which
]);
435 JoystickHandle
[ev
.which
] := nil
439 (* --------- Input --------- *)
441 function HandleWindow (var ev
: TSDL_WindowEvent
): Boolean;
445 e_LogWritefln('Window Event: event = %s, data1 = %s, data2 = %s', [ev
.event
, ev
.data1
, ev
.data2
]);
447 SDL_WINDOWEVENT_RESIZED
: UpdateSize(ev
.data1
, ev
.data2
);
448 SDL_WINDOWEVENT_EXPOSED
: sys_Repaint
;
449 SDL_WINDOWEVENT_CLOSE
: result
:= true;
450 SDL_WINDOWEVENT_MOVED
:
455 SDL_WINDOWEVENT_FOCUS_LOST
, SDL_WINDOWEVENT_MINIMIZED
:
458 if gMuteWhenInactive
then
459 e_MuteChannels(true);
460 {$IFDEF ENABLE_HOLMES}
461 if assigned(winBlurCB
) then winBlurCB
;
464 SDL_WINDOWEVENT_FOCUS_GAINED
, SDL_WINDOWEVENT_MAXIMIZED
, SDL_WINDOWEVENT_RESTORED
:
466 if ev
.event
= SDL_WINDOWEVENT_MAXIMIZED
then
468 gWinMaximized
:= true;
469 gRC_Maximized
:= true
471 else if ev
.event
= SDL_WINDOWEVENT_RESTORED
then
473 gWinMaximized
:= false;
474 gRC_Maximized
:= false
476 e_MuteChannels(false);
477 {$IFDEF ENABLE_HOLMES}
478 if assigned(winFocusCB
) then winFocusCB
;
484 procedure HandleKeyboard (var ev
: TSDL_KeyboardEvent
);
485 var down
: Boolean; key
: Integer;
487 key
:= ev
.keysym
.scancode
;
488 down
:= (ev
.type_
= SDL_KEYDOWN
);
489 if key
= SDL_SCANCODE_AC_BACK
then
490 key
:= SDL_SCANCODE_ESCAPE
;
491 {$IFDEF ENABLE_HOLMES}
492 if fuiOnSDLEvent(PSDL_Event(@ev
)^) then
494 // event eaten, but...
495 if not down
then e_KeyUpDown(key
, false);
499 if ev
._repeat
= 0 then
502 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down
, key
]);
503 e_KeyUpDown(key
, down
);
504 g_Console_ProcessBind(key
, down
);
506 else if gConsoleShow
or gChatShow
or (g_ActiveWindow
<> nil) then
508 KeyPress(key
) // key repeat in menus and shit
512 procedure HandleTextInput (var ev
: TSDL_TextInputEvent
);
513 var ch
: UnicodeChar
; sch
: AnsiChar;
515 Utf8ToUnicode(@ch
, PChar(ev
.text), 1);
516 sch
:= AnsiChar(wchar2win(ch
));
518 e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev
.text, Ord(ch
), Ord(sch
)]);
519 if IsValid1251(Word(ch
)) and IsPrintable1251(ch
) then
523 function sys_HandleInput (): Boolean;
527 ZeroMemory(@ev
, sizeof(ev
));
528 while SDL_PollEvent(@ev
) <> 0 do
531 SDL_QUITEV
: result
:= true;
532 SDL_WINDOWEVENT
: result
:= HandleWindow(ev
.window
);
533 SDL_KEYUP
, SDL_KEYDOWN
: HandleKeyboard(ev
.key
);
534 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
: HandleJoyButton(ev
.jbutton
);
535 SDL_JOYAXISMOTION
: HandleJoyAxis(ev
.jaxis
);
536 SDL_JOYHATMOTION
: HandleJoyHat(ev
.jhat
);
537 SDL_JOYDEVICEADDED
: HandleJoyAdd(ev
.jdevice
);
538 SDL_JOYDEVICEREMOVED
: HandleJoyRemove(ev
.jdevice
);
539 SDL_TEXTINPUT
: HandleTextInput(ev
.text);
540 SDL_FINGERMOTION
, SDL_FINGERDOWN
, SDL_FINGERUP
: g_Touch_HandleEvent(ev
.tfinger
);
541 {$IFDEF ENABLE_HOLMES}
542 SDL_MOUSEBUTTONDOWN
, SDL_MOUSEBUTTONUP
, SDL_MOUSEWHEEL
, SDL_MOUSEMOTION
: fuiOnSDLEvent(ev
);
548 procedure sys_RequestQuit
;
551 ev
.type_
:= SDL_QUITEV
;
555 (* --------- Init --------- *)
560 e_WriteLog('Init SDL2', TMsgType
.Notify
);
562 {$IFDEF USE_SDLMIXER}
563 flags
:= SDL_INIT_TIMER
or SDL_INIT_AUDIO
or $00004000;
565 flags
:= SDL_INIT_TIMER
or $00004000;
568 flags
:= SDL_INIT_TIMER
or SDL_INIT_VIDEO
;
570 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK
, '0');
571 if SDL_Init(flags
) <> 0 then
572 raise Exception
.Create('SDL: Init failed: ' + SDL_GetError
);
574 if SDL_InitSubSystem(SDL_INIT_JOYSTICK
) <> 0 then
575 e_LogWritefln('SDL: Init subsystem failed: %s', [SDL_GetError()]);
577 SDL_ShowCursor(SDL_DISABLE
);
582 e_WriteLog('Releasing SDL2', TMsgType
.Notify
);
583 if context
<> nil then
586 SDL_GL_DeleteContext(context
);
589 if window
<> nil then
591 SDL_DestroyWindow(window
);
598 conRegVar('sdl2_display_index', @display
, 'use display index as base', '');
599 conRegVar('sdl2_window_x', @wx
, 'window position x', '');
600 conRegVar('sdl2_window_y', @wy
, 'window position y', '');
601 conRegVar('sdl2_window_center', @wc
, 'force window creation at center', '');
603 wx
:= SDL_WINDOWPOS_CENTERED
;
604 wy
:= SDL_WINDOWPOS_CENTERED
;