DEADSOFTWARE

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