DEADSOFTWARE

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