DEADSOFTWARE

4e84b5d53eb9823a4b5234c44a38cbbc99ca9077
[d2df-sdl.git] / src / game / g_window.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_window;
18 interface
20 uses
21 utils;
23 function SDLMain (): Integer;
24 procedure ResetTimer ();
25 procedure ProcessLoading (forceUpdate: Boolean=false);
27 var
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;
33 implementation
35 uses
36 {$IFDEF WINDOWS}Windows,{$ENDIF}
37 {$IFDEF ENABLE_HOLMES}
38 g_holmes, sdlcarcass, fui_ctls,
39 {$ENDIF}
40 SysUtils, Classes, MAPDEF, Math,
41 r_window, e_log, g_main,
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;
47 var
48 Time, Time_Delta, Time_Old: Int64;
49 Frame: Int64;
50 flag: Boolean;
51 wNeedTimeReset: Boolean = false;
52 wLoadingQuit: Boolean = false;
54 procedure ResetTimer ();
55 begin
56 wNeedTimeReset := true;
57 end;
59 procedure ProcessLoading (forceUpdate: Boolean=false);
60 begin
61 if sys_HandleInput() = True then
62 Exit;
64 {$IFNDEF HEADLESS}
65 r_Window_DrawLoading(forceUpdate);
66 {$ENDIF}
68 e_SoundUpdate();
70 if NetMode = NET_SERVER then
71 g_Net_Host_Update()
72 else if (NetMode = NET_CLIENT) and (NetState <> NET_STATE_AUTH) then
73 g_Net_Client_UpdateWhileLoading();
74 end;
77 function ProcessMessage (): Boolean;
78 var
79 i, t: Integer;
80 begin
81 result := sys_HandleInput();
83 Time := sys_GetTicks();
84 Time_Delta := Time-Time_Old;
86 flag := false;
88 if wNeedTimeReset then
89 begin
90 Frame := 0;
91 Time_Delta := 28;
92 wNeedTimeReset := false;
93 end;
95 g_Map_ProfilersBegin();
96 g_Mons_ProfilersBegin();
98 t := Time_Delta div 28;
99 if (t > 0) then
100 begin
101 flag := true;
102 for i := 1 to t do
103 Update();
104 end;
106 g_Map_ProfilersEnd();
107 g_Mons_ProfilersEnd();
109 if wLoadingQuit then
110 begin
111 g_Game_Free();
112 g_Game_Quit();
113 end;
115 if (gExit = EXIT_QUIT) then
116 begin
117 result := true;
118 exit;
119 end;
121 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
122 if flag then
123 Time_Old := Time - (Time_Delta mod 28);
125 // don't wait if VSync is on, GL already probably waits enough
126 if gLerpActors then
127 flag := (Time - Frame >= gFrameTime) or gVSync;
129 if flag then
130 begin
131 if gPause or (not gLerpActors) or (gState = STATE_FOLD) then
132 gLerpFactor := 1.0
133 else
134 gLerpFactor := nmin(1.0, (Time - Time_Old) / 28.0);
135 Draw;
136 sys_Repaint;
137 Frame := Time
138 end
139 else
140 sys_Delay(1);
142 e_SoundUpdate();
143 end;
145 function SDLMain (): Integer;
146 var
147 idx: Integer;
148 arg: AnsiString;
149 mdfo: TStream;
150 {$IFDEF ENABLE_HOLMES}
151 itmp: Integer;
152 valres: Word;
153 {$ENDIF}
154 begin
156 idx := 1;
157 while (idx <= ParamCount) do
158 begin
159 arg := ParamStr(idx);
160 Inc(idx);
161 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
162 if arg = '--jah' then g_profile_history_size := 100;
163 if arg = '--no-particles' then gpart_dbg_enabled := false;
164 if arg = '--no-los' then gmon_dbg_los_enabled := false;
166 if arg = '--profile-render' then g_profile_frame_draw := true;
167 if arg = '--profile-coldet' then g_profile_collision := true;
168 if arg = '--profile-los' then g_profile_los := true;
170 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
171 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
172 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
173 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
174 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
175 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
177 if arg = '--debug-input' then g_dbg_input := True;
179 {.$IF DEFINED(D2F_DEBUG)}
180 if arg = '--aimline' then g_dbg_aimline_on := true;
181 {.$ENDIF}
183 {$IFDEF ENABLE_HOLMES}
184 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
186 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
187 begin
188 if (idx <= ParamCount) then
189 begin
190 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
191 Inc(idx);
192 end;
193 end;
195 if (arg = '--holmes-font') or (arg = '-holmes-font') then
196 begin
197 if (idx <= ParamCount) then
198 begin
199 itmp := 0;
200 val(ParamStr(idx), itmp, valres);
201 {$IFNDEF HEADLESS}
202 if (valres = 0) and (not g_holmes_imfunctional) then
203 begin
204 case itmp of
205 8: uiContext.font := 'win8';
206 14: uiContext.font := 'win14';
207 16: uiContext.font := 'win16';
208 end;
209 end;
210 {$ELSE}
211 // fuck off, fpc!
212 itmp := itmp;
213 valres := valres;
214 {$ENDIF}
215 Inc(idx);
216 end;
217 end;
218 {$ENDIF}
220 if (arg = '--game-scale') or (arg = '-game-scale') then
221 begin
222 if (idx <= ParamCount) then
223 begin
224 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
225 Inc(idx);
226 end;
227 end;
229 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
230 begin
231 mdfo := createDiskFile('mapdef.txt');
232 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
233 mdfo.Free();
234 Halt(0);
235 end;
237 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
238 begin
239 if (idx <= ParamCount) then
240 begin
241 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
242 Inc(idx);
243 end;
244 end;
245 end;
247 r_Window_Initialize;
249 Init;
250 Time_Old := sys_GetTicks();
252 g_Net_InitLowLevel();
254 // Êîìàíäíàÿ ñòðîêà
255 if (ParamCount > 0) then g_Game_Process_Params();
257 {$IFNDEF HEADLESS}
258 // Çàïðîñ ÿçûêà
259 if (not gGameOn) and gAskLanguage then g_Menu_AskLanguage();
260 {$ENDIF}
262 e_WriteLog('Entering the main loop', TMsgType.Notify);
264 // main loop
265 while not ProcessMessage() do begin end;
267 g_Net_Slist_ShutdownAll();
269 Release();
271 g_Net_DeinitLowLevel();
272 result := 0;
273 end;
276 initialization
277 conRegVar('d_input', @g_dbg_input, '', '')
278 end.