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;
53 {$IFDEF WINDOWS}Windows
,{$ENDIF}
54 {$INCLUDE ../nogl/noGLuses.inc}
55 {$IFDEF ENABLE_HOLMES}
56 g_holmes
, sdlcarcass
, fui_ctls
,
58 SysUtils
, Classes
, MAPDEF
, Math
,
59 SDL2
, e_graphics
, e_log
, e_texture
, g_main
,
60 g_console
, e_input
, g_options
, g_game
,
61 g_basic
, g_textures
, e_sound
, g_sound
, g_menu
, ENet
, g_net
,
62 g_map
, g_gfx
, g_monsters
, xprofiler
,
67 ProgressUpdateMSecs
= 1;//100;
70 h_Wnd
: PSDL_Window
= nil;
71 h_GL
: TSDL_GLContext
= nil;
72 Time
, Time_Delta
, Time_Old
: Int64;
74 {$IF not DEFINED(HEADLESS)}
76 wasFullscreen
: Boolean = true; // so we need to recreate the window
78 wNeedTimeReset
: Boolean = false;
79 wMinimized
: Boolean = false;
80 wMaximized
: Boolean = false;
81 wLoadingProgress
: Boolean = false;
82 wLoadingQuit
: Boolean = false;
84 ticksOverflow
: Int64 = -1;
85 lastTicks
: Uint32
= 0; // to detect overflow
87 JoystickHatState
: array [0..e_MaxJoys
, 0..e_MaxJoyHats
, HAT_LEFT
..HAT_DOWN
] of Boolean;
88 JoystickZeroAxes
: array [0..e_MaxJoys
, 0..e_MaxJoyAxes
] of Integer;
90 procedure KillGLWindow (preserveGL
: Boolean);
92 {$IFDEF ENABLE_HOLMES}
93 if (h_GL
<> nil) and (not preserveGL
) then begin if (assigned(oglDeinitCB
)) then oglDeinitCB(); end;
95 if (h_Wnd
<> nil) then SDL_DestroyWindow(h_Wnd
);
96 if (h_GL
<> nil) and (not preserveGL
) then
107 SDL_GL_DeleteContext(h_GL
);
110 if (not preserveGL
) then h_GL
:= nil;
114 function g_Window_SetDisplay (preserveGL
: Boolean = false): Boolean;
115 {$IF not DEFINED(HEADLESS)}
117 mode
, cmode
: TSDL_DisplayMode
;
118 wFlags
: LongWord = 0;
122 {$IF not DEFINED(HEADLESS)}
125 e_WriteLog('Setting display mode...', TMsgType
.Notify
);
127 wFlags
:= SDL_WINDOW_OPENGL
{or SDL_WINDOW_RESIZABLE};
128 if gFullscreen
then wFlags
:= wFlags
{or SDL_WINDOW_FULLSCREEN} else wFlags
:= wFlags
or SDL_WINDOW_RESIZABLE
;
129 if (not gFullscreen
) and (not preserveGL
) and gWinMaximized
then wFlags
:= wFlags
or SDL_WINDOW_MAXIMIZED
else gWinMaximized
:= false;
133 mode
.w
:= gScreenWidth
;
134 mode
.h
:= gScreenHeight
;
136 mode
.refresh_rate
:= 0;
137 mode
.driverdata
:= nil;
138 if (SDL_GetClosestDisplayMode(0, @mode
, @cmode
) = nil) then
140 e_WriteLog('SDL: cannot find display mode for '+IntToStr(gScreenWidth
), TMsgType
.Notify
);
142 gScreenHeight
:= 600;
146 e_WriteLog('SDL: found display mode for '+IntToStr(gScreenWidth
)+'x'+IntToStr(gScreenHeight
)+': '+IntToStr(cmode
.w
)+'x'+IntToStr(cmode
.h
), TMsgType
.Notify
);
147 gScreenWidth
:= cmode
.w
;
148 gScreenHeight
:= cmode
.h
;
152 if (preserveGL
) and (h_Wnd
<> nil) and (not gFullscreen
) and (not wasFullscreen
) then
154 //SDL_SetWindowMaximumSize(h_Wnd, gScreenWidth, gScreenHeight);
155 //SDL_SetWindowDisplayMode(h_Wnd, @cmode);
156 if (wMaximized
) then SDL_RestoreWindow(h_Wnd
);
158 gWinMaximized
:= false;
159 SDL_SetWindowSize(h_Wnd
, gScreenWidth
, gScreenHeight
);
160 //SDL_SetWindowFullscreen(h_Wnd, SDL_WINDOW_FULLSCREEN);
161 //SDL_SetWindowFullscreen(h_Wnd, 0);
165 KillGLWindow(preserveGL
);
166 h_Wnd
:= SDL_CreateWindow(PChar(wTitle
), gWinRealPosX
, gWinRealPosY
, gScreenWidth
, gScreenHeight
, wFlags
);
168 SDL_SetWindowFullscreen(h_Wnd
, SDL_WINDOW_FULLSCREEN
);
169 if (h_Wnd
= nil) then exit
;
171 wasFullscreen
:= gFullscreen
;
173 SDL_GL_MakeCurrent(h_Wnd
, h_GL
);
174 SDL_ShowCursor(SDL_DISABLE
);
175 if (h_GL
<> nil) then g_SetVSync(gVSync
);
176 if (gFullscreen
) then
180 SDL_GetWindowSize(h_Wnd
, @nw
, @nh
);
181 if (nw
> 128) and (nh
> 128) then
183 e_WriteLog('SDL: fullscreen window got size '+IntToStr(nw
)+'x'+IntToStr(nh
)+': '+IntToStr(gScreenWidth
)+'x'+IntToStr(gScreenHeight
), TMsgType
.Notify
);
189 e_WriteLog('SDL: fullscreen window got invalid size: '+IntToStr(nw
)+'x'+IntToStr(nh
), TMsgType
.Notify
);
193 {$IFDEF ENABLE_HOLMES}
194 fuiScrWdt
:= gScreenWidth
;
195 fuiScrHgt
:= gScreenHeight
;
196 if (h_GL
<> nil) and (not preserveGL
) then begin if (assigned(oglInitCB
)) then oglInitCB(); end;
204 function GetDisplayModes (dbpp
: LongWord; var selres
: LongWord): SSArray
;
206 mode
: TSDL_DisplayMode
;
207 res
, i
, k
, n
, pw
, ph
: Integer;
209 SetLength(result
, 0);
210 {$IFDEF HEADLESS}exit
;{$ENDIF}
212 n
:= SDL_GetNumDisplayModes(0);
216 res
:= SDL_GetDisplayMode(0, i
, @mode
);
217 if res
< 0 then continue
;
218 if SDL_BITSPERPIXEL(mode
.format
) = gBPP
then continue
;
219 if (mode
.w
= pw
) and (mode
.h
= ph
) then continue
;
220 if (mode
.w
= gScreenWidth
) and (mode
.h
= gScreenHeight
) then
223 SetLength(result
, k
);
224 result
[k
-1] := IntToStr(mode
.w
) + 'x' + IntToStr(mode
.h
);
225 pw
:= mode
.w
; ph
:= mode
.h
228 e_WriteLog('SDL: Got ' + IntToStr(k
) + ' resolutions.', TMsgType
.Notify
);
232 procedure Sleep (ms
: LongWord);
238 procedure ChangeWindowSize (requested
: Boolean);
240 e_LogWritefln(' ChangeWindowSize: (ws=%dx%d) (ss=%dx%d)', [gWinSizeX
, gWinSizeY
, gScreenWidth
, gScreenHeight
]);
241 gWinSizeX
:= gScreenWidth
;
242 gWinSizeY
:= gScreenHeight
;
243 {$IF not DEFINED(HEADLESS)}
244 {$IFDEF ENABLE_HOLMES}
245 fuiScrWdt
:= gScreenWidth
;
246 fuiScrHgt
:= gScreenHeight
;
248 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
249 g_Game_SetupScreenSize();
250 {$IF DEFINED(ANDROID)}
251 (* This will fix menu reset on keyboard showing *)
257 g_Game_ClearLoading();
262 function g_Window_SetSize (w
, h
: Word; fullscreen
: Boolean): Boolean;
263 {$IF not DEFINED(HEADLESS)}
269 {$IF not DEFINED(HEADLESS)}
272 if (gScreenWidth
<> w
) or (gScreenHeight
<> h
) then
280 if (gFullscreen
<> fullscreen
) then
284 gFullscreen
:= fullscreen
;
290 g_Window_SetDisplay(preserve
);
291 ChangeWindowSize(true);
297 function WindowEventHandler (constref ev
: TSDL_WindowEvent
): Boolean;
299 wActivate
, wDeactivate
: Boolean;
303 wDeactivate
:= false;
306 SDL_WINDOWEVENT_MOVED
:
308 if not (gFullscreen
or gWinMaximized
) then
310 gWinRealPosX
:= ev
.data1
;
311 gWinRealPosY
:= ev
.data2
;
315 SDL_WINDOWEVENT_MINIMIZED
:
318 if not wMinimized
then
320 e_ResizeWindow(0, 0);
322 if g_debug_WinMsgs
then
324 g_Console_Add('Now minimized');
325 e_WriteLog('[DEBUG] WinMsgs: Now minimized', TMsgType
.Notify
);
331 SDL_WINDOWEVENT_RESIZED
:
333 e_LogWritefln('Resize: (os=%dx%d) (ns=%dx%d)', [gScreenWidth
, gScreenHeight
, Integer(ev
.data1
), Integer(ev
.data2
)]);
334 {if (gFullscreen) then
336 e_LogWriteln(' fullscreen fix applied.');
337 if (gScreenWidth <> ev.data1) or (gScreenHeight <> ev.data2) then
339 SDL_SetWindowSize(h_Wnd, gScreenWidth, gScreenHeight);
344 gScreenWidth
:= ev
.data1
;
345 gScreenHeight
:= ev
.data2
;
347 ChangeWindowSize(false);
349 if g_debug_WinMsgs
then
351 g_Console_Add('Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
));
352 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
), TMsgType
.Notify
);
356 SDL_WINDOWEVENT_EXPOSED
:
359 SDL_WINDOWEVENT_MAXIMIZED
:
364 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
368 if (not gWinMaximized
) and (not gFullscreen
) then
370 gWinMaximized
:= true;
371 if g_debug_WinMsgs
then
373 g_Console_Add('Now maximized');
374 e_WriteLog('[DEBUG] WinMsgs: Now maximized', TMsgType
.Notify
);
379 SDL_WINDOWEVENT_RESTORED
:
384 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
388 gWinMaximized
:= false;
389 if g_debug_WinMsgs
then
391 g_Console_Add('Now restored');
392 e_WriteLog('[DEBUG] WinMsgs: Now restored', TMsgType
.Notify
);
396 SDL_WINDOWEVENT_FOCUS_GAINED
:
399 //e_WriteLog('window gained focus!', MSG_NOTIFY);
402 SDL_WINDOWEVENT_FOCUS_LOST
:
406 //e_WriteLog('window lost focus!', MSG_NOTIFY);
414 e_WriteLog('deactivating window', TMsgType
.Notify
);
417 if gMuteWhenInactive
then
419 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
420 e_MuteChannels(true);
423 if g_debug_WinMsgs
then
425 g_Console_Add('Now inactive');
426 e_WriteLog('[DEBUG] WinMsgs: Now inactive', TMsgType
.Notify
);
431 {$IFDEF ENABLE_HOLMES}
432 if assigned(winBlurCB
) then winBlurCB();
436 else if wActivate
then
438 if not gWinActive
then
440 //e_WriteLog('activating window', MSG_NOTIFY);
442 if gMuteWhenInactive
then
444 //e_WriteLog('activating sounds', MSG_NOTIFY);
445 e_MuteChannels(false);
448 if g_debug_WinMsgs
then
450 g_Console_Add('Now active');
451 e_WriteLog('[DEBUG] WinMsgs: Now active', TMsgType
.Notify
);
456 {$IFDEF ENABLE_HOLMES}
457 if assigned(winFocusCB
) then winFocusCB();
464 function EventHandler (var ev
: TSDL_Event
): Boolean;
466 key
, keychr
, minuskey
: Word;
470 hat
: array [HAT_LEFT
..HAT_DOWN
] of Boolean;
477 result
:= WindowEventHandler(ev
.window
);
481 if (gExit
<> EXIT_QUIT
) then
483 if not wLoadingProgress
then
490 wLoadingQuit
:= true;
496 SDL_KEYDOWN
, SDL_KEYUP
:
498 key
:= ev
.key
.keysym
.scancode
;
499 if key
= SDL_SCANCODE_AC_BACK
then
500 key
:= SDL_SCANCODE_ESCAPE
;
501 down
:= (ev
.type_
= SDL_KEYDOWN
);
502 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
503 if fuiOnSDLEvent(ev
) then
505 // event eaten, but...
506 if not down
then e_KeyUpDown(key
, false);
510 if ev
.key
._repeat
= 0 then
512 e_KeyUpDown(key
, down
);
513 g_Console_ProcessBind(key
, down
)
515 if down
then KeyPress(key
);
518 SDL_JOYBUTTONDOWN
, SDL_JOYBUTTONUP
:
519 if (ev
.jbutton
.which
< e_MaxJoys
) and (ev
.jbutton
.button
< e_MaxJoyBtns
) then
521 key
:= e_JoyButtonToKey(ev
.jbutton
.which
, ev
.jbutton
.button
);
522 down
:= ev
.type_
= SDL_JOYBUTTONDOWN
;
523 e_KeyUpDown(key
, down
);
524 g_Console_ProcessBind(key
, down
);
525 if down
then KeyPress(key
)
529 if (ev
.jaxis
.which
< e_MaxJoys
) and (ev
.jaxis
.axis
< e_MaxJoyAxes
) then
531 key
:= e_JoyAxisToKey(ev
.jaxis
.which
, ev
.jaxis
.axis
, AX_PLUS
);
532 minuskey
:= e_JoyAxisToKey(ev
.jaxis
.which
, ev
.jaxis
.axis
, AX_MINUS
);
534 if ev
.jaxis
.value
< JoystickZeroAxes
[ev
.jaxis
.which
, ev
.jaxis
.axis
] - e_JoystickDeadzones
[ev
.jaxis
.which
] then
536 if (e_KeyPressed(key
)) then
538 e_KeyUpDown(key
, False);
539 g_Console_ProcessBind(key
, False);
541 e_KeyUpDown(minuskey
, True);
542 g_Console_ProcessBind(minuskey
, True);
545 else if ev
.jaxis
.value
> JoystickZeroAxes
[ev
.jaxis
.which
, ev
.jaxis
.axis
] + e_JoystickDeadzones
[ev
.jaxis
.which
] then
547 if (e_KeyPressed(minuskey
)) then
549 e_KeyUpDown(minuskey
, False);
550 g_Console_ProcessBind(minuskey
, False);
552 e_KeyUpDown(key
, True);
553 g_Console_ProcessBind(key
, True);
558 if (e_KeyPressed(minuskey
)) then
560 e_KeyUpDown(minuskey
, False);
561 g_Console_ProcessBind(minuskey
, False);
563 if (e_KeyPressed(key
)) then
565 e_KeyUpDown(key
, False);
566 g_Console_ProcessBind(key
, False);
572 if (ev
.jhat
.which
< e_MaxJoys
) and (ev
.jhat
.hat
< e_MaxJoyHats
) then
574 hat
[HAT_UP
] := LongBool(ev
.jhat
.value
and SDL_HAT_UP
);
575 hat
[HAT_DOWN
] := LongBool(ev
.jhat
.value
and SDL_HAT_DOWN
);
576 hat
[HAT_LEFT
] := LongBool(ev
.jhat
.value
and SDL_HAT_LEFT
);
577 hat
[HAT_RIGHT
] := LongBool(ev
.jhat
.value
and SDL_HAT_RIGHT
);
578 for i
:= HAT_LEFT
to HAT_DOWN
do
580 if JoystickHatState
[ev
.jhat
.which
, ev
.jhat
.hat
, i
] <> hat
[i
] then
583 key
:= e_JoyHatToKey(ev
.jhat
.which
, ev
.jhat
.hat
, i
);
584 e_KeyUpDown(key
, down
);
585 g_Console_ProcessBind(key
, down
);
586 if down
then KeyPress(key
)
589 JoystickHatState
[ev
.jhat
.which
, ev
.jhat
.hat
] := hat
593 if (ev
.jdevice
.which
< e_MaxJoys
) then
595 joy
:= SDL_JoystickOpen(ev
.jdevice
.which
);
597 e_LogWritefln('Added Joystick %s', [ev
.jdevice
.which
]);
598 e_JoystickAvailable
[ev
.jdevice
.which
] := True;
599 for i
:= 0 to Min(SDL_JoystickNumAxes(joy
), e_MaxJoyAxes
) do
600 JoystickZeroAxes
[ev
.jdevice
.which
, i
] := SDL_JoystickGetAxis(joy
, i
);
601 SDL_JoystickClose(joy
)
604 SDL_JOYDEVICEREMOVED
:
605 if (ev
.jdevice
.which
< e_MaxJoys
) then
607 e_JoystickAvailable
[ev
.jdevice
.which
] := False;
608 e_LogWritefln('Removed Joystick %s', [ev
.jdevice
.which
])
611 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
612 SDL_MOUSEBUTTONDOWN
, SDL_MOUSEBUTTONUP
, SDL_MOUSEWHEEL
, SDL_MOUSEMOTION
:
618 Utf8ToUnicode(@uc
, PChar(ev
.text.text), 1);
620 if (keychr
> 127) then keychr
:= Word(wchar2win(WideChar(keychr
)));
621 if (keychr
> 0) and (keychr
<= 255) then CharPress(AnsiChar(keychr
));
624 SDL_FINGERMOTION
, SDL_FINGERDOWN
, SDL_FINGERUP
:
625 g_Touch_HandleEvent(ev
.tfinger
);
627 // other key presses and joysticks are handled in e_input
632 procedure SwapBuffers ();
634 {$IF not DEFINED(HEADLESS)}
635 SDL_GL_SwapWindow(h_Wnd
);
640 function CreateGLWindow (Title
: PChar): Boolean;
644 gWinSizeX
:= gScreenWidth
;
645 gWinSizeY
:= gScreenHeight
;
647 {$IF not DEFINED(HEADLESS)}
650 e_WriteLog('Creating window', TMsgType
.Notify
);
652 if not g_Window_SetDisplay() then
655 e_WriteLog('Window creation error (resolution not supported?)', TMsgType
.Fatal
);
659 {$IF not DEFINED(HEADLESS)}
660 h_GL
:= SDL_GL_CreateContext(h_Wnd
);
661 if (h_GL
= nil) then exit
;
662 {$IFDEF ENABLE_HOLMES}
663 fuiScrWdt
:= gScreenWidth
;
664 fuiScrHgt
:= gScreenHeight
;
666 SDL_GL_MakeCurrent(h_Wnd
, h_GL
);
668 if nanoGL_Init() = 0 then
671 e_WriteLog('nanoGL initialization error', TMsgType
.Fatal
);
678 {$IFDEF ENABLE_HOLMES}
679 if (assigned(oglInitCB
)) then oglInitCB();
681 if (h_GL
<> nil) then g_SetVSync(gVSync
);
684 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
692 // windoze sux; in headless mode `GetTickCount()` (and SDL) returns shit
693 function GetTimer (): Int64;
697 QueryPerformanceFrequency(F
);
698 QueryPerformanceCounter(C
);
699 result
:= Round(C
/F
*1000{000});
702 function GetTimer (): Int64;
708 if (ticksOverflow
= -1) then
715 if (lastTicks
> t
) then
717 // overflow, increment overflow ;-)
718 ticksOverflow
:= ticksOverflow
+(Int64($ffffffff)+Int64(1));
719 tt
:= (Int64($ffffffff)+Int64(1))+Int64(t
);
720 t
:= Uint32(tt
-lastTicks
);
724 result
:= ticksOverflow
+Int64(t
);
729 procedure ResetTimer ();
731 wNeedTimeReset
:= true;
735 procedure PushExitEvent ();
739 ev
.type_
:= SDL_QUITEV
;
745 prevLoadingUpdateTime
: UInt64 = 0;
747 procedure ProcessLoading (forceUpdate
: Boolean=false);
753 FillChar(ev
, sizeof(ev
), 0);
754 wLoadingProgress
:= true;
756 while (SDL_PollEvent(@ev
) > 0) do
759 if (ev
.type_
= SDL_QUITEV
) then break
;
763 if (ev
.type_
= SDL_QUITEV
) or (gExit
= EXIT_QUIT
) then
765 wLoadingProgress
:= false;
769 if not wMinimized
then
773 prevLoadingUpdateTime
:= getTimeMilli();
777 stt
:= getTimeMilli();
778 if (stt
< prevLoadingUpdateTime
) or (stt
-prevLoadingUpdateTime
>= ProgressUpdateMSecs
) then
780 prevLoadingUpdateTime
:= stt
;
787 if g_Texture_Get('INTER', ID
) then
789 e_DrawSize(ID
, 0, 0, 0, false, false, gScreenWidth
, gScreenHeight
);
790 e_DarkenQuadWH(0, 0, gScreenWidth
, gScreenHeight
, 150);
794 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
804 if NetMode
= NET_SERVER
then
810 if (NetMode
= NET_CLIENT
) and (NetState
<> NET_STATE_AUTH
) then g_Net_Client_UpdateWhileLoading();
813 wLoadingProgress
:= false;
817 function g_ProcessMessages (): Boolean;
822 FillChar(ev
, SizeOf(ev
), 0);
823 while (SDL_PollEvent(@ev
) > 0) do
825 result
:= EventHandler(ev
);
826 if (ev
.type_
= SDL_QUITEV
) then exit
;
832 function ProcessMessage (): Boolean;
836 result
:= g_ProcessMessages();
839 Time_Delta
:= Time
-Time_Old
;
843 if wNeedTimeReset
then
846 wNeedTimeReset
:= false;
849 g_Map_ProfilersBegin();
850 g_Mons_ProfilersBegin();
852 t
:= Time_Delta
div 28;
858 if (NetMode
= NET_SERVER
) then g_Net_Host_Update()
859 else if (NetMode
= NET_CLIENT
) then g_Net_Client_Update();
865 if (NetMode
= NET_SERVER
) then g_Net_Host_Update()
866 else if (NetMode
= NET_CLIENT
) then g_Net_Client_Update();
869 g_Map_ProfilersEnd();
870 g_Mons_ProfilersEnd();
878 if (gExit
= EXIT_QUIT
) then
884 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
887 Time_Old
:= Time
-(Time_Delta
mod 28);
888 if (not wMinimized
) then
896 Sleep(1); // release time slice, so we won't eat 100% CPU
903 procedure ReDrawWindow ();
909 procedure g_SetVSync (vsync
: Boolean);
910 {$IF not DEFINED(HEADLESS)}
915 {$IF not DEFINED(HEADLESS)}
916 if vsync
then v
:= 1 else v
:= 0;
917 if (SDL_GL_SetSwapInterval(v
) <> 0) then
919 e_WriteLog('oops; can''t change vsync option, restart required', TMsgType
.Warning
);
923 if vsync
then e_WriteLog('VSync: ON', TMsgType
.Notify
) else e_WriteLog('VSync: OFF', TMsgType
.Notify
);
929 procedure InitOpenGL ();
931 {$IF not DEFINED(HEADLESS)}
933 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 1);
934 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
935 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK
, SDL_GL_CONTEXT_PROFILE_ES
);
937 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
938 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
939 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
940 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
941 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
942 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
943 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
944 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
950 function glHasExtension (const name
: AnsiString): Boolean;
955 extName
: ShortString;
958 if (Length(name
) = 0) then exit
;
959 exts
:= glGetString(GL_EXTENSIONS
);
960 if (exts
= nil) then exit
;
961 while (exts
[0] <> #0) and (exts
[0] = ' ') do Inc(exts
);
962 while (exts
[0] <> #0) do
964 if gwin_dump_extensions
then
967 while (exts
[i
] <> #0) and (exts
[i
] <> ' ') do Inc(i
);
970 e_WriteLog('FUUUUUUUUUUUUU', TMsgType
.Warning
);
974 Move(exts
^, extName
[1], i
);
975 extName
[0] := Char(i
);
976 e_WriteLog(Format('EXT: %s', [extName
]), TMsgType
.Notify
);
980 for i
:= 0 to length(name
)-1 do
982 if (exts
[i
] = #0) then begin found
:= false; break
; end;
983 if (exts
[i
] <> name
[i
+1]) then begin found
:= false; break
; end;
985 if found
and ((exts
[Length(name
)] = #0) or (exts
[Length(name
)] = ' ')) then begin result
:= true; exit
; end;
986 while (exts
[0] <> #0) and (exts
[0] <> ' ') do Inc(exts
);
987 while (exts
[0] <> #0) and (exts
[0] = ' ') do Inc(exts
);
992 function SDLMain (): Integer;
995 {$IF not DEFINED(HEADLESS)}
1000 {$IFDEF ENABLE_HOLMES}
1006 e_NoGraphics
:= true;
1008 {$IFDEF ENABLE_HOLMES}
1009 if (not g_holmes_imfunctional
) then
1012 uiContext
.font
:= 'win14';
1018 while (idx
<= ParamCount
) do
1020 arg
:= ParamStr(idx
);
1022 if arg
= '--opengl-dump-exts' then gwin_dump_extensions
:= true;
1023 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
1024 if arg
= '--jah' then g_profile_history_size
:= 100;
1025 if arg
= '--no-particles' then gpart_dbg_enabled
:= false;
1026 if arg
= '--no-los' then gmon_dbg_los_enabled
:= false;
1028 if arg
= '--profile-render' then g_profile_frame_draw
:= true;
1029 if arg
= '--profile-coldet' then g_profile_collision
:= true;
1030 if arg
= '--profile-los' then g_profile_los
:= true;
1032 if arg
= '--no-part-phys' then gpart_dbg_phys_enabled
:= false;
1033 if arg
= '--no-part-physics' then gpart_dbg_phys_enabled
:= false;
1034 if arg
= '--no-particles-phys' then gpart_dbg_phys_enabled
:= false;
1035 if arg
= '--no-particles-physics' then gpart_dbg_phys_enabled
:= false;
1036 if arg
= '--no-particle-phys' then gpart_dbg_phys_enabled
:= false;
1037 if arg
= '--no-particle-physics' then gpart_dbg_phys_enabled
:= false;
1039 {.$IF DEFINED(D2F_DEBUG)}
1040 if arg
= '--aimline' then g_dbg_aimline_on
:= true;
1043 {$IFDEF ENABLE_HOLMES}
1044 if arg
= '--holmes' then begin g_holmes_enabled
:= true; g_Game_SetDebugMode(); end;
1046 if (arg
= '--holmes-ui-scale') or (arg
= '-holmes-ui-scale') then
1048 if (idx
<= ParamCount
) then
1050 if not conParseFloat(fuiRenderScale
, ParamStr(idx
)) then fuiRenderScale
:= 1.0;
1055 if (arg
= '--holmes-font') or (arg
= '-holmes-font') then
1057 if (idx
<= ParamCount
) then
1060 val(ParamStr(idx
), itmp
, valres
);
1062 if (valres
= 0) and (not g_holmes_imfunctional
) then
1065 8: uiContext
.font
:= 'win8';
1066 14: uiContext
.font
:= 'win14';
1067 16: uiContext
.font
:= 'win16';
1080 if (arg
= '--game-scale') or (arg
= '-game-scale') then
1082 if (idx
<= ParamCount
) then
1084 if not conParseFloat(g_dbg_scale
, ParamStr(idx
)) then g_dbg_scale
:= 1.0;
1089 if (arg
= '--write-mapdef') or (arg
= '-write-mapdef') then
1091 mdfo
:= createDiskFile('mapdef.txt');
1092 mdfo
.WriteBuffer(defaultMapDef
[1], Length(defaultMapDef
));
1098 e_WriteLog('Initializing OpenGL', TMsgType
.Notify
);
1101 e_WriteLog('Creating GL window', TMsgType
.Notify
);
1102 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION
]))) then
1105 e_WriteLog('Unable to create GL window: ' + SDL_GetError(), TMsgType
.Fatal
);
1109 {EnumDisplayModes();}
1112 //gwin_k8_enable_light_experiments := false;
1113 gwin_has_stencil
:= false;
1114 glLegacyNPOT
:= false;
1115 gwin_dump_extensions
:= false;
1117 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE
, @ltmp
);
1118 e_LogWritefln('stencil buffer size: %s', [ltmp
]);
1119 gwin_has_stencil
:= (ltmp
> 0);
1121 if glHasExtension('GL_ARB_texture_non_power_of_two') or
1122 glHasExtension('GL_OES_texture_npot') then
1124 e_WriteLog('NPOT textures: YES', TMsgType
.Notify
);
1125 glLegacyNPOT
:= false;
1129 e_WriteLog('NPOT textures: NO', TMsgType
.Warning
);
1130 glLegacyNPOT
:= true;
1132 gwin_dump_extensions
:= false;
1136 Time_Old
:= GetTimer();
1139 if (ParamCount
> 0) then g_Game_Process_Params();
1142 if (not gGameOn
) and gAskLanguage
then g_Menu_AskLanguage();
1144 e_WriteLog('Entering the main loop', TMsgType
.Notify
);
1147 while not ProcessMessage() do begin end;
1150 KillGLWindow(false);