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 function CreateGLWindow (Title
: PChar): Boolean;
28 procedure KillGLWindow ();
29 procedure PushExitEvent ();
30 function ProcessMessage (): Boolean;
31 procedure ReDrawWindow ();
32 procedure SwapBuffers ();
33 procedure Sleep (ms
: LongWord);
34 function GetDisplayModes (dbpp
: LongWord; var selres
: LongWord): SSArray
;
35 function g_Window_SetDisplay (preserveGL
: Boolean=false): Boolean;
36 function g_Window_SetSize (w
, h
: Word; fullscreen
: Boolean): Boolean;
38 procedure ProcessLoading (forceUpdate
: Boolean=false);
40 // returns `true` if quit event was received
41 function g_ProcessMessages (): Boolean;
45 gwin_dump_extensions
: Boolean = false;
46 gwin_has_stencil
: Boolean = false;
47 gwin_k8_enable_light_experiments
: Boolean = false;
48 g_dbg_aimline_on
: Boolean = false;
54 {$IFDEF WINDOWS}Windows
,{$ENDIF}
55 SysUtils
, Classes
, MAPDEF
,
56 SDL2
, GL
, GLExt
, e_graphics
, e_log
, e_texture
, g_main
,
57 g_console
, e_input
, g_options
, g_game
,
58 g_basic
, g_textures
, e_sound
, g_sound
, g_menu
, ENet
, g_net
,
59 g_map
, g_gfx
, g_monsters
, g_holmes
, xprofiler
,
64 ProgressUpdateMSecs
= 100;
67 h_Wnd
: PSDL_Window
= nil;
68 h_GL
: TSDL_GLContext
= nil;
69 Time
, Time_Delta
, Time_Old
: Int64;
71 {$IF not DEFINED(HEADLESS)}
74 wNeedTimeReset
: Boolean = false;
75 wMinimized
: Boolean = false;
76 wLoadingProgress
: Boolean = false;
77 wLoadingQuit
: Boolean = false;
79 ticksOverflow
: Int64 = -1;
80 lastTicks
: Uint32
= 0; // to detect overflow
84 procedure KillGLWindow ();
86 if (h_GL
<> nil) then begin if (assigned(oglDeinitCB
)) then oglDeinitCB(); end;
87 if (h_Wnd
<> nil) then SDL_DestroyWindow(h_Wnd
);
88 if (h_GL
<> nil) then SDL_GL_DeleteContext(h_GL
);
94 function g_Window_SetDisplay (preserveGL
: Boolean = false): Boolean;
95 {$IF not DEFINED(HEADLESS)}
97 mode
, cmode
: TSDL_DisplayMode
;
101 {$IF not DEFINED(HEADLESS)}
104 e_WriteLog('Setting display mode...', TMsgType
.Notify
);
106 wFlags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
107 if gFullscreen
then wFlags
:= wFlags
or SDL_WINDOW_FULLSCREEN
;
108 if gWinMaximized
then wFlags
:= wFlags
or SDL_WINDOW_MAXIMIZED
;
114 mode
.w
:= gScreenWidth
;
115 mode
.h
:= gScreenHeight
;
117 mode
.refresh_rate
:= 0;
118 mode
.driverdata
:= nil;
119 if (SDL_GetClosestDisplayMode(0, @mode
, @cmode
) = nil) then
122 gScreenHeight
:= 600;
126 gScreenWidth
:= cmode
.w
;
127 gScreenHeight
:= cmode
.h
;
131 h_Wnd
:= SDL_CreateWindow(PChar(wTitle
), gWinRealPosX
, gWinRealPosY
, gScreenWidth
, gScreenHeight
, wFlags
);
132 if (h_Wnd
= nil) then exit
;
134 SDL_GL_MakeCurrent(h_Wnd
, h_GL
);
135 SDL_ShowCursor(SDL_DISABLE
);
136 if (h_GL
<> nil) then begin if (assigned(oglInitCB
)) then oglInitCB(); end;
143 function GetDisplayModes (dbpp
: LongWord; var selres
: LongWord): SSArray
;
145 mode
: TSDL_DisplayMode
;
146 res
, i
, k
, n
, pw
, ph
: Integer;
148 SetLength(result
, 0);
149 {$IFDEF HEADLESS}exit
;{$ENDIF}
151 n
:= SDL_GetNumDisplayModes(0);
155 res
:= SDL_GetDisplayMode(0, i
, @mode
);
156 if res
< 0 then continue
;
157 if SDL_BITSPERPIXEL(mode
.format
) = gBPP
then continue
;
158 if (mode
.w
= pw
) and (mode
.h
= ph
) then continue
;
159 if (mode
.w
= gScreenWidth
) and (mode
.h
= gScreenHeight
) then
162 SetLength(result
, k
);
163 result
[k
-1] := IntToStr(mode
.w
) + 'x' + IntToStr(mode
.h
);
164 pw
:= mode
.w
; ph
:= mode
.h
167 e_WriteLog('SDL: Got ' + IntToStr(k
) + ' resolutions.', TMsgType
.Notify
);
171 procedure Sleep (ms
: LongWord);
177 procedure ChangeWindowSize ();
179 gWinSizeX
:= gScreenWidth
;
180 gWinSizeY
:= gScreenHeight
;
181 {$IF not DEFINED(HEADLESS)}
182 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
183 g_Game_SetupScreenSize();
185 g_Game_ClearLoading();
190 function g_Window_SetSize (w
, h
: Word; fullscreen
: Boolean): Boolean;
191 {$IF not DEFINED(HEADLESS)}
197 {$IF not DEFINED(HEADLESS)}
200 if (gScreenWidth
<> w
) or (gScreenHeight
<> h
) then
207 if (gFullscreen
<> fullscreen
) then
210 gFullscreen
:= fullscreen
;
216 g_Window_SetDisplay(preserve
);
223 function WindowEventHandler (constref ev
: TSDL_WindowEvent
): Boolean;
225 wActivate
, wDeactivate
: Boolean;
229 wDeactivate
:= false;
232 SDL_WINDOWEVENT_MOVED
:
234 if not (gFullscreen
or gWinMaximized
) then
236 gWinRealPosX
:= ev
.data1
;
237 gWinRealPosY
:= ev
.data2
;
241 SDL_WINDOWEVENT_MINIMIZED
:
244 if not wMinimized
then
246 e_ResizeWindow(0, 0);
248 if g_debug_WinMsgs
then
250 g_Console_Add('Now minimized');
251 e_WriteLog('[DEBUG] WinMsgs: Now minimized', TMsgType
.Notify
);
257 SDL_WINDOWEVENT_RESIZED
:
259 gScreenWidth
:= ev
.data1
;
260 gScreenHeight
:= ev
.data2
;
263 if g_debug_WinMsgs
then
265 g_Console_Add('Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
));
266 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
), TMsgType
.Notify
);
270 SDL_WINDOWEVENT_EXPOSED
:
273 SDL_WINDOWEVENT_MAXIMIZED
:
277 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
281 if not gWinMaximized
then
283 gWinMaximized
:= true;
284 if g_debug_WinMsgs
then
286 g_Console_Add('Now maximized');
287 e_WriteLog('[DEBUG] WinMsgs: Now maximized', TMsgType
.Notify
);
292 SDL_WINDOWEVENT_RESTORED
:
296 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
300 if gWinMaximized
then gWinMaximized
:= false;
301 if g_debug_WinMsgs
then
303 g_Console_Add('Now restored');
304 e_WriteLog('[DEBUG] WinMsgs: Now restored', TMsgType
.Notify
);
308 SDL_WINDOWEVENT_FOCUS_GAINED
:
311 //e_WriteLog('window gained focus!', MSG_NOTIFY);
314 SDL_WINDOWEVENT_FOCUS_LOST
:
318 //e_WriteLog('window lost focus!', MSG_NOTIFY);
326 e_WriteLog('deactivating window', TMsgType
.Notify
);
327 e_EnableInput
:= false;
328 e_ClearInputBuffer();
330 if gMuteWhenInactive
then
332 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
333 e_MuteChannels(true);
336 if g_debug_WinMsgs
then
338 g_Console_Add('Now inactive');
339 e_WriteLog('[DEBUG] WinMsgs: Now inactive', TMsgType
.Notify
);
344 if assigned(winBlurCB
) then winBlurCB();
347 else if wActivate
then
349 if not gWinActive
then
351 //e_WriteLog('activating window', MSG_NOTIFY);
352 e_EnableInput
:= true;
354 if gMuteWhenInactive
then
356 //e_WriteLog('activating sounds', MSG_NOTIFY);
357 e_MuteChannels(false);
360 if g_debug_WinMsgs
then
362 g_Console_Add('Now active');
363 e_WriteLog('[DEBUG] WinMsgs: Now active', TMsgType
.Notify
);
367 if assigned(winFocusCB
) then winFocusCB();
373 function EventHandler (var ev
: TSDL_Event
): Boolean;
383 result
:= WindowEventHandler(ev
.window
);
387 if (gExit
<> EXIT_QUIT
) then
389 if not wLoadingProgress
then
396 wLoadingQuit
:= true;
402 SDL_KEYDOWN
, SDL_KEYUP
:
404 key
:= ev
.key
.keysym
.scancode
;
405 down
:= (ev
.type_
= SDL_KEYDOWN
);
406 {$IF not DEFINED(HEADLESS)}
407 if fuiOnSDLEvent(ev
) then
409 // event eaten, but...
410 if not down
then e_KeyUpDown(key
, false);
414 if down
then KeyPress(key
);
415 e_KeyUpDown(key
, down
);
418 {$IF not DEFINED(HEADLESS)}
419 SDL_MOUSEBUTTONDOWN
, SDL_MOUSEBUTTONUP
, SDL_MOUSEWHEEL
, SDL_MOUSEMOTION
:
425 Utf8ToUnicode(@uc
, PChar(ev
.text.text), 1);
427 if (keychr
> 127) then keychr
:= Word(wchar2win(WideChar(keychr
)));
428 if (keychr
> 0) and (keychr
<= 255) then CharPress(AnsiChar(keychr
));
431 // other key presses and joysticks are handled in e_input
436 procedure SwapBuffers ();
438 {$IF not DEFINED(HEADLESS)}
439 SDL_GL_SwapWindow(h_Wnd
);
444 function CreateGLWindow (Title
: PChar): Boolean;
448 gWinSizeX
:= gScreenWidth
;
449 gWinSizeY
:= gScreenHeight
;
451 {$IF not DEFINED(HEADLESS)}
454 e_WriteLog('Creating window', TMsgType
.Notify
);
456 if not g_Window_SetDisplay() then
459 e_WriteLog('Window creation error (resolution not supported?)', TMsgType
.Fatal
);
463 {$IF not DEFINED(HEADLESS)}
464 h_Gl
:= SDL_GL_CreateContext(h_Wnd
);
465 if (h_Gl
= nil) then exit
;
466 if (assigned(oglInitCB
)) then oglInitCB();
469 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
477 // windoze sux; in headless mode `GetTickCount()` (and SDL) returns shit
478 function GetTimer (): Int64;
482 QueryPerformanceFrequency(F
);
483 QueryPerformanceCounter(C
);
484 result
:= Round(C
/F
*1000{000});
487 function GetTimer (): Int64;
493 if (ticksOverflow
= -1) then
500 if (lastTicks
> t
) then
502 // overflow, increment overflow ;-)
503 ticksOverflow
:= ticksOverflow
+(Int64($ffffffff)+Int64(1));
504 tt
:= (Int64($ffffffff)+Int64(1))+Int64(t
);
505 t
:= Uint32(tt
-lastTicks
);
509 result
:= ticksOverflow
+Int64(t
);
514 procedure ResetTimer ();
516 wNeedTimeReset
:= true;
520 procedure PushExitEvent ();
524 ev
.type_
:= SDL_QUITEV
;
530 prevLoadingUpdateTime
: UInt64 = 0;
532 procedure ProcessLoading (forceUpdate
: Boolean=false);
538 FillChar(ev
, sizeof(ev
), 0);
539 wLoadingProgress
:= true;
541 while (SDL_PollEvent(@ev
) > 0) do
544 if (ev
.type_
= SDL_QUITEV
) then break
;
548 if (ev
.type_
= SDL_QUITEV
) or (gExit
= EXIT_QUIT
) then
550 wLoadingProgress
:= false;
554 if not wMinimized
then
558 prevLoadingUpdateTime
:= getTimeMilli();
562 stt
:= getTimeMilli();
563 if (stt
< prevLoadingUpdateTime
) or (stt
-prevLoadingUpdateTime
>= ProgressUpdateMSecs
) then
565 prevLoadingUpdateTime
:= stt
;
572 if g_Texture_Get('INTER', ID
) then
574 e_DrawSize(ID
, 0, 0, 0, false, false, gScreenWidth
, gScreenHeight
)
578 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
588 if NetMode
= NET_SERVER
then
594 if (NetMode
= NET_CLIENT
) and (NetState
<> NET_STATE_AUTH
) then g_Net_Client_UpdateWhileLoading();
597 wLoadingProgress
:= false;
601 function g_ProcessMessages (): Boolean;
606 FillChar(ev
, SizeOf(ev
), 0);
607 while (SDL_PollEvent(@ev
) > 0) do
609 result
:= EventHandler(ev
);
610 if (ev
.type_
= SDL_QUITEV
) then exit
;
616 function ProcessMessage (): Boolean;
620 result
:= g_ProcessMessages();
623 Time_Delta
:= Time
-Time_Old
;
627 if wNeedTimeReset
then
630 wNeedTimeReset
:= false;
633 g_Map_ProfilersBegin();
634 g_Mons_ProfilersBegin();
636 t
:= Time_Delta
div 28;
642 if (NetMode
= NET_SERVER
) then g_Net_Host_Update()
643 else if (NetMode
= NET_CLIENT
) then g_Net_Client_Update();
649 if (NetMode
= NET_SERVER
) then g_Net_Host_Update()
650 else if (NetMode
= NET_CLIENT
) then g_Net_Client_Update();
653 g_Map_ProfilersEnd();
654 g_Mons_ProfilersEnd();
662 if (gExit
= EXIT_QUIT
) then
668 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
671 Time_Old
:= Time
-(Time_Delta
mod 28);
672 if (not wMinimized
) then
680 Sleep(1); // release time slice, so we won't eat 100% CPU
687 procedure ReDrawWindow ();
693 procedure InitOpenGL (vsync
: Boolean);
694 {$IF not DEFINED(HEADLESS)}
699 {$IF not DEFINED(HEADLESS)}
700 if vsync
then v
:= 1 else v
:= 0;
701 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
702 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
703 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
704 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
705 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
706 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
707 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
708 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE
, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
709 SDL_GL_SetSwapInterval(v
);
714 function glHasExtension (const name
: AnsiString): Boolean;
719 extName
: ShortString;
722 if (Length(name
) = 0) then exit
;
723 exts
:= glGetString(GL_EXTENSIONS
);
724 if (exts
= nil) then exit
;
725 while (exts
[0] <> #0) and (exts
[0] = ' ') do Inc(exts
);
726 while (exts
[0] <> #0) do
728 if gwin_dump_extensions
then
731 while (exts
[i
] <> #0) and (exts
[i
] <> ' ') do Inc(i
);
734 e_WriteLog('FUUUUUUUUUUUUU', TMsgType
.Warning
);
738 Move(exts
^, extName
[1], i
);
739 extName
[0] := Char(i
);
740 e_WriteLog(Format('EXT: %s', [extName
]), TMsgType
.Notify
);
744 for i
:= 0 to length(name
)-1 do
746 if (exts
[i
] = #0) then begin found
:= false; break
; end;
747 if (exts
[i
] <> name
[i
+1]) then begin found
:= false; break
; end;
749 if found
and ((exts
[Length(name
)] = #0) or (exts
[Length(name
)] = ' ')) then begin result
:= true; exit
; end;
750 while (exts
[0] <> #0) and (exts
[0] <> ' ') do Inc(exts
);
751 while (exts
[0] <> #0) and (exts
[0] = ' ') do Inc(exts
);
756 function SDLMain (): Integer;
759 {$IF not DEFINED(HEADLESS)}
768 e_NoGraphics
:= true;
770 if (not g_holmes_imfunctional
) then
773 uiContext
.font
:= 'win14';
778 while (idx
<= ParamCount
) do
780 arg
:= ParamStr(idx
);
782 if arg
= '--opengl-dump-exts' then gwin_dump_extensions
:= true;
783 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
784 if arg
= '--jah' then g_profile_history_size
:= 100;
785 if arg
= '--no-particles' then gpart_dbg_enabled
:= false;
786 if arg
= '--no-los' then gmon_dbg_los_enabled
:= false;
788 if arg
= '--profile-render' then g_profile_frame_draw
:= true;
789 if arg
= '--profile-coldet' then g_profile_collision
:= true;
790 if arg
= '--profile-los' then g_profile_los
:= true;
792 if arg
= '--no-part-phys' then gpart_dbg_phys_enabled
:= false;
793 if arg
= '--no-part-physics' then gpart_dbg_phys_enabled
:= false;
794 if arg
= '--no-particles-phys' then gpart_dbg_phys_enabled
:= false;
795 if arg
= '--no-particles-physics' then gpart_dbg_phys_enabled
:= false;
796 if arg
= '--no-particle-phys' then gpart_dbg_phys_enabled
:= false;
797 if arg
= '--no-particle-physics' then gpart_dbg_phys_enabled
:= false;
799 {.$IF DEFINED(D2F_DEBUG)}
800 if arg
= '--aimline' then g_dbg_aimline_on
:= true;
803 if arg
= '--holmes' then begin g_holmes_enabled
:= true; g_Game_SetDebugMode(); end;
805 if (arg
= '--holmes-ui-scale') or (arg
= '-holmes-ui-scale') then
807 if (idx
<= ParamCount
) then
809 if not conParseFloat(fuiRenderScale
, ParamStr(idx
)) then fuiRenderScale
:= 1.0;
814 if (arg
= '--holmes-font') or (arg
= '-holmes-font') then
816 if (idx
<= ParamCount
) then
819 val(ParamStr(idx
), itmp
, valres
);
821 if (valres
= 0) and (not g_holmes_imfunctional
) then
824 8: uiContext
.font
:= 'win8';
825 14: uiContext
.font
:= 'win14';
826 16: uiContext
.font
:= 'win16';
838 if (arg
= '--game-scale') or (arg
= '-game-scale') then
840 if (idx
<= ParamCount
) then
842 if not conParseFloat(g_dbg_scale
, ParamStr(idx
)) then g_dbg_scale
:= 1.0;
847 if (arg
= '--write-mapdef') or (arg
= '-write-mapdef') then
849 mdfo
:= createDiskFile('mapdef.txt');
850 mdfo
.WriteBuffer(defaultMapDef
[1], Length(defaultMapDef
));
856 e_WriteLog('Initializing OpenGL', TMsgType
.Notify
);
859 e_WriteLog('Creating GL window', TMsgType
.Notify
);
860 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION
]))) then
866 {EnumDisplayModes();}
869 //gwin_k8_enable_light_experiments := false;
870 gwin_has_stencil
:= false;
871 glLegacyNPOT
:= false;
872 gwin_dump_extensions
:= false;
874 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE
, @ltmp
);
875 e_LogWritefln('stencil buffer size: %s', [ltmp
]);
876 gwin_has_stencil
:= (ltmp
> 0);
878 if not glHasExtension('GL_ARB_texture_non_power_of_two') then
880 e_WriteLog('NPOT textures: NO', TMsgType
.Warning
);
881 glLegacyNPOT
:= true;
885 e_WriteLog('NPOT textures: YES', TMsgType
.Notify
);
886 glLegacyNPOT
:= false;
888 gwin_dump_extensions
:= false;
892 Time_Old
:= GetTimer();
895 if (ParamCount
> 0) then g_Game_Process_Params();
898 if (not gGameOn
) and gAskLanguage
then g_Menu_AskLanguage();
900 e_WriteLog('Entering the main loop', TMsgType
.Notify
);
903 while not ProcessMessage() do begin end;