DEADSOFTWARE

MORE DARKNESS!!!
[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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_window;
19 interface
21 uses
22 utils;
24 function SDLMain (): Integer;
25 function GetTimer (): Int64;
26 procedure ResetTimer ();
27 function CreateGLWindow (Title: PChar): Boolean;
28 procedure KillGLWindow ();
29 procedure PushExitEvent ();
30 function ProcessMessage (): Boolean;
31 procedure ReDrawWindow ();
32 procedure SwapBuffers ();
33 procedure Sleep (ms: LongWord);
34 function GetDisplayModes (dbpp: LongWord; var selres: LongWord): SSArray;
35 function g_Window_SetDisplay (preserveGL: Boolean=false): Boolean;
36 function g_Window_SetSize (w, h: Word; fullscreen: Boolean): Boolean;
38 procedure ProcessLoading (forceUpdate: Boolean=false);
40 // returns `true` if quit event was received
41 function g_ProcessMessages (): Boolean;
44 var
45 gwin_dump_extensions: Boolean = false;
46 gwin_has_stencil: Boolean = false;
47 gwin_k8_enable_light_experiments: Boolean = false;
48 g_dbg_aimline_on: Boolean = false;
51 implementation
53 uses
54 {$IFDEF WINDOWS}Windows,{$ENDIF}
55 SysUtils, Classes, MAPDEF,
56 SDL2, GL, GLExt, e_graphics, e_log, e_texture, g_main,
57 g_console, e_input, g_options, g_game,
58 g_basic, g_textures, e_sound, g_sound, g_menu, ENet, g_net,
59 g_map, g_gfx, g_monsters, g_holmes, xprofiler,
60 sdlcarcass, fui_ctls;
63 const
64 ProgressUpdateMSecs = 1;//100;
66 var
67 h_Wnd: PSDL_Window = nil;
68 h_GL: TSDL_GLContext = nil;
69 Time, Time_Delta, Time_Old: Int64;
70 flag: Boolean;
71 {$IF not DEFINED(HEADLESS)}
72 wTitle: PChar = nil;
73 {$ENDIF}
74 wNeedTimeReset: Boolean = false;
75 wMinimized: Boolean = false;
76 wLoadingProgress: Boolean = false;
77 wLoadingQuit: Boolean = false;
78 {$IFNDEF WINDOWS}
79 ticksOverflow: Int64 = -1;
80 lastTicks: Uint32 = 0; // to detect overflow
81 {$ENDIF}
84 procedure KillGLWindow ();
85 begin
86 if (h_GL <> nil) then begin if (assigned(oglDeinitCB)) then oglDeinitCB(); end;
87 if (h_Wnd <> nil) then SDL_DestroyWindow(h_Wnd);
88 if (h_GL <> nil) then SDL_GL_DeleteContext(h_GL);
89 h_Wnd := nil;
90 h_GL := nil;
91 end;
94 function g_Window_SetDisplay (preserveGL: Boolean = false): Boolean;
95 {$IF not DEFINED(HEADLESS)}
96 var
97 mode, cmode: TSDL_DisplayMode;
98 wFlags: LongWord = 0;
99 {$ENDIF}
100 begin
101 {$IF not DEFINED(HEADLESS)}
102 result := false;
104 e_WriteLog('Setting display mode...', TMsgType.Notify);
106 wFlags := SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE;
107 if gFullscreen then wFlags := wFlags or SDL_WINDOW_FULLSCREEN;
108 if gWinMaximized then wFlags := wFlags or SDL_WINDOW_MAXIMIZED;
110 KillGLWindow();
112 if gFullscreen then
113 begin
114 mode.w := gScreenWidth;
115 mode.h := gScreenHeight;
116 mode.format := 0;
117 mode.refresh_rate := 0;
118 mode.driverdata := nil;
119 if (SDL_GetClosestDisplayMode(0, @mode, @cmode) = nil) then
120 begin
121 gScreenWidth := 800;
122 gScreenHeight := 600;
123 end
124 else
125 begin
126 gScreenWidth := cmode.w;
127 gScreenHeight := cmode.h;
128 end;
129 end;
131 h_Wnd := SDL_CreateWindow(PChar(wTitle), gWinRealPosX, gWinRealPosY, gScreenWidth, gScreenHeight, wFlags);
132 if (h_Wnd = nil) then exit;
134 SDL_GL_MakeCurrent(h_Wnd, h_GL);
135 SDL_ShowCursor(SDL_DISABLE);
136 fuiScrWdt := gScreenWidth;
137 fuiScrHgt := gScreenHeight;
138 if (h_GL <> nil) then begin if (assigned(oglInitCB)) then oglInitCB(); end;
139 {$ENDIF}
141 result := true;
142 end;
145 function GetDisplayModes (dbpp: LongWord; var selres: LongWord): SSArray;
146 var
147 mode: TSDL_DisplayMode;
148 res, i, k, n, pw, ph: Integer;
149 begin
150 SetLength(result, 0);
151 {$IFDEF HEADLESS}exit;{$ENDIF}
152 k := 0; selres := 0;
153 n := SDL_GetNumDisplayModes(0);
154 pw := 0; ph := 0;
155 for i := 0 to n do
156 begin
157 res := SDL_GetDisplayMode(0, i, @mode);
158 if res < 0 then continue;
159 if SDL_BITSPERPIXEL(mode.format) = gBPP then continue;
160 if (mode.w = pw) and (mode.h = ph) then continue;
161 if (mode.w = gScreenWidth) and (mode.h = gScreenHeight) then
162 selres := k;
163 Inc(k);
164 SetLength(result, k);
165 result[k-1] := IntToStr(mode.w) + 'x' + IntToStr(mode.h);
166 pw := mode.w; ph := mode.h
167 end;
169 e_WriteLog('SDL: Got ' + IntToStr(k) + ' resolutions.', TMsgType.Notify);
170 end;
173 procedure Sleep (ms: LongWord);
174 begin
175 SDL_Delay(ms);
176 end;
179 procedure ChangeWindowSize ();
180 begin
181 gWinSizeX := gScreenWidth;
182 gWinSizeY := gScreenHeight;
183 {$IF not DEFINED(HEADLESS)}
184 fuiScrWdt := gScreenWidth;
185 fuiScrHgt := gScreenHeight;
186 e_ResizeWindow(gScreenWidth, gScreenHeight);
187 g_Game_SetupScreenSize();
188 g_Menu_Reset();
189 g_Game_ClearLoading();
190 {$ENDIF}
191 end;
194 function g_Window_SetSize (w, h: Word; fullscreen: Boolean): Boolean;
195 {$IF not DEFINED(HEADLESS)}
196 var
197 preserve: Boolean;
198 {$ENDIF}
199 begin
200 result := false;
201 {$IF not DEFINED(HEADLESS)}
202 preserve := false;
204 if (gScreenWidth <> w) or (gScreenHeight <> h) then
205 begin
206 result := true;
207 gScreenWidth := w;
208 gScreenHeight := h;
209 end;
211 if (gFullscreen <> fullscreen) then
212 begin
213 result := true;
214 gFullscreen := fullscreen;
215 preserve := true;
216 end;
218 if result then
219 begin
220 g_Window_SetDisplay(preserve);
221 ChangeWindowSize();
222 end;
223 {$ENDIF}
224 end;
227 function WindowEventHandler (constref ev: TSDL_WindowEvent): Boolean;
228 var
229 wActivate, wDeactivate: Boolean;
230 begin
231 result := false;
232 wActivate := false;
233 wDeactivate := false;
235 case ev.event of
236 SDL_WINDOWEVENT_MOVED:
237 begin
238 if not (gFullscreen or gWinMaximized) then
239 begin
240 gWinRealPosX := ev.data1;
241 gWinRealPosY := ev.data2;
242 end;
243 end;
245 SDL_WINDOWEVENT_MINIMIZED:
246 begin
247 e_UnpressAllKeys();
248 if not wMinimized then
249 begin
250 e_ResizeWindow(0, 0);
251 wMinimized := true;
252 if g_debug_WinMsgs then
253 begin
254 g_Console_Add('Now minimized');
255 e_WriteLog('[DEBUG] WinMsgs: Now minimized', TMsgType.Notify);
256 end;
257 wDeactivate := true;
258 end;
259 end;
261 SDL_WINDOWEVENT_RESIZED:
262 begin
263 gScreenWidth := ev.data1;
264 gScreenHeight := ev.data2;
265 ChangeWindowSize();
266 SwapBuffers();
267 if g_debug_WinMsgs then
268 begin
269 g_Console_Add('Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2));
270 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2), TMsgType.Notify);
271 end;
272 end;
274 SDL_WINDOWEVENT_EXPOSED:
275 SwapBuffers();
277 SDL_WINDOWEVENT_MAXIMIZED:
278 begin
279 if wMinimized then
280 begin
281 e_ResizeWindow(gScreenWidth, gScreenHeight);
282 wMinimized := false;
283 wActivate := true;
284 end;
285 if not gWinMaximized then
286 begin
287 gWinMaximized := true;
288 if g_debug_WinMsgs then
289 begin
290 g_Console_Add('Now maximized');
291 e_WriteLog('[DEBUG] WinMsgs: Now maximized', TMsgType.Notify);
292 end;
293 end;
294 end;
296 SDL_WINDOWEVENT_RESTORED:
297 begin
298 if wMinimized then
299 begin
300 e_ResizeWindow(gScreenWidth, gScreenHeight);
301 wMinimized := false;
302 wActivate := true;
303 end;
304 if gWinMaximized then gWinMaximized := false;
305 if g_debug_WinMsgs then
306 begin
307 g_Console_Add('Now restored');
308 e_WriteLog('[DEBUG] WinMsgs: Now restored', TMsgType.Notify);
309 end;
310 end;
312 SDL_WINDOWEVENT_FOCUS_GAINED:
313 begin
314 wActivate := true;
315 //e_WriteLog('window gained focus!', MSG_NOTIFY);
316 end;
318 SDL_WINDOWEVENT_FOCUS_LOST:
319 begin
320 wDeactivate := true;
321 e_UnpressAllKeys();
322 //e_WriteLog('window lost focus!', MSG_NOTIFY);
323 end;
324 end;
326 if wDeactivate then
327 begin
328 if gWinActive then
329 begin
330 e_WriteLog('deactivating window', TMsgType.Notify);
331 e_EnableInput := false;
332 e_ClearInputBuffer();
334 if gMuteWhenInactive then
335 begin
336 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
337 e_MuteChannels(true);
338 end;
340 if g_debug_WinMsgs then
341 begin
342 g_Console_Add('Now inactive');
343 e_WriteLog('[DEBUG] WinMsgs: Now inactive', TMsgType.Notify);
344 end;
346 gWinActive := false;
348 if assigned(winBlurCB) then winBlurCB();
349 end;
350 end
351 else if wActivate then
352 begin
353 if not gWinActive then
354 begin
355 //e_WriteLog('activating window', MSG_NOTIFY);
356 e_EnableInput := true;
358 if gMuteWhenInactive then
359 begin
360 //e_WriteLog('activating sounds', MSG_NOTIFY);
361 e_MuteChannels(false);
362 end;
364 if g_debug_WinMsgs then
365 begin
366 g_Console_Add('Now active');
367 e_WriteLog('[DEBUG] WinMsgs: Now active', TMsgType.Notify);
368 end;
370 gWinActive := true;
371 if assigned(winFocusCB) then winFocusCB();
372 end;
373 end;
374 end;
377 function EventHandler (var ev: TSDL_Event): Boolean;
378 var
379 key, keychr: Word;
380 uc: UnicodeChar;
381 down: Boolean;
382 begin
383 result := false;
385 case ev.type_ of
386 SDL_WINDOWEVENT:
387 result := WindowEventHandler(ev.window);
389 SDL_QUITEV:
390 begin
391 if (gExit <> EXIT_QUIT) then
392 begin
393 if not wLoadingProgress then
394 begin
395 g_Game_Free();
396 g_Game_Quit();
397 end
398 else
399 begin
400 wLoadingQuit := true;
401 end;
402 end;
403 result := true;
404 end;
406 SDL_KEYDOWN, SDL_KEYUP:
407 begin
408 key := ev.key.keysym.scancode;
409 down := (ev.type_ = SDL_KEYDOWN);
410 {$IF not DEFINED(HEADLESS)}
411 if fuiOnSDLEvent(ev) then
412 begin
413 // event eaten, but...
414 if not down then e_KeyUpDown(key, false);
415 exit;
416 end;
417 {$ENDIF}
418 if down then KeyPress(key);
419 e_KeyUpDown(key, down);
420 end;
422 {$IF not DEFINED(HEADLESS)}
423 SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION:
424 fuiOnSDLEvent(ev);
425 {$ENDIF}
427 SDL_TEXTINPUT:
428 begin
429 Utf8ToUnicode(@uc, PChar(ev.text.text), 1);
430 keychr := Word(uc);
431 if (keychr > 127) then keychr := Word(wchar2win(WideChar(keychr)));
432 if (keychr > 0) and (keychr <= 255) then CharPress(AnsiChar(keychr));
433 end;
435 // other key presses and joysticks are handled in e_input
436 end;
437 end;
440 procedure SwapBuffers ();
441 begin
442 {$IF not DEFINED(HEADLESS)}
443 SDL_GL_SwapWindow(h_Wnd);
444 {$ENDIF}
445 end;
448 function CreateGLWindow (Title: PChar): Boolean;
449 begin
450 result := false;
452 gWinSizeX := gScreenWidth;
453 gWinSizeY := gScreenHeight;
455 {$IF not DEFINED(HEADLESS)}
456 wTitle := Title;
457 {$ENDIF}
458 e_WriteLog('Creating window', TMsgType.Notify);
460 if not g_Window_SetDisplay() then
461 begin
462 KillGLWindow();
463 e_WriteLog('Window creation error (resolution not supported?)', TMsgType.Fatal);
464 exit;
465 end;
467 {$IF not DEFINED(HEADLESS)}
468 h_Gl := SDL_GL_CreateContext(h_Wnd);
469 if (h_Gl = nil) then exit;
470 fuiScrWdt := gScreenWidth;
471 fuiScrHgt := gScreenHeight;
472 if (assigned(oglInitCB)) then oglInitCB();
473 {$ENDIF}
475 e_ResizeWindow(gScreenWidth, gScreenHeight);
476 e_InitGL();
478 result := true;
479 end;
482 {$IFDEF WINDOWS}
483 // windoze sux; in headless mode `GetTickCount()` (and SDL) returns shit
484 function GetTimer (): Int64;
485 var
486 F, C: Int64;
487 begin
488 QueryPerformanceFrequency(F);
489 QueryPerformanceCounter(C);
490 result := Round(C/F*1000{000});
491 end;
492 {$ELSE}
493 function GetTimer (): Int64;
494 var
495 t: Uint32;
496 tt: Int64;
497 begin
498 t := SDL_GetTicks();
499 if (ticksOverflow = -1) then
500 begin
501 ticksOverflow := 0;
502 lastTicks := t;
503 end
504 else
505 begin
506 if (lastTicks > t) then
507 begin
508 // overflow, increment overflow ;-)
509 ticksOverflow := ticksOverflow+(Int64($ffffffff)+Int64(1));
510 tt := (Int64($ffffffff)+Int64(1))+Int64(t);
511 t := Uint32(tt-lastTicks);
512 end;
513 end;
514 lastTicks := t;
515 result := ticksOverflow+Int64(t);
516 end;
517 {$ENDIF}
520 procedure ResetTimer ();
521 begin
522 wNeedTimeReset := true;
523 end;
526 procedure PushExitEvent ();
527 var
528 ev: TSDL_Event;
529 begin
530 ev.type_ := SDL_QUITEV;
531 SDL_PushEvent(@ev);
532 end;
535 var
536 prevLoadingUpdateTime: UInt64 = 0;
538 procedure ProcessLoading (forceUpdate: Boolean=false);
539 var
540 ev: TSDL_Event;
541 ID: LongWord;
542 stt: UInt64;
543 begin
544 FillChar(ev, sizeof(ev), 0);
545 wLoadingProgress := true;
547 while (SDL_PollEvent(@ev) > 0) do
548 begin
549 EventHandler(ev);
550 if (ev.type_ = SDL_QUITEV) then break;
551 end;
552 e_PollJoysticks();
554 if (ev.type_ = SDL_QUITEV) or (gExit = EXIT_QUIT) then
555 begin
556 wLoadingProgress := false;
557 exit;
558 end;
560 if not wMinimized then
561 begin
562 if forceUpdate then
563 begin
564 prevLoadingUpdateTime := getTimeMilli();
565 end
566 else
567 begin
568 stt := getTimeMilli();
569 if (stt < prevLoadingUpdateTime) or (stt-prevLoadingUpdateTime >= ProgressUpdateMSecs) then
570 begin
571 prevLoadingUpdateTime := stt;
572 forceUpdate := true;
573 end;
574 end;
576 if forceUpdate then
577 begin
578 if g_Texture_Get('INTER', ID) then
579 begin
580 e_DrawSize(ID, 0, 0, 0, false, false, gScreenWidth, gScreenHeight);
581 e_DarkenQuadWH(0, 0, gScreenWidth, gScreenHeight, 150);
582 end
583 else
584 begin
585 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
586 end;
588 DrawLoadingStat();
589 SwapBuffers();
590 end;
591 end;
593 e_SoundUpdate();
595 if NetMode = NET_SERVER then
596 begin
597 g_Net_Host_Update();
598 end
599 else
600 begin
601 if (NetMode = NET_CLIENT) and (NetState <> NET_STATE_AUTH) then g_Net_Client_UpdateWhileLoading();
602 end;
604 wLoadingProgress := false;
605 end;
608 function g_ProcessMessages (): Boolean;
609 var
610 ev: TSDL_Event;
611 begin
612 result := false;
613 FillChar(ev, SizeOf(ev), 0);
614 while (SDL_PollEvent(@ev) > 0) do
615 begin
616 result := EventHandler(ev);
617 if (ev.type_ = SDL_QUITEV) then exit;
618 end;
619 e_PollJoysticks();
620 end;
623 function ProcessMessage (): Boolean;
624 var
625 i, t: Integer;
626 begin
627 result := g_ProcessMessages();
629 Time := GetTimer();
630 Time_Delta := Time-Time_Old;
632 flag := false;
634 if wNeedTimeReset then
635 begin
636 Time_Delta := 28;
637 wNeedTimeReset := false;
638 end;
640 g_Map_ProfilersBegin();
641 g_Mons_ProfilersBegin();
643 t := Time_Delta div 28;
644 if (t > 0) then
645 begin
646 flag := true;
647 for i := 1 to t do
648 begin
649 if (NetMode = NET_SERVER) then g_Net_Host_Update()
650 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
651 Update();
652 end;
653 end
654 else
655 begin
656 if (NetMode = NET_SERVER) then g_Net_Host_Update()
657 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
658 end;
660 g_Map_ProfilersEnd();
661 g_Mons_ProfilersEnd();
663 if wLoadingQuit then
664 begin
665 g_Game_Free();
666 g_Game_Quit();
667 end;
669 if (gExit = EXIT_QUIT) then
670 begin
671 result := true;
672 exit;
673 end;
675 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
676 if flag then
677 begin
678 Time_Old := Time-(Time_Delta mod 28);
679 if (not wMinimized) then
680 begin
681 Draw();
682 SwapBuffers();
683 end;
684 end
685 else
686 begin
687 Sleep(1); // release time slice, so we won't eat 100% CPU
688 end;
690 e_SoundUpdate();
691 end;
694 procedure ReDrawWindow ();
695 begin
696 SwapBuffers();
697 end;
700 procedure InitOpenGL (vsync: Boolean);
701 {$IF not DEFINED(HEADLESS)}
702 var
703 v: Byte;
704 {$ENDIF}
705 begin
706 {$IF not DEFINED(HEADLESS)}
707 if vsync then v := 1 else v := 0;
708 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
709 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
710 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
711 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
712 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
713 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
714 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
715 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
716 SDL_GL_SetSwapInterval(v);
717 {$ENDIF}
718 end;
721 function glHasExtension (const name: AnsiString): Boolean;
722 var
723 exts: PChar;
724 i: Integer;
725 found: Boolean;
726 extName: ShortString;
727 begin
728 result := false;
729 if (Length(name) = 0) then exit;
730 exts := glGetString(GL_EXTENSIONS);
731 if (exts = nil) then exit;
732 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
733 while (exts[0] <> #0) do
734 begin
735 if gwin_dump_extensions then
736 begin
737 i := 0;
738 while (exts[i] <> #0) and (exts[i] <> ' ') do Inc(i);
739 if i > 255 then
740 begin
741 e_WriteLog('FUUUUUUUUUUUUU', TMsgType.Warning);
742 end
743 else
744 begin
745 Move(exts^, extName[1], i);
746 extName[0] := Char(i);
747 e_WriteLog(Format('EXT: %s', [extName]), TMsgType.Notify);
748 end;
749 end;
750 found := true;
751 for i := 0 to length(name)-1 do
752 begin
753 if (exts[i] = #0) then begin found := false; break; end;
754 if (exts[i] <> name[i+1]) then begin found := false; break; end;
755 end;
756 if found and ((exts[Length(name)] = #0) or (exts[Length(name)] = ' ')) then begin result := true; exit; end;
757 while (exts[0] <> #0) and (exts[0] <> ' ') do Inc(exts);
758 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
759 end;
760 end;
763 function SDLMain (): Integer;
764 var
765 idx: Integer;
766 {$IF not DEFINED(HEADLESS)}
767 ltmp: Integer;
768 {$ENDIF}
769 arg: AnsiString;
770 mdfo: TStream;
771 itmp: Integer;
772 valres: Word;
773 begin
774 {$IFDEF HEADLESS}
775 e_NoGraphics := true;
776 {$ELSE}
777 if (not g_holmes_imfunctional) then
778 begin
779 uiInitialize();
780 uiContext.font := 'win14';
781 end;
782 {$ENDIF}
784 idx := 1;
785 while (idx <= ParamCount) do
786 begin
787 arg := ParamStr(idx);
788 Inc(idx);
789 if arg = '--opengl-dump-exts' then gwin_dump_extensions := true;
790 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
791 if arg = '--jah' then g_profile_history_size := 100;
792 if arg = '--no-particles' then gpart_dbg_enabled := false;
793 if arg = '--no-los' then gmon_dbg_los_enabled := false;
795 if arg = '--profile-render' then g_profile_frame_draw := true;
796 if arg = '--profile-coldet' then g_profile_collision := true;
797 if arg = '--profile-los' then g_profile_los := true;
799 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
800 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
801 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
802 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
803 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
804 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
806 {.$IF DEFINED(D2F_DEBUG)}
807 if arg = '--aimline' then g_dbg_aimline_on := true;
808 {.$ENDIF}
810 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
812 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
813 begin
814 if (idx <= ParamCount) then
815 begin
816 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
817 Inc(idx);
818 end;
819 end;
821 if (arg = '--holmes-font') or (arg = '-holmes-font') then
822 begin
823 if (idx <= ParamCount) then
824 begin
825 itmp := 0;
826 val(ParamStr(idx), itmp, valres);
827 {$IFNDEF HEADLESS}
828 if (valres = 0) and (not g_holmes_imfunctional) then
829 begin
830 case itmp of
831 8: uiContext.font := 'win8';
832 14: uiContext.font := 'win14';
833 16: uiContext.font := 'win16';
834 end;
835 end;
836 {$ELSE}
837 // fuck off, fpc!
838 itmp := itmp;
839 valres := valres;
840 {$ENDIF}
841 Inc(idx);
842 end;
843 end;
845 if (arg = '--game-scale') or (arg = '-game-scale') then
846 begin
847 if (idx <= ParamCount) then
848 begin
849 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
850 Inc(idx);
851 end;
852 end;
854 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
855 begin
856 mdfo := createDiskFile('mapdef.txt');
857 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
858 mdfo.Free();
859 Halt(0);
860 end;
861 end;
863 e_WriteLog('Initializing OpenGL', TMsgType.Notify);
864 InitOpenGL(gVSync);
866 e_WriteLog('Creating GL window', TMsgType.Notify);
867 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION]))) then
868 begin
869 result := 0;
870 exit;
871 end;
873 {EnumDisplayModes();}
875 {$IFDEF HEADLESS}
876 //gwin_k8_enable_light_experiments := false;
877 gwin_has_stencil := false;
878 glLegacyNPOT := false;
879 gwin_dump_extensions := false;
880 {$ELSE}
881 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, @ltmp);
882 e_LogWritefln('stencil buffer size: %s', [ltmp]);
883 gwin_has_stencil := (ltmp > 0);
885 if not glHasExtension('GL_ARB_texture_non_power_of_two') then
886 begin
887 e_WriteLog('NPOT textures: NO', TMsgType.Warning);
888 glLegacyNPOT := true;
889 end
890 else
891 begin
892 e_WriteLog('NPOT textures: YES', TMsgType.Notify);
893 glLegacyNPOT := false;
894 end;
895 gwin_dump_extensions := false;
896 {$ENDIF}
898 Init();
899 Time_Old := GetTimer();
901 // Êîìàíäíàÿ ñòðîêà
902 if (ParamCount > 0) then g_Game_Process_Params();
904 // Çàïðîñ ÿçûêà
905 if (not gGameOn) and gAskLanguage then g_Menu_AskLanguage();
907 e_WriteLog('Entering the main loop', TMsgType.Notify);
909 // main loop
910 while not ProcessMessage() do begin end;
912 Release();
913 KillGLWindow();
915 result := 0;
916 end;
919 end.