DEADSOFTWARE

network now always ticks at 36 tick
[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_dump_extensions: Boolean = false;
29 gwin_has_stencil: Boolean = false;
30 gwin_k8_enable_light_experiments: Boolean = false;
31 g_dbg_aimline_on: Boolean = false;
32 g_dbg_input: Boolean = False;
34 implementation
36 uses
37 {$IFDEF WINDOWS}Windows,{$ENDIF}
38 {$IFDEF ENABLE_HOLMES}
39 g_holmes, sdlcarcass, fui_ctls,
40 {$ENDIF}
41 {$INCLUDE ../nogl/noGLuses.inc}
42 SysUtils, Classes, MAPDEF, Math,
43 e_graphics, e_log, e_texture, g_main,
44 g_console, e_input, g_options, g_game,
45 g_basic, g_textures, e_sound, g_sound, g_menu, ENet, g_net,
46 g_map, g_gfx, g_monsters, xprofiler,
47 g_touch, g_gui, g_system, g_netmaster;
50 const
51 ProgressUpdateMSecs = 35; //1;//100;
53 var
54 Time, Time_Delta, Time_Old: Int64;
55 Frame: Int64;
56 flag: Boolean;
57 wNeedTimeReset: Boolean = false;
58 wMinimized: Boolean = false;
59 wLoadingQuit: Boolean = false;
61 procedure ResetTimer ();
62 begin
63 wNeedTimeReset := true;
64 end;
66 {$IFNDEF HEADLESS}
67 var
68 prevLoadingUpdateTime: UInt64 = 0;
69 {$ENDIF}
71 procedure ProcessLoading (forceUpdate: Boolean=false);
72 {$IFNDEF HEADLESS}
73 var
74 stt: UInt64;
75 {$ENDIF}
76 begin
77 if sys_HandleInput() = True then
78 Exit;
80 {$IFNDEF HEADLESS}
81 if not wMinimized then
82 begin
83 if not forceUpdate then
84 begin
85 stt := getTimeMilli();
86 forceUpdate := (stt < prevLoadingUpdateTime) or (stt-prevLoadingUpdateTime >= ProgressUpdateMSecs);
87 end;
89 if forceUpdate then
90 begin
91 DrawMenuBackground('INTER');
92 e_DarkenQuadWH(0, 0, gScreenWidth, gScreenHeight, 150);
94 DrawLoadingStat();
95 g_Console_Draw(True);
96 sys_Repaint;
97 prevLoadingUpdateTime := getTimeMilli();
98 end;
99 end;
100 {$ENDIF}
102 e_SoundUpdate();
104 if NetMode = NET_SERVER then
105 begin
106 g_Net_Host_Update();
107 end
108 else
109 begin
110 if (NetMode = NET_CLIENT) and (NetState <> NET_STATE_AUTH) then g_Net_Client_UpdateWhileLoading();
111 end;
112 end;
115 function ProcessMessage (): Boolean;
116 var
117 i, t: Integer;
118 begin
119 result := sys_HandleInput();
121 Time := sys_GetTicks();
122 Time_Delta := Time-Time_Old;
124 flag := false;
126 if wNeedTimeReset then
127 begin
128 Frame := 0;
129 Time_Delta := 28;
130 wNeedTimeReset := false;
131 end;
133 g_Map_ProfilersBegin();
134 g_Mons_ProfilersBegin();
136 t := Time_Delta div 28;
137 if (t > 0) then
138 begin
139 flag := true;
140 for i := 1 to t do
141 begin
142 if (NetMode = NET_SERVER) then g_Net_Host_Update()
143 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
144 Update();
145 end;
146 end;
148 if NetMode = NET_SERVER then g_Net_Flush();
150 g_Map_ProfilersEnd();
151 g_Mons_ProfilersEnd();
153 if wLoadingQuit then
154 begin
155 g_Game_Free();
156 g_Game_Quit();
157 end;
159 if (gExit = EXIT_QUIT) then
160 begin
161 result := true;
162 exit;
163 end;
165 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
166 if flag then
167 Time_Old := Time - (Time_Delta mod 28);
169 // don't wait if VSync is on, GL already probably waits enough
170 if gLerpActors then
171 flag := (Time - Frame >= gFrameTime) or gVSync;
173 if flag then
174 begin
175 if (not wMinimized) then
176 begin
177 if gPause or not gLerpActors then
178 gLerpFactor := 1.0
179 else
180 gLerpFactor := nmin(1.0, (Time - Time_Old) / 28.0);
181 Draw;
182 sys_Repaint
183 end;
184 Frame := Time
185 end
186 else
187 sys_Delay(1);
189 e_SoundUpdate();
190 end;
192 function GLExtensionList (): SSArray;
193 var
194 s: PChar;
195 i, j, num: GLint;
196 begin
197 result := nil;
198 s := glGetString(GL_EXTENSIONS);
199 if s <> nil then
200 begin
201 num := 0;
202 i := 0;
203 j := 0;
204 while (s[i] <> #0) and (s[i] = ' ') do Inc(i);
205 while (s[i] <> #0) do
206 begin
207 while (s[i] <> #0) and (s[i] <> ' ') do Inc(i);
208 SetLength(result, num+1);
209 result[num] := Copy(s, j+1, i-j);
210 while (s[i] <> #0) and (s[i] = ' ') do Inc(i);
211 j := i;
212 Inc(num);
213 end;
214 end;
215 end;
217 function GLExtensionSupported (ext: AnsiString): Boolean;
218 var
219 exts: SSArray;
220 e: AnsiString;
221 begin
222 result := false;
223 exts := GLExtensionList();
224 for e in exts do
225 begin
226 //writeln('<', e, '> : [', ext, '] = ', strEquCI1251(e, ext));
227 if (strEquCI1251(e, ext)) then begin result := true; exit; end;
228 end;
229 end;
231 procedure PrintGLSupportedExtensions;
232 begin
233 e_LogWritefln('GL Vendor: %s', [glGetString(GL_VENDOR)]);
234 e_LogWritefln('GL Renderer: %s', [glGetString(GL_RENDERER)]);
235 e_LogWritefln('GL Version: %s', [glGetString(GL_VERSION)]);
236 e_LogWritefln('GL Shaders: %s', [glGetString(GL_SHADING_LANGUAGE_VERSION)]);
237 e_LogWritefln('GL Extensions: %s', [glGetString(GL_EXTENSIONS)]);
238 end;
240 function SDLMain (): Integer;
241 var
242 idx: Integer;
243 arg: AnsiString;
244 mdfo: TStream;
245 {$IFDEF ENABLE_HOLMES}
246 itmp: Integer;
247 valres: Word;
248 {$ENDIF}
249 begin
250 {$IFDEF HEADLESS}
251 e_NoGraphics := true;
252 {$ENDIF}
254 idx := 1;
255 while (idx <= ParamCount) do
256 begin
257 arg := ParamStr(idx);
258 Inc(idx);
259 if arg = '--opengl-dump-exts' then gwin_dump_extensions := true;
260 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
261 if arg = '--jah' then g_profile_history_size := 100;
262 if arg = '--no-particles' then gpart_dbg_enabled := false;
263 if arg = '--no-los' then gmon_dbg_los_enabled := false;
265 if arg = '--profile-render' then g_profile_frame_draw := true;
266 if arg = '--profile-coldet' then g_profile_collision := true;
267 if arg = '--profile-los' then g_profile_los := true;
269 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
270 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
271 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
272 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
273 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
274 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
276 if arg = '--debug-input' then g_dbg_input := True;
278 {.$IF DEFINED(D2F_DEBUG)}
279 if arg = '--aimline' then g_dbg_aimline_on := true;
280 {.$ENDIF}
282 {$IFDEF ENABLE_HOLMES}
283 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
285 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
286 begin
287 if (idx <= ParamCount) then
288 begin
289 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
290 Inc(idx);
291 end;
292 end;
294 if (arg = '--holmes-font') or (arg = '-holmes-font') then
295 begin
296 if (idx <= ParamCount) then
297 begin
298 itmp := 0;
299 val(ParamStr(idx), itmp, valres);
300 {$IFNDEF HEADLESS}
301 if (valres = 0) and (not g_holmes_imfunctional) then
302 begin
303 case itmp of
304 8: uiContext.font := 'win8';
305 14: uiContext.font := 'win14';
306 16: uiContext.font := 'win16';
307 end;
308 end;
309 {$ELSE}
310 // fuck off, fpc!
311 itmp := itmp;
312 valres := valres;
313 {$ENDIF}
314 Inc(idx);
315 end;
316 end;
317 {$ENDIF}
319 if (arg = '--game-scale') or (arg = '-game-scale') then
320 begin
321 if (idx <= ParamCount) then
322 begin
323 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
324 Inc(idx);
325 end;
326 end;
328 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
329 begin
330 mdfo := createDiskFile('mapdef.txt');
331 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
332 mdfo.Free();
333 Halt(0);
334 end;
336 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
337 begin
338 if (idx <= ParamCount) then
339 begin
340 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
341 Inc(idx);
342 end;
343 end;
344 end;
346 {$IFNDEF USE_SYSSTUB}
347 PrintGLSupportedExtensions;
348 glLegacyNPOT := not (GLExtensionSupported('GL_ARB_texture_non_power_of_two') or GLExtensionSupported('GL_OES_texture_npot'));
349 {$ELSE}
350 glLegacyNPOT := False;
351 glRenderToFBO := False;
352 {$ENDIF}
353 if glNPOTOverride and glLegacyNPOT then
354 begin
355 glLegacyNPOT := true;
356 e_logWriteln('NPOT texture emulation: FORCED');
357 end
358 else
359 begin
360 if (glLegacyNPOT) then e_logWriteln('NPOT texture emulation: enabled')
361 else e_logWriteln('NPOT texture emulation: disabled');
362 end;
363 gwin_dump_extensions := false;
365 Init;
366 Time_Old := sys_GetTicks();
368 g_Net_InitLowLevel();
370 // Êîìàíäíàÿ ñòðîêà
371 if (ParamCount > 0) then g_Game_Process_Params();
373 {$IFNDEF HEADLESS}
374 // Çàïðîñ ÿçûêà
375 if (not gGameOn) and gAskLanguage then g_Menu_AskLanguage();
376 {$ENDIF}
378 e_WriteLog('Entering the main loop', TMsgType.Notify);
380 // main loop
381 while not ProcessMessage() do begin end;
383 g_Net_Slist_ShutdownAll();
385 Release();
387 g_Net_DeinitLowLevel();
388 result := 0;
389 end;
392 initialization
393 conRegVar('d_input', @g_dbg_input, '', '')
394 end.