DEADSOFTWARE

6750d68e6364570a0485647bc8c61d7e1bf832d4
[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 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, SDL2, Math,
44 e_log, e_graphics, e_input, e_sound,
45 {$INCLUDE ../nogl/noGLuses.inc}
46 {$IFDEF ENABLE_HOLMES}
47 g_holmes, sdlcarcass, fui_ctls,
48 {$ENDIF}
49 g_touch, g_options, g_window, g_console, g_game, g_menu, g_gui, g_main, g_basic;
51 const
52 GameTitle = 'Doom 2D: Forever (SDL 2, %s)';
54 var
55 window: PSDL_Window;
56 context: TSDL_GLContext;
57 display: Integer;
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 procedure UpdateSize (w, h: Integer);
77 begin
78 gWinSizeX := w;
79 gWinSizeY := h;
80 gWinRealPosX := 0;
81 gWinRealPosY := 0;
82 gScreenWidth := w;
83 gScreenHeight := h;
84 {$IFDEF ENABLE_HOLMES}
85 fuiScrWdt := w;
86 fuiScrHgt := h;
87 {$ENDIF}
88 e_ResizeWindow(w, h);
89 e_InitGL;
90 g_Game_SetupScreenSize;
91 {$IFNDEF ANDOIRD}
92 (* This will fix menu reset on keyboard showing *)
93 g_Menu_Reset;
94 {$ENDIF}
95 g_Game_ClearLoading;
96 {$IFDEF ENABLE_HOLMES}
97 if assigned(oglInitCB) then oglInitCB;
98 {$ENDIF}
99 end;
101 function GetTitle (): PChar;
102 var info: AnsiString;
103 begin
104 info := g_GetBuildHash(false);
105 if info = 'custom build' then
106 info := info + ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME;
107 result := PChar(Format(GameTitle, [info]))
108 end;
110 function InitWindow (w, h, bpp: Integer; fullScreen: Boolean): Boolean;
111 var flags: UInt32;
112 begin
113 // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
114 e_LogWritefln('InitWindow %s %s %s %s', [w, h, bpp, fullScreen]);
115 result := false;
116 if window = nil then
117 begin
118 {$IFDEF USE_GLES1}
119 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
120 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
121 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
122 {$ELSE}
123 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
124 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
125 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
126 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
127 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
128 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
129 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
130 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
131 {$ENDIF}
132 flags := SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE;
133 if fullScreen then flags := flags or SDL_WINDOW_FULLSCREEN;
134 window := SDL_CreateWindow(GetTitle(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
135 if window <> nil then
136 begin
137 context := SDL_GL_CreateContext(window);
138 if context <> nil then
139 begin
140 {$IFDEF NOGL_INIT}
141 nogl_Init;
142 {$ENDIF}
143 UpdateSize(w, h);
144 result := true
145 end
146 else
147 begin
148 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError])
149 end
150 end
151 else
152 begin
153 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError])
154 end
155 end
156 else
157 begin
158 if fullScreen then flags := SDL_WINDOW_FULLSCREEN else flags := 0;
159 SDL_SetWindowSize(window, w, h);
160 SDL_SetWindowFullscreen(window, flags);
161 UpdateSize(w, h);
162 result := true
163 end
164 end;
166 procedure sys_Repaint;
167 begin
168 SDL_GL_SwapWindow(window)
169 end;
171 procedure sys_EnableVSync (yes: Boolean);
172 begin
173 if yes then
174 SDL_GL_SetSwapInterval(1)
175 else
176 SDL_GL_SetSwapInterval(0)
177 end;
179 function sys_GetDisplayModes (bpp: Integer): SSArray;
180 var i, count, num, pw, ph: Integer; m: TSDL_DisplayMode;
181 begin
182 result := nil;
183 num := SDL_GetNumDisplayModes(display);
184 if num < 0 then
185 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError]);
186 if num > 0 then
187 begin
188 e_LogWritefln('Video modes for display %s:', [display]);
189 SetLength(result, num);
190 i := 0; count := 0; pw := 0; ph := 0;
191 while i < num do
192 begin
193 SDL_GetDisplayMode(display, i, @m);
194 if ((pw <> m.w) or (ph <> m.h)) then
195 begin
196 e_LogWritefln('* %sx%sx%s@%s', [m.w, m.h, SDL_BITSPERPIXEL(m.format), m.refresh_rate]);
197 pw := m.w; ph := m.h;
198 result[count] := IntToStr(m.w) + 'x' + IntToStr(m.h);
199 Inc(count);
200 end
201 else
202 begin
203 e_LogWritefln('- %sx%sx%s@%s', [m.w, m.h, SDL_BITSPERPIXEL(m.format), m.refresh_rate]);
204 end;
205 Inc(i)
206 end;
207 SetLength(result, count)
208 end
209 end;
211 function sys_SetDisplayMode (w, h, bpp: Integer; fullScreen: Boolean): Boolean;
212 begin
213 result := InitWindow(w, h, bpp, fullScreen)
214 end;
216 (* --------- Joystick --------- *)
218 procedure HandleJoyButton (var ev: TSDL_JoyButtonEvent);
219 var down: Boolean; key: Integer;
220 begin
221 if (ev.which < e_MaxJoys) and (ev.button < e_MaxJoyBtns) then
222 begin
223 key := e_JoyButtonToKey(ev.which, ev.button);
224 down := ev.type_ = SDL_JOYBUTTONDOWN;
225 if g_dbg_input then
226 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev.which, ev.button, key, down]);
227 e_KeyUpDown(key, down);
228 g_Console_ProcessBind(key, down)
229 end
230 else
231 begin
232 if g_dbg_input then
233 begin
234 down := ev.type_ = SDL_JOYBUTTONDOWN;
235 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev.which, ev.button, down])
236 end
237 end
238 end;
240 procedure HandleJoyAxis (var ev: TSDL_JoyAxisEvent);
241 var key, minuskey: Integer;
242 begin
243 if (ev.which < e_MaxJoys) and (ev.axis < e_MaxJoyAxes) then
244 begin
245 key := e_JoyAxisToKey(ev.which, ev.axis, AX_PLUS);
246 minuskey := e_JoyAxisToKey(ev.which, ev.axis, AX_MINUS);
248 if g_dbg_input then
249 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]]);
251 if ev.value < JoystickZeroAxes[ev.which, ev.axis] - e_JoystickDeadzones[ev.which] then
252 begin
253 if (e_KeyPressed(key)) then
254 begin
255 e_KeyUpDown(key, False);
256 g_Console_ProcessBind(key, False)
257 end;
258 e_KeyUpDown(minuskey, True);
259 g_Console_ProcessBind(minuskey, True)
260 end
261 else if ev.value > JoystickZeroAxes[ev.which, ev.axis] + e_JoystickDeadzones[ev.which] then
262 begin
263 if (e_KeyPressed(minuskey)) then
264 begin
265 e_KeyUpDown(minuskey, False);
266 g_Console_ProcessBind(minuskey, False)
267 end;
268 e_KeyUpDown(key, True);
269 g_Console_ProcessBind(key, True)
270 end
271 else
272 begin
273 if (e_KeyPressed(minuskey)) then
274 begin
275 e_KeyUpDown(minuskey, False);
276 g_Console_ProcessBind(minuskey, False)
277 end;
278 if (e_KeyPressed(key)) then
279 begin
280 e_KeyUpDown(key, False);
281 g_Console_ProcessBind(key, False)
282 end
283 end
284 end
285 else
286 begin
287 if g_dbg_input then
288 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]])
289 end
290 end;
292 procedure HandleJoyHat (var ev: TSDL_JoyHatEvent);
293 var
294 down: Boolean;
295 i, key: Integer;
296 hat: array [HAT_LEFT..HAT_DOWN] of Boolean;
297 begin
298 if (ev.which < e_MaxJoys) and (ev.hat < e_MaxJoyHats) then
299 begin
300 if g_dbg_input then
301 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev.which, ev.hat, ev.value]);
302 hat[HAT_UP] := LongBool(ev.value and SDL_HAT_UP);
303 hat[HAT_DOWN] := LongBool(ev.value and SDL_HAT_DOWN);
304 hat[HAT_LEFT] := LongBool(ev.value and SDL_HAT_LEFT);
305 hat[HAT_RIGHT] := LongBool(ev.value and SDL_HAT_RIGHT);
306 for i := HAT_LEFT to HAT_DOWN do
307 begin
308 if JoystickHatState[ev.which, ev.hat, i] <> hat[i] then
309 begin
310 down := hat[i];
311 key := e_JoyHatToKey(ev.which, ev.hat, i);
312 e_KeyUpDown(key, down);
313 g_Console_ProcessBind(key, down)
314 end
315 end;
316 JoystickHatState[ev.which, ev.hat] := hat
317 end
318 else
319 begin
320 if g_dbg_input then
321 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev.which, ev.hat, ev.value])
322 end
323 end;
325 procedure HandleJoyAdd (var ev: TSDL_JoyDeviceEvent);
326 var i: Integer;
327 begin
328 if (ev.which < e_MaxJoys) then
329 begin
330 JoystickHandle[ev.which] := SDL_JoystickOpen(ev.which);
331 if JoystickHandle[ev.which] <> nil then
332 begin
333 e_LogWritefln('Added Joystick %s', [ev.which]);
334 e_JoystickAvailable[ev.which] := True;
335 for i := 0 to Min(SDL_JoystickNumAxes(JoystickHandle[ev.which]), e_MaxJoyAxes) - 1 do
336 JoystickZeroAxes[ev.which, i] := SDL_JoystickGetAxis(JoystickHandle[ev.which], i)
337 end
338 else
339 begin
340 e_LogWritefln('Warning! Failed to open Joystick %s', [ev.which])
341 end
342 end
343 else
344 begin
345 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev.which, e_MaxJoys])
346 end
347 end;
349 procedure HandleJoyRemove (var ev: TSDL_JoyDeviceEvent);
350 begin
351 e_LogWritefln('Removed Joystick %s', [ev.which]);
352 if (ev.which < e_MaxJoys) then
353 begin
354 e_JoystickAvailable[ev.which] := False;
355 if JoystickHandle[ev.which] <> nil then
356 SDL_JoystickClose(JoystickHandle[ev.which]);
357 JoystickHandle[ev.which] := nil
358 end
359 end;
361 (* --------- Input --------- *)
363 function HandleWindow (var ev: TSDL_WindowEvent): Boolean;
364 begin
365 result := false;
366 case ev.event of
367 SDL_WINDOWEVENT_RESIZED: UpdateSize(ev.data1, ev.data2);
368 SDL_WINDOWEVENT_EXPOSED: sys_Repaint;
369 SDL_WINDOWEVENT_CLOSE: result := true;
370 SDL_WINDOWEVENT_FOCUS_LOST, SDL_WINDOWEVENT_MINIMIZED:
371 begin
372 e_UnpressAllKeys;
373 if gMuteWhenInactive then
374 e_MuteChannels(true);
375 {$IFDEF ENABLE_HOLMES}
376 if assigned(winBlurCB) then winBlurCB;
377 {$ENDIF}
378 end;
379 SDL_WINDOWEVENT_FOCUS_GAINED, SDL_WINDOWEVENT_MAXIMIZED, SDL_WINDOWEVENT_RESTORED:
380 begin
381 e_MuteChannels(false);
382 {$IFDEF ENABLE_HOLMES}
383 if assigned(winFocusCB) then winFocusCB;
384 {$ENDIF}
385 end;
386 end
387 end;
389 procedure HandleKeyboard (var ev: TSDL_KeyboardEvent);
390 var down: Boolean; key: Integer;
391 begin
392 key := ev.keysym.scancode;
393 down := (ev.type_ = SDL_KEYDOWN);
394 if key = SDL_SCANCODE_AC_BACK then
395 key := SDL_SCANCODE_ESCAPE;
396 {$IFDEF ENABLE_HOLMES}
397 if fuiOnSDLEvent(PSDL_Event(@ev)^) then
398 begin
399 // event eaten, but...
400 if not down then e_KeyUpDown(key, false);
401 exit;
402 end;
403 {$ENDIF}
404 if ev._repeat = 0 then
405 begin
406 if g_dbg_input then
407 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down, key]);
408 e_KeyUpDown(key, down);
409 g_Console_ProcessBind(key, down);
410 end
411 else if gConsoleShow or gChatShow or (g_ActiveWindow <> nil) then
412 begin
413 KeyPress(key) // key repeat in menus and shit
414 end
415 end;
417 procedure HandleTextInput (var ev: TSDL_TextInputEvent);
418 var ch: UnicodeChar; sch: AnsiChar;
419 begin
420 Utf8ToUnicode(@ch, PChar(ev.text), 1);
421 sch := AnsiChar(wchar2win(ch));
422 if g_dbg_input then
423 e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev.text, Ord(ch), Ord(sch)]);
424 if IsValid1251(Word(ch)) and IsPrintable1251(ch) then
425 CharPress(sch);
426 end;
428 function sys_HandleInput (): Boolean;
429 var ev: TSDL_Event;
430 begin
431 result := false;
432 ZeroMemory(@ev, sizeof(ev));
433 while SDL_PollEvent(@ev) <> 0 do
434 begin
435 case ev.type_ of
436 SDL_QUITEV: result := true;
437 SDL_WINDOWEVENT: result := HandleWindow(ev.window);
438 SDL_KEYUP, SDL_KEYDOWN: HandleKeyboard(ev.key);
439 SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP: HandleJoyButton(ev.jbutton);
440 SDL_JOYAXISMOTION: HandleJoyAxis(ev.jaxis);
441 SDL_JOYHATMOTION: HandleJoyHat(ev.jhat);
442 SDL_JOYDEVICEADDED: HandleJoyAdd(ev.jdevice);
443 SDL_JOYDEVICEREMOVED: HandleJoyRemove(ev.jdevice);
444 SDL_TEXTINPUT: HandleTextInput(ev.text);
445 SDL_FINGERMOTION, SDL_FINGERDOWN, SDL_FINGERUP: g_Touch_HandleEvent(ev.tfinger);
446 {$IFDEF ENABLE_HOLMES}
447 SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION: fuiOnSDLEvent(ev);
448 {$ENDIF}
449 end
450 end
451 end;
453 procedure sys_RequestQuit;
454 var ev: TSDL_Event;
455 begin
456 ev.type_ := SDL_QUITEV;
457 SDL_PushEvent(@ev)
458 end;
460 (* --------- Init --------- *)
462 procedure sys_Init;
463 var flags: UInt32;
464 begin
465 e_WriteLog('Init SDL2', TMsgType.Notify);
466 {$IFDEF HEADLESS}
467 {$IFDEF USE_SDLMIXER}
468 flags := SDL_INIT_TIMER or SDL_INIT_AUDIO or $00004000;
469 {$ELSE}
470 flags := SDL_INIT_TIMER or $00004000;
471 {$ENDIF}
472 {$ELSE}
473 flags := SDL_INIT_JOYSTICK or SDL_INIT_TIMER or SDL_INIT_VIDEO;
474 {$ENDIF}
475 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, '0');
476 if SDL_Init(flags) <> 0 then
477 raise Exception.Create('SDL: Init failed: ' + SDL_GetError);
478 SDL_ShowCursor(SDL_DISABLE);
479 end;
481 procedure sys_Final;
482 begin
483 e_WriteLog('Releasing SDL2', TMsgType.Notify);
484 if context <> nil then
485 begin
486 {$IFDEF NOGL_INIT}
487 nogl_Quit;
488 {$ENDIF}
489 SDL_GL_DeleteContext(context);
490 context := nil;
491 end;
492 if window <> nil then
493 begin
494 SDL_DestroyWindow(window);
495 window := nil;
496 end;
497 SDL_Quit
498 end;
500 initialization
501 conRegVar('sdl2_display_index', @display, 'use display index as base', '');
502 end.