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
;
43 SysUtils
, SDL2
, GL
, Math
,
44 e_log
, e_graphics
, e_input
,
46 g_options
, g_window
, g_console
, g_game
, g_menu
, g_gui
, g_main
;
49 GameTitle
= 'Doom 2D: Forever (SDL 2)';
53 context
: TSDL_GLContext
;
55 JoystickHandle
: array [0..e_MaxJoys
- 1] of PSDL_Joystick
;
56 JoystickHatState
: array [0..e_MaxJoys
- 1, 0..e_MaxJoyHats
- 1, HAT_LEFT
..HAT_DOWN
] of Boolean;
57 JoystickZeroAxes
: array [0..e_MaxJoys
- 1, 0..e_MaxJoyAxes
- 1] of Integer;
59 (* --------- Utils --------- *)
61 function sys_GetTicks (): Int64;
63 result
:= SDL_GetTicks()
66 procedure sys_Delay (ms
: Integer);
71 (* --------- Graphics --------- *)
73 procedure UpdateSize (w
, h
: Integer);
81 {$IFDEF ENABLE_HOLMES}
87 g_Game_SetupScreenSize
;
92 function InitWindow (w
, h
, bpp
: Integer; fullScreen
: Boolean): Boolean;
95 e_LogWritefln('InitWindow %s %s %s %s', [w
, h
, bpp
, fullScreen
]);
100 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 1);
101 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
102 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK
, SDL_GL_CONTEXT_PROFILE_ES
);
104 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
105 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
106 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
107 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
108 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
109 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
110 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
111 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
113 flags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
114 if fullScreen
then flags
:= flags
or SDL_WINDOW_FULLSCREEN
;
115 window
:= SDL_CreateWindow(GameTitle
, SDL_WINDOWPOS_CENTERED
, SDL_WINDOWPOS_CENTERED
, w
, h
, flags
);
116 if window
<> nil then
118 context
:= SDL_GL_CreateContext(window
);
119 if context
<> nil then
126 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError
])
131 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError
])
136 if fullScreen
then flags
:= SDL_WINDOW_FULLSCREEN
else flags
:= 0;
137 SDL_SetWindowSize(window
, w
, h
);
138 SDL_SetWindowFullscreen(window
, flags
);
144 procedure sys_Repaint
;
146 SDL_GL_SwapWindow(window
)
149 procedure sys_EnableVSync (yes
: Boolean);
152 SDL_GL_SetSwapInterval(1)
154 SDL_GL_SetSwapInterval(0)
157 function sys_GetDispalyModes (bpp
: Integer): SSArray
;
158 var i
, count
, num
, pw
, ph
: Integer; m
: TSDL_DisplayMode
;
161 num
:= SDL_GetNumDisplayModes(display
);
163 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError
]);
166 e_LogWritefln('Video modes for display %s:', [display
]);
167 SetLength(result
, num
);
168 i
:= 0; count
:= 0; pw
:= 0; ph
:= 0;
171 SDL_GetDisplayMode(display
, i
, @m
);
172 if ((pw
<> m
.w
) or (ph
<> m
.h
)) then
174 e_LogWritefln('* %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
175 pw
:= m
.w
; ph
:= m
.h
;
176 result
[count
] := IntToStr(m
.w
) + 'x' + IntToStr(m
.h
);
181 e_LogWritefln('- %sx%sx%s@%s', [m
.w
, m
.h
, SDL_BITSPERPIXEL(m
.format
), m
.refresh_rate
]);
185 SetLength(result
, count
)
189 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullScreen
: Boolean): Boolean;
191 result
:= InitWindow(w
, h
, bpp
, fullScreen
)
194 (* --------- Joystick --------- *)
196 procedure HandleJoyButton (var ev
: TSDL_JoyButtonEvent
);
197 var down
: Boolean; key
: Integer;
199 if (ev
.which
< e_MaxJoys
) and (ev
.button
< e_MaxJoyBtns
) then
201 key
:= e_JoyButtonToKey(ev
.which
, ev
.button
);
202 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
204 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev
.which
, ev
.button
, key
, down
]);
205 e_KeyUpDown(key
, down
);
206 g_Console_ProcessBind(key
, down
)
212 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
213 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev
.which
, ev
.button
, down
])
218 procedure HandleJoyAxis (var ev
: TSDL_JoyAxisEvent
);
219 var key
, minuskey
: Integer;
221 if (ev
.which
< e_MaxJoys
) and (ev
.axis
< e_MaxJoyAxes
) then
223 key
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_PLUS
);
224 minuskey
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_MINUS
);
227 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
]]);
229 if ev
.value
< JoystickZeroAxes
[ev
.which
, ev
.axis
] - e_JoystickDeadzones
[ev
.which
] then
231 if (e_KeyPressed(key
)) then
233 e_KeyUpDown(key
, False);
234 g_Console_ProcessBind(key
, False)
236 e_KeyUpDown(minuskey
, True);
237 g_Console_ProcessBind(minuskey
, True)
239 else if ev
.value
> JoystickZeroAxes
[ev
.which
, ev
.axis
] + e_JoystickDeadzones
[ev
.which
] then
241 if (e_KeyPressed(minuskey
)) then
243 e_KeyUpDown(minuskey
, False);
244 g_Console_ProcessBind(minuskey
, False)
246 e_KeyUpDown(key
, True);
247 g_Console_ProcessBind(key
, True)
251 if (e_KeyPressed(minuskey
)) then
253 e_KeyUpDown(minuskey
, False);
254 g_Console_ProcessBind(minuskey
, False)
256 if (e_KeyPressed(key
)) then
258 e_KeyUpDown(key
, False);
259 g_Console_ProcessBind(key
, False)
266 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
]])
270 procedure HandleJoyHat (var ev
: TSDL_JoyHatEvent
);
274 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
276 if (ev
.which
< e_MaxJoys
) and (ev
.hat
< e_MaxJoyHats
) then
279 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
]);
280 hat
[HAT_UP
] := LongBool(ev
.value
and SDL_HAT_UP
);
281 hat
[HAT_DOWN
] := LongBool(ev
.value
and SDL_HAT_DOWN
);
282 hat
[HAT_LEFT
] := LongBool(ev
.value
and SDL_HAT_LEFT
);
283 hat
[HAT_RIGHT
] := LongBool(ev
.value
and SDL_HAT_RIGHT
);
284 for i
:= HAT_LEFT
to HAT_DOWN
do
286 if JoystickHatState
[ev
.which
, ev
.hat
, i
] <> hat
[i
] then
289 key
:= e_JoyHatToKey(ev
.which
, ev
.hat
, i
);
290 e_KeyUpDown(key
, down
);
291 g_Console_ProcessBind(key
, down
)
294 JoystickHatState
[ev
.which
, ev
.hat
] := hat
299 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
])
303 procedure HandleJoyAdd (var ev
: TSDL_JoyDeviceEvent
);
306 if (ev
.which
< e_MaxJoys
) then
308 JoystickHandle
[ev
.which
] := SDL_JoystickOpen(ev
.which
);
309 if JoystickHandle
[ev
.which
] <> nil then
311 e_LogWritefln('Added Joystick %s', [ev
.which
]);
312 e_JoystickAvailable
[ev
.which
] := True;
313 for i
:= 0 to Min(SDL_JoystickNumAxes(JoystickHandle
[ev
.which
]), e_MaxJoyAxes
) - 1 do
314 JoystickZeroAxes
[ev
.which
, i
] := SDL_JoystickGetAxis(JoystickHandle
[ev
.which
], i
)
318 e_LogWritefln('Warning! Failed to open Joystick %s', [ev
.which
])
323 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev
.which
, e_MaxJoys
])
327 procedure HandleJoyRemove (var ev
: TSDL_JoyDeviceEvent
);
329 e_LogWritefln('Removed Joystick %s', [ev
.which
]);
330 if (ev
.which
< e_MaxJoys
) then
332 e_JoystickAvailable
[ev
.which
] := False;
333 if JoystickHandle
[ev
.which
] <> nil then
334 SDL_JoystickClose(JoystickHandle
[ev
.which
]);
335 JoystickHandle
[ev
.which
] := nil
339 (* --------- Input --------- *)
341 function HandleWindow (var ev
: TSDL_WindowEvent
): Boolean;
345 SDL_WINDOWEVENT_RESIZED
: UpdateSize(ev
.data1
, ev
.data2
);
346 SDL_WINDOWEVENT_EXPOSED
: sys_Repaint
;
347 SDL_WINDOWEVENT_CLOSE
: result
:= true;
351 procedure HandleKeyboard (var ev
: TSDL_KeyboardEvent
);
352 var down
: Boolean; key
: Integer;
354 key
:= ev
.keysym
.scancode
;
355 down
:= (ev
.type_
= SDL_KEYDOWN
);
356 if key
= SDL_SCANCODE_AC_BACK
then
357 key
:= SDL_SCANCODE_ESCAPE
;
358 if ev
._repeat
= 0 then
361 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down
, key
]);
362 e_KeyUpDown(key
, down
);
363 g_Console_ProcessBind(key
, down
);
365 else if gConsoleShow
or gChatShow
or (g_ActiveWindow
<> nil) then
367 KeyPress(key
) // key repeat in menus and shit
371 procedure HandleTextInput (var ev
: TSDL_TextInputEvent
);
372 var ch
: UnicodeChar
; sch
: AnsiChar;
375 e_LogWritefln('Input Debug: text, text=%s', [ev
.text]);
376 Utf8ToUnicode(@ch
, PChar(ev
.text), 1);
377 if IsValid1251(Word(ch
)) then
379 sch
:= AnsiChar(wchar2win(ch
));
384 function sys_HandleInput (): Boolean;
388 while SDL_PollEvent(@ev
) <> 0 do
391 SDL_QUITEV
: result
:= true;
392 SDL_WINDOWEVENT
: result
:= HandleWindow(ev
.window
);
393 SDL_KEYUP
, SDL_KEYDOWN
: HandleKeyboard(ev
.key
);
394 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
: HandleJoyButton(ev
.jbutton
);
395 SDL_JOYAXISMOTION
: HandleJoyAxis(ev
.jaxis
);
396 SDL_JOYHATMOTION
: HandleJoyHat(ev
.jhat
);
397 SDL_JOYDEVICEADDED
: HandleJoyAdd(ev
.jdevice
);
398 SDL_JOYDEVICEREMOVED
: HandleJoyRemove(ev
.jdevice
);
399 SDL_TEXTINPUT
: HandleTextInput(ev
.text);
400 SDL_FINGERMOTION
, SDL_FINGERDOWN
, SDL_FINGERUP
: g_Touch_HandleEvent(ev
.tfinger
);
405 procedure sys_RequestQuit
;
408 ev
.type_
:= SDL_QUITEV
;
412 (* --------- Init --------- *)
415 var flags
: UInt32
; ok
: Boolean;
417 e_WriteLog('Init SDL2', TMsgType
.Notify
);
419 {$IFDEF USE_SDLMIXER}
420 flags
:= SDL_INIT_TIMER
or SDL_INIT_AUDIO
or $00004000;
422 flags
:= SDL_INIT_TIMER
or $00004000;
425 flags
:= SDL_INIT_JOYSTICK
or SDL_INIT_TIMER
or SDL_INIT_VIDEO
;
427 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK
, '0');
428 if SDL_Init(flags
) <> 0 then
429 raise Exception
.Create('SDL: Init failed: ' + SDL_GetError
);
430 ok
:= InitWindow(gScreenWidth
, gScreenHeight
, gBPP
, gFullscreen
);
432 raise Exception
.Create('SDL: Failed to set videomode: ' + SDL_GetError
);
437 e_WriteLog('Releasing SDL2', TMsgType
.Notify
);
438 if context
<> nil then
439 SDL_GL_DeleteContext(context
);
440 if window
<> nil then
441 SDL_DestroyWindow(window
);
448 conRegVar('sdl2_display_index', @display
, 'use display index as base', '');