DEADSOFTWARE

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