DEADSOFTWARE

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