DEADSOFTWARE

system: remove sys_GetTicks
[d2df-sdl.git] / src / game / sdl2 / 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 procedure sys_Delay (ms: Integer);
25 (* --- Graphics --- *)
26 function sys_GetDisplayModes (bpp: Integer): SSArray;
27 function sys_SetDisplayMode (w, h, bpp: Integer; fullscreen, maximized: Boolean): Boolean;
28 procedure sys_EnableVSync (yes: Boolean);
29 procedure sys_Repaint;
31 (* --- Input --- *)
32 function sys_HandleInput (): Boolean;
33 procedure sys_RequestQuit;
35 (* --- Init --- *)
36 procedure sys_Init;
37 procedure sys_Final;
39 var (* hooks *)
40 sys_CharPress: procedure (ch: AnsiChar) = nil;
41 sys_ScreenResize: procedure (w, h: Integer) = nil;
43 implementation
45 uses
46 SysUtils, SDL2, Math, ctypes,
47 e_log, e_input, e_sound,
48 {$IFDEF ENABLE_HOLMES}
49 sdlcarcass,
50 {$ENDIF}
51 g_touch, g_options, g_console, g_game, g_basic
52 ;
54 const
55 GameTitle = 'Doom 2D: Forever (SDL 2, %s)';
57 var
58 window: PSDL_Window;
59 context: TSDL_GLContext;
60 display, wx, wy: Integer;
61 wc: Boolean;
62 JoystickHandle: array [0..e_MaxJoys - 1] of PSDL_Joystick;
63 JoystickHatState: array [0..e_MaxJoys - 1, 0..e_MaxJoyHats - 1, HAT_LEFT..HAT_DOWN] of Boolean;
64 JoystickZeroAxes: array [0..e_MaxJoys - 1, 0..e_MaxJoyAxes - 1] of Integer;
66 (* --------- Utils --------- *)
68 procedure sys_Delay (ms: Integer);
69 begin
70 SDL_Delay(ms)
71 end;
73 (* --------- Graphics --------- *)
75 function GetTitle (): AnsiString;
76 var info: AnsiString;
77 begin
78 info := g_GetBuildHash(false);
79 if info = 'custom build' then
80 info := info + ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME;
81 result := Format(GameTitle, [info]);
82 end;
84 function InitWindow (w, h, bpp: Integer; fullScreen, maximized: Boolean): Boolean;
85 var flags: UInt32; x, y: cint; title: AnsiString;
86 begin
87 // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
88 e_LogWritefln('InitWindow %s %s %s %s', [w, h, bpp, fullScreen]);
89 result := false;
90 if window = nil then
91 begin
92 {$IFDEF USE_GLES1}
93 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
94 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
95 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
96 {$ELSE}
97 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
98 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
99 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
100 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
101 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
102 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
103 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
104 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
105 {$ENDIF}
106 flags := SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE;
107 if fullScreen then flags := flags or SDL_WINDOW_FULLSCREEN;
108 if maximized then flags := flags or SDL_WINDOW_MAXIMIZED;
109 if wc then
110 begin
111 x := SDL_WINDOWPOS_CENTERED;
112 y := SDL_WINDOWPOS_CENTERED
113 end
114 else
115 begin
116 x := wx;
117 y := wy
118 end;
119 title := GetTitle();
120 window := SDL_CreateWindow(PChar(title), x, y, w, h, flags);
121 if window <> nil then
122 begin
123 context := SDL_GL_CreateContext(window);
124 if context <> nil then
125 begin
126 if (fullscreen = false) and (maximized = false) and (wc = false) then
127 begin
128 SDL_GetWindowPosition(window, @x, @y);
129 wx := x; wy := y
130 end;
131 gFullScreen := fullscreen;
132 gWinMaximized := maximized;
133 gRC_FullScreen := fullscreen;
134 gRC_Maximized := maximized;
135 if @sys_ScreenResize <> nil then
136 sys_ScreenResize(w, h);
137 result := true
138 end
139 else
140 begin
141 // SDL_DestroyWindow(window);
142 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError])
143 end
144 end
145 else
146 begin
147 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError])
148 end
149 end
150 else
151 begin
152 if fullScreen then flags := SDL_WINDOW_FULLSCREEN else flags := 0;
153 SDL_SetWindowFullscreen(window, flags);
154 SDL_SetWindowSize(window, w, h);
155 if maximized then SDL_MaximizeWindow(window);
156 // always reset to center when changing fullscreen->windowed for safety purposes
157 if wc or (gFullscreen and not fullscreen) or (gWinMaximized and not maximized) then
158 begin
159 x := SDL_WINDOWPOS_CENTERED;
160 y := SDL_WINDOWPOS_CENTERED
161 end
162 else
163 begin
164 x := wx;
165 y := wy
166 end;
167 SDL_SetWindowPosition(window, x, y);
168 if (fullscreen = false) and (maximized = false) and (wc = false) then
169 begin
170 SDL_GetWindowPosition(window, @x, @y);
171 wx := x; wy := y
172 end;
173 gFullScreen := fullscreen;
174 gWinMaximized := maximized;
175 gRC_FullScreen := fullscreen;
176 gRC_Maximized := maximized;
177 if @sys_ScreenResize <> nil then
178 sys_ScreenResize(w, h);
179 result := true
180 end
181 end;
183 procedure sys_Repaint;
184 begin
185 SDL_GL_SwapWindow(window)
186 end;
188 procedure sys_EnableVSync (yes: Boolean);
189 begin
190 if yes then
191 SDL_GL_SetSwapInterval(1)
192 else
193 SDL_GL_SetSwapInterval(0)
194 end;
196 function sys_GetDisplayModes (bpp: Integer): SSArray;
197 var i, count, num, pw, ph: Integer; m: TSDL_DisplayMode;
198 begin
199 result := nil;
200 num := SDL_GetNumDisplayModes(display);
201 if num < 0 then
202 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError]);
203 if num > 0 then
204 begin
205 e_LogWritefln('Video modes for display %s:', [display]);
206 SetLength(result, num);
207 i := 0; count := 0; pw := 0; ph := 0;
208 while i < num do
209 begin
210 SDL_GetDisplayMode(display, i, @m);
211 if ((pw <> m.w) or (ph <> m.h)) then
212 begin
213 e_LogWritefln('* %sx%sx%s@%s', [m.w, m.h, SDL_BITSPERPIXEL(m.format), m.refresh_rate]);
214 pw := m.w; ph := m.h;
215 result[count] := IntToStr(m.w) + 'x' + IntToStr(m.h);
216 Inc(count);
217 end
218 else
219 begin
220 e_LogWritefln('- %sx%sx%s@%s', [m.w, m.h, SDL_BITSPERPIXEL(m.format), m.refresh_rate]);
221 end;
222 Inc(i)
223 end;
224 SetLength(result, count)
225 end
226 end;
228 function sys_SetDisplayMode (w, h, bpp: Integer; fullScreen, maximized: Boolean): Boolean;
229 begin
230 result := InitWindow(w, h, bpp, fullScreen, maximized)
231 end;
233 (* --------- Joystick --------- *)
235 procedure HandleJoyButton (var ev: TSDL_JoyButtonEvent);
236 var down: Boolean; key: Integer;
237 begin
238 if (ev.which < e_MaxJoys) and (ev.button < e_MaxJoyBtns) then
239 begin
240 key := e_JoyButtonToKey(ev.which, ev.button);
241 down := ev.type_ = SDL_JOYBUTTONDOWN;
242 if g_dbg_input then
243 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev.which, ev.button, key, down]);
244 e_KeyUpDown(key, down);
245 g_Console_ProcessBind(key, down)
246 end
247 else
248 begin
249 if g_dbg_input then
250 begin
251 down := ev.type_ = SDL_JOYBUTTONDOWN;
252 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev.which, ev.button, down])
253 end
254 end
255 end;
257 procedure HandleJoyAxis (var ev: TSDL_JoyAxisEvent);
258 var key, minuskey: Integer;
259 begin
260 if (ev.which < e_MaxJoys) and (ev.axis < e_MaxJoyAxes) then
261 begin
262 key := e_JoyAxisToKey(ev.which, ev.axis, AX_PLUS);
263 minuskey := e_JoyAxisToKey(ev.which, ev.axis, AX_MINUS);
265 if g_dbg_input then
266 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]]);
268 if ev.value < JoystickZeroAxes[ev.which, ev.axis] - e_JoystickDeadzones[ev.which] then
269 begin
270 if (e_KeyPressed(key)) then
271 begin
272 e_KeyUpDown(key, False);
273 g_Console_ProcessBind(key, False)
274 end;
275 e_KeyUpDown(minuskey, True);
276 g_Console_ProcessBind(minuskey, True)
277 end
278 else if ev.value > JoystickZeroAxes[ev.which, ev.axis] + e_JoystickDeadzones[ev.which] then
279 begin
280 if (e_KeyPressed(minuskey)) then
281 begin
282 e_KeyUpDown(minuskey, False);
283 g_Console_ProcessBind(minuskey, False)
284 end;
285 e_KeyUpDown(key, True);
286 g_Console_ProcessBind(key, True)
287 end
288 else
289 begin
290 if (e_KeyPressed(minuskey)) then
291 begin
292 e_KeyUpDown(minuskey, False);
293 g_Console_ProcessBind(minuskey, False)
294 end;
295 if (e_KeyPressed(key)) then
296 begin
297 e_KeyUpDown(key, False);
298 g_Console_ProcessBind(key, False)
299 end
300 end
301 end
302 else
303 begin
304 if g_dbg_input then
305 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]])
306 end
307 end;
309 procedure HandleJoyHat (var ev: TSDL_JoyHatEvent);
310 var
311 down: Boolean;
312 i, key: Integer;
313 hat: array [HAT_LEFT..HAT_DOWN] of Boolean;
314 begin
315 if (ev.which < e_MaxJoys) and (ev.hat < e_MaxJoyHats) then
316 begin
317 if g_dbg_input then
318 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev.which, ev.hat, ev.value]);
319 hat[HAT_UP] := LongBool(ev.value and SDL_HAT_UP);
320 hat[HAT_DOWN] := LongBool(ev.value and SDL_HAT_DOWN);
321 hat[HAT_LEFT] := LongBool(ev.value and SDL_HAT_LEFT);
322 hat[HAT_RIGHT] := LongBool(ev.value and SDL_HAT_RIGHT);
323 for i := HAT_LEFT to HAT_DOWN do
324 begin
325 if JoystickHatState[ev.which, ev.hat, i] <> hat[i] then
326 begin
327 down := hat[i];
328 key := e_JoyHatToKey(ev.which, ev.hat, i);
329 e_KeyUpDown(key, down);
330 g_Console_ProcessBind(key, down)
331 end
332 end;
333 JoystickHatState[ev.which, ev.hat] := hat
334 end
335 else
336 begin
337 if g_dbg_input then
338 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev.which, ev.hat, ev.value])
339 end
340 end;
342 procedure HandleJoyAdd (var ev: TSDL_JoyDeviceEvent);
343 var i: Integer;
344 begin
345 if (ev.which < e_MaxJoys) then
346 begin
347 JoystickHandle[ev.which] := SDL_JoystickOpen(ev.which);
348 if JoystickHandle[ev.which] <> nil then
349 begin
350 e_LogWritefln('Added Joystick %s', [ev.which]);
351 e_JoystickAvailable[ev.which] := True;
352 for i := 0 to Min(SDL_JoystickNumAxes(JoystickHandle[ev.which]), e_MaxJoyAxes) - 1 do
353 JoystickZeroAxes[ev.which, i] := SDL_JoystickGetAxis(JoystickHandle[ev.which], i)
354 end
355 else
356 begin
357 e_LogWritefln('Warning! Failed to open Joystick %s', [ev.which])
358 end
359 end
360 else
361 begin
362 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev.which, e_MaxJoys])
363 end
364 end;
366 procedure HandleJoyRemove (var ev: TSDL_JoyDeviceEvent);
367 begin
368 e_LogWritefln('Removed Joystick %s', [ev.which]);
369 if (ev.which < e_MaxJoys) then
370 begin
371 e_JoystickAvailable[ev.which] := False;
372 if JoystickHandle[ev.which] <> nil then
373 SDL_JoystickClose(JoystickHandle[ev.which]);
374 JoystickHandle[ev.which] := nil
375 end
376 end;
378 (* --------- Input --------- *)
380 function HandleWindow (var ev: TSDL_WindowEvent): Boolean;
381 begin
382 result := false;
383 if g_dbg_input then
384 e_LogWritefln('Window Event: event = %s, data1 = %s, data2 = %s', [ev.event, ev.data1, ev.data2]);
385 case ev.event of
386 SDL_WINDOWEVENT_RESIZED:
387 if @sys_ScreenResize <> nil then
388 sys_ScreenResize(ev.data1, ev.data2);
389 SDL_WINDOWEVENT_EXPOSED: sys_Repaint;
390 SDL_WINDOWEVENT_CLOSE: result := true;
391 SDL_WINDOWEVENT_MOVED:
392 begin
393 wx := ev.data1;
394 wy := ev.data2
395 end;
396 SDL_WINDOWEVENT_FOCUS_LOST, SDL_WINDOWEVENT_MINIMIZED:
397 begin
398 e_UnpressAllKeys;
399 if gMuteWhenInactive then
400 e_MuteChannels(true);
401 {$IFDEF ENABLE_HOLMES}
402 if assigned(winBlurCB) then winBlurCB;
403 {$ENDIF}
404 end;
405 SDL_WINDOWEVENT_FOCUS_GAINED, SDL_WINDOWEVENT_MAXIMIZED, SDL_WINDOWEVENT_RESTORED:
406 begin
407 if ev.event = SDL_WINDOWEVENT_MAXIMIZED then
408 begin
409 gWinMaximized := true;
410 gRC_Maximized := true
411 end
412 else if ev.event = SDL_WINDOWEVENT_RESTORED then
413 begin
414 gWinMaximized := false;
415 gRC_Maximized := false
416 end;
417 e_MuteChannels(false);
418 {$IFDEF ENABLE_HOLMES}
419 if assigned(winFocusCB) then winFocusCB;
420 {$ENDIF}
421 end;
422 end
423 end;
425 procedure HandleKeyboard (var ev: TSDL_KeyboardEvent);
426 var down: Boolean; key: Integer;
427 begin
428 key := ev.keysym.scancode;
429 down := (ev.type_ = SDL_KEYDOWN);
430 if key = SDL_SCANCODE_AC_BACK then
431 key := SDL_SCANCODE_ESCAPE;
432 {$IFDEF ENABLE_HOLMES}
433 if fuiOnSDLEvent(PSDL_Event(@ev)^) then
434 begin
435 // event eaten, but...
436 if not down then e_KeyUpDown(key, false);
437 exit;
438 end;
439 {$ENDIF}
440 if ev._repeat = 0 then
441 begin
442 if g_dbg_input then
443 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down, key]);
444 e_KeyUpDown(key, down);
445 g_Console_ProcessBind(key, down);
446 end
447 else
448 begin
449 if g_dbg_input then
450 e_LogWritefln('Input Debug: keyrep, scancode=%s', [key]);
451 g_Console_ProcessBindRepeat(key);
452 end
453 end;
455 procedure HandleTextInput (var ev: TSDL_TextInputEvent);
456 var ch: UnicodeChar; sch: AnsiChar;
457 begin
458 Utf8ToUnicode(@ch, PChar(ev.text), 1);
459 sch := AnsiChar(wchar2win(ch));
460 if g_dbg_input then
461 e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev.text, Ord(ch), Ord(sch)]);
462 if @sys_CharPress <> nil then
463 if IsValid1251(Word(ch)) and IsPrintable1251(ch) then
464 sys_CharPress(sch)
465 end;
467 function sys_HandleInput (): Boolean;
468 var ev: TSDL_Event;
469 begin
470 result := false;
471 ZeroMemory(@ev, sizeof(ev));
472 while SDL_PollEvent(@ev) <> 0 do
473 begin
474 case ev.type_ of
475 SDL_QUITEV: result := true;
476 SDL_WINDOWEVENT: result := HandleWindow(ev.window);
477 SDL_KEYUP, SDL_KEYDOWN: HandleKeyboard(ev.key);
478 SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP: HandleJoyButton(ev.jbutton);
479 SDL_JOYAXISMOTION: HandleJoyAxis(ev.jaxis);
480 SDL_JOYHATMOTION: HandleJoyHat(ev.jhat);
481 SDL_JOYDEVICEADDED: HandleJoyAdd(ev.jdevice);
482 SDL_JOYDEVICEREMOVED: HandleJoyRemove(ev.jdevice);
483 SDL_TEXTINPUT: HandleTextInput(ev.text);
484 SDL_FINGERMOTION, SDL_FINGERDOWN, SDL_FINGERUP: g_Touch_HandleEvent(ev.tfinger);
485 {$IFDEF ENABLE_HOLMES}
486 SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION: fuiOnSDLEvent(ev);
487 {$ENDIF}
488 end
489 end
490 end;
492 procedure sys_RequestQuit;
493 var ev: TSDL_Event;
494 begin
495 ev.type_ := SDL_QUITEV;
496 SDL_PushEvent(@ev)
497 end;
499 (* --------- Init --------- *)
501 procedure sys_Init;
502 var flags: UInt32;
503 begin
504 e_WriteLog('Init SDL2', TMsgType.Notify);
505 {$IFDEF HEADLESS}
506 {$IFDEF USE_SDLMIXER}
507 flags := SDL_INIT_TIMER or SDL_INIT_AUDIO or $00004000;
508 {$ELSE}
509 flags := SDL_INIT_TIMER or $00004000;
510 {$ENDIF}
511 {$ELSE}
512 flags := SDL_INIT_TIMER or SDL_INIT_VIDEO;
513 {$ENDIF}
514 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, '0');
515 if SDL_Init(flags) <> 0 then
516 raise Exception.Create('SDL: Init failed: ' + SDL_GetError);
517 {$IFNDEF HEADLESS}
518 if SDL_InitSubSystem(SDL_INIT_JOYSTICK) <> 0 then
519 e_LogWritefln('SDL: Init subsystem failed: %s', [SDL_GetError()]);
520 {$ENDIF}
521 SDL_ShowCursor(SDL_DISABLE);
522 end;
524 procedure sys_Final;
525 begin
526 e_WriteLog('Releasing SDL2', TMsgType.Notify);
527 if context <> nil then
528 begin
529 SDL_GL_DeleteContext(context);
530 context := nil;
531 end;
532 if window <> nil then
533 begin
534 SDL_DestroyWindow(window);
535 window := nil;
536 end;
537 SDL_Quit
538 end;
540 initialization
541 conRegVar('sdl2_display_index', @display, 'use display index as base', '');
542 conRegVar('sdl2_window_x', @wx, 'window position x', '');
543 conRegVar('sdl2_window_y', @wy, 'window position y', '');
544 conRegVar('sdl2_window_center', @wc, 'force window creation at center', '');
545 display := 0;
546 wx := SDL_WINDOWPOS_CENTERED;
547 wy := SDL_WINDOWPOS_CENTERED;
548 wc := false
549 end.