DEADSOFTWARE

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