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 {$INCLUDE ../nogl/noGLuses.inc}
45 e_log
, e_graphics
, e_input
, e_sound
,
46 g_options
, g_window
, g_console
, g_game
, g_menu
, g_gui
, g_main
, g_basic
;
49 GameTitle
= 'Doom 2D: Forever (SDL 1.2, %s)';
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 function LoadGL
: Boolean;
78 if glRenderToFBO
and (not nogl_ExtensionSupported('GL_OES_framebuffer_object')) then
80 if glRenderToFBO
and (not Load_GL_ARB_framebuffer_object
) then
83 e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
84 glRenderToFBO
:= false;
95 procedure UpdateSize (w
, h
: Integer);
101 if glRenderToFBO
then
103 // store real window size in gWinSize, downscale resolution now
104 w
:= round(w
/ r_pixel_scale
);
105 h
:= round(h
/ r_pixel_scale
);
106 e_ResizeFramebuffer(w
, h
);
110 {$IFDEF ENABLE_HOLMES}
114 e_ResizeWindow(w
, h
);
116 g_Game_SetupScreenSize
;
121 function GetTitle (): PChar;
122 var info
: AnsiString;
124 info
:= g_GetBuildHash(false);
125 if info
= 'custom build' then
126 info
:= info
+ ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE
+ ' ' + GAME_BUILDTIME
;
127 result
:= PChar(Format(GameTitle
, [info
]))
130 function InitWindow (w
, h
, bpp
: Integer; fullScreen
: Boolean): Boolean;
133 e_LogWritefln('InitWindow %s %s %s %s', [w
, h
, bpp
, fullScreen
]);
135 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
136 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
137 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
138 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
139 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
140 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
142 if fullScreen
then flags
:= flags
or SDL_FULLSCREEN
;
143 if userResize
then flags
:= flags
or SDL_VIDEORESIZE
;
144 if (screen
= nil) or (SDL_VideoModeOk(w
, h
, bpp
, flags
) <> 0) then
146 SDL_FreeSurface(screen
);
147 screen
:= SDL_SetVideoMode(w
, h
, bpp
, flags
);
148 if screen
<> nil then
152 e_LogWriteln('GL: unable to load OpenGL functions', TMsgType
.Fatal
);
155 SDL_WM_SetCaption(GetTitle(), nil);
156 gFullScreen
:= fullscreen
;
157 gRC_FullScreen
:= fullscreen
;
164 e_LogWritefln('SDL: video mode not supported', [])
168 procedure sys_Repaint
;
170 if glRenderToFBO
then
171 e_BlitFramebuffer(gWinSizeX
, gWinSizeY
);
175 procedure sys_EnableVSync (yes
: Boolean);
180 function sys_GetDisplayModes (bpp
: Integer): SSArray
;
181 var m
: PPSDL_Rect
; f
: TSDL_PixelFormat
; i
, count
: Integer;
183 SetLength(result
, 0);
184 FillChar(f
, sizeof(f
), 0);
186 f
.BitsPerPixel
:= bpp
;
187 f
.BytesPerPixel
:= (bpp
+ 7) div 8;
188 m
:= SDL_ListModes(@f
, SDL_OPENGL
or SDL_FULLSCREEN
);
189 if (m
<> NIL) and (UIntPtr(m
) <> UIntPtr(-1)) then
192 while m
[count
] <> nil do inc(count
);
193 SetLength(result
, count
);
194 for i
:= 0 to count
- 1 do
195 result
[i
] := IntToStr(m
[i
].w
) + 'x' + IntToStr(m
[i
].h
);
199 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullscreen
, maximized
: Boolean): Boolean;
201 result
:= InitWindow(w
, h
, bpp
, fullscreen
)
204 (* --------- Joystick --------- *)
206 procedure HandleJoyButton (var ev
: TSDL_JoyButtonEvent
);
207 var down
: Boolean; key
: Integer;
209 if (ev
.which
< e_MaxJoys
) and (ev
.button
< e_MaxJoyBtns
) then
211 key
:= e_JoyButtonToKey(ev
.which
, ev
.button
);
212 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
214 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev
.which
, ev
.button
, key
, down
]);
215 e_KeyUpDown(key
, down
);
216 g_Console_ProcessBind(key
, down
)
222 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
223 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev
.which
, ev
.button
, down
])
228 procedure HandleJoyAxis (var ev
: TSDL_JoyAxisEvent
);
229 var key
, minuskey
: Integer;
231 if (ev
.which
< e_MaxJoys
) and (ev
.axis
< e_MaxJoyAxes
) then
233 key
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_PLUS
);
234 minuskey
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_MINUS
);
237 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
]]);
239 if ev
.value
< JoystickZeroAxes
[ev
.which
, ev
.axis
] - e_JoystickDeadzones
[ev
.which
] then
241 if (e_KeyPressed(key
)) then
243 e_KeyUpDown(key
, False);
244 g_Console_ProcessBind(key
, False)
246 e_KeyUpDown(minuskey
, True);
247 g_Console_ProcessBind(minuskey
, True)
249 else if ev
.value
> JoystickZeroAxes
[ev
.which
, ev
.axis
] + e_JoystickDeadzones
[ev
.which
] then
251 if (e_KeyPressed(minuskey
)) then
253 e_KeyUpDown(minuskey
, False);
254 g_Console_ProcessBind(minuskey
, False)
256 e_KeyUpDown(key
, True);
257 g_Console_ProcessBind(key
, True)
261 if (e_KeyPressed(minuskey
)) then
263 e_KeyUpDown(minuskey
, False);
264 g_Console_ProcessBind(minuskey
, False)
266 if (e_KeyPressed(key
)) then
268 e_KeyUpDown(key
, False);
269 g_Console_ProcessBind(key
, False)
276 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
]])
280 procedure HandleJoyHat (var ev
: TSDL_JoyHatEvent
);
284 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
286 if (ev
.which
< e_MaxJoys
) and (ev
.hat
< e_MaxJoyHats
) then
289 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
]);
290 hat
[HAT_UP
] := LongBool(ev
.value
and SDL_HAT_UP
);
291 hat
[HAT_DOWN
] := LongBool(ev
.value
and SDL_HAT_DOWN
);
292 hat
[HAT_LEFT
] := LongBool(ev
.value
and SDL_HAT_LEFT
);
293 hat
[HAT_RIGHT
] := LongBool(ev
.value
and SDL_HAT_RIGHT
);
294 for i
:= HAT_LEFT
to HAT_DOWN
do
296 if JoystickHatState
[ev
.which
, ev
.hat
, i
] <> hat
[i
] then
299 key
:= e_JoyHatToKey(ev
.which
, ev
.hat
, i
);
300 e_KeyUpDown(key
, down
);
301 g_Console_ProcessBind(key
, down
)
304 JoystickHatState
[ev
.which
, ev
.hat
] := hat
309 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
])
313 procedure AddJoystick (which
: Integer);
316 assert(which
< e_MaxJoys
);
317 JoystickHandle
[which
] := SDL_JoystickOpen(which
);
318 if JoystickHandle
[which
] <> nil then
320 e_LogWritefln('Added Joystick %s', [which
]);
321 e_JoystickAvailable
[which
] := True;
322 for i
:= 0 to Min(SDL_JoystickNumAxes(JoystickHandle
[which
]), e_MaxJoyAxes
) - 1 do
323 JoystickZeroAxes
[which
, i
] := SDL_JoystickGetAxis(JoystickHandle
[which
], i
)
327 e_LogWritefln('Failed to open Joystick %s', [which
])
331 procedure RemoveJoystick (which
: Integer);
333 assert(which
< e_MaxJoys
);
334 e_LogWritefln('Remove Joystick %s', [which
]);
335 e_JoystickAvailable
[which
] := False;
336 if JoystickHandle
[which
] <> nil then
337 SDL_JoystickClose(JoystickHandle
[which
]);
338 JoystickHandle
[which
] := nil
341 (* --------- Input --------- *)
343 function Key2Stub (key
: Integer): Integer;
347 SDLK_ESCAPE
: x
:= IK_ESCAPE
;
348 SDLK_RETURN
: x
:= IK_RETURN
;
349 SDLK_KP_ENTER
: x
:= IK_KPRETURN
;
350 SDLK_KP0
: x
:= IK_KPINSERT
;
352 SDLK_KP8
: x
:= IK_KPUP
;
353 SDLK_DOWN
: x
:= IK_DOWN
;
354 SDLK_KP2
: x
:= IK_KPDOWN
;
355 SDLK_LEFT
: x
:= IK_LEFT
;
356 SDLK_KP4
: x
:= IK_KPLEFT
;
357 SDLK_RIGHT
: x
:= IK_RIGHT
;
358 SDLK_KP6
: x
:= IK_KPRIGHT
;
359 SDLK_DELETE
: x
:= IK_DELETE
;
360 SDLK_HOME
: x
:= IK_HOME
;
361 SDLK_KP7
: x
:= IK_KPHOME
;
362 SDLK_INSERT
: x
:= IK_INSERT
;
363 SDLK_SPACE
: x
:= IK_SPACE
;
364 SDLK_LSHIFT
: x
:= IK_SHIFT
;
365 SDLK_LALT
: x
:= IK_ALT
;
366 SDLK_TAB
: x
:= IK_TAB
;
367 SDLK_PAGEUP
: x
:= IK_PAGEUP
;
368 SDLK_KP9
: x
:= IK_KPPAGEUP
;
369 SDLK_PAGEDOWN
: x
:= IK_PAGEDN
;
370 SDLK_KP3
: x
:= IK_KPPAGEDN
;
371 SDLK_KP5
: x
:= IK_KP5
;
372 SDLK_NUMLOCK
: x
:= IK_NUMLOCK
;
373 SDLK_KP_DIVIDE
: x
:= IK_KPDIVIDE
;
374 SDLK_KP_MULTIPLY
: x
:= IK_KPMULTIPLE
;
375 SDLK_KP_MINUS
: x
:= IK_KPMINUS
;
376 SDLK_KP_PLUS
: x
:= IK_KPPLUS
;
377 SDLK_KP_PERIOD
: x
:= IK_KPDOT
;
378 SDLK_CAPSLOCK
: x
:= IK_CAPSLOCK
;
379 SDLK_RSHIFT
: x
:= IK_RSHIFT
;
380 SDLK_LCTRL
: x
:= IK_CTRL
;
381 SDLK_RCTRL
: x
:= IK_RCTRL
;
382 SDLK_RALT
: x
:= IK_RALT
;
383 SDLK_LSUPER
: x
:= IK_WIN
;
384 SDLK_RSUPER
: x
:= IK_RWIN
;
385 SDLK_MENU
: x
:= IK_MENU
;
386 SDLK_PRINT
: x
:= IK_PRINTSCR
;
387 SDLK_SCROLLOCK
: x
:= IK_SCROLLLOCK
;
388 SDLK_LEFTBRACKET
: x
:= IK_LBRACKET
;
389 SDLK_RIGHTBRACKET
: x
:= IK_RBRACKET
;
390 SDLK_SEMICOLON
: x
:= IK_SEMICOLON
;
391 SDLK_QUOTE
: x
:= IK_QUOTE
;
392 SDLK_BACKSLASH
: x
:= IK_BACKSLASH
;
393 SDLK_SLASH
: x
:= IK_SLASH
;
394 SDLK_COMMA
: x
:= IK_COMMA
;
395 SDLK_PERIOD
: x
:= IK_DOT
;
396 SDLK_EQUALS
: x
:= IK_EQUALS
;
416 SDLK_F10
: x
:= IK_F10
;
417 SDLK_F11
: x
:= IK_F11
;
418 SDLK_F12
: x
:= IK_F12
;
419 SDLK_END
: x
:= IK_END
;
420 SDLK_KP1
: x
:= IK_KPEND
;
421 SDLK_BACKSPACE
: x
:= IK_BACKSPACE
;
422 SDLK_BACKQUOTE
: x
:= IK_BACKQUOTE
;
423 SDLK_PAUSE
: x
:= IK_PAUSE
;
424 SDLK_A
..SDLK_Z
: x
:= IK_A
+ (key
- SDLK_A
);
425 SDLK_MINUS
: x
:= IK_MINUS
;
426 SDLK_RMETA
: x
:= IK_RMETA
;
427 SDLK_LMETA
: x
:= IK_LMETA
;
434 procedure HandleKeyboard (var ev
: TSDL_KeyboardEvent
);
435 var down
, repeated
: Boolean; key
: Integer; ch
: Char;
437 key
:= Key2Stub(ev
.keysym
.sym
);
438 down
:= (ev
.type_
= SDL_KEYDOWN
);
439 repeated
:= down
and e_KeyPressed(key
);
440 ch
:= wchar2win(WideChar(ev
.keysym
.unicode
));
442 e_LogWritefln('Input Debug: keysym, down=%s, sym=%s, state=%s, unicode=%s, stubsym=%s, cp1251=%s', [down
, ev
.keysym
.sym
, ev
.state
, ev
.keysym
.unicode
, key
, Ord(ch
)]);
445 e_KeyUpDown(key
, down
);
446 g_Console_ProcessBind(key
, down
);
448 else if gConsoleShow
or gChatShow
or (g_ActiveWindow
<> nil) then
450 KeyPress(key
) // key repeat in menus and shit
452 if down
and IsValid1251(ev
.keysym
.unicode
) and IsPrintable1251(ch
) then
456 procedure HandleResize (var ev
: TSDL_ResizeEvent
);
459 e_LogWritefln('Input Debug: SDL_VIDEORESIZE %s %s', [ev
.w
, ev
.h
]);
460 if modeResize
= 1 then
461 UpdateSize(ev
.w
, ev
.h
)
462 else if modeResize
> 1 then
463 InitWindow(ev
.w
, ev
.h
, gBPP
, gFullscreen
)
466 function sys_HandleInput (): Boolean;
470 while SDL_PollEvent(@ev
) <> 0 do
473 SDL_QUITEV
: result
:= true;
474 SDL_VIDEORESIZE
: HandleResize(ev
.resize
);
475 SDL_KEYUP
, SDL_KEYDOWN
: HandleKeyboard(ev
.key
);
476 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
: HandleJoyButton(ev
.jbutton
);
477 SDL_JOYAXISMOTION
: HandleJoyAxis(ev
.jaxis
);
478 SDL_JOYHATMOTION
: HandleJoyHat(ev
.jhat
);
479 SDL_VIDEOEXPOSE
: sys_Repaint
;
480 SDL_ACTIVEEVENT
: e_MuteChannels((ev
.active
.gain
= 0) and gMuteWhenInactive
);
485 procedure sys_RequestQuit
;
488 ev
.quit
.type_
:= SDL_QUITEV
;
492 (* --------- Init --------- *)
495 var flags
: Uint32
; i
: Integer;
497 e_WriteLog('Init SDL', TMsgType
.Notify
);
498 flags
:= SDL_INIT_VIDEO
or SDL_INIT_AUDIO
or
499 SDL_INIT_TIMER
or SDL_INIT_JOYSTICK
500 (*or SDL_INIT_NOPARACHUTE*);
501 if SDL_Init(flags
) <> 0 then
502 raise Exception
.Create('SDL: Init failed: ' + SDL_GetError
);
503 SDL_EnableUNICODE(1);
504 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
505 for i
:= 0 to e_MaxJoys
- 1 do
512 e_WriteLog('Releasing SDL', TMsgType
.Notify
);
513 for i
:= 0 to e_MaxJoys
- 1 do
515 if screen
<> nil then
518 SDL_FreeSurface(screen
)
524 (* window resize are broken both on linux and osx, so disabled by default *)
525 conRegVar('sdl_allow_resize', @userResize
, 'allow to resize window by user', 'allow to resize window by user');
526 conRegVar('sdl_resize_action', @modeResize
, 'set window resize mode (0: ignore, 1: change, 2: reset)', '');