DEADSOFTWARE

cleanup: remove g_main.pas
[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 var (* hooks *)
41 sys_CharPress: procedure (ch: AnsiChar) = nil;
43 implementation
45 uses
46 SysUtils, SDL, Math,
47 {$INCLUDE ../nogl/noGLuses.inc}
48 e_log, r_graphics, e_input, e_sound,
49 g_options, g_window, g_console, g_game, g_menu, g_gui, g_basic;
51 const
52 GameTitle = 'Doom 2D: Forever (SDL 1.2, %s)';
54 var
55 userResize: Boolean;
56 modeResize: Integer;
57 screen: PSDL_Surface;
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;
65 begin
66 result := SDL_GetTicks()
67 end;
69 procedure sys_Delay (ms: Integer);
70 begin
71 SDL_Delay(ms)
72 end;
74 (* --------- Graphics --------- *)
76 function LoadGL: Boolean;
77 begin
78 result := true;
79 {$IFDEF NOGL_INIT}
80 nogl_Init;
81 if glRenderToFBO and (not nogl_ExtensionSupported('GL_OES_framebuffer_object')) then
82 {$ELSE}
83 if glRenderToFBO and (not Load_GL_ARB_framebuffer_object) then
84 {$ENDIF}
85 begin
86 e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
87 glRenderToFBO := false;
88 end;
89 end;
91 procedure FreeGL;
92 begin
93 {$IFDEF NOGL_INIT}
94 nogl_Quit();
95 {$ENDIF}
96 end;
98 procedure UpdateSize (w, h: Integer);
99 begin
100 gWinSizeX := w;
101 gWinSizeY := h;
102 gRC_Width := w;
103 gRC_Height := h;
104 if glRenderToFBO then
105 begin
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
110 begin
111 e_LogWriteln('GL: could not create framebuffer, falling back to --no-fbo');
112 glRenderToFBO := False;
113 w := gWinSizeX;
114 h := gWinSizeY;
115 end;
116 end;
117 gScreenWidth := w;
118 gScreenHeight := h;
119 {$IFDEF ENABLE_HOLMES}
120 fuiScrWdt := w;
121 fuiScrHgt := h;
122 {$ENDIF}
123 e_ResizeWindow(w, h);
124 e_InitGL;
125 g_Game_SetupScreenSize;
126 g_Menu_Reset;
127 g_Game_ClearLoading;
128 end;
130 function GetTitle (): PChar;
131 var info: AnsiString;
132 begin
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]))
137 end;
139 function InitWindow (w, h, bpp: Integer; fullScreen: Boolean): Boolean;
140 var flags: Uint32;
141 begin
142 e_LogWritefln('InitWindow %s %s %s %s', [w, h, bpp, fullScreen]);
143 result := false;
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...
150 flags := SDL_OPENGL;
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
154 begin
155 SDL_FreeSurface(screen);
156 screen := SDL_SetVideoMode(w, h, bpp, flags);
157 if screen <> nil then
158 begin
159 if not LoadGL then
160 begin
161 e_LogWriteln('GL: unable to load OpenGL functions', TMsgType.Fatal);
162 exit;
163 end;
164 SDL_WM_SetCaption(GetTitle(), nil);
165 gFullScreen := fullscreen;
166 gRC_FullScreen := fullscreen;
167 UpdateSize(w, h);
168 result := True
169 end
170 end
171 else
172 begin
173 e_LogWritefln('SDL: video mode not supported', [])
174 end
175 end;
177 procedure sys_Repaint;
178 begin
179 SDL_GL_SwapBuffers
180 end;
182 procedure sys_EnableVSync (yes: Boolean);
183 begin
184 (* ??? *)
185 end;
187 function sys_GetDisplayModes (bpp: Integer): SSArray;
188 var m: PPSDL_Rect; f: TSDL_PixelFormat; i, count: Integer;
189 begin
190 SetLength(result, 0);
191 FillChar(f, sizeof(f), 0);
192 f.palette := nil;
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
197 begin
198 count := 0;
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);
203 end
204 end;
206 function sys_SetDisplayMode (w, h, bpp: Integer; fullscreen, maximized: Boolean): Boolean;
207 begin
208 result := InitWindow(w, h, bpp, fullscreen)
209 end;
211 (* --------- Joystick --------- *)
213 procedure HandleJoyButton (var ev: TSDL_JoyButtonEvent);
214 var down: Boolean; key: Integer;
215 begin
216 if (ev.which < e_MaxJoys) and (ev.button < e_MaxJoyBtns) then
217 begin
218 key := e_JoyButtonToKey(ev.which, ev.button);
219 down := ev.type_ = SDL_JOYBUTTONDOWN;
220 if g_dbg_input then
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)
224 end
225 else
226 begin
227 if g_dbg_input then
228 begin
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])
231 end
232 end
233 end;
235 procedure HandleJoyAxis (var ev: TSDL_JoyAxisEvent);
236 var key, minuskey: Integer;
237 begin
238 if (ev.which < e_MaxJoys) and (ev.axis < e_MaxJoyAxes) then
239 begin
240 key := e_JoyAxisToKey(ev.which, ev.axis, AX_PLUS);
241 minuskey := e_JoyAxisToKey(ev.which, ev.axis, AX_MINUS);
243 if g_dbg_input then
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
247 begin
248 if (e_KeyPressed(key)) then
249 begin
250 e_KeyUpDown(key, False);
251 g_Console_ProcessBind(key, False)
252 end;
253 e_KeyUpDown(minuskey, True);
254 g_Console_ProcessBind(minuskey, True)
255 end
256 else if ev.value > JoystickZeroAxes[ev.which, ev.axis] + e_JoystickDeadzones[ev.which] then
257 begin
258 if (e_KeyPressed(minuskey)) then
259 begin
260 e_KeyUpDown(minuskey, False);
261 g_Console_ProcessBind(minuskey, False)
262 end;
263 e_KeyUpDown(key, True);
264 g_Console_ProcessBind(key, True)
265 end
266 else
267 begin
268 if (e_KeyPressed(minuskey)) then
269 begin
270 e_KeyUpDown(minuskey, False);
271 g_Console_ProcessBind(minuskey, False)
272 end;
273 if (e_KeyPressed(key)) then
274 begin
275 e_KeyUpDown(key, False);
276 g_Console_ProcessBind(key, False)
277 end
278 end
279 end
280 else
281 begin
282 if g_dbg_input then
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]])
284 end
285 end;
287 procedure HandleJoyHat (var ev: TSDL_JoyHatEvent);
288 var
289 down: Boolean;
290 i, key: Integer;
291 hat: array [HAT_LEFT..HAT_DOWN] of Boolean;
292 begin
293 if (ev.which < e_MaxJoys) and (ev.hat < e_MaxJoyHats) then
294 begin
295 if g_dbg_input 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
302 begin
303 if JoystickHatState[ev.which, ev.hat, i] <> hat[i] then
304 begin
305 down := hat[i];
306 key := e_JoyHatToKey(ev.which, ev.hat, i);
307 e_KeyUpDown(key, down);
308 g_Console_ProcessBind(key, down)
309 end
310 end;
311 JoystickHatState[ev.which, ev.hat] := hat
312 end
313 else
314 begin
315 if g_dbg_input then
316 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev.which, ev.hat, ev.value])
317 end
318 end;
320 procedure AddJoystick (which: Integer);
321 var i: Integer;
322 begin
323 assert(which < e_MaxJoys);
324 JoystickHandle[which] := SDL_JoystickOpen(which);
325 if JoystickHandle[which] <> nil then
326 begin
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)
331 end
332 else
333 begin
334 e_LogWritefln('Failed to open Joystick %s', [which])
335 end
336 end;
338 procedure RemoveJoystick (which: Integer);
339 begin
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
346 end;
348 (* --------- Input --------- *)
350 function Key2Stub (key: Integer): Integer;
351 var x: Integer;
352 begin
353 case key of
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;
358 SDLK_UP: x := IK_UP;
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;
404 SDLK_0: x := IK_0;
405 SDLK_1: x := IK_1;
406 SDLK_2: x := IK_2;
407 SDLK_3: x := IK_3;
408 SDLK_4: x := IK_4;
409 SDLK_5: x := IK_5;
410 SDLK_6: x := IK_6;
411 SDLK_7: x := IK_7;
412 SDLK_8: x := IK_8;
413 SDLK_9: x := IK_9;
414 SDLK_F1: x := IK_F1;
415 SDLK_F2: x := IK_F2;
416 SDLK_F3: x := IK_F3;
417 SDLK_F4: x := IK_F4;
418 SDLK_F5: x := IK_F5;
419 SDLK_F6: x := IK_F6;
420 SDLK_F7: x := IK_F7;
421 SDLK_F8: x := IK_F8;
422 SDLK_F9: x := IK_F9;
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;
435 else
436 x := IK_INVALID
437 end;
438 result := x
439 end;
441 procedure HandleKeyboard (var ev: TSDL_KeyboardEvent);
442 var down, repeated: Boolean; key: Integer; ch: Char;
443 begin
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));
448 if g_dbg_input then
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)]);
450 if not repeated then
451 begin
452 e_KeyUpDown(key, down);
453 g_Console_ProcessBind(key, down);
454 end
455 else
456 begin
457 g_Console_ProcessBindRepeat(key)
458 end;
459 if @sys_CharPress <> nil then
460 if down and IsValid1251(ev.keysym.unicode) and IsPrintable1251(ch) then
461 sys_CharPress(ch)
462 end;
464 procedure HandleResize (var ev: TSDL_ResizeEvent);
465 begin
466 if g_dbg_input then
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)
472 end;
474 function sys_HandleInput (): Boolean;
475 var ev: TSDL_Event;
476 begin
477 result := false;
478 while SDL_PollEvent(@ev) <> 0 do
479 begin
480 case ev.type_ of
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);
489 end
490 end
491 end;
493 procedure sys_RequestQuit;
494 var ev: TSDL_Event;
495 begin
496 ev.quit.type_ := SDL_QUITEV;
497 SDL_PushEvent(@ev)
498 end;
500 (* --------- Init --------- *)
502 procedure sys_Init;
503 var flags: Uint32; i: Integer;
504 begin
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
514 AddJoystick(i)
515 end;
517 procedure sys_Final;
518 var i: Integer;
519 begin
520 e_WriteLog('Releasing SDL', TMsgType.Notify);
521 for i := 0 to e_MaxJoys - 1 do
522 RemoveJoystick(i);
523 if screen <> nil then
524 begin
525 FreeGL;
526 SDL_FreeSurface(screen)
527 end;
528 SDL_Quit
529 end;
531 initialization
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)', '');
535 userResize := false;
536 modeResize := 0;
537 end.