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
;
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
;
58 JoystickHandle
: array [0..e_MaxJoys
- 1] of PSDL_Joystick
;
59 JoystickHatState
: array [0..e_MaxJoys
- 1, 0..e_MaxJoyHats
- 1, HAT_LEFT
..HAT_DOWN
] of Boolean;
60 JoystickZeroAxes
: array [0..e_MaxJoys
- 1, 0..e_MaxJoyAxes
- 1] of Integer;
62 (* --------- Utils --------- *)
64 function sys_GetTicks (): Int64;
66 result
:= SDL_GetTicks()
69 procedure sys_Delay (ms
: Integer);
74 (* --------- Graphics --------- *)
76 procedure UpdateSize (w
, h
: Integer);
84 {$IFDEF ENABLE_HOLMES}
90 g_Game_SetupScreenSize
;
92 (* This will fix menu reset on keyboard showing *)
96 {$IFDEF ENABLE_HOLMES}
97 if assigned(oglInitCB
) then oglInitCB
;
101 function GetTitle (): PChar;
102 var info
: AnsiString;
104 info
:= g_GetBuildHash(false);
105 if info
= 'custom build' then
106 info
:= info
+ ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE
+ ' ' + GAME_BUILDTIME
;
107 result
:= PChar(Format(GameTitle
, [info
]))
110 function InitWindow (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
111 var flags
: UInt32
; x
, y
: Integer;
113 // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
114 e_LogWritefln('InitWindow %s %s %s %s', [w
, h
, bpp
, fullScreen
]);
119 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 1);
120 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
121 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK
, SDL_GL_CONTEXT_PROFILE_ES
);
123 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
124 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
125 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
126 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
127 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
128 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
129 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
130 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
132 flags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
133 if fullScreen
then flags
:= flags
or SDL_WINDOW_FULLSCREEN
;
134 if maximized
then flags
:= flags
or SDL_WINDOW_MAXIMIZED
;
135 x
:= SDL_WINDOWPOS_CENTERED
;
136 y
:= SDL_WINDOWPOS_CENTERED
;
137 window
:= SDL_CreateWindow(GetTitle(), x
, y
, w
, h
, flags
);
138 if window
<> nil then
140 context
:= SDL_GL_CreateContext(window
);
141 if context
<> nil then
146 gFullScreen
:= fullscreen
;
147 gWinMaximized
:= maximized
;
148 gRC_FullScreen
:= fullscreen
;
149 gRC_Maximized
:= maximized
;
155 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError
])
160 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError
])
165 SDL_SetWindowSize(window
, w
, h
);
167 SDL_MaximizeWindow(window
);
168 if fullScreen
then flags
:= SDL_WINDOW_FULLSCREEN
else flags
:= 0;
169 SDL_SetWindowFullscreen(window
, flags
);
170 gFullScreen
:= fullscreen
;
171 gWinMaximized
:= maximized
;
172 gRC_FullScreen
:= fullscreen
;
173 gRC_Maximized
:= maximized
;
179 procedure sys_Repaint
;
181 SDL_GL_SwapWindow(window
)
184 procedure sys_EnableVSync (yes
: Boolean);
187 SDL_GL_SetSwapInterval(1)
189 SDL_GL_SetSwapInterval(0)
192 function sys_GetDisplayModes (bpp
: Integer): SSArray
;
193 var i
, count
, num
, pw
, ph
: Integer; m
: TSDL_DisplayMode
;
196 num
:= SDL_GetNumDisplayModes(display
);
198 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError
]);
201 e_LogWritefln('Video modes for display %s:', [display
]);
202 SetLength(result
, num
);
203 i
:= 0; count
:= 0; pw
:= 0; ph
:= 0;
206 SDL_GetDisplayMode(display
, i
, @m
);
207 if ((pw
<> m
.w
) or (ph
<> m
.h
)) then
209 e_LogWritefln('* %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
210 pw
:= m
.w
; ph
:= m
.h
;
211 result
[count
] := IntToStr(m
.w
) + 'x' + IntToStr(m
.h
);
216 e_LogWritefln('- %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
220 SetLength(result
, count
)
224 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullScreen
, maximized
: Boolean): Boolean;
226 result
:= InitWindow(w
, h
, bpp
, fullScreen
, maximized
)
229 (* --------- Joystick --------- *)
231 procedure HandleJoyButton (var ev
: TSDL_JoyButtonEvent
);
232 var down
: Boolean; key
: Integer;
234 if (ev
.which
< e_MaxJoys
) and (ev
.button
< e_MaxJoyBtns
) then
236 key
:= e_JoyButtonToKey(ev
.which
, ev
.button
);
237 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
239 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev
.which
, ev
.button
, key
, down
]);
240 e_KeyUpDown(key
, down
);
241 g_Console_ProcessBind(key
, down
)
247 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
248 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev
.which
, ev
.button
, down
])
253 procedure HandleJoyAxis (var ev
: TSDL_JoyAxisEvent
);
254 var key
, minuskey
: Integer;
256 if (ev
.which
< e_MaxJoys
) and (ev
.axis
< e_MaxJoyAxes
) then
258 key
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_PLUS
);
259 minuskey
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_MINUS
);
262 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
]]);
264 if ev
.value
< JoystickZeroAxes
[ev
.which
, ev
.axis
] - e_JoystickDeadzones
[ev
.which
] then
266 if (e_KeyPressed(key
)) then
268 e_KeyUpDown(key
, False);
269 g_Console_ProcessBind(key
, False)
271 e_KeyUpDown(minuskey
, True);
272 g_Console_ProcessBind(minuskey
, True)
274 else if ev
.value
> JoystickZeroAxes
[ev
.which
, ev
.axis
] + e_JoystickDeadzones
[ev
.which
] then
276 if (e_KeyPressed(minuskey
)) then
278 e_KeyUpDown(minuskey
, False);
279 g_Console_ProcessBind(minuskey
, False)
281 e_KeyUpDown(key
, True);
282 g_Console_ProcessBind(key
, True)
286 if (e_KeyPressed(minuskey
)) then
288 e_KeyUpDown(minuskey
, False);
289 g_Console_ProcessBind(minuskey
, False)
291 if (e_KeyPressed(key
)) then
293 e_KeyUpDown(key
, False);
294 g_Console_ProcessBind(key
, False)
301 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
]])
305 procedure HandleJoyHat (var ev
: TSDL_JoyHatEvent
);
309 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
311 if (ev
.which
< e_MaxJoys
) and (ev
.hat
< e_MaxJoyHats
) then
314 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
]);
315 hat
[HAT_UP
] := LongBool(ev
.value
and SDL_HAT_UP
);
316 hat
[HAT_DOWN
] := LongBool(ev
.value
and SDL_HAT_DOWN
);
317 hat
[HAT_LEFT
] := LongBool(ev
.value
and SDL_HAT_LEFT
);
318 hat
[HAT_RIGHT
] := LongBool(ev
.value
and SDL_HAT_RIGHT
);
319 for i
:= HAT_LEFT
to HAT_DOWN
do
321 if JoystickHatState
[ev
.which
, ev
.hat
, i
] <> hat
[i
] then
324 key
:= e_JoyHatToKey(ev
.which
, ev
.hat
, i
);
325 e_KeyUpDown(key
, down
);
326 g_Console_ProcessBind(key
, down
)
329 JoystickHatState
[ev
.which
, ev
.hat
] := hat
334 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
])
338 procedure HandleJoyAdd (var ev
: TSDL_JoyDeviceEvent
);
341 if (ev
.which
< e_MaxJoys
) then
343 JoystickHandle
[ev
.which
] := SDL_JoystickOpen(ev
.which
);
344 if JoystickHandle
[ev
.which
] <> nil then
346 e_LogWritefln('Added Joystick %s', [ev
.which
]);
347 e_JoystickAvailable
[ev
.which
] := True;
348 for i
:= 0 to Min(SDL_JoystickNumAxes(JoystickHandle
[ev
.which
]), e_MaxJoyAxes
) - 1 do
349 JoystickZeroAxes
[ev
.which
, i
] := SDL_JoystickGetAxis(JoystickHandle
[ev
.which
], i
)
353 e_LogWritefln('Warning! Failed to open Joystick %s', [ev
.which
])
358 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev
.which
, e_MaxJoys
])
362 procedure HandleJoyRemove (var ev
: TSDL_JoyDeviceEvent
);
364 e_LogWritefln('Removed Joystick %s', [ev
.which
]);
365 if (ev
.which
< e_MaxJoys
) then
367 e_JoystickAvailable
[ev
.which
] := False;
368 if JoystickHandle
[ev
.which
] <> nil then
369 SDL_JoystickClose(JoystickHandle
[ev
.which
]);
370 JoystickHandle
[ev
.which
] := nil
374 (* --------- Input --------- *)
376 function HandleWindow (var ev
: TSDL_WindowEvent
): Boolean;
380 e_LogWritefln('Window Event: event = %s, data1 = %s, data2 = %s', [ev
.event
, ev
.data1
, ev
.data2
]);
382 SDL_WINDOWEVENT_RESIZED
: UpdateSize(ev
.data1
, ev
.data2
);
383 SDL_WINDOWEVENT_EXPOSED
: sys_Repaint
;
384 SDL_WINDOWEVENT_CLOSE
: result
:= true;
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
397 gRC_Maximized
:= true
398 else if ev
.event
= SDL_WINDOWEVENT_RESTORED
then
399 gRC_Maximized
:= false;
400 e_MuteChannels(false);
401 {$IFDEF ENABLE_HOLMES}
402 if assigned(winFocusCB
) then winFocusCB
;
408 procedure HandleKeyboard (var ev
: TSDL_KeyboardEvent
);
409 var down
: Boolean; key
: Integer;
411 key
:= ev
.keysym
.scancode
;
412 down
:= (ev
.type_
= SDL_KEYDOWN
);
413 if key
= SDL_SCANCODE_AC_BACK
then
414 key
:= SDL_SCANCODE_ESCAPE
;
415 {$IFDEF ENABLE_HOLMES}
416 if fuiOnSDLEvent(PSDL_Event(@ev
)^) then
418 // event eaten, but...
419 if not down
then e_KeyUpDown(key
, false);
423 if ev
._repeat
= 0 then
426 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down
, key
]);
427 e_KeyUpDown(key
, down
);
428 g_Console_ProcessBind(key
, down
);
430 else if gConsoleShow
or gChatShow
or (g_ActiveWindow
<> nil) then
432 KeyPress(key
) // key repeat in menus and shit
436 procedure HandleTextInput (var ev
: TSDL_TextInputEvent
);
437 var ch
: UnicodeChar
; sch
: AnsiChar;
439 Utf8ToUnicode(@ch
, PChar(ev
.text), 1);
440 sch
:= AnsiChar(wchar2win(ch
));
442 e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev
.text, Ord(ch
), Ord(sch
)]);
443 if IsValid1251(Word(ch
)) and IsPrintable1251(ch
) then
447 function sys_HandleInput (): Boolean;
451 ZeroMemory(@ev
, sizeof(ev
));
452 while SDL_PollEvent(@ev
) <> 0 do
455 SDL_QUITEV
: result
:= true;
456 SDL_WINDOWEVENT
: result
:= HandleWindow(ev
.window
);
457 SDL_KEYUP
, SDL_KEYDOWN
: HandleKeyboard(ev
.key
);
458 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
: HandleJoyButton(ev
.jbutton
);
459 SDL_JOYAXISMOTION
: HandleJoyAxis(ev
.jaxis
);
460 SDL_JOYHATMOTION
: HandleJoyHat(ev
.jhat
);
461 SDL_JOYDEVICEADDED
: HandleJoyAdd(ev
.jdevice
);
462 SDL_JOYDEVICEREMOVED
: HandleJoyRemove(ev
.jdevice
);
463 SDL_TEXTINPUT
: HandleTextInput(ev
.text);
464 SDL_FINGERMOTION
, SDL_FINGERDOWN
, SDL_FINGERUP
: g_Touch_HandleEvent(ev
.tfinger
);
465 {$IFDEF ENABLE_HOLMES}
466 SDL_MOUSEBUTTONDOWN
, SDL_MOUSEBUTTONUP
, SDL_MOUSEWHEEL
, SDL_MOUSEMOTION
: fuiOnSDLEvent(ev
);
472 procedure sys_RequestQuit
;
475 ev
.type_
:= SDL_QUITEV
;
479 (* --------- Init --------- *)
484 e_WriteLog('Init SDL2', TMsgType
.Notify
);
486 {$IFDEF USE_SDLMIXER}
487 flags
:= SDL_INIT_TIMER
or SDL_INIT_AUDIO
or $00004000;
489 flags
:= SDL_INIT_TIMER
or $00004000;
492 flags
:= SDL_INIT_JOYSTICK
or SDL_INIT_TIMER
or SDL_INIT_VIDEO
;
494 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK
, '0');
495 if SDL_Init(flags
) <> 0 then
496 raise Exception
.Create('SDL: Init failed: ' + SDL_GetError
);
497 SDL_ShowCursor(SDL_DISABLE
);
502 e_WriteLog('Releasing SDL2', TMsgType
.Notify
);
503 if context
<> nil then
508 SDL_GL_DeleteContext(context
);
511 if window
<> nil then
513 SDL_DestroyWindow(window
);
520 conRegVar('sdl2_display_index', @display
, 'use display index as base', '');