DEADSOFTWARE

render: remove g_window dependency on r_graphics and GL
[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 // TODO: At the moment, I left here only host network processing, because the client code must
71 // handle network events on its own. Otherwise separate network cases that use different calls to
72 // enet_host_service() WILL lose their packets (for example, resource downloading). So they have
73 // to handle everything by themselves. But in general, this MUST be removed completely, since
74 // updating the window should never affect the network. Use single enet_host_service(), period.
75 if NetMode = NET_SERVER
76 then g_Net_Host_Update();
77 end;
80 function ProcessMessage (): Boolean;
81 var
82 i, t: Integer;
83 begin
84 result := sys_HandleInput();
86 Time := sys_GetTicks();
87 Time_Delta := Time-Time_Old;
89 flag := false;
91 if wNeedTimeReset then
92 begin
93 Frame := 0;
94 Time_Delta := 28;
95 wNeedTimeReset := false;
96 end;
98 g_Map_ProfilersBegin();
99 g_Mons_ProfilersBegin();
101 t := Time_Delta div 28;
102 if (t > 0) then
103 begin
104 flag := true;
105 for i := 1 to t do
106 Update();
107 end;
109 g_Map_ProfilersEnd();
110 g_Mons_ProfilersEnd();
112 if wLoadingQuit then
113 begin
114 g_Game_Free();
115 g_Game_Quit();
116 end;
118 if (gExit = EXIT_QUIT) then
119 begin
120 result := true;
121 exit;
122 end;
124 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
125 if flag then
126 Time_Old := Time - (Time_Delta mod 28);
128 // don't wait if VSync is on, GL already probably waits enough
129 if gLerpActors then
130 flag := (Time - Frame >= gFrameTime) or gVSync;
132 if flag then
133 begin
134 if gPause or (not gLerpActors) or (gState = STATE_FOLD) then
135 gLerpFactor := 1.0
136 else
137 gLerpFactor := nmin(1.0, (Time - Time_Old) / 28.0);
138 Draw;
139 sys_Repaint;
140 Frame := Time
141 end
142 else
143 sys_Delay(1);
145 e_SoundUpdate();
146 end;
148 function SDLMain (): Integer;
149 var
150 idx: Integer;
151 arg: AnsiString;
152 mdfo: TStream;
153 {$IFDEF ENABLE_HOLMES}
154 itmp: Integer;
155 valres: Word;
156 {$ENDIF}
157 begin
159 idx := 1;
160 while (idx <= ParamCount) do
161 begin
162 arg := ParamStr(idx);
163 Inc(idx);
164 if arg = '--jah' then g_profile_history_size := 100;
165 if arg = '--no-particles' then gpart_dbg_enabled := false;
166 if arg = '--no-los' then gmon_dbg_los_enabled := false;
168 if arg = '--profile-render' then g_profile_frame_draw := true;
169 if arg = '--profile-coldet' then g_profile_collision := true;
170 if arg = '--profile-los' then g_profile_los := true;
172 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
173 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
174 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
175 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
176 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
177 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
179 if arg = '--debug-input' then g_dbg_input := True;
181 {.$IF DEFINED(D2F_DEBUG)}
182 if arg = '--aimline' then g_dbg_aimline_on := true;
183 {.$ENDIF}
185 {$IFDEF ENABLE_HOLMES}
186 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
188 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
189 begin
190 if (idx <= ParamCount) then
191 begin
192 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
193 Inc(idx);
194 end;
195 end;
197 if (arg = '--holmes-font') or (arg = '-holmes-font') then
198 begin
199 if (idx <= ParamCount) then
200 begin
201 itmp := 0;
202 val(ParamStr(idx), itmp, valres);
203 {$IFNDEF HEADLESS}
204 if (valres = 0) and (not g_holmes_imfunctional) then
205 begin
206 case itmp of
207 8: uiContext.font := 'win8';
208 14: uiContext.font := 'win14';
209 16: uiContext.font := 'win16';
210 end;
211 end;
212 {$ELSE}
213 // fuck off, fpc!
214 itmp := itmp;
215 valres := valres;
216 {$ENDIF}
217 Inc(idx);
218 end;
219 end;
220 {$ENDIF}
222 if (arg = '--game-scale') or (arg = '-game-scale') then
223 begin
224 if (idx <= ParamCount) then
225 begin
226 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
227 Inc(idx);
228 end;
229 end;
231 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
232 begin
233 mdfo := createDiskFile('mapdef.txt');
234 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
235 mdfo.Free();
236 Halt(0);
237 end;
239 if (arg = '--pixel-scale') or (arg = '-pixel-scale') then
240 begin
241 if (idx <= ParamCount) then
242 begin
243 if not conParseFloat(r_pixel_scale, ParamStr(idx)) then r_pixel_scale := 1.0;
244 Inc(idx);
245 end;
246 end;
247 end;
249 r_Window_Initialize;
251 Init;
252 Time_Old := sys_GetTicks();
254 g_Net_InitLowLevel();
256 // Êîìàíäíàÿ ñòðîêà
257 if (ParamCount > 0) then g_Game_Process_Params();
259 {$IFNDEF HEADLESS}
260 // Çàïðîñ ÿçûêà
261 if (not gGameOn) and gAskLanguage then g_Menu_AskLanguage();
262 {$ENDIF}
264 e_WriteLog('Entering the main loop', TMsgType.Notify);
266 // main loop
267 while not ProcessMessage() do begin end;
269 g_Net_Slist_ShutdownAll();
271 Release();
273 g_Net_DeinitLowLevel();
274 result := 0;
275 end;
278 initialization
279 conRegVar('d_input', @g_dbg_input, '', '')
280 end.