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_GetDispalyModes (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
;
52 GameTitle
= 'Doom 2D: Forever (SDL 2)';
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 InitWindow (w
, h
, bpp
: Integer; fullScreen
: Boolean): Boolean;
104 // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
105 e_LogWritefln('InitWindow %s %s %s %s', [w
, h
, bpp
, fullScreen
]);
110 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 1);
111 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
112 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK
, SDL_GL_CONTEXT_PROFILE_ES
);
114 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
115 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
116 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
117 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
118 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
119 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
120 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
121 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
123 flags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
124 if fullScreen
then flags
:= flags
or SDL_WINDOW_FULLSCREEN
;
125 window
:= SDL_CreateWindow(GameTitle
, SDL_WINDOWPOS_CENTERED
, SDL_WINDOWPOS_CENTERED
, w
, h
, flags
);
126 if window
<> nil then
128 context
:= SDL_GL_CreateContext(window
);
129 if context
<> nil then
139 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError
])
144 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError
])
149 if fullScreen
then flags
:= SDL_WINDOW_FULLSCREEN
else flags
:= 0;
150 SDL_SetWindowSize(window
, w
, h
);
151 SDL_SetWindowFullscreen(window
, flags
);
157 procedure sys_Repaint
;
159 SDL_GL_SwapWindow(window
)
162 procedure sys_EnableVSync (yes
: Boolean);
165 SDL_GL_SetSwapInterval(1)
167 SDL_GL_SetSwapInterval(0)
170 function sys_GetDispalyModes (bpp
: Integer): SSArray
;
171 var i
, count
, num
, pw
, ph
: Integer; m
: TSDL_DisplayMode
;
174 num
:= SDL_GetNumDisplayModes(display
);
176 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError
]);
179 e_LogWritefln('Video modes for display %s:', [display
]);
180 SetLength(result
, num
);
181 i
:= 0; count
:= 0; pw
:= 0; ph
:= 0;
184 SDL_GetDisplayMode(display
, i
, @m
);
185 if ((pw
<> m
.w
) or (ph
<> m
.h
)) then
187 e_LogWritefln('* %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
188 pw
:= m
.w
; ph
:= m
.h
;
189 result
[count
] := IntToStr(m
.w
) + 'x' + IntToStr(m
.h
);
194 e_LogWritefln('- %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
198 SetLength(result
, count
)
202 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullScreen
: Boolean): Boolean;
204 result
:= InitWindow(w
, h
, bpp
, fullScreen
)
207 (* --------- Joystick --------- *)
209 procedure HandleJoyButton (var ev
: TSDL_JoyButtonEvent
);
210 var down
: Boolean; key
: Integer;
212 if (ev
.which
< e_MaxJoys
) and (ev
.button
< e_MaxJoyBtns
) then
214 key
:= e_JoyButtonToKey(ev
.which
, ev
.button
);
215 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
217 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev
.which
, ev
.button
, key
, down
]);
218 e_KeyUpDown(key
, down
);
219 g_Console_ProcessBind(key
, down
)
225 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
226 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev
.which
, ev
.button
, down
])
231 procedure HandleJoyAxis (var ev
: TSDL_JoyAxisEvent
);
232 var key
, minuskey
: Integer;
234 if (ev
.which
< e_MaxJoys
) and (ev
.axis
< e_MaxJoyAxes
) then
236 key
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_PLUS
);
237 minuskey
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_MINUS
);
240 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
]]);
242 if ev
.value
< JoystickZeroAxes
[ev
.which
, ev
.axis
] - e_JoystickDeadzones
[ev
.which
] then
244 if (e_KeyPressed(key
)) then
246 e_KeyUpDown(key
, False);
247 g_Console_ProcessBind(key
, False)
249 e_KeyUpDown(minuskey
, True);
250 g_Console_ProcessBind(minuskey
, True)
252 else if ev
.value
> JoystickZeroAxes
[ev
.which
, ev
.axis
] + e_JoystickDeadzones
[ev
.which
] then
254 if (e_KeyPressed(minuskey
)) then
256 e_KeyUpDown(minuskey
, False);
257 g_Console_ProcessBind(minuskey
, False)
259 e_KeyUpDown(key
, True);
260 g_Console_ProcessBind(key
, True)
264 if (e_KeyPressed(minuskey
)) then
266 e_KeyUpDown(minuskey
, False);
267 g_Console_ProcessBind(minuskey
, False)
269 if (e_KeyPressed(key
)) then
271 e_KeyUpDown(key
, False);
272 g_Console_ProcessBind(key
, False)
279 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
]])
283 procedure HandleJoyHat (var ev
: TSDL_JoyHatEvent
);
287 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
289 if (ev
.which
< e_MaxJoys
) and (ev
.hat
< e_MaxJoyHats
) then
292 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
]);
293 hat
[HAT_UP
] := LongBool(ev
.value
and SDL_HAT_UP
);
294 hat
[HAT_DOWN
] := LongBool(ev
.value
and SDL_HAT_DOWN
);
295 hat
[HAT_LEFT
] := LongBool(ev
.value
and SDL_HAT_LEFT
);
296 hat
[HAT_RIGHT
] := LongBool(ev
.value
and SDL_HAT_RIGHT
);
297 for i
:= HAT_LEFT
to HAT_DOWN
do
299 if JoystickHatState
[ev
.which
, ev
.hat
, i
] <> hat
[i
] then
302 key
:= e_JoyHatToKey(ev
.which
, ev
.hat
, i
);
303 e_KeyUpDown(key
, down
);
304 g_Console_ProcessBind(key
, down
)
307 JoystickHatState
[ev
.which
, ev
.hat
] := hat
312 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
])
316 procedure HandleJoyAdd (var ev
: TSDL_JoyDeviceEvent
);
319 if (ev
.which
< e_MaxJoys
) then
321 JoystickHandle
[ev
.which
] := SDL_JoystickOpen(ev
.which
);
322 if JoystickHandle
[ev
.which
] <> nil then
324 e_LogWritefln('Added Joystick %s', [ev
.which
]);
325 e_JoystickAvailable
[ev
.which
] := True;
326 for i
:= 0 to Min(SDL_JoystickNumAxes(JoystickHandle
[ev
.which
]), e_MaxJoyAxes
) - 1 do
327 JoystickZeroAxes
[ev
.which
, i
] := SDL_JoystickGetAxis(JoystickHandle
[ev
.which
], i
)
331 e_LogWritefln('Warning! Failed to open Joystick %s', [ev
.which
])
336 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev
.which
, e_MaxJoys
])
340 procedure HandleJoyRemove (var ev
: TSDL_JoyDeviceEvent
);
342 e_LogWritefln('Removed Joystick %s', [ev
.which
]);
343 if (ev
.which
< e_MaxJoys
) then
345 e_JoystickAvailable
[ev
.which
] := False;
346 if JoystickHandle
[ev
.which
] <> nil then
347 SDL_JoystickClose(JoystickHandle
[ev
.which
]);
348 JoystickHandle
[ev
.which
] := nil
352 (* --------- Input --------- *)
354 function HandleWindow (var ev
: TSDL_WindowEvent
): Boolean;
358 SDL_WINDOWEVENT_RESIZED
: UpdateSize(ev
.data1
, ev
.data2
);
359 SDL_WINDOWEVENT_EXPOSED
: sys_Repaint
;
360 SDL_WINDOWEVENT_CLOSE
: result
:= true;
361 SDL_WINDOWEVENT_FOCUS_LOST
, SDL_WINDOWEVENT_MINIMIZED
:
364 if gMuteWhenInactive
then
365 e_MuteChannels(true);
366 {$IFDEF ENABLE_HOLMES}
367 if assigned(winBlurCB
) then winBlurCB
;
370 SDL_WINDOWEVENT_FOCUS_GAINED
, SDL_WINDOWEVENT_MAXIMIZED
, SDL_WINDOWEVENT_RESTORED
:
372 e_MuteChannels(false);
373 {$IFDEF ENABLE_HOLMES}
374 if assigned(winFocusCB
) then winFocusCB
;
380 procedure HandleKeyboard (var ev
: TSDL_KeyboardEvent
);
381 var down
: Boolean; key
: Integer;
383 key
:= ev
.keysym
.scancode
;
384 down
:= (ev
.type_
= SDL_KEYDOWN
);
385 if key
= SDL_SCANCODE_AC_BACK
then
386 key
:= SDL_SCANCODE_ESCAPE
;
387 {$IFDEF ENABLE_HOLMES}
388 if fuiOnSDLEvent(PSDL_Event(@ev
)^) then
390 // event eaten, but...
391 if not down
then e_KeyUpDown(key
, false);
395 if ev
._repeat
= 0 then
398 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down
, key
]);
399 e_KeyUpDown(key
, down
);
400 g_Console_ProcessBind(key
, down
);
402 else if gConsoleShow
or gChatShow
or (g_ActiveWindow
<> nil) then
404 KeyPress(key
) // key repeat in menus and shit
408 procedure HandleTextInput (var ev
: TSDL_TextInputEvent
);
409 var ch
: UnicodeChar
; sch
: AnsiChar;
412 e_LogWritefln('Input Debug: text, text=%s', [ev
.text]);
413 Utf8ToUnicode(@ch
, PChar(ev
.text), 1);
414 if IsValid1251(Word(ch
)) then
416 sch
:= AnsiChar(wchar2win(ch
));
421 function sys_HandleInput (): Boolean;
425 ZeroMemory(@ev
, sizeof(ev
));
426 while SDL_PollEvent(@ev
) <> 0 do
429 SDL_QUITEV
: result
:= true;
430 SDL_WINDOWEVENT
: result
:= HandleWindow(ev
.window
);
431 SDL_KEYUP
, SDL_KEYDOWN
: HandleKeyboard(ev
.key
);
432 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
: HandleJoyButton(ev
.jbutton
);
433 SDL_JOYAXISMOTION
: HandleJoyAxis(ev
.jaxis
);
434 SDL_JOYHATMOTION
: HandleJoyHat(ev
.jhat
);
435 SDL_JOYDEVICEADDED
: HandleJoyAdd(ev
.jdevice
);
436 SDL_JOYDEVICEREMOVED
: HandleJoyRemove(ev
.jdevice
);
437 SDL_TEXTINPUT
: HandleTextInput(ev
.text);
438 SDL_FINGERMOTION
, SDL_FINGERDOWN
, SDL_FINGERUP
: g_Touch_HandleEvent(ev
.tfinger
);
439 {$IFDEF ENABLE_HOLMES}
440 SDL_MOUSEBUTTONDOWN
, SDL_MOUSEBUTTONUP
, SDL_MOUSEWHEEL
, SDL_MOUSEMOTION
: fuiOnSDLEvent(ev
);
446 procedure sys_RequestQuit
;
449 ev
.type_
:= SDL_QUITEV
;
453 (* --------- Init --------- *)
458 e_WriteLog('Init SDL2', TMsgType
.Notify
);
460 {$IFDEF USE_SDLMIXER}
461 flags
:= SDL_INIT_TIMER
or SDL_INIT_AUDIO
or $00004000;
463 flags
:= SDL_INIT_TIMER
or $00004000;
466 flags
:= SDL_INIT_JOYSTICK
or SDL_INIT_TIMER
or SDL_INIT_VIDEO
;
468 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK
, '0');
469 if SDL_Init(flags
) <> 0 then
470 raise Exception
.Create('SDL: Init failed: ' + SDL_GetError
);
471 SDL_ShowCursor(SDL_DISABLE
);
476 e_WriteLog('Releasing SDL2', TMsgType
.Notify
);
477 if context
<> nil then
482 SDL_GL_DeleteContext(context
);
485 if window
<> nil then
487 SDL_DestroyWindow(window
);
494 conRegVar('sdl2_display_index', @display
, 'use display index as base', '');