1 (* Copyright (C) Doom 2D: Forever Developers
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, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
24 function SDLMain (): Integer;
25 function GetTimer (): Int64;
26 procedure ResetTimer ();
27 procedure PushExitEvent ();
28 function ProcessMessage (): Boolean;
29 procedure ReDrawWindow ();
30 procedure SwapBuffers ();
31 procedure Sleep (ms
: LongWord);
32 function GetDisplayModes (dbpp
: LongWord; var selres
: LongWord): SSArray
;
33 function g_Window_SetDisplay (preserveGL
: Boolean=false): Boolean;
34 function g_Window_SetSize (w
, h
: Word; fullscreen
: Boolean): Boolean;
35 procedure g_SetVSync (vsync
: Boolean);
37 procedure ProcessLoading (forceUpdate
: Boolean=false);
39 // returns `true` if quit event was received
40 function g_ProcessMessages (): Boolean;
44 gwin_dump_extensions
: Boolean = false;
45 gwin_has_stencil
: Boolean = false;
46 gwin_k8_enable_light_experiments
: Boolean = false;
47 g_dbg_aimline_on
: Boolean = false;
48 g_dbg_input
: Boolean = False;
54 {$IFDEF WINDOWS}Windows
,{$ENDIF}
55 {$INCLUDE ../nogl/noGLuses.inc}
56 {$IFDEF ENABLE_HOLMES}
57 g_holmes
, sdlcarcass
, fui_ctls
,
59 SysUtils
, Classes
, MAPDEF
, Math
,
60 SDL2
, e_graphics
, e_log
, e_texture
, g_main
,
61 g_console
, e_input
, g_options
, g_game
,
62 g_basic
, g_textures
, e_sound
, g_sound
, g_menu
, ENet
, g_net
,
63 g_map
, g_gfx
, g_monsters
, xprofiler
,
68 ProgressUpdateMSecs
= 1;//100;
71 h_Wnd
: PSDL_Window
= nil;
72 h_GL
: TSDL_GLContext
= nil;
73 Time
, Time_Delta
, Time_Old
: Int64;
75 {$IF not DEFINED(HEADLESS)}
77 wasFullscreen
: Boolean = true; // so we need to recreate the window
79 wNeedTimeReset
: Boolean = false;
80 wMinimized
: Boolean = false;
81 wMaximized
: Boolean = false;
82 wLoadingProgress
: Boolean = false;
83 wLoadingQuit
: Boolean = false;
85 ticksOverflow
: Int64 = -1;
86 lastTicks
: Uint32
= 0; // to detect overflow
88 JoystickHandle
: array [0..e_MaxJoys
- 1] of PSDL_Joystick
;
89 JoystickHatState
: array [0..e_MaxJoys
- 1, 0..e_MaxJoyHats
- 1, HAT_LEFT
..HAT_DOWN
] of Boolean;
90 JoystickZeroAxes
: array [0..e_MaxJoys
- 1, 0..e_MaxJoyAxes
- 1] of Integer;
92 procedure KillGLWindow (preserveGL
: Boolean);
94 {$IFDEF ENABLE_HOLMES}
95 if (h_GL
<> nil) and (not preserveGL
) then begin if (assigned(oglDeinitCB
)) then oglDeinitCB(); end;
97 if (h_Wnd
<> nil) then SDL_DestroyWindow(h_Wnd
);
98 if (h_GL
<> nil) and (not preserveGL
) then
109 SDL_GL_DeleteContext(h_GL
);
112 if (not preserveGL
) then h_GL
:= nil;
116 function g_Window_SetDisplay (preserveGL
: Boolean = false): Boolean;
117 {$IF not DEFINED(HEADLESS)}
119 mode
, cmode
: TSDL_DisplayMode
;
120 wFlags
: LongWord = 0;
124 {$IF not DEFINED(HEADLESS)}
127 e_WriteLog('Setting display mode...', TMsgType
.Notify
);
129 wFlags
:= SDL_WINDOW_OPENGL
{or SDL_WINDOW_RESIZABLE};
130 if gFullscreen
then wFlags
:= wFlags
{or SDL_WINDOW_FULLSCREEN} else wFlags
:= wFlags
or SDL_WINDOW_RESIZABLE
;
131 if (not gFullscreen
) and (not preserveGL
) and gWinMaximized
then wFlags
:= wFlags
or SDL_WINDOW_MAXIMIZED
else gWinMaximized
:= false;
135 mode
.w
:= gScreenWidth
;
136 mode
.h
:= gScreenHeight
;
138 mode
.refresh_rate
:= 0;
139 mode
.driverdata
:= nil;
140 if (SDL_GetClosestDisplayMode(0, @mode
, @cmode
) = nil) then
142 e_WriteLog('SDL: cannot find display mode for '+IntToStr(gScreenWidth
), TMsgType
.Notify
);
144 gScreenHeight
:= 600;
148 e_WriteLog('SDL: found display mode for '+IntToStr(gScreenWidth
)+'x'+IntToStr(gScreenHeight
)+': '+IntToStr(cmode
.w
)+'x'+IntToStr(cmode
.h
), TMsgType
.Notify
);
149 gScreenWidth
:= cmode
.w
;
150 gScreenHeight
:= cmode
.h
;
154 if (preserveGL
) and (h_Wnd
<> nil) and (not gFullscreen
) and (not wasFullscreen
) then
156 //SDL_SetWindowMaximumSize(h_Wnd, gScreenWidth, gScreenHeight);
157 //SDL_SetWindowDisplayMode(h_Wnd, @cmode);
158 if (wMaximized
) then SDL_RestoreWindow(h_Wnd
);
160 gWinMaximized
:= false;
161 SDL_SetWindowSize(h_Wnd
, gScreenWidth
, gScreenHeight
);
162 //SDL_SetWindowFullscreen(h_Wnd, SDL_WINDOW_FULLSCREEN);
163 //SDL_SetWindowFullscreen(h_Wnd, 0);
167 KillGLWindow(preserveGL
);
168 h_Wnd
:= SDL_CreateWindow(PChar(wTitle
), gWinRealPosX
, gWinRealPosY
, gScreenWidth
, gScreenHeight
, wFlags
);
170 SDL_SetWindowFullscreen(h_Wnd
, SDL_WINDOW_FULLSCREEN
);
171 if (h_Wnd
= nil) then exit
;
173 wasFullscreen
:= gFullscreen
;
175 SDL_GL_MakeCurrent(h_Wnd
, h_GL
);
176 SDL_ShowCursor(SDL_DISABLE
);
177 if (h_GL
<> nil) then g_SetVSync(gVSync
);
178 if (gFullscreen
) then
182 SDL_GetWindowSize(h_Wnd
, @nw
, @nh
);
183 if (nw
> 128) and (nh
> 128) then
185 e_WriteLog('SDL: fullscreen window got size '+IntToStr(nw
)+'x'+IntToStr(nh
)+': '+IntToStr(gScreenWidth
)+'x'+IntToStr(gScreenHeight
), TMsgType
.Notify
);
191 e_WriteLog('SDL: fullscreen window got invalid size: '+IntToStr(nw
)+'x'+IntToStr(nh
), TMsgType
.Notify
);
195 {$IFDEF ENABLE_HOLMES}
196 fuiScrWdt
:= gScreenWidth
;
197 fuiScrHgt
:= gScreenHeight
;
198 if (h_GL
<> nil) and (not preserveGL
) then begin if (assigned(oglInitCB
)) then oglInitCB(); end;
206 function GetDisplayModes (dbpp
: LongWord; var selres
: LongWord): SSArray
;
208 mode
: TSDL_DisplayMode
;
209 res
, i
, k
, n
, pw
, ph
: Integer;
211 SetLength(result
, 0);
212 {$IFDEF HEADLESS}exit
;{$ENDIF}
214 n
:= SDL_GetNumDisplayModes(0);
218 res
:= SDL_GetDisplayMode(0, i
, @mode
);
219 if res
< 0 then continue
;
220 if SDL_BITSPERPIXEL(mode
.format
) = gBPP
then continue
;
221 if (mode
.w
= pw
) and (mode
.h
= ph
) then continue
;
222 if (mode
.w
= gScreenWidth
) and (mode
.h
= gScreenHeight
) then
225 SetLength(result
, k
);
226 result
[k
-1] := IntToStr(mode
.w
) + 'x' + IntToStr(mode
.h
);
227 pw
:= mode
.w
; ph
:= mode
.h
230 e_WriteLog('SDL: Got ' + IntToStr(k
) + ' resolutions.', TMsgType
.Notify
);
234 procedure Sleep (ms
: LongWord);
240 procedure ChangeWindowSize (requested
: Boolean);
242 e_LogWritefln(' ChangeWindowSize: (ws=%dx%d) (ss=%dx%d)', [gWinSizeX
, gWinSizeY
, gScreenWidth
, gScreenHeight
]);
243 gWinSizeX
:= gScreenWidth
;
244 gWinSizeY
:= gScreenHeight
;
245 {$IF not DEFINED(HEADLESS)}
246 {$IFDEF ENABLE_HOLMES}
247 fuiScrWdt
:= gScreenWidth
;
248 fuiScrHgt
:= gScreenHeight
;
250 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
251 g_Game_SetupScreenSize();
252 {$IF DEFINED(ANDROID)}
253 (* This will fix menu reset on keyboard showing *)
259 g_Game_ClearLoading();
264 function g_Window_SetSize (w
, h
: Word; fullscreen
: Boolean): Boolean;
265 {$IF not DEFINED(HEADLESS)}
271 {$IF not DEFINED(HEADLESS)}
274 if (gScreenWidth
<> w
) or (gScreenHeight
<> h
) then
282 if (gFullscreen
<> fullscreen
) then
286 gFullscreen
:= fullscreen
;
292 g_Window_SetDisplay(preserve
);
293 ChangeWindowSize(true);
299 function WindowEventHandler (constref ev
: TSDL_WindowEvent
): Boolean;
301 wActivate
, wDeactivate
: Boolean;
305 wDeactivate
:= false;
308 SDL_WINDOWEVENT_MOVED
:
310 if not (gFullscreen
or gWinMaximized
) then
312 gWinRealPosX
:= ev
.data1
;
313 gWinRealPosY
:= ev
.data2
;
317 SDL_WINDOWEVENT_MINIMIZED
:
320 if not wMinimized
then
322 e_ResizeWindow(0, 0);
324 if g_debug_WinMsgs
then
326 g_Console_Add('Now minimized');
327 e_WriteLog('[DEBUG] WinMsgs: Now minimized', TMsgType
.Notify
);
333 SDL_WINDOWEVENT_RESIZED
:
335 e_LogWritefln('Resize: (os=%dx%d) (ns=%dx%d)', [gScreenWidth
, gScreenHeight
, Integer(ev
.data1
), Integer(ev
.data2
)]);
336 {if (gFullscreen) then
338 e_LogWriteln(' fullscreen fix applied.');
339 if (gScreenWidth <> ev.data1) or (gScreenHeight <> ev.data2) then
341 SDL_SetWindowSize(h_Wnd, gScreenWidth, gScreenHeight);
346 gScreenWidth
:= ev
.data1
;
347 gScreenHeight
:= ev
.data2
;
349 ChangeWindowSize(false);
351 if g_debug_WinMsgs
then
353 g_Console_Add('Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
));
354 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
), TMsgType
.Notify
);
358 SDL_WINDOWEVENT_EXPOSED
:
361 SDL_WINDOWEVENT_MAXIMIZED
:
366 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
370 if (not gWinMaximized
) and (not gFullscreen
) then
372 gWinMaximized
:= true;
373 if g_debug_WinMsgs
then
375 g_Console_Add('Now maximized');
376 e_WriteLog('[DEBUG] WinMsgs: Now maximized', TMsgType
.Notify
);
381 SDL_WINDOWEVENT_RESTORED
:
386 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
390 gWinMaximized
:= false;
391 if g_debug_WinMsgs
then
393 g_Console_Add('Now restored');
394 e_WriteLog('[DEBUG] WinMsgs: Now restored', TMsgType
.Notify
);
398 SDL_WINDOWEVENT_FOCUS_GAINED
:
401 //e_WriteLog('window gained focus!', MSG_NOTIFY);
404 SDL_WINDOWEVENT_FOCUS_LOST
:
408 //e_WriteLog('window lost focus!', MSG_NOTIFY);
416 e_WriteLog('deactivating window', TMsgType
.Notify
);
419 if gMuteWhenInactive
then
421 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
422 e_MuteChannels(true);
425 if g_debug_WinMsgs
then
427 g_Console_Add('Now inactive');
428 e_WriteLog('[DEBUG] WinMsgs: Now inactive', TMsgType
.Notify
);
433 {$IFDEF ENABLE_HOLMES}
434 if assigned(winBlurCB
) then winBlurCB();
438 else if wActivate
then
440 if not gWinActive
then
442 //e_WriteLog('activating window', MSG_NOTIFY);
444 if gMuteWhenInactive
then
446 //e_WriteLog('activating sounds', MSG_NOTIFY);
447 e_MuteChannels(false);
450 if g_debug_WinMsgs
then
452 g_Console_Add('Now active');
453 e_WriteLog('[DEBUG] WinMsgs: Now active', TMsgType
.Notify
);
458 {$IFDEF ENABLE_HOLMES}
459 if assigned(winFocusCB
) then winFocusCB();
466 function EventHandler (var ev
: TSDL_Event
): Boolean;
468 key
, keychr
, minuskey
: Word;
472 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
478 result
:= WindowEventHandler(ev
.window
);
482 if (gExit
<> EXIT_QUIT
) then
484 if not wLoadingProgress
then
491 wLoadingQuit
:= true;
497 SDL_KEYDOWN
, SDL_KEYUP
:
499 key
:= ev
.key
.keysym
.scancode
;
500 if key
= SDL_SCANCODE_AC_BACK
then
501 key
:= SDL_SCANCODE_ESCAPE
;
502 down
:= (ev
.type_
= SDL_KEYDOWN
);
503 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
504 if fuiOnSDLEvent(ev
) then
506 // event eaten, but...
507 if not down
then e_KeyUpDown(key
, false);
511 if ev
.key
._repeat
= 0 then
514 e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s', [down
, key
]);
515 e_KeyUpDown(key
, down
);
516 g_Console_ProcessBind(key
, down
);
518 else if gConsoleShow
or gChatShow
or (g_ActiveWindow
<> nil) then
520 // key repeat in menus and shit
525 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
:
526 if (ev
.jbutton
.which
< e_MaxJoys
) and (ev
.jbutton
.button
< e_MaxJoyBtns
) then
528 key
:= e_JoyButtonToKey(ev
.jbutton
.which
, ev
.jbutton
.button
);
529 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
531 e_LogWritefln('Input Debug: jbutton, joy=%s, button=%s, keycode=%s, press=%s', [ev
.jbutton
.which
, ev
.jbutton
.button
, key
, down
]);
532 e_KeyUpDown(key
, down
);
533 g_Console_ProcessBind(key
, down
);
539 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
540 e_LogWritefln('Input Debug: NOT IN RANGE! jbutton, joy=%s, button=%s, press=%s', [ev
.jbutton
.which
, ev
.jbutton
.button
, down
])
545 if (ev
.jaxis
.which
< e_MaxJoys
) and (ev
.jaxis
.axis
< e_MaxJoyAxes
) then
547 key
:= e_JoyAxisToKey(ev
.jaxis
.which
, ev
.jaxis
.axis
, AX_PLUS
);
548 minuskey
:= e_JoyAxisToKey(ev
.jaxis
.which
, ev
.jaxis
.axis
, AX_MINUS
);
551 e_LogWritefln('Input Debug: jaxis, joy=%s, axis=%s, value=%s, zeroaxes=%s, deadzone=%s', [ev
.jaxis
.which
, ev
.jaxis
.axis
, ev
.jaxis
.value
, JoystickZeroAxes
[ev
.jaxis
.which
, ev
.jaxis
.axis
], e_JoystickDeadzones
[ev
.jaxis
.which
]]);
553 if ev
.jaxis
.value
< JoystickZeroAxes
[ev
.jaxis
.which
, ev
.jaxis
.axis
] - e_JoystickDeadzones
[ev
.jaxis
.which
] then
555 if (e_KeyPressed(key
)) then
557 e_KeyUpDown(key
, False);
558 g_Console_ProcessBind(key
, False);
560 e_KeyUpDown(minuskey
, True);
561 g_Console_ProcessBind(minuskey
, True);
563 else if ev
.jaxis
.value
> JoystickZeroAxes
[ev
.jaxis
.which
, ev
.jaxis
.axis
] + e_JoystickDeadzones
[ev
.jaxis
.which
] then
565 if (e_KeyPressed(minuskey
)) then
567 e_KeyUpDown(minuskey
, False);
568 g_Console_ProcessBind(minuskey
, False);
570 e_KeyUpDown(key
, True);
571 g_Console_ProcessBind(key
, True);
575 if (e_KeyPressed(minuskey
)) then
577 e_KeyUpDown(minuskey
, False);
578 g_Console_ProcessBind(minuskey
, False);
580 if (e_KeyPressed(key
)) then
582 e_KeyUpDown(key
, False);
583 g_Console_ProcessBind(key
, False);
590 e_LogWritefln('Input Debug: NOT IN RANGE! jaxis, joy=%s, axis=%s, value=%s, zeroaxes=%s, deadzone=%s', [ev
.jaxis
.which
, ev
.jaxis
.axis
, ev
.jaxis
.value
, JoystickZeroAxes
[ev
.jaxis
.which
, ev
.jaxis
.axis
], e_JoystickDeadzones
[ev
.jaxis
.which
]])
594 if (ev
.jhat
.which
< e_MaxJoys
) and (ev
.jhat
.hat
< e_MaxJoyHats
) then
597 e_LogWritefln('Input Debug: jhat, joy=%s, hat=%s, value=%s', [ev
.jhat
.which
, ev
.jhat
.hat
, ev
.jhat
.value
]);
598 hat
[HAT_UP
] := LongBool(ev
.jhat
.value
and SDL_HAT_UP
);
599 hat
[HAT_DOWN
] := LongBool(ev
.jhat
.value
and SDL_HAT_DOWN
);
600 hat
[HAT_LEFT
] := LongBool(ev
.jhat
.value
and SDL_HAT_LEFT
);
601 hat
[HAT_RIGHT
] := LongBool(ev
.jhat
.value
and SDL_HAT_RIGHT
);
602 for i
:= HAT_LEFT
to HAT_DOWN
do
604 if JoystickHatState
[ev
.jhat
.which
, ev
.jhat
.hat
, i
] <> hat
[i
] then
607 key
:= e_JoyHatToKey(ev
.jhat
.which
, ev
.jhat
.hat
, i
);
608 e_KeyUpDown(key
, down
);
609 g_Console_ProcessBind(key
, down
);
612 JoystickHatState
[ev
.jhat
.which
, ev
.jhat
.hat
] := hat
617 e_LogWritefln('Input Debug: NOT IN RANGE! jhat, joy=%s, hat=%s, value=%s', [ev
.jhat
.which
, ev
.jhat
.hat
, ev
.jhat
.value
])
621 if (ev
.jdevice
.which
< e_MaxJoys
) then
623 JoystickHandle
[ev
.jdevice
.which
] := SDL_JoystickOpen(ev
.jdevice
.which
);
624 if JoystickHandle
[ev
.jdevice
.which
] <> nil then
626 e_LogWritefln('Added Joystick %s', [ev
.jdevice
.which
]);
627 e_JoystickAvailable
[ev
.jdevice
.which
] := True;
628 for i
:= 0 to Min(SDL_JoystickNumAxes(JoystickHandle
[ev
.jdevice
.which
]), e_MaxJoyAxes
) - 1 do
629 JoystickZeroAxes
[ev
.jdevice
.which
, i
] := SDL_JoystickGetAxis(JoystickHandle
[ev
.jdevice
.which
], i
)
632 e_LogWritefln('Warning! Failed to open Joystick %s', [ev
.jdevice
.which
])
636 e_LogWritefln('Warning! Added Joystick %s, but we support only <= %s', [ev
.jdevice
.which
, e_MaxJoys
])
639 SDL_JOYDEVICEREMOVED
:
641 e_LogWritefln('Removed Joystick %s', [ev
.jdevice
.which
]);
642 if (ev
.jdevice
.which
< e_MaxJoys
) then
644 e_JoystickAvailable
[ev
.jdevice
.which
] := False;
645 if JoystickHandle
[ev
.jdevice
.which
] <> nil then
646 SDL_JoystickClose(JoystickHandle
[ev
.jdevice
.which
]);
647 JoystickHandle
[ev
.jdevice
.which
] := nil
651 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
652 SDL_MOUSEBUTTONDOWN
, SDL_MOUSEBUTTONUP
, SDL_MOUSEWHEEL
, SDL_MOUSEMOTION
:
659 e_LogWritefln('Input Debug: text, text=%s', [ev
.text.text]);
660 Utf8ToUnicode(@uc
, PChar(ev
.text.text), 1);
662 if (keychr
> 127) then keychr
:= Word(wchar2win(WideChar(keychr
)));
663 if (keychr
> 0) and (keychr
<= 255) then CharPress(AnsiChar(keychr
));
666 SDL_FINGERMOTION
, SDL_FINGERDOWN
, SDL_FINGERUP
:
667 g_Touch_HandleEvent(ev
.tfinger
);
669 // other key presses and joysticks are handled in e_input
674 procedure SwapBuffers ();
676 {$IF not DEFINED(HEADLESS)}
677 SDL_GL_SwapWindow(h_Wnd
);
682 function CreateGLWindow (Title
: PChar): Boolean;
686 gWinSizeX
:= gScreenWidth
;
687 gWinSizeY
:= gScreenHeight
;
689 {$IF not DEFINED(HEADLESS)}
692 e_WriteLog('Creating window', TMsgType
.Notify
);
694 if not g_Window_SetDisplay() then
697 e_WriteLog('Window creation error (resolution not supported?)', TMsgType
.Fatal
);
701 {$IF not DEFINED(HEADLESS)}
702 h_GL
:= SDL_GL_CreateContext(h_Wnd
);
703 if (h_GL
= nil) then exit
;
704 {$IFDEF ENABLE_HOLMES}
705 fuiScrWdt
:= gScreenWidth
;
706 fuiScrHgt
:= gScreenHeight
;
708 SDL_GL_MakeCurrent(h_Wnd
, h_GL
);
710 if nanoGL_Init() = 0 then
713 e_WriteLog('nanoGL initialization error', TMsgType
.Fatal
);
720 {$IFDEF ENABLE_HOLMES}
721 if (assigned(oglInitCB
)) then oglInitCB();
723 if (h_GL
<> nil) then g_SetVSync(gVSync
);
726 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
734 // windoze sux; in headless mode `GetTickCount()` (and SDL) returns shit
735 function GetTimer (): Int64;
739 QueryPerformanceFrequency(F
);
740 QueryPerformanceCounter(C
);
741 result
:= Round(C
/F
*1000{000});
744 function GetTimer (): Int64;
750 if (ticksOverflow
= -1) then
757 if (lastTicks
> t
) then
759 // overflow, increment overflow ;-)
760 ticksOverflow
:= ticksOverflow
+(Int64($ffffffff)+Int64(1));
761 tt
:= (Int64($ffffffff)+Int64(1))+Int64(t
);
762 t
:= Uint32(tt
-lastTicks
);
766 result
:= ticksOverflow
+Int64(t
);
771 procedure ResetTimer ();
773 wNeedTimeReset
:= true;
777 procedure PushExitEvent ();
781 ev
.type_
:= SDL_QUITEV
;
787 prevLoadingUpdateTime
: UInt64 = 0;
789 procedure ProcessLoading (forceUpdate
: Boolean=false);
795 FillChar(ev
, sizeof(ev
), 0);
796 wLoadingProgress
:= true;
798 while (SDL_PollEvent(@ev
) > 0) do
801 if (ev
.type_
= SDL_QUITEV
) then break
;
805 if (ev
.type_
= SDL_QUITEV
) or (gExit
= EXIT_QUIT
) then
807 wLoadingProgress
:= false;
811 if not wMinimized
then
815 prevLoadingUpdateTime
:= getTimeMilli();
819 stt
:= getTimeMilli();
820 if (stt
< prevLoadingUpdateTime
) or (stt
-prevLoadingUpdateTime
>= ProgressUpdateMSecs
) then
822 prevLoadingUpdateTime
:= stt
;
829 DrawMenuBackground('INTER', ID
);
830 e_DarkenQuadWH(0, 0, gScreenWidth
, gScreenHeight
, 150);
839 if NetMode
= NET_SERVER
then
845 if (NetMode
= NET_CLIENT
) and (NetState
<> NET_STATE_AUTH
) then g_Net_Client_UpdateWhileLoading();
848 wLoadingProgress
:= false;
852 function g_ProcessMessages (): Boolean;
857 FillChar(ev
, SizeOf(ev
), 0);
858 while (SDL_PollEvent(@ev
) > 0) do
860 result
:= EventHandler(ev
);
861 if (ev
.type_
= SDL_QUITEV
) then exit
;
867 function ProcessMessage (): Boolean;
871 result
:= g_ProcessMessages();
874 Time_Delta
:= Time
-Time_Old
;
878 if wNeedTimeReset
then
881 wNeedTimeReset
:= false;
884 g_Map_ProfilersBegin();
885 g_Mons_ProfilersBegin();
887 t
:= Time_Delta
div 28;
893 if (NetMode
= NET_SERVER
) then g_Net_Host_Update()
894 else if (NetMode
= NET_CLIENT
) then g_Net_Client_Update();
900 if (NetMode
= NET_SERVER
) then g_Net_Host_Update()
901 else if (NetMode
= NET_CLIENT
) then g_Net_Client_Update();
904 g_Map_ProfilersEnd();
905 g_Mons_ProfilersEnd();
913 if (gExit
= EXIT_QUIT
) then
919 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
922 Time_Old
:= Time
-(Time_Delta
mod 28);
923 if (not wMinimized
) then
931 Sleep(1); // release time slice, so we won't eat 100% CPU
938 procedure ReDrawWindow ();
944 procedure g_SetVSync (vsync
: Boolean);
945 {$IF not DEFINED(HEADLESS)}
950 {$IF not DEFINED(HEADLESS)}
951 if vsync
then v
:= 1 else v
:= 0;
952 if (SDL_GL_SetSwapInterval(v
) <> 0) then
954 e_WriteLog('oops; can''t change vsync option, restart required', TMsgType
.Warning
);
958 if vsync
then e_WriteLog('VSync: ON', TMsgType
.Notify
) else e_WriteLog('VSync: OFF', TMsgType
.Notify
);
964 procedure InitOpenGL ();
966 {$IF not DEFINED(HEADLESS)}
968 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 1);
969 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
970 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK
, SDL_GL_CONTEXT_PROFILE_ES
);
972 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
973 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
974 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
975 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
976 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
977 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
978 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
979 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
985 function glHasExtension (const name
: AnsiString): Boolean;
990 extName
: ShortString;
993 if (Length(name
) = 0) then exit
;
994 exts
:= glGetString(GL_EXTENSIONS
);
995 if (exts
= nil) then exit
;
996 while (exts
[0] <> #0) and (exts
[0] = ' ') do Inc(exts
);
997 while (exts
[0] <> #0) do
999 if gwin_dump_extensions
then
1002 while (exts
[i
] <> #0) and (exts
[i
] <> ' ') do Inc(i
);
1005 e_WriteLog('FUUUUUUUUUUUUU', TMsgType
.Warning
);
1009 Move(exts
^, extName
[1], i
);
1010 extName
[0] := Char(i
);
1011 e_WriteLog(Format('EXT: %s', [extName
]), TMsgType
.Notify
);
1015 for i
:= 0 to length(name
)-1 do
1017 if (exts
[i
] = #0) then begin found
:= false; break
; end;
1018 if (exts
[i
] <> name
[i
+1]) then begin found
:= false; break
; end;
1020 if found
and ((exts
[Length(name
)] = #0) or (exts
[Length(name
)] = ' ')) then begin result
:= true; exit
; end;
1021 while (exts
[0] <> #0) and (exts
[0] <> ' ') do Inc(exts
);
1022 while (exts
[0] <> #0) and (exts
[0] = ' ') do Inc(exts
);
1027 function SDLMain (): Integer;
1030 {$IF not DEFINED(HEADLESS)}
1035 {$IFDEF ENABLE_HOLMES}
1041 e_NoGraphics
:= true;
1043 {$IFDEF ENABLE_HOLMES}
1044 if (not g_holmes_imfunctional
) then
1047 uiContext
.font
:= 'win14';
1053 while (idx
<= ParamCount
) do
1055 arg
:= ParamStr(idx
);
1057 if arg
= '--opengl-dump-exts' then gwin_dump_extensions
:= true;
1058 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
1059 if arg
= '--jah' then g_profile_history_size
:= 100;
1060 if arg
= '--no-particles' then gpart_dbg_enabled
:= false;
1061 if arg
= '--no-los' then gmon_dbg_los_enabled
:= false;
1063 if arg
= '--profile-render' then g_profile_frame_draw
:= true;
1064 if arg
= '--profile-coldet' then g_profile_collision
:= true;
1065 if arg
= '--profile-los' then g_profile_los
:= true;
1067 if arg
= '--no-part-phys' then gpart_dbg_phys_enabled
:= false;
1068 if arg
= '--no-part-physics' then gpart_dbg_phys_enabled
:= false;
1069 if arg
= '--no-particles-phys' then gpart_dbg_phys_enabled
:= false;
1070 if arg
= '--no-particles-physics' then gpart_dbg_phys_enabled
:= false;
1071 if arg
= '--no-particle-phys' then gpart_dbg_phys_enabled
:= false;
1072 if arg
= '--no-particle-physics' then gpart_dbg_phys_enabled
:= false;
1074 if arg
= '--debug-input' then g_dbg_input
:= True;
1076 {.$IF DEFINED(D2F_DEBUG)}
1077 if arg
= '--aimline' then g_dbg_aimline_on
:= true;
1080 {$IFDEF ENABLE_HOLMES}
1081 if arg
= '--holmes' then begin g_holmes_enabled
:= true; g_Game_SetDebugMode(); end;
1083 if (arg
= '--holmes-ui-scale') or (arg
= '-holmes-ui-scale') then
1085 if (idx
<= ParamCount
) then
1087 if not conParseFloat(fuiRenderScale
, ParamStr(idx
)) then fuiRenderScale
:= 1.0;
1092 if (arg
= '--holmes-font') or (arg
= '-holmes-font') then
1094 if (idx
<= ParamCount
) then
1097 val(ParamStr(idx
), itmp
, valres
);
1099 if (valres
= 0) and (not g_holmes_imfunctional
) then
1102 8: uiContext
.font
:= 'win8';
1103 14: uiContext
.font
:= 'win14';
1104 16: uiContext
.font
:= 'win16';
1117 if (arg
= '--game-scale') or (arg
= '-game-scale') then
1119 if (idx
<= ParamCount
) then
1121 if not conParseFloat(g_dbg_scale
, ParamStr(idx
)) then g_dbg_scale
:= 1.0;
1126 if (arg
= '--write-mapdef') or (arg
= '-write-mapdef') then
1128 mdfo
:= createDiskFile('mapdef.txt');
1129 mdfo
.WriteBuffer(defaultMapDef
[1], Length(defaultMapDef
));
1135 e_WriteLog('Initializing OpenGL', TMsgType
.Notify
);
1138 e_WriteLog('Creating GL window', TMsgType
.Notify
);
1139 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION
]))) then
1142 e_WriteLog('Unable to create GL window: ' + SDL_GetError(), TMsgType
.Fatal
);
1146 {EnumDisplayModes();}
1149 //gwin_k8_enable_light_experiments := false;
1150 gwin_has_stencil
:= false;
1151 glLegacyNPOT
:= false;
1152 gwin_dump_extensions
:= false;
1154 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE
, @ltmp
);
1155 e_LogWritefln('stencil buffer size: %s', [ltmp
]);
1156 gwin_has_stencil
:= (ltmp
> 0);
1158 if glHasExtension('GL_ARB_texture_non_power_of_two') or
1159 glHasExtension('GL_OES_texture_npot') then
1161 e_WriteLog('NPOT textures: YES', TMsgType
.Notify
);
1162 glLegacyNPOT
:= false;
1166 e_WriteLog('NPOT textures: NO', TMsgType
.Warning
);
1167 glLegacyNPOT
:= true;
1169 gwin_dump_extensions
:= false;
1173 Time_Old
:= GetTimer();
1176 if (ParamCount
> 0) then g_Game_Process_Params();
1179 if (not gGameOn
) and gAskLanguage
then g_Menu_AskLanguage();
1181 e_WriteLog('Entering the main loop', TMsgType
.Notify
);
1184 while not ProcessMessage() do begin end;
1187 KillGLWindow(false);
1194 conRegVar('d_input', @g_dbg_input
, '', '')