DEADSOFTWARE

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