DEADSOFTWARE

cleanup: remove g_options dependency on r_graphics
[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 {$INCLUDE ../nogl/noGLuses.inc}
41 SysUtils, Classes, MAPDEF, Math,
42 r_graphics, e_log, e_texture, g_main,
43 g_console, r_console, e_input, g_options, g_game, r_game,
44 g_basic, g_textures, e_sound, g_sound, g_menu, ENet, g_net,
45 g_map, g_gfx, g_monsters, xprofiler,
46 g_touch, g_gui, g_system, g_netmaster;
49 const
50 ProgressUpdateMSecs = 35; //1;//100;
52 var
53 Time, Time_Delta, Time_Old: Int64;
54 Frame: Int64;
55 flag: Boolean;
56 wNeedTimeReset: Boolean = false;
57 wMinimized: Boolean = false;
58 wLoadingQuit: Boolean = false;
60 procedure ResetTimer ();
61 begin
62 wNeedTimeReset := true;
63 end;
65 {$IFNDEF HEADLESS}
66 var
67 prevLoadingUpdateTime: UInt64 = 0;
68 {$ENDIF}
70 procedure ProcessLoading (forceUpdate: Boolean);
71 {$IFNDEF HEADLESS}
72 var
73 stt: UInt64;
74 {$ENDIF}
75 begin
76 if sys_HandleInput() = True then
77 Exit;
79 {$IFNDEF HEADLESS}
80 if not wMinimized then
81 begin
82 if not forceUpdate then
83 begin
84 stt := getTimeMilli();
85 forceUpdate := (stt < prevLoadingUpdateTime) or (stt-prevLoadingUpdateTime >= ProgressUpdateMSecs);
86 end;
88 if forceUpdate then
89 begin
90 e_SetRendertarget(True);
91 e_SetViewPort(0, 0, gScreenWidth, gScreenHeight);
93 r_Game_DrawMenuBackground('INTER');
94 e_DarkenQuadWH(0, 0, gScreenWidth, gScreenHeight, 150);
95 r_Game_DrawLoadingStat();
96 r_Console_Draw(True);
98 e_SetRendertarget(False);
99 e_SetViewPort(0, 0, gWinSizeX, gWinSizeY);
100 e_BlitFramebuffer(gWinSizeX, gWinSizeY);
102 sys_Repaint;
103 prevLoadingUpdateTime := getTimeMilli();
104 end;
105 end;
106 {$ENDIF}
108 e_SoundUpdate();
110 // TODO: At the moment, I left here only host network processing, because the client code must
111 // handle network events on its own. Otherwise separate network cases that use different calls to
112 // enet_host_service() WILL lose their packets (for example, resource downloading). So they have
113 // to handle everything by themselves. But in general, this MUST be removed completely, since
114 // updating the window should never affect the network. Use single enet_host_service(), period.
115 if NetMode = NET_SERVER
116 then g_Net_Host_Update();
117 end;
120 function ProcessMessage (): Boolean;
121 var
122 i, t: Integer;
123 begin
124 result := sys_HandleInput();
126 Time := sys_GetTicks();
127 Time_Delta := Time-Time_Old;
129 flag := false;
131 if wNeedTimeReset then
132 begin
133 Frame := 0;
134 Time_Delta := 28;
135 wNeedTimeReset := false;
136 end;
138 g_Map_ProfilersBegin();
139 g_Mons_ProfilersBegin();
141 t := Time_Delta div 28;
142 if (t > 0) then
143 begin
144 flag := true;
145 for i := 1 to t do
146 Update();
147 end;
149 g_Map_ProfilersEnd();
150 g_Mons_ProfilersEnd();
152 if wLoadingQuit then
153 begin
154 g_Game_Free();
155 g_Game_Quit();
156 end;
158 if (gExit = EXIT_QUIT) then
159 begin
160 result := true;
161 exit;
162 end;
164 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
165 if flag then
166 Time_Old := Time - (Time_Delta mod 28);
168 // don't wait if VSync is on, GL already probably waits enough
169 if gLerpActors then
170 flag := (Time - Frame >= gFrameTime) or gVSync;
172 if flag then
173 begin
174 if (not wMinimized) then
175 begin
176 if gPause or (not gLerpActors) or (gState = STATE_FOLD) then
177 gLerpFactor := 1.0
178 else
179 gLerpFactor := nmin(1.0, (Time - Time_Old) / 28.0);
180 Draw;
181 sys_Repaint
182 end;
183 Frame := Time
184 end
185 else
186 sys_Delay(1);
188 e_SoundUpdate();
189 end;
191 function GLExtensionList (): SSArray;
192 var
193 s: PChar;
194 i, j, num: GLint;
195 begin
196 result := nil;
197 s := glGetString(GL_EXTENSIONS);
198 if s <> nil then
199 begin
200 num := 0;
201 i := 0;
202 j := 0;
203 while (s[i] <> #0) and (s[i] = ' ') do Inc(i);
204 while (s[i] <> #0) do
205 begin
206 while (s[i] <> #0) and (s[i] <> ' ') do Inc(i);
207 SetLength(result, num+1);
208 result[num] := Copy(s, j+1, i-j);
209 while (s[i] <> #0) and (s[i] = ' ') do Inc(i);
210 j := i;
211 Inc(num);
212 end;
213 end;
214 end;
216 function GLExtensionSupported (ext: AnsiString): Boolean;
217 var
218 exts: SSArray;
219 e: AnsiString;
220 begin
221 result := false;
222 exts := GLExtensionList();
223 for e in exts do
224 begin
225 //writeln('<', e, '> : [', ext, '] = ', strEquCI1251(e, ext));
226 if (strEquCI1251(e, ext)) then begin result := true; exit; end;
227 end;
228 end;
230 procedure PrintGLSupportedExtensions;
231 begin
232 e_LogWritefln('GL Vendor: %s', [glGetString(GL_VENDOR)]);
233 e_LogWritefln('GL Renderer: %s', [glGetString(GL_RENDERER)]);
234 e_LogWritefln('GL Version: %s', [glGetString(GL_VERSION)]);
235 e_LogWritefln('GL Shaders: %s', [glGetString(GL_SHADING_LANGUAGE_VERSION)]);
236 e_LogWritefln('GL Extensions: %s', [glGetString(GL_EXTENSIONS)]);
237 end;
239 function SDLMain (): Integer;
240 var
241 idx: Integer;
242 arg: AnsiString;
243 mdfo: TStream;
244 {$IFDEF ENABLE_HOLMES}
245 itmp: Integer;
246 valres: Word;
247 {$ENDIF}
248 begin
250 idx := 1;
251 while (idx <= ParamCount) do
252 begin
253 arg := ParamStr(idx);
254 Inc(idx);
255 if arg = '--jah' then g_profile_history_size := 100;
256 if arg = '--no-particles' then gpart_dbg_enabled := false;
257 if arg = '--no-los' then gmon_dbg_los_enabled := false;
259 if arg = '--profile-render' then g_profile_frame_draw := true;
260 if arg = '--profile-coldet' then g_profile_collision := true;
261 if arg = '--profile-los' then g_profile_los := true;
263 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
264 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
265 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
266 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
267 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
268 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
270 if arg = '--debug-input' then g_dbg_input := True;
272 {.$IF DEFINED(D2F_DEBUG)}
273 if arg = '--aimline' then g_dbg_aimline_on := true;
274 {.$ENDIF}
276 {$IFDEF ENABLE_HOLMES}
277 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
279 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
280 begin
281 if (idx <= ParamCount) then
282 begin
283 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
284 Inc(idx);
285 end;
286 end;
288 if (arg = '--holmes-font') or (arg = '-holmes-font') then
289 begin
290 if (idx <= ParamCount) then
291 begin
292 itmp := 0;
293 val(ParamStr(idx), itmp, valres);
294 {$IFNDEF HEADLESS}
295 if (valres = 0) and (not g_holmes_imfunctional) then
296 begin
297 case itmp of
298 8: uiContext.font := 'win8';
299 14: uiContext.font := 'win14';
300 16: uiContext.font := 'win16';
301 end;
302 end;
303 {$ELSE}
304 // fuck off, fpc!
305 itmp := itmp;
306 valres := valres;
307 {$ENDIF}
308 Inc(idx);
309 end;
310 end;
311 {$ENDIF}
313 if (arg = '--game-scale') or (arg = '-game-scale') then
314 begin
315 if (idx <= ParamCount) then
316 begin
317 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
318 Inc(idx);
319 end;
320 end;
322 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
323 begin
324 mdfo := createDiskFile('mapdef.txt');
325 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
326 mdfo.Free();
327 Halt(0);
328 end;
330 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
331 begin
332 if (idx <= ParamCount) then
333 begin
334 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
335 Inc(idx);
336 end;
337 end;
338 end;
340 {$IFNDEF USE_SYSSTUB}
341 PrintGLSupportedExtensions;
342 glLegacyNPOT := not (GLExtensionSupported('GL_ARB_texture_non_power_of_two') or GLExtensionSupported('GL_OES_texture_npot'));
343 {$ELSE}
344 glLegacyNPOT := False;
345 glRenderToFBO := False;
346 {$ENDIF}
347 if glNPOTOverride and glLegacyNPOT then
348 begin
349 glLegacyNPOT := true;
350 e_logWriteln('NPOT texture emulation: FORCED');
351 end
352 else
353 begin
354 if (glLegacyNPOT) then e_logWriteln('NPOT texture emulation: enabled')
355 else e_logWriteln('NPOT texture emulation: disabled');
356 end;
358 Init;
359 Time_Old := sys_GetTicks();
361 g_Net_InitLowLevel();
363 // Êîìàíäíàÿ ñòðîêà
364 if (ParamCount > 0) then g_Game_Process_Params();
366 {$IFNDEF HEADLESS}
367 // Çàïðîñ ÿçûêà
368 if (not gGameOn) and gAskLanguage then g_Menu_AskLanguage();
369 {$ENDIF}
371 e_WriteLog('Entering the main loop', TMsgType.Notify);
373 // main loop
374 while not ProcessMessage() do begin end;
376 g_Net_Slist_ShutdownAll();
378 Release();
380 g_Net_DeinitLowLevel();
381 result := 0;
382 end;
385 initialization
386 conRegVar('d_input', @g_dbg_input, '', '')
387 end.