DEADSOFTWARE

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