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
: 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);
82 {$IFDEF ENABLE_HOLMES}
88 g_Game_SetupScreenSize
;
90 (* This will fix menu reset on keyboard showing *)
94 {$IFDEF ENABLE_HOLMES}
95 if assigned(oglInitCB
) then oglInitCB
;
99 function GetTitle (): PChar
;
100 var info
: AnsiString
;
102 info
:= g_GetBuildHash(false);
103 if info
= 'custom build' then
104 info
:= info
+ ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE
+ ' ' + GAME_BUILDTIME
;
105 result
:= PChar(Format(GameTitle
, [info
]))
108 function InitWindow (w
, h
, bpp
: Integer; fullScreen
: Boolean): Boolean;
109 var flags
: UInt32
; x
, y
: Integer;
111 // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
112 e_LogWritefln('InitWindow %s %s %s %s', [w
, h
, bpp
, fullScreen
]);
117 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 1);
118 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
119 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK
, SDL_GL_CONTEXT_PROFILE_ES
);
121 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
122 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
123 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
124 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
125 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
126 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
127 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
128 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
130 flags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
131 if fullScreen
then flags
:= flags
or SDL_WINDOW_FULLSCREEN
;
132 x
:= SDL_WINDOWPOS_CENTERED
;
133 y
:= SDL_WINDOWPOS_CENTERED
;
134 window
:= SDL_CreateWindow(GetTitle(), x
, y
, w
, h
, flags
);
135 if window
<> nil then
137 context
:= SDL_GL_CreateContext(window
);
138 if context
<> nil then
148 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError
])
153 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError
])
158 if fullScreen
then flags
:= SDL_WINDOW_FULLSCREEN
else flags
:= 0;
159 SDL_SetWindowSize(window
, w
, h
);
160 SDL_SetWindowFullscreen(window
, flags
);
166 procedure sys_Repaint
;
168 SDL_GL_SwapWindow(window
)
171 procedure sys_EnableVSync (yes
: Boolean);
174 SDL_GL_SetSwapInterval(1)
176 SDL_GL_SetSwapInterval(0)
179 function sys_GetDisplayModes (bpp
: Integer): SSArray
;
180 var i
, count
, num
, pw
, ph
: Integer; m
: TSDL_DisplayMode
;
183 num
:= SDL_GetNumDisplayModes(display
);
185 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError
]);
188 e_LogWritefln('Video modes for display %s:', [display
]);
189 SetLength(result
, num
);
190 i
:= 0; count
:= 0; pw
:= 0; ph
:= 0;
193 SDL_GetDisplayMode(display
, i
, @m
);
194 if ((pw
<> m
.w
) or (ph
<> m
.h
)) then
196 e_LogWritefln('* %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
197 pw
:= m
.w
; ph
:= m
.h
;
198 result
[count
] := IntToStr(m
.w
) + 'x' + IntToStr(m
.h
);
203 e_LogWritefln('- %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
207 SetLength(result
, count
)
211 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullScreen
: Boolean): Boolean;
213 result
:= InitWindow(w
, h
, bpp
, fullScreen
)
216 (* --------- Joystick --------- *)
218 procedure HandleJoyButton (var ev
: TSDL_JoyButtonEvent
);
219 var down
: Boolean; key
: Integer;
221 if (ev
.which
< e_MaxJoys
) and (ev
.button
< e_MaxJoyBtns
) then
223 key
:= e_JoyButtonToKey(ev
.which
, ev
.button
);
224 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
226 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev
.which
, ev
.button
, key
, down
]);
227 e_KeyUpDown(key
, down
);
228 g_Console_ProcessBind(key
, down
)
234 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
235 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev
.which
, ev
.button
, down
])
240 procedure HandleJoyAxis (var ev
: TSDL_JoyAxisEvent
);
241 var key
, minuskey
: Integer;
243 if (ev
.which
< e_MaxJoys
) and (ev
.axis
< e_MaxJoyAxes
) then
245 key
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_PLUS
);
246 minuskey
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_MINUS
);
249 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
]]);
251 if ev
.value
< JoystickZeroAxes
[ev
.which
, ev
.axis
] - e_JoystickDeadzones
[ev
.which
] then
253 if (e_KeyPressed(key
)) then
255 e_KeyUpDown(key
, False);
256 g_Console_ProcessBind(key
, False)
258 e_KeyUpDown(minuskey
, True);
259 g_Console_ProcessBind(minuskey
, True)
261 else if ev
.value
> JoystickZeroAxes
[ev
.which
, ev
.axis
] + e_JoystickDeadzones
[ev
.which
] then
263 if (e_KeyPressed(minuskey
)) then
265 e_KeyUpDown(minuskey
, False);
266 g_Console_ProcessBind(minuskey
, False)
268 e_KeyUpDown(key
, True);
269 g_Console_ProcessBind(key
, True)
273 if (e_KeyPressed(minuskey
)) then
275 e_KeyUpDown(minuskey
, False);
276 g_Console_ProcessBind(minuskey
, False)
278 if (e_KeyPressed(key
)) then
280 e_KeyUpDown(key
, False);
281 g_Console_ProcessBind(key
, False)
288 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
]])
292 procedure HandleJoyHat (var ev
: TSDL_JoyHatEvent
);
296 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
298 if (ev
.which
< e_MaxJoys
) and (ev
.hat
< e_MaxJoyHats
) then
301 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
]);
302 hat
[HAT_UP
] := LongBool(ev
.value
and SDL_HAT_UP
);
303 hat
[HAT_DOWN
] := LongBool(ev
.value
and SDL_HAT_DOWN
);
304 hat
[HAT_LEFT
] := LongBool(ev
.value
and SDL_HAT_LEFT
);
305 hat
[HAT_RIGHT
] := LongBool(ev
.value
and SDL_HAT_RIGHT
);
306 for i
:= HAT_LEFT
to HAT_DOWN
do
308 if JoystickHatState
[ev
.which
, ev
.hat
, i
] <> hat
[i
] then
311 key
:= e_JoyHatToKey(ev
.which
, ev
.hat
, i
);
312 e_KeyUpDown(key
, down
);
313 g_Console_ProcessBind(key
, down
)
316 JoystickHatState
[ev
.which
, ev
.hat
] := hat
321 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
])
325 procedure HandleJoyAdd (var ev
: TSDL_JoyDeviceEvent
);
328 if (ev
.which
< e_MaxJoys
) then
330 JoystickHandle
[ev
.which
] := SDL_JoystickOpen(ev
.which
);
331 if JoystickHandle
[ev
.which
] <> nil then
333 e_LogWritefln('Added Joystick %s', [ev
.which
]);
334 e_JoystickAvailable
[ev
.which
] := True;
335 for i
:= 0 to Min(SDL_JoystickNumAxes(JoystickHandle
[ev
.which
]), e_MaxJoyAxes
) - 1 do
336 JoystickZeroAxes
[ev
.which
, i
] := SDL_JoystickGetAxis(JoystickHandle
[ev
.which
], i
)
340 e_LogWritefln('Warning! Failed to open Joystick %s', [ev
.which
])
345 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev
.which
, e_MaxJoys
])
349 procedure HandleJoyRemove (var ev
: TSDL_JoyDeviceEvent
);
351 e_LogWritefln('Removed Joystick %s', [ev
.which
]);
352 if (ev
.which
< e_MaxJoys
) then
354 e_JoystickAvailable
[ev
.which
] := False;
355 if JoystickHandle
[ev
.which
] <> nil then
356 SDL_JoystickClose(JoystickHandle
[ev
.which
]);
357 JoystickHandle
[ev
.which
] := nil
361 (* --------- Input --------- *)
363 function HandleWindow (var ev
: TSDL_WindowEvent
): Boolean;
367 SDL_WINDOWEVENT_RESIZED
: UpdateSize(ev
.data1
, ev
.data2
);
368 SDL_WINDOWEVENT_EXPOSED
: sys_Repaint
;
369 SDL_WINDOWEVENT_CLOSE
: result
:= true;
370 SDL_WINDOWEVENT_FOCUS_LOST
, SDL_WINDOWEVENT_MINIMIZED
:
373 if gMuteWhenInactive
then
374 e_MuteChannels(true);
375 {$IFDEF ENABLE_HOLMES}
376 if assigned(winBlurCB
) then winBlurCB
;
379 SDL_WINDOWEVENT_FOCUS_GAINED
, SDL_WINDOWEVENT_MAXIMIZED
, SDL_WINDOWEVENT_RESTORED
:
381 e_MuteChannels(false);
382 {$IFDEF ENABLE_HOLMES}
383 if assigned(winFocusCB
) then winFocusCB
;
389 procedure HandleKeyboard (var ev
: TSDL_KeyboardEvent
);
390 var down
: Boolean; key
: Integer;
392 key
:= ev
.keysym
.scancode
;
393 down
:= (ev
.type_
= SDL_KEYDOWN
);
394 if key
= SDL_SCANCODE_AC_BACK
then
395 key
:= SDL_SCANCODE_ESCAPE
;
396 {$IFDEF ENABLE_HOLMES}
397 if fuiOnSDLEvent(PSDL_Event(@ev
)^) then
399 // event eaten, but...
400 if not down
then e_KeyUpDown(key
, false);
404 if ev
._repeat
= 0 then
407 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down
, key
]);
408 e_KeyUpDown(key
, down
);
409 g_Console_ProcessBind(key
, down
);
411 else if gConsoleShow
or gChatShow
or (g_ActiveWindow
<> nil) then
413 KeyPress(key
) // key repeat in menus and shit
417 procedure HandleTextInput (var ev
: TSDL_TextInputEvent
);
418 var ch
: UnicodeChar
; sch
: AnsiChar
;
420 Utf8ToUnicode(@ch
, PChar(ev
.text), 1);
421 sch
:= AnsiChar(wchar2win(ch
));
423 e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev
.text, Ord(ch
), Ord(sch
)]);
424 if IsValid1251(Word(ch
)) and IsPrintable1251(ch
) then
428 function sys_HandleInput (): Boolean;
432 ZeroMemory(@ev
, sizeof(ev
));
433 while SDL_PollEvent(@ev
) <> 0 do
436 SDL_QUITEV
: result
:= true;
437 SDL_WINDOWEVENT
: result
:= HandleWindow(ev
.window
);
438 SDL_KEYUP
, SDL_KEYDOWN
: HandleKeyboard(ev
.key
);
439 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
: HandleJoyButton(ev
.jbutton
);
440 SDL_JOYAXISMOTION
: HandleJoyAxis(ev
.jaxis
);
441 SDL_JOYHATMOTION
: HandleJoyHat(ev
.jhat
);
442 SDL_JOYDEVICEADDED
: HandleJoyAdd(ev
.jdevice
);
443 SDL_JOYDEVICEREMOVED
: HandleJoyRemove(ev
.jdevice
);
444 SDL_TEXTINPUT
: HandleTextInput(ev
.text);
445 SDL_FINGERMOTION
, SDL_FINGERDOWN
, SDL_FINGERUP
: g_Touch_HandleEvent(ev
.tfinger
);
446 {$IFDEF ENABLE_HOLMES}
447 SDL_MOUSEBUTTONDOWN
, SDL_MOUSEBUTTONUP
, SDL_MOUSEWHEEL
, SDL_MOUSEMOTION
: fuiOnSDLEvent(ev
);
453 procedure sys_RequestQuit
;
456 ev
.type_
:= SDL_QUITEV
;
460 (* --------- Init --------- *)
465 e_WriteLog('Init SDL2', TMsgType
.Notify
);
467 {$IFDEF USE_SDLMIXER}
468 flags
:= SDL_INIT_TIMER
or SDL_INIT_AUDIO
or $00004000;
470 flags
:= SDL_INIT_TIMER
or $00004000;
473 flags
:= SDL_INIT_JOYSTICK
or SDL_INIT_TIMER
or SDL_INIT_VIDEO
;
475 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK
, '0');
476 if SDL_Init(flags
) <> 0 then
477 raise Exception
.Create('SDL: Init failed: ' + SDL_GetError
);
478 SDL_ShowCursor(SDL_DISABLE
);
483 e_WriteLog('Releasing SDL2', TMsgType
.Notify
);
484 if context
<> nil then
489 SDL_GL_DeleteContext(context
);
492 if window
<> nil then
494 SDL_DestroyWindow(window
);
501 conRegVar('sdl2_display_index', @display
, 'use display index as base', '');