DEADSOFTWARE

save window options after resizing by user
[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, maximized: 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 gScreenWidth := w;
81 gScreenHeight := h;
82 gRC_Width := w;
83 gRC_Height := 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, maximized: Boolean): Boolean;
111 var flags: UInt32; x, y: Integer;
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 if maximized then flags := flags or SDL_WINDOW_MAXIMIZED;
135 x := SDL_WINDOWPOS_CENTERED;
136 y := SDL_WINDOWPOS_CENTERED;
137 window := SDL_CreateWindow(GetTitle(), x, y, w, h, flags);
138 if window <> nil then
139 begin
140 context := SDL_GL_CreateContext(window);
141 if context <> nil then
142 begin
143 {$IFDEF NOGL_INIT}
144 nogl_Init;
145 {$ENDIF}
146 gFullScreen := fullscreen;
147 gWinMaximized := maximized;
148 gRC_FullScreen := fullscreen;
149 gRC_Maximized := maximized;
150 UpdateSize(w, h);
151 result := true
152 end
153 else
154 begin
155 e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError])
156 end
157 end
158 else
159 begin
160 e_LogWritefln('SDL: unable to create window: %s', [SDL_GetError])
161 end
162 end
163 else
164 begin
165 SDL_SetWindowSize(window, w, h);
166 if maximized then
167 SDL_MaximizeWindow(window);
168 if fullScreen then flags := SDL_WINDOW_FULLSCREEN else flags := 0;
169 SDL_SetWindowFullscreen(window, flags);
170 gFullScreen := fullscreen;
171 gWinMaximized := maximized;
172 gRC_FullScreen := fullscreen;
173 gRC_Maximized := maximized;
174 UpdateSize(w, h);
175 result := true
176 end
177 end;
179 procedure sys_Repaint;
180 begin
181 SDL_GL_SwapWindow(window)
182 end;
184 procedure sys_EnableVSync (yes: Boolean);
185 begin
186 if yes then
187 SDL_GL_SetSwapInterval(1)
188 else
189 SDL_GL_SetSwapInterval(0)
190 end;
192 function sys_GetDisplayModes (bpp: Integer): SSArray;
193 var i, count, num, pw, ph: Integer; m: TSDL_DisplayMode;
194 begin
195 result := nil;
196 num := SDL_GetNumDisplayModes(display);
197 if num < 0 then
198 e_LogWritefln('SDL: unable to get numer of available display modes: %s', [SDL_GetError]);
199 if num > 0 then
200 begin
201 e_LogWritefln('Video modes for display %s:', [display]);
202 SetLength(result, num);
203 i := 0; count := 0; pw := 0; ph := 0;
204 while i < num do
205 begin
206 SDL_GetDisplayMode(display, i, @m);
207 if ((pw <> m.w) or (ph <> m.h)) then
208 begin
209 e_LogWritefln('* %sx%sx%s@%s', [m.w, m.h, SDL_BITSPERPIXEL(m.format), m.refresh_rate]);
210 pw := m.w; ph := m.h;
211 result[count] := IntToStr(m.w) + 'x' + IntToStr(m.h);
212 Inc(count);
213 end
214 else
215 begin
216 e_LogWritefln('- %sx%sx%s@%s', [m.w, m.h, SDL_BITSPERPIXEL(m.format), m.refresh_rate]);
217 end;
218 Inc(i)
219 end;
220 SetLength(result, count)
221 end
222 end;
224 function sys_SetDisplayMode (w, h, bpp: Integer; fullScreen, maximized: Boolean): Boolean;
225 begin
226 result := InitWindow(w, h, bpp, fullScreen, maximized)
227 end;
229 (* --------- Joystick --------- *)
231 procedure HandleJoyButton (var ev: TSDL_JoyButtonEvent);
232 var down: Boolean; key: Integer;
233 begin
234 if (ev.which < e_MaxJoys) and (ev.button < e_MaxJoyBtns) then
235 begin
236 key := e_JoyButtonToKey(ev.which, ev.button);
237 down := ev.type_ = SDL_JOYBUTTONDOWN;
238 if g_dbg_input then
239 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev.which, ev.button, key, down]);
240 e_KeyUpDown(key, down);
241 g_Console_ProcessBind(key, down)
242 end
243 else
244 begin
245 if g_dbg_input then
246 begin
247 down := ev.type_ = SDL_JOYBUTTONDOWN;
248 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev.which, ev.button, down])
249 end
250 end
251 end;
253 procedure HandleJoyAxis (var ev: TSDL_JoyAxisEvent);
254 var key, minuskey: Integer;
255 begin
256 if (ev.which < e_MaxJoys) and (ev.axis < e_MaxJoyAxes) then
257 begin
258 key := e_JoyAxisToKey(ev.which, ev.axis, AX_PLUS);
259 minuskey := e_JoyAxisToKey(ev.which, ev.axis, AX_MINUS);
261 if g_dbg_input then
262 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]]);
264 if ev.value < JoystickZeroAxes[ev.which, ev.axis] - e_JoystickDeadzones[ev.which] then
265 begin
266 if (e_KeyPressed(key)) then
267 begin
268 e_KeyUpDown(key, False);
269 g_Console_ProcessBind(key, False)
270 end;
271 e_KeyUpDown(minuskey, True);
272 g_Console_ProcessBind(minuskey, True)
273 end
274 else if ev.value > JoystickZeroAxes[ev.which, ev.axis] + e_JoystickDeadzones[ev.which] then
275 begin
276 if (e_KeyPressed(minuskey)) then
277 begin
278 e_KeyUpDown(minuskey, False);
279 g_Console_ProcessBind(minuskey, False)
280 end;
281 e_KeyUpDown(key, True);
282 g_Console_ProcessBind(key, True)
283 end
284 else
285 begin
286 if (e_KeyPressed(minuskey)) then
287 begin
288 e_KeyUpDown(minuskey, False);
289 g_Console_ProcessBind(minuskey, False)
290 end;
291 if (e_KeyPressed(key)) then
292 begin
293 e_KeyUpDown(key, False);
294 g_Console_ProcessBind(key, False)
295 end
296 end
297 end
298 else
299 begin
300 if g_dbg_input then
301 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]])
302 end
303 end;
305 procedure HandleJoyHat (var ev: TSDL_JoyHatEvent);
306 var
307 down: Boolean;
308 i, key: Integer;
309 hat: array [HAT_LEFT..HAT_DOWN] of Boolean;
310 begin
311 if (ev.which < e_MaxJoys) and (ev.hat < e_MaxJoyHats) then
312 begin
313 if g_dbg_input then
314 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev.which, ev.hat, ev.value]);
315 hat[HAT_UP] := LongBool(ev.value and SDL_HAT_UP);
316 hat[HAT_DOWN] := LongBool(ev.value and SDL_HAT_DOWN);
317 hat[HAT_LEFT] := LongBool(ev.value and SDL_HAT_LEFT);
318 hat[HAT_RIGHT] := LongBool(ev.value and SDL_HAT_RIGHT);
319 for i := HAT_LEFT to HAT_DOWN do
320 begin
321 if JoystickHatState[ev.which, ev.hat, i] <> hat[i] then
322 begin
323 down := hat[i];
324 key := e_JoyHatToKey(ev.which, ev.hat, i);
325 e_KeyUpDown(key, down);
326 g_Console_ProcessBind(key, down)
327 end
328 end;
329 JoystickHatState[ev.which, ev.hat] := hat
330 end
331 else
332 begin
333 if g_dbg_input then
334 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev.which, ev.hat, ev.value])
335 end
336 end;
338 procedure HandleJoyAdd (var ev: TSDL_JoyDeviceEvent);
339 var i: Integer;
340 begin
341 if (ev.which < e_MaxJoys) then
342 begin
343 JoystickHandle[ev.which] := SDL_JoystickOpen(ev.which);
344 if JoystickHandle[ev.which] <> nil then
345 begin
346 e_LogWritefln('Added Joystick %s', [ev.which]);
347 e_JoystickAvailable[ev.which] := True;
348 for i := 0 to Min(SDL_JoystickNumAxes(JoystickHandle[ev.which]), e_MaxJoyAxes) - 1 do
349 JoystickZeroAxes[ev.which, i] := SDL_JoystickGetAxis(JoystickHandle[ev.which], i)
350 end
351 else
352 begin
353 e_LogWritefln('Warning! Failed to open Joystick %s', [ev.which])
354 end
355 end
356 else
357 begin
358 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev.which, e_MaxJoys])
359 end
360 end;
362 procedure HandleJoyRemove (var ev: TSDL_JoyDeviceEvent);
363 begin
364 e_LogWritefln('Removed Joystick %s', [ev.which]);
365 if (ev.which < e_MaxJoys) then
366 begin
367 e_JoystickAvailable[ev.which] := False;
368 if JoystickHandle[ev.which] <> nil then
369 SDL_JoystickClose(JoystickHandle[ev.which]);
370 JoystickHandle[ev.which] := nil
371 end
372 end;
374 (* --------- Input --------- *)
376 function HandleWindow (var ev: TSDL_WindowEvent): Boolean;
377 begin
378 result := false;
379 if g_dbg_input then
380 e_LogWritefln('Window Event: event = %s, data1 = %s, data2 = %s', [ev.event, ev.data1, ev.data2]);
381 case ev.event of
382 SDL_WINDOWEVENT_RESIZED: UpdateSize(ev.data1, ev.data2);
383 SDL_WINDOWEVENT_EXPOSED: sys_Repaint;
384 SDL_WINDOWEVENT_CLOSE: result := true;
385 SDL_WINDOWEVENT_FOCUS_LOST, SDL_WINDOWEVENT_MINIMIZED:
386 begin
387 e_UnpressAllKeys;
388 if gMuteWhenInactive then
389 e_MuteChannels(true);
390 {$IFDEF ENABLE_HOLMES}
391 if assigned(winBlurCB) then winBlurCB;
392 {$ENDIF}
393 end;
394 SDL_WINDOWEVENT_FOCUS_GAINED, SDL_WINDOWEVENT_MAXIMIZED, SDL_WINDOWEVENT_RESTORED:
395 begin
396 if ev.event = SDL_WINDOWEVENT_MAXIMIZED then
397 gRC_Maximized := true
398 else if ev.event = SDL_WINDOWEVENT_RESTORED then
399 gRC_Maximized := false;
400 e_MuteChannels(false);
401 {$IFDEF ENABLE_HOLMES}
402 if assigned(winFocusCB) then winFocusCB;
403 {$ENDIF}
404 end;
405 end
406 end;
408 procedure HandleKeyboard (var ev: TSDL_KeyboardEvent);
409 var down: Boolean; key: Integer;
410 begin
411 key := ev.keysym.scancode;
412 down := (ev.type_ = SDL_KEYDOWN);
413 if key = SDL_SCANCODE_AC_BACK then
414 key := SDL_SCANCODE_ESCAPE;
415 {$IFDEF ENABLE_HOLMES}
416 if fuiOnSDLEvent(PSDL_Event(@ev)^) then
417 begin
418 // event eaten, but...
419 if not down then e_KeyUpDown(key, false);
420 exit;
421 end;
422 {$ENDIF}
423 if ev._repeat = 0 then
424 begin
425 if g_dbg_input then
426 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down, key]);
427 e_KeyUpDown(key, down);
428 g_Console_ProcessBind(key, down);
429 end
430 else if gConsoleShow or gChatShow or (g_ActiveWindow <> nil) then
431 begin
432 KeyPress(key) // key repeat in menus and shit
433 end
434 end;
436 procedure HandleTextInput (var ev: TSDL_TextInputEvent);
437 var ch: UnicodeChar; sch: AnsiChar;
438 begin
439 Utf8ToUnicode(@ch, PChar(ev.text), 1);
440 sch := AnsiChar(wchar2win(ch));
441 if g_dbg_input then
442 e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev.text, Ord(ch), Ord(sch)]);
443 if IsValid1251(Word(ch)) and IsPrintable1251(ch) then
444 CharPress(sch);
445 end;
447 function sys_HandleInput (): Boolean;
448 var ev: TSDL_Event;
449 begin
450 result := false;
451 ZeroMemory(@ev, sizeof(ev));
452 while SDL_PollEvent(@ev) <> 0 do
453 begin
454 case ev.type_ of
455 SDL_QUITEV: result := true;
456 SDL_WINDOWEVENT: result := HandleWindow(ev.window);
457 SDL_KEYUP, SDL_KEYDOWN: HandleKeyboard(ev.key);
458 SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP: HandleJoyButton(ev.jbutton);
459 SDL_JOYAXISMOTION: HandleJoyAxis(ev.jaxis);
460 SDL_JOYHATMOTION: HandleJoyHat(ev.jhat);
461 SDL_JOYDEVICEADDED: HandleJoyAdd(ev.jdevice);
462 SDL_JOYDEVICEREMOVED: HandleJoyRemove(ev.jdevice);
463 SDL_TEXTINPUT: HandleTextInput(ev.text);
464 SDL_FINGERMOTION, SDL_FINGERDOWN, SDL_FINGERUP: g_Touch_HandleEvent(ev.tfinger);
465 {$IFDEF ENABLE_HOLMES}
466 SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION: fuiOnSDLEvent(ev);
467 {$ENDIF}
468 end
469 end
470 end;
472 procedure sys_RequestQuit;
473 var ev: TSDL_Event;
474 begin
475 ev.type_ := SDL_QUITEV;
476 SDL_PushEvent(@ev)
477 end;
479 (* --------- Init --------- *)
481 procedure sys_Init;
482 var flags: UInt32;
483 begin
484 e_WriteLog('Init SDL2', TMsgType.Notify);
485 {$IFDEF HEADLESS}
486 {$IFDEF USE_SDLMIXER}
487 flags := SDL_INIT_TIMER or SDL_INIT_AUDIO or $00004000;
488 {$ELSE}
489 flags := SDL_INIT_TIMER or $00004000;
490 {$ENDIF}
491 {$ELSE}
492 flags := SDL_INIT_JOYSTICK or SDL_INIT_TIMER or SDL_INIT_VIDEO;
493 {$ENDIF}
494 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, '0');
495 if SDL_Init(flags) <> 0 then
496 raise Exception.Create('SDL: Init failed: ' + SDL_GetError);
497 SDL_ShowCursor(SDL_DISABLE);
498 end;
500 procedure sys_Final;
501 begin
502 e_WriteLog('Releasing SDL2', TMsgType.Notify);
503 if context <> nil then
504 begin
505 {$IFDEF NOGL_INIT}
506 nogl_Quit;
507 {$ENDIF}
508 SDL_GL_DeleteContext(context);
509 context := nil;
510 end;
511 if window <> nil then
512 begin
513 SDL_DestroyWindow(window);
514 window := nil;
515 end;
516 SDL_Quit
517 end;
519 initialization
520 conRegVar('sdl2_display_index', @display, 'use display index as base', '');
521 end.