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
;
41 sys_CharPress
: procedure (ch
: AnsiChar) = nil;
47 {$INCLUDE ../nogl/noGLuses.inc}
48 e_log
, r_graphics
, e_input
, e_sound
,
49 g_options
, g_console
, g_game
, g_menu
, g_gui
, g_basic
;
52 GameTitle
= 'Doom 2D: Forever (SDL 1.2, %s)';
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 function LoadGL
: Boolean;
81 if glRenderToFBO
and (not nogl_ExtensionSupported('GL_OES_framebuffer_object')) then
83 if glRenderToFBO
and (not Load_GL_ARB_framebuffer_object
) then
86 e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
87 glRenderToFBO
:= false;
98 procedure UpdateSize (w
, h
: Integer);
104 if glRenderToFBO
then
106 // store real window size in gWinSize, downscale resolution now
107 w
:= round(w
/ r_pixel_scale
);
108 h
:= round(h
/ r_pixel_scale
);
109 if not e_ResizeFramebuffer(w
, h
) then
111 e_LogWriteln('GL: could not create framebuffer, falling back to --no-fbo');
112 glRenderToFBO
:= False;
119 {$IFDEF ENABLE_HOLMES}
123 e_ResizeWindow(w
, h
);
125 g_Game_SetupScreenSize
;
130 function GetTitle (): PChar;
131 var info
: AnsiString;
133 info
:= g_GetBuildHash(false);
134 if info
= 'custom build' then
135 info
:= info
+ ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE
+ ' ' + GAME_BUILDTIME
;
136 result
:= PChar(Format(GameTitle
, [info
]))
139 function InitWindow (w
, h
, bpp
: Integer; fullScreen
: Boolean): Boolean;
142 e_LogWritefln('InitWindow %s %s %s %s', [w
, h
, bpp
, fullScreen
]);
144 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
145 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
146 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
147 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
148 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
149 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
151 if fullScreen
then flags
:= flags
or SDL_FULLSCREEN
;
152 if userResize
then flags
:= flags
or SDL_VIDEORESIZE
;
153 if (screen
= nil) or (SDL_VideoModeOk(w
, h
, bpp
, flags
) <> 0) then
155 SDL_FreeSurface(screen
);
156 screen
:= SDL_SetVideoMode(w
, h
, bpp
, flags
);
157 if screen
<> nil then
161 e_LogWriteln('GL: unable to load OpenGL functions', TMsgType
.Fatal
);
164 SDL_WM_SetCaption(GetTitle(), nil);
165 gFullScreen
:= fullscreen
;
166 gRC_FullScreen
:= fullscreen
;
173 e_LogWritefln('SDL: video mode not supported', [])
177 procedure sys_Repaint
;
182 procedure sys_EnableVSync (yes
: Boolean);
187 function sys_GetDisplayModes (bpp
: Integer): SSArray
;
188 var m
: PPSDL_Rect
; f
: TSDL_PixelFormat
; i
, count
: Integer;
190 SetLength(result
, 0);
191 FillChar(f
, sizeof(f
), 0);
193 f
.BitsPerPixel
:= bpp
;
194 f
.BytesPerPixel
:= (bpp
+ 7) div 8;
195 m
:= SDL_ListModes(@f
, SDL_OPENGL
or SDL_FULLSCREEN
);
196 if (m
<> NIL) and (UIntPtr(m
) <> UIntPtr(-1)) then
199 while m
[count
] <> nil do inc(count
);
200 SetLength(result
, count
);
201 for i
:= 0 to count
- 1 do
202 result
[i
] := IntToStr(m
[i
].w
) + 'x' + IntToStr(m
[i
].h
);
206 function sys_SetDisplayMode (w
, h
, bpp
: Integer; fullscreen
, maximized
: Boolean): Boolean;
208 result
:= InitWindow(w
, h
, bpp
, fullscreen
)
211 (* --------- Joystick --------- *)
213 procedure HandleJoyButton (var ev
: TSDL_JoyButtonEvent
);
214 var down
: Boolean; key
: Integer;
216 if (ev
.which
< e_MaxJoys
) and (ev
.button
< e_MaxJoyBtns
) then
218 key
:= e_JoyButtonToKey(ev
.which
, ev
.button
);
219 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
221 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev
.which
, ev
.button
, key
, down
]);
222 e_KeyUpDown(key
, down
);
223 g_Console_ProcessBind(key
, down
)
229 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
230 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev
.which
, ev
.button
, down
])
235 procedure HandleJoyAxis (var ev
: TSDL_JoyAxisEvent
);
236 var key
, minuskey
: Integer;
238 if (ev
.which
< e_MaxJoys
) and (ev
.axis
< e_MaxJoyAxes
) then
240 key
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_PLUS
);
241 minuskey
:= e_JoyAxisToKey(ev
.which
, ev
.axis
, AX_MINUS
);
244 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
]]);
246 if ev
.value
< JoystickZeroAxes
[ev
.which
, ev
.axis
] - e_JoystickDeadzones
[ev
.which
] then
248 if (e_KeyPressed(key
)) then
250 e_KeyUpDown(key
, False);
251 g_Console_ProcessBind(key
, False)
253 e_KeyUpDown(minuskey
, True);
254 g_Console_ProcessBind(minuskey
, True)
256 else if ev
.value
> JoystickZeroAxes
[ev
.which
, ev
.axis
] + e_JoystickDeadzones
[ev
.which
] then
258 if (e_KeyPressed(minuskey
)) then
260 e_KeyUpDown(minuskey
, False);
261 g_Console_ProcessBind(minuskey
, False)
263 e_KeyUpDown(key
, True);
264 g_Console_ProcessBind(key
, True)
268 if (e_KeyPressed(minuskey
)) then
270 e_KeyUpDown(minuskey
, False);
271 g_Console_ProcessBind(minuskey
, False)
273 if (e_KeyPressed(key
)) then
275 e_KeyUpDown(key
, False);
276 g_Console_ProcessBind(key
, False)
283 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
]])
287 procedure HandleJoyHat (var ev
: TSDL_JoyHatEvent
);
291 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
293 if (ev
.which
< e_MaxJoys
) and (ev
.hat
< e_MaxJoyHats
) then
296 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
]);
297 hat
[HAT_UP
] := LongBool(ev
.value
and SDL_HAT_UP
);
298 hat
[HAT_DOWN
] := LongBool(ev
.value
and SDL_HAT_DOWN
);
299 hat
[HAT_LEFT
] := LongBool(ev
.value
and SDL_HAT_LEFT
);
300 hat
[HAT_RIGHT
] := LongBool(ev
.value
and SDL_HAT_RIGHT
);
301 for i
:= HAT_LEFT
to HAT_DOWN
do
303 if JoystickHatState
[ev
.which
, ev
.hat
, i
] <> hat
[i
] then
306 key
:= e_JoyHatToKey(ev
.which
, ev
.hat
, i
);
307 e_KeyUpDown(key
, down
);
308 g_Console_ProcessBind(key
, down
)
311 JoystickHatState
[ev
.which
, ev
.hat
] := hat
316 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.which
, ev
.hat
, ev
.value
])
320 procedure AddJoystick (which
: Integer);
323 assert(which
< e_MaxJoys
);
324 JoystickHandle
[which
] := SDL_JoystickOpen(which
);
325 if JoystickHandle
[which
] <> nil then
327 e_LogWritefln('Added Joystick %s', [which
]);
328 e_JoystickAvailable
[which
] := True;
329 for i
:= 0 to Min(SDL_JoystickNumAxes(JoystickHandle
[which
]), e_MaxJoyAxes
) - 1 do
330 JoystickZeroAxes
[which
, i
] := SDL_JoystickGetAxis(JoystickHandle
[which
], i
)
334 e_LogWritefln('Failed to open Joystick %s', [which
])
338 procedure RemoveJoystick (which
: Integer);
340 assert(which
< e_MaxJoys
);
341 e_LogWritefln('Remove Joystick %s', [which
]);
342 e_JoystickAvailable
[which
] := False;
343 if JoystickHandle
[which
] <> nil then
344 SDL_JoystickClose(JoystickHandle
[which
]);
345 JoystickHandle
[which
] := nil
348 (* --------- Input --------- *)
350 function Key2Stub (key
: Integer): Integer;
354 SDLK_ESCAPE
: x
:= IK_ESCAPE
;
355 SDLK_RETURN
: x
:= IK_RETURN
;
356 SDLK_KP_ENTER
: x
:= IK_KPRETURN
;
357 SDLK_KP0
: x
:= IK_KPINSERT
;
359 SDLK_KP8
: x
:= IK_KPUP
;
360 SDLK_DOWN
: x
:= IK_DOWN
;
361 SDLK_KP2
: x
:= IK_KPDOWN
;
362 SDLK_LEFT
: x
:= IK_LEFT
;
363 SDLK_KP4
: x
:= IK_KPLEFT
;
364 SDLK_RIGHT
: x
:= IK_RIGHT
;
365 SDLK_KP6
: x
:= IK_KPRIGHT
;
366 SDLK_DELETE
: x
:= IK_DELETE
;
367 SDLK_HOME
: x
:= IK_HOME
;
368 SDLK_KP7
: x
:= IK_KPHOME
;
369 SDLK_INSERT
: x
:= IK_INSERT
;
370 SDLK_SPACE
: x
:= IK_SPACE
;
371 SDLK_LSHIFT
: x
:= IK_SHIFT
;
372 SDLK_LALT
: x
:= IK_ALT
;
373 SDLK_TAB
: x
:= IK_TAB
;
374 SDLK_PAGEUP
: x
:= IK_PAGEUP
;
375 SDLK_KP9
: x
:= IK_KPPAGEUP
;
376 SDLK_PAGEDOWN
: x
:= IK_PAGEDN
;
377 SDLK_KP3
: x
:= IK_KPPAGEDN
;
378 SDLK_KP5
: x
:= IK_KP5
;
379 SDLK_NUMLOCK
: x
:= IK_NUMLOCK
;
380 SDLK_KP_DIVIDE
: x
:= IK_KPDIVIDE
;
381 SDLK_KP_MULTIPLY
: x
:= IK_KPMULTIPLE
;
382 SDLK_KP_MINUS
: x
:= IK_KPMINUS
;
383 SDLK_KP_PLUS
: x
:= IK_KPPLUS
;
384 SDLK_KP_PERIOD
: x
:= IK_KPDOT
;
385 SDLK_CAPSLOCK
: x
:= IK_CAPSLOCK
;
386 SDLK_RSHIFT
: x
:= IK_RSHIFT
;
387 SDLK_LCTRL
: x
:= IK_CTRL
;
388 SDLK_RCTRL
: x
:= IK_RCTRL
;
389 SDLK_RALT
: x
:= IK_RALT
;
390 SDLK_LSUPER
: x
:= IK_WIN
;
391 SDLK_RSUPER
: x
:= IK_RWIN
;
392 SDLK_MENU
: x
:= IK_MENU
;
393 SDLK_PRINT
: x
:= IK_PRINTSCR
;
394 SDLK_SCROLLOCK
: x
:= IK_SCROLLLOCK
;
395 SDLK_LEFTBRACKET
: x
:= IK_LBRACKET
;
396 SDLK_RIGHTBRACKET
: x
:= IK_RBRACKET
;
397 SDLK_SEMICOLON
: x
:= IK_SEMICOLON
;
398 SDLK_QUOTE
: x
:= IK_QUOTE
;
399 SDLK_BACKSLASH
: x
:= IK_BACKSLASH
;
400 SDLK_SLASH
: x
:= IK_SLASH
;
401 SDLK_COMMA
: x
:= IK_COMMA
;
402 SDLK_PERIOD
: x
:= IK_DOT
;
403 SDLK_EQUALS
: x
:= IK_EQUALS
;
423 SDLK_F10
: x
:= IK_F10
;
424 SDLK_F11
: x
:= IK_F11
;
425 SDLK_F12
: x
:= IK_F12
;
426 SDLK_END
: x
:= IK_END
;
427 SDLK_KP1
: x
:= IK_KPEND
;
428 SDLK_BACKSPACE
: x
:= IK_BACKSPACE
;
429 SDLK_BACKQUOTE
: x
:= IK_BACKQUOTE
;
430 SDLK_PAUSE
: x
:= IK_PAUSE
;
431 SDLK_A
..SDLK_Z
: x
:= IK_A
+ (key
- SDLK_A
);
432 SDLK_MINUS
: x
:= IK_MINUS
;
433 SDLK_RMETA
: x
:= IK_RMETA
;
434 SDLK_LMETA
: x
:= IK_LMETA
;
441 procedure HandleKeyboard (var ev
: TSDL_KeyboardEvent
);
442 var down
, repeated
: Boolean; key
: Integer; ch
: Char;
444 key
:= Key2Stub(ev
.keysym
.sym
);
445 down
:= (ev
.type_
= SDL_KEYDOWN
);
446 repeated
:= down
and e_KeyPressed(key
);
447 ch
:= wchar2win(WideChar(ev
.keysym
.unicode
));
449 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
)]);
452 e_KeyUpDown(key
, down
);
453 g_Console_ProcessBind(key
, down
);
457 g_Console_ProcessBindRepeat(key
)
459 if @sys_CharPress
<> nil then
460 if down
and IsValid1251(ev
.keysym
.unicode
) and IsPrintable1251(ch
) then
464 procedure HandleResize (var ev
: TSDL_ResizeEvent
);
467 e_LogWritefln('Input Debug: SDL_VIDEORESIZE %s %s', [ev
.w
, ev
.h
]);
468 if modeResize
= 1 then
469 UpdateSize(ev
.w
, ev
.h
)
470 else if modeResize
> 1 then
471 InitWindow(ev
.w
, ev
.h
, gBPP
, gFullscreen
)
474 function sys_HandleInput (): Boolean;
478 while SDL_PollEvent(@ev
) <> 0 do
481 SDL_QUITEV
: result
:= true;
482 SDL_VIDEORESIZE
: HandleResize(ev
.resize
);
483 SDL_KEYUP
, SDL_KEYDOWN
: HandleKeyboard(ev
.key
);
484 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
: HandleJoyButton(ev
.jbutton
);
485 SDL_JOYAXISMOTION
: HandleJoyAxis(ev
.jaxis
);
486 SDL_JOYHATMOTION
: HandleJoyHat(ev
.jhat
);
487 SDL_VIDEOEXPOSE
: sys_Repaint
;
488 SDL_ACTIVEEVENT
: e_MuteChannels((ev
.active
.gain
= 0) and gMuteWhenInactive
);
493 procedure sys_RequestQuit
;
496 ev
.quit
.type_
:= SDL_QUITEV
;
500 (* --------- Init --------- *)
503 var flags
: Uint32
; i
: Integer;
505 e_WriteLog('Init SDL', TMsgType
.Notify
);
506 flags
:= SDL_INIT_VIDEO
or SDL_INIT_AUDIO
or
507 SDL_INIT_TIMER
or SDL_INIT_JOYSTICK
508 (*or SDL_INIT_NOPARACHUTE*);
509 if SDL_Init(flags
) <> 0 then
510 raise Exception
.Create('SDL: Init failed: ' + SDL_GetError
);
511 SDL_EnableUNICODE(1);
512 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
513 for i
:= 0 to e_MaxJoys
- 1 do
520 e_WriteLog('Releasing SDL', TMsgType
.Notify
);
521 for i
:= 0 to e_MaxJoys
- 1 do
523 if screen
<> nil then
526 SDL_FreeSurface(screen
)
532 (* window resize are broken both on linux and osx, so disabled by default *)
533 conRegVar('sdl_allow_resize', @userResize
, 'allow to resize window by user', 'allow to resize window by user');
534 conRegVar('sdl_resize_action', @modeResize
, 'set window resize mode (0: ignore, 1: change, 2: reset)', '');