DEADSOFTWARE

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