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, version 3 of the License ONLY.
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.
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/>.
15 {$INCLUDE ../shared/a_modes.inc}
23 function SDLMain (): Integer;
24 procedure ResetTimer ();
25 procedure ProcessLoading (forceUpdate
: Boolean = False);
28 gwin_has_stencil
: Boolean = false;
29 gwin_k8_enable_light_experiments
: Boolean = false;
30 g_dbg_aimline_on
: Boolean = false;
31 g_dbg_input
: Boolean = False;
36 {$IFDEF WINDOWS}Windows
,{$ENDIF}
37 {$IFDEF ENABLE_HOLMES}
38 g_holmes
, sdlcarcass
, fui_ctls
,
40 SysUtils
, Classes
, MAPDEF
, Math
, r_graphics
,
41 r_window
, e_log
, e_res
, envvars
, r_game
,
42 g_console
, r_console
, e_input
, g_options
, g_game
,
43 g_basic
, g_textures
, e_sound
, g_sound
, g_menu
, ENet
, g_net
,
44 g_map
, g_gfx
, g_monsters
, xprofiler
,
45 g_touch
, g_gui
, g_system
, g_netmaster
;
48 Time
, Time_Delta
, Time_Old
: Int64;
51 wNeedTimeReset
: Boolean = false;
52 wLoadingQuit
: Boolean = false;
55 UseNativeMusic
: Boolean;
60 // remember old mobj positions, prepare for update
62 // server: receive client commands for new frame
63 // client: receive game state changes from server
64 if (NetMode
= NET_SERVER
) then g_Net_Host_Update()
65 else if (NetMode
= NET_CLIENT
) then g_Net_Client_Update();
68 // server: send any accumulated outgoing data to clients
69 if NetMode
= NET_SERVER
then g_Net_Flush();
81 var timiditycfg
: AnsiString;
82 var oldcwd
, newcwd
: RawByteString;
90 NoSound
:= False; // hope env has set SDL_AUDIODRIVER to dummy
92 NoSound
:= True; // FMOD backend will sort it out
101 if (e_JoysticksAvailable > 0) then
102 e_WriteLog('Input: Joysticks available.', TMsgType.Notify)
104 e_WriteLog('Input: No Joysticks.', TMsgType.Notify);
107 if gNoSound
= false then
109 e_WriteLog('Initializing sound system', TMsgType
.Notify
);
110 {$IFDEF USE_SDLMIXER}
112 if UseNativeMusic
then
113 SetEnvVar('SDL_NATIVE_MUSIC', '1');
114 timiditycfg
:= GetEnvironmentVariable('TIMIDITY_CFG');
115 if timiditycfg
= '' then
117 timiditycfg
:= 'timidity.cfg';
118 if e_FindResource(ConfigDirs
, timiditycfg
) OR e_FindResource(DataDirs
, timiditycfg
) then
120 timiditycfg
:= ExpandFileName(timiditycfg
);
121 newcwd
:= ExtractFileDir(timiditycfg
);
122 SetEnvVar('TIMIDITY_CFG', timiditycfg
);
127 e_LogWritefln('TIMIDITY_CFG = "%s"', [timiditycfg
]);
128 e_LogWritefln('SDL_NATIVE_MUSIC = "%s"', [GetEnvironmentVariable('SDL_NATIVE_MUSIC')]);
130 e_InitSoundSystem(NoSound
);
131 {$IFDEF USE_SDLMIXER}
132 if e_TimidityDecoder
and (newcwd
<> '') then
134 (* HACK: Set CWD to load GUS patches relatively to cfg file. *)
135 (* CWD not restored after sound init because timidity *)
136 (* store relative pathes internally and load patches *)
137 (* later. I hope game never relies on CWD. *)
141 e_logwritefln('WARNING: USED TIMIDITY CONFIG HACK, CWD SWITCHED "%s" -> "%s"', [oldcwd
, newcwd
]);
146 e_WriteLog('Init game', TMsgType
.Notify
);
149 // FillChar(charbuff, sizeof(charbuff), ' ');
154 e_WriteLog('Releasing engine', TMsgType
.Notify
);
157 e_WriteLog('Releasing input', TMsgType
.Notify
);
162 e_WriteLog('Releasing sound', TMsgType
.Notify
);
163 e_ReleaseSoundSystem();
167 procedure ResetTimer ();
169 wNeedTimeReset
:= true;
172 procedure ProcessLoading (forceUpdate
: Boolean=false);
174 if sys_HandleInput() = True then
178 r_Window_DrawLoading(forceUpdate
);
183 // TODO: At the moment, I left here only host network processing, because the client code must
184 // handle network events on its own. Otherwise separate network cases that use different calls to
185 // enet_host_service() WILL lose their packets (for example, resource downloading). So they have
186 // to handle everything by themselves. But in general, this MUST be removed completely, since
187 // updating the window should never affect the network. Use single enet_host_service(), period.
188 if NetMode
= NET_SERVER
189 then g_Net_Host_Update();
193 function ProcessMessage (): Boolean;
197 result
:= sys_HandleInput();
199 Time
:= sys_GetTicks();
200 Time_Delta
:= Time
-Time_Old
;
204 if wNeedTimeReset
then
208 wNeedTimeReset
:= false;
211 g_Map_ProfilersBegin();
212 g_Mons_ProfilersBegin();
214 t
:= Time_Delta
div 28;
222 g_Map_ProfilersEnd();
223 g_Mons_ProfilersEnd();
231 if (gExit
= EXIT_QUIT
) then
237 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
239 Time_Old
:= Time
- (Time_Delta
mod 28);
241 // don't wait if VSync is on, GL already probably waits enough
243 flag
:= (Time
- Frame
>= gFrameTime
) or gVSync
;
247 if gPause
or (not gLerpActors
) or (gState
= STATE_FOLD
) then
250 gLerpFactor
:= nmin(1.0, (Time
- Time_Old
) / 28.0);
261 function SDLMain (): Integer;
266 {$IFDEF ENABLE_HOLMES}
273 while (idx
<= ParamCount
) do
275 arg
:= ParamStr(idx
);
277 if arg
= '--jah' then g_profile_history_size
:= 100;
278 if arg
= '--no-particles' then gpart_dbg_enabled
:= false;
279 if arg
= '--no-los' then gmon_dbg_los_enabled
:= false;
281 if arg
= '--profile-render' then g_profile_frame_draw
:= true;
282 if arg
= '--profile-coldet' then g_profile_collision
:= true;
283 if arg
= '--profile-los' then g_profile_los
:= true;
285 if arg
= '--no-part-phys' then gpart_dbg_phys_enabled
:= false;
286 if arg
= '--no-part-physics' then gpart_dbg_phys_enabled
:= false;
287 if arg
= '--no-particles-phys' then gpart_dbg_phys_enabled
:= false;
288 if arg
= '--no-particles-physics' then gpart_dbg_phys_enabled
:= false;
289 if arg
= '--no-particle-phys' then gpart_dbg_phys_enabled
:= false;
290 if arg
= '--no-particle-physics' then gpart_dbg_phys_enabled
:= false;
292 if arg
= '--debug-input' then g_dbg_input
:= True;
294 {.$IF DEFINED(D2F_DEBUG)}
295 if arg
= '--aimline' then g_dbg_aimline_on
:= true;
298 {$IFDEF ENABLE_HOLMES}
299 if arg
= '--holmes' then begin g_holmes_enabled
:= true; g_Game_SetDebugMode(); end;
301 if (arg
= '--holmes-ui-scale') or (arg
= '-holmes-ui-scale') then
303 if (idx
<= ParamCount
) then
305 if not conParseFloat(fuiRenderScale
, ParamStr(idx
)) then fuiRenderScale
:= 1.0;
310 if (arg
= '--holmes-font') or (arg
= '-holmes-font') then
312 if (idx
<= ParamCount
) then
315 val(ParamStr(idx
), itmp
, valres
);
317 if (valres
= 0) and (not g_holmes_imfunctional
) then
320 8: uiContext
.font
:= 'win8';
321 14: uiContext
.font
:= 'win14';
322 16: uiContext
.font
:= 'win16';
335 if (arg
= '--game-scale') or (arg
= '-game-scale') then
337 if (idx
<= ParamCount
) then
339 if not conParseFloat(g_dbg_scale
, ParamStr(idx
)) then g_dbg_scale
:= 1.0;
344 if (arg
= '--write-mapdef') or (arg
= '-write-mapdef') then
346 mdfo
:= createDiskFile('mapdef.txt');
347 mdfo
.WriteBuffer(defaultMapDef
[1], Length(defaultMapDef
));
352 if (arg
= '--pixel-scale') or (arg
= '-pixel-scale') then
354 if (idx
<= ParamCount
) then
356 if not conParseFloat(r_pixel_scale
, ParamStr(idx
)) then r_pixel_scale
:= 1.0;
365 Time_Old
:= sys_GetTicks();
367 g_Net_InitLowLevel();
370 if (ParamCount
> 0) then g_Game_Process_Params();
374 if (not gGameOn
) and gAskLanguage
then g_Menu_AskLanguage();
377 e_WriteLog('Entering the main loop', TMsgType
.Notify
);
380 while not ProcessMessage() do begin end;
382 g_Net_Slist_ShutdownAll();
386 g_Net_DeinitLowLevel();
392 {$IFDEF USE_SDLMIXER}
393 conRegVar('sdl_native_music', @UseNativeMusic
, 'use native midi music output when possible', 'use native midi');
395 UseNativeMusic
:= true; (* OSX have a good midi support, so why not? *)
397 UseNativeMusic
:= false;
400 conRegVar('d_input', @g_dbg_input
, '', '')