DEADSOFTWARE

check FBO status and fall back to no-fbo if something is wrong
[d2df-sdl.git] / src / game / sdl / g_system.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
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.
6 *
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.
11 *
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/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_system;
18 interface
20 uses Utils;
22 (* --- Utils --- *)
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;
32 (* --- Input --- *)
33 function sys_HandleInput (): Boolean;
34 procedure sys_RequestQuit;
36 (* --- Init --- *)
37 procedure sys_Init;
38 procedure sys_Final;
40 implementation
42 uses
43 SysUtils, SDL, Math,
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;
48 const
49 GameTitle = 'Doom 2D: Forever (SDL 1.2, %s)';
51 var
52 userResize: Boolean;
53 modeResize: Integer;
54 screen: PSDL_Surface;
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;
62 begin
63 result := SDL_GetTicks()
64 end;
66 procedure sys_Delay (ms: Integer);
67 begin
68 SDL_Delay(ms)
69 end;
71 (* --------- Graphics --------- *)
73 function LoadGL: Boolean;
74 begin
75 result := true;
76 {$IFDEF NOGL_INIT}
77 nogl_Init;
78 if glRenderToFBO and (not nogl_ExtensionSupported('GL_OES_framebuffer_object')) then
79 {$ELSE}
80 if glRenderToFBO and (not Load_GL_ARB_framebuffer_object) then
81 {$ENDIF}
82 begin
83 e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
84 glRenderToFBO := false;
85 end;
86 end;
88 procedure FreeGL;
89 begin
90 {$IFDEF NOGL_INIT}
91 nogl_Quit();
92 {$ENDIF}
93 end;
95 procedure UpdateSize (w, h: Integer);
96 begin
97 gWinSizeX := w;
98 gWinSizeY := h;
99 gRC_Width := w;
100 gRC_Height := h;
101 if glRenderToFBO then
102 begin
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 if not e_ResizeFramebuffer(w, h) then
107 begin
108 e_LogWriteln('GL: could not create framebuffer, falling back to --no-fbo');
109 glRenderToFBO := False;
110 w := gWinSizeX;
111 h := gWinSizeY;
112 end;
113 end;
114 gScreenWidth := w;
115 gScreenHeight := h;
116 {$IFDEF ENABLE_HOLMES}
117 fuiScrWdt := w;
118 fuiScrHgt := h;
119 {$ENDIF}
120 e_ResizeWindow(w, h);
121 e_InitGL;
122 g_Game_SetupScreenSize;
123 g_Menu_Reset;
124 g_Game_ClearLoading;
125 end;
127 function GetTitle (): PChar;
128 var info: AnsiString;
129 begin
130 info := g_GetBuildHash(false);
131 if info = 'custom build' then
132 info := info + ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME;
133 result := PChar(Format(GameTitle, [info]))
134 end;
136 function InitWindow (w, h, bpp: Integer; fullScreen: Boolean): Boolean;
137 var flags: Uint32;
138 begin
139 e_LogWritefln('InitWindow %s %s %s %s', [w, h, bpp, fullScreen]);
140 result := false;
141 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
142 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
143 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
144 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
145 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
146 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
147 flags := SDL_OPENGL;
148 if fullScreen then flags := flags or SDL_FULLSCREEN;
149 if userResize then flags := flags or SDL_VIDEORESIZE;
150 if (screen = nil) or (SDL_VideoModeOk(w, h, bpp, flags) <> 0) then
151 begin
152 SDL_FreeSurface(screen);
153 screen := SDL_SetVideoMode(w, h, bpp, flags);
154 if screen <> nil then
155 begin
156 if not LoadGL then
157 begin
158 e_LogWriteln('GL: unable to load OpenGL functions', TMsgType.Fatal);
159 exit;
160 end;
161 SDL_WM_SetCaption(GetTitle(), nil);
162 gFullScreen := fullscreen;
163 gRC_FullScreen := fullscreen;
164 UpdateSize(w, h);
165 result := True
166 end
167 end
168 else
169 begin
170 e_LogWritefln('SDL: video mode not supported', [])
171 end
172 end;
174 procedure sys_Repaint;
175 begin
176 if glRenderToFBO then
177 e_BlitFramebuffer(gWinSizeX, gWinSizeY);
178 SDL_GL_SwapBuffers
179 end;
181 procedure sys_EnableVSync (yes: Boolean);
182 begin
183 (* ??? *)
184 end;
186 function sys_GetDisplayModes (bpp: Integer): SSArray;
187 var m: PPSDL_Rect; f: TSDL_PixelFormat; i, count: Integer;
188 begin
189 SetLength(result, 0);
190 FillChar(f, sizeof(f), 0);
191 f.palette := nil;
192 f.BitsPerPixel := bpp;
193 f.BytesPerPixel := (bpp + 7) div 8;
194 m := SDL_ListModes(@f, SDL_OPENGL or SDL_FULLSCREEN);
195 if (m <> NIL) and (UIntPtr(m) <> UIntPtr(-1)) then
196 begin
197 count := 0;
198 while m[count] <> nil do inc(count);
199 SetLength(result, count);
200 for i := 0 to count - 1 do
201 result[i] := IntToStr(m[i].w) + 'x' + IntToStr(m[i].h);
202 end
203 end;
205 function sys_SetDisplayMode (w, h, bpp: Integer; fullscreen, maximized: Boolean): Boolean;
206 begin
207 result := InitWindow(w, h, bpp, fullscreen)
208 end;
210 (* --------- Joystick --------- *)
212 procedure HandleJoyButton (var ev: TSDL_JoyButtonEvent);
213 var down: Boolean; key: Integer;
214 begin
215 if (ev.which < e_MaxJoys) and (ev.button < e_MaxJoyBtns) then
216 begin
217 key := e_JoyButtonToKey(ev.which, ev.button);
218 down := ev.type_ = SDL_JOYBUTTONDOWN;
219 if g_dbg_input then
220 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev.which, ev.button, key, down]);
221 e_KeyUpDown(key, down);
222 g_Console_ProcessBind(key, down)
223 end
224 else
225 begin
226 if g_dbg_input then
227 begin
228 down := ev.type_ = SDL_JOYBUTTONDOWN;
229 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev.which, ev.button, down])
230 end
231 end
232 end;
234 procedure HandleJoyAxis (var ev: TSDL_JoyAxisEvent);
235 var key, minuskey: Integer;
236 begin
237 if (ev.which < e_MaxJoys) and (ev.axis < e_MaxJoyAxes) then
238 begin
239 key := e_JoyAxisToKey(ev.which, ev.axis, AX_PLUS);
240 minuskey := e_JoyAxisToKey(ev.which, ev.axis, AX_MINUS);
242 if g_dbg_input then
243 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]]);
245 if ev.value < JoystickZeroAxes[ev.which, ev.axis] - e_JoystickDeadzones[ev.which] then
246 begin
247 if (e_KeyPressed(key)) then
248 begin
249 e_KeyUpDown(key, False);
250 g_Console_ProcessBind(key, False)
251 end;
252 e_KeyUpDown(minuskey, True);
253 g_Console_ProcessBind(minuskey, True)
254 end
255 else if ev.value > JoystickZeroAxes[ev.which, ev.axis] + e_JoystickDeadzones[ev.which] then
256 begin
257 if (e_KeyPressed(minuskey)) then
258 begin
259 e_KeyUpDown(minuskey, False);
260 g_Console_ProcessBind(minuskey, False)
261 end;
262 e_KeyUpDown(key, True);
263 g_Console_ProcessBind(key, True)
264 end
265 else
266 begin
267 if (e_KeyPressed(minuskey)) then
268 begin
269 e_KeyUpDown(minuskey, False);
270 g_Console_ProcessBind(minuskey, False)
271 end;
272 if (e_KeyPressed(key)) then
273 begin
274 e_KeyUpDown(key, False);
275 g_Console_ProcessBind(key, False)
276 end
277 end
278 end
279 else
280 begin
281 if g_dbg_input then
282 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 end
284 end;
286 procedure HandleJoyHat (var ev: TSDL_JoyHatEvent);
287 var
288 down: Boolean;
289 i, key: Integer;
290 hat: array [HAT_LEFT..HAT_DOWN] of Boolean;
291 begin
292 if (ev.which < e_MaxJoys) and (ev.hat < e_MaxJoyHats) then
293 begin
294 if g_dbg_input then
295 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev.which, ev.hat, ev.value]);
296 hat[HAT_UP] := LongBool(ev.value and SDL_HAT_UP);
297 hat[HAT_DOWN] := LongBool(ev.value and SDL_HAT_DOWN);
298 hat[HAT_LEFT] := LongBool(ev.value and SDL_HAT_LEFT);
299 hat[HAT_RIGHT] := LongBool(ev.value and SDL_HAT_RIGHT);
300 for i := HAT_LEFT to HAT_DOWN do
301 begin
302 if JoystickHatState[ev.which, ev.hat, i] <> hat[i] then
303 begin
304 down := hat[i];
305 key := e_JoyHatToKey(ev.which, ev.hat, i);
306 e_KeyUpDown(key, down);
307 g_Console_ProcessBind(key, down)
308 end
309 end;
310 JoystickHatState[ev.which, ev.hat] := hat
311 end
312 else
313 begin
314 if g_dbg_input then
315 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev.which, ev.hat, ev.value])
316 end
317 end;
319 procedure AddJoystick (which: Integer);
320 var i: Integer;
321 begin
322 assert(which < e_MaxJoys);
323 JoystickHandle[which] := SDL_JoystickOpen(which);
324 if JoystickHandle[which] <> nil then
325 begin
326 e_LogWritefln('Added Joystick %s', [which]);
327 e_JoystickAvailable[which] := True;
328 for i := 0 to Min(SDL_JoystickNumAxes(JoystickHandle[which]), e_MaxJoyAxes) - 1 do
329 JoystickZeroAxes[which, i] := SDL_JoystickGetAxis(JoystickHandle[which], i)
330 end
331 else
332 begin
333 e_LogWritefln('Failed to open Joystick %s', [which])
334 end
335 end;
337 procedure RemoveJoystick (which: Integer);
338 begin
339 assert(which < e_MaxJoys);
340 e_LogWritefln('Remove Joystick %s', [which]);
341 e_JoystickAvailable[which] := False;
342 if JoystickHandle[which] <> nil then
343 SDL_JoystickClose(JoystickHandle[which]);
344 JoystickHandle[which] := nil
345 end;
347 (* --------- Input --------- *)
349 function Key2Stub (key: Integer): Integer;
350 var x: Integer;
351 begin
352 case key of
353 SDLK_ESCAPE: x := IK_ESCAPE;
354 SDLK_RETURN: x := IK_RETURN;
355 SDLK_KP_ENTER: x := IK_KPRETURN;
356 SDLK_KP0: x := IK_KPINSERT;
357 SDLK_UP: x := IK_UP;
358 SDLK_KP8: x := IK_KPUP;
359 SDLK_DOWN: x := IK_DOWN;
360 SDLK_KP2: x := IK_KPDOWN;
361 SDLK_LEFT: x := IK_LEFT;
362 SDLK_KP4: x := IK_KPLEFT;
363 SDLK_RIGHT: x := IK_RIGHT;
364 SDLK_KP6: x := IK_KPRIGHT;
365 SDLK_DELETE: x := IK_DELETE;
366 SDLK_HOME: x := IK_HOME;
367 SDLK_KP7: x := IK_KPHOME;
368 SDLK_INSERT: x := IK_INSERT;
369 SDLK_SPACE: x := IK_SPACE;
370 SDLK_LSHIFT: x := IK_SHIFT;
371 SDLK_LALT: x := IK_ALT;
372 SDLK_TAB: x := IK_TAB;
373 SDLK_PAGEUP: x := IK_PAGEUP;
374 SDLK_KP9: x := IK_KPPAGEUP;
375 SDLK_PAGEDOWN: x := IK_PAGEDN;
376 SDLK_KP3: x := IK_KPPAGEDN;
377 SDLK_KP5: x := IK_KP5;
378 SDLK_NUMLOCK: x := IK_NUMLOCK;
379 SDLK_KP_DIVIDE: x := IK_KPDIVIDE;
380 SDLK_KP_MULTIPLY: x := IK_KPMULTIPLE;
381 SDLK_KP_MINUS: x := IK_KPMINUS;
382 SDLK_KP_PLUS: x := IK_KPPLUS;
383 SDLK_KP_PERIOD: x := IK_KPDOT;
384 SDLK_CAPSLOCK: x := IK_CAPSLOCK;
385 SDLK_RSHIFT: x := IK_RSHIFT;
386 SDLK_LCTRL: x := IK_CTRL;
387 SDLK_RCTRL: x := IK_RCTRL;
388 SDLK_RALT: x := IK_RALT;
389 SDLK_LSUPER: x := IK_WIN;
390 SDLK_RSUPER: x := IK_RWIN;
391 SDLK_MENU: x := IK_MENU;
392 SDLK_PRINT: x := IK_PRINTSCR;
393 SDLK_SCROLLOCK: x := IK_SCROLLLOCK;
394 SDLK_LEFTBRACKET: x := IK_LBRACKET;
395 SDLK_RIGHTBRACKET: x := IK_RBRACKET;
396 SDLK_SEMICOLON: x := IK_SEMICOLON;
397 SDLK_QUOTE: x := IK_QUOTE;
398 SDLK_BACKSLASH: x := IK_BACKSLASH;
399 SDLK_SLASH: x := IK_SLASH;
400 SDLK_COMMA: x := IK_COMMA;
401 SDLK_PERIOD: x := IK_DOT;
402 SDLK_EQUALS: x := IK_EQUALS;
403 SDLK_0: x := IK_0;
404 SDLK_1: x := IK_1;
405 SDLK_2: x := IK_2;
406 SDLK_3: x := IK_3;
407 SDLK_4: x := IK_4;
408 SDLK_5: x := IK_5;
409 SDLK_6: x := IK_6;
410 SDLK_7: x := IK_7;
411 SDLK_8: x := IK_8;
412 SDLK_9: x := IK_9;
413 SDLK_F1: x := IK_F1;
414 SDLK_F2: x := IK_F2;
415 SDLK_F3: x := IK_F3;
416 SDLK_F4: x := IK_F4;
417 SDLK_F5: x := IK_F5;
418 SDLK_F6: x := IK_F6;
419 SDLK_F7: x := IK_F7;
420 SDLK_F8: x := IK_F8;
421 SDLK_F9: x := IK_F9;
422 SDLK_F10: x := IK_F10;
423 SDLK_F11: x := IK_F11;
424 SDLK_F12: x := IK_F12;
425 SDLK_END: x := IK_END;
426 SDLK_KP1: x := IK_KPEND;
427 SDLK_BACKSPACE: x := IK_BACKSPACE;
428 SDLK_BACKQUOTE: x := IK_BACKQUOTE;
429 SDLK_PAUSE: x := IK_PAUSE;
430 SDLK_A..SDLK_Z: x := IK_A + (key - SDLK_A);
431 SDLK_MINUS: x := IK_MINUS;
432 SDLK_RMETA: x := IK_RMETA;
433 SDLK_LMETA: x := IK_LMETA;
434 else
435 x := IK_INVALID
436 end;
437 result := x
438 end;
440 procedure HandleKeyboard (var ev: TSDL_KeyboardEvent);
441 var down, repeated: Boolean; key: Integer; ch: Char;
442 begin
443 key := Key2Stub(ev.keysym.sym);
444 down := (ev.type_ = SDL_KEYDOWN);
445 repeated := down and e_KeyPressed(key);
446 ch := wchar2win(WideChar(ev.keysym.unicode));
447 if g_dbg_input then
448 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)]);
449 if not repeated then
450 begin
451 e_KeyUpDown(key, down);
452 g_Console_ProcessBind(key, down);
453 end
454 else if gConsoleShow or gChatShow or (g_ActiveWindow <> nil) then
455 begin
456 KeyPress(key) // key repeat in menus and shit
457 end;
458 if down and IsValid1251(ev.keysym.unicode) and IsPrintable1251(ch) then
459 CharPress(ch)
460 end;
462 procedure HandleResize (var ev: TSDL_ResizeEvent);
463 begin
464 if g_dbg_input then
465 e_LogWritefln('Input Debug: SDL_VIDEORESIZE %s %s', [ev.w, ev.h]);
466 if modeResize = 1 then
467 UpdateSize(ev.w, ev.h)
468 else if modeResize > 1 then
469 InitWindow(ev.w, ev.h, gBPP, gFullscreen)
470 end;
472 function sys_HandleInput (): Boolean;
473 var ev: TSDL_Event;
474 begin
475 result := false;
476 while SDL_PollEvent(@ev) <> 0 do
477 begin
478 case ev.type_ of
479 SDL_QUITEV: result := true;
480 SDL_VIDEORESIZE: HandleResize(ev.resize);
481 SDL_KEYUP, SDL_KEYDOWN: HandleKeyboard(ev.key);
482 SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP: HandleJoyButton(ev.jbutton);
483 SDL_JOYAXISMOTION: HandleJoyAxis(ev.jaxis);
484 SDL_JOYHATMOTION: HandleJoyHat(ev.jhat);
485 SDL_VIDEOEXPOSE: sys_Repaint;
486 SDL_ACTIVEEVENT: e_MuteChannels((ev.active.gain = 0) and gMuteWhenInactive);
487 end
488 end
489 end;
491 procedure sys_RequestQuit;
492 var ev: TSDL_Event;
493 begin
494 ev.quit.type_ := SDL_QUITEV;
495 SDL_PushEvent(@ev)
496 end;
498 (* --------- Init --------- *)
500 procedure sys_Init;
501 var flags: Uint32; i: Integer;
502 begin
503 e_WriteLog('Init SDL', TMsgType.Notify);
504 flags := SDL_INIT_VIDEO or SDL_INIT_AUDIO or
505 SDL_INIT_TIMER or SDL_INIT_JOYSTICK
506 (*or SDL_INIT_NOPARACHUTE*);
507 if SDL_Init(flags) <> 0 then
508 raise Exception.Create('SDL: Init failed: ' + SDL_GetError);
509 SDL_EnableUNICODE(1);
510 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
511 for i := 0 to e_MaxJoys - 1 do
512 AddJoystick(i)
513 end;
515 procedure sys_Final;
516 var i: Integer;
517 begin
518 e_WriteLog('Releasing SDL', TMsgType.Notify);
519 for i := 0 to e_MaxJoys - 1 do
520 RemoveJoystick(i);
521 if screen <> nil then
522 begin
523 FreeGL;
524 SDL_FreeSurface(screen)
525 end;
526 SDL_Quit
527 end;
529 initialization
530 (* window resize are broken both on linux and osx, so disabled by default *)
531 conRegVar('sdl_allow_resize', @userResize, 'allow to resize window by user', 'allow to resize window by user');
532 conRegVar('sdl_resize_action', @modeResize, 'set window resize mode (0: ignore, 1: change, 2: reset)', '');
533 userResize := false;
534 modeResize := 0;
535 end.