DEADSOFTWARE

Merge branch 'master' of ssh://repo.or.cz/d2df-sdl
[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 = 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 end
582 else
583 begin
584 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
585 end;
587 DrawLoadingStat();
588 SwapBuffers();
589 end;
590 end;
592 e_SoundUpdate();
594 if NetMode = NET_SERVER then
595 begin
596 g_Net_Host_Update();
597 end
598 else
599 begin
600 if (NetMode = NET_CLIENT) and (NetState <> NET_STATE_AUTH) then g_Net_Client_UpdateWhileLoading();
601 end;
603 wLoadingProgress := false;
604 end;
607 function g_ProcessMessages (): Boolean;
608 var
609 ev: TSDL_Event;
610 begin
611 result := false;
612 FillChar(ev, SizeOf(ev), 0);
613 while (SDL_PollEvent(@ev) > 0) do
614 begin
615 result := EventHandler(ev);
616 if (ev.type_ = SDL_QUITEV) then exit;
617 end;
618 e_PollJoysticks();
619 end;
622 function ProcessMessage (): Boolean;
623 var
624 i, t: Integer;
625 begin
626 result := g_ProcessMessages();
628 Time := GetTimer();
629 Time_Delta := Time-Time_Old;
631 flag := false;
633 if wNeedTimeReset then
634 begin
635 Time_Delta := 28;
636 wNeedTimeReset := false;
637 end;
639 g_Map_ProfilersBegin();
640 g_Mons_ProfilersBegin();
642 t := Time_Delta div 28;
643 if (t > 0) then
644 begin
645 flag := true;
646 for i := 1 to t do
647 begin
648 if (NetMode = NET_SERVER) then g_Net_Host_Update()
649 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
650 Update();
651 end;
652 end
653 else
654 begin
655 if (NetMode = NET_SERVER) then g_Net_Host_Update()
656 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
657 end;
659 g_Map_ProfilersEnd();
660 g_Mons_ProfilersEnd();
662 if wLoadingQuit then
663 begin
664 g_Game_Free();
665 g_Game_Quit();
666 end;
668 if (gExit = EXIT_QUIT) then
669 begin
670 result := true;
671 exit;
672 end;
674 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
675 if flag then
676 begin
677 Time_Old := Time-(Time_Delta mod 28);
678 if (not wMinimized) then
679 begin
680 Draw();
681 SwapBuffers();
682 end;
683 end
684 else
685 begin
686 Sleep(1); // release time slice, so we won't eat 100% CPU
687 end;
689 e_SoundUpdate();
690 end;
693 procedure ReDrawWindow ();
694 begin
695 SwapBuffers();
696 end;
699 procedure InitOpenGL (vsync: Boolean);
700 {$IF not DEFINED(HEADLESS)}
701 var
702 v: Byte;
703 {$ENDIF}
704 begin
705 {$IF not DEFINED(HEADLESS)}
706 if vsync then v := 1 else v := 0;
707 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
708 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
709 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
710 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
711 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
712 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
713 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
714 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
715 SDL_GL_SetSwapInterval(v);
716 {$ENDIF}
717 end;
720 function glHasExtension (const name: AnsiString): Boolean;
721 var
722 exts: PChar;
723 i: Integer;
724 found: Boolean;
725 extName: ShortString;
726 begin
727 result := false;
728 if (Length(name) = 0) then exit;
729 exts := glGetString(GL_EXTENSIONS);
730 if (exts = nil) then exit;
731 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
732 while (exts[0] <> #0) do
733 begin
734 if gwin_dump_extensions then
735 begin
736 i := 0;
737 while (exts[i] <> #0) and (exts[i] <> ' ') do Inc(i);
738 if i > 255 then
739 begin
740 e_WriteLog('FUUUUUUUUUUUUU', TMsgType.Warning);
741 end
742 else
743 begin
744 Move(exts^, extName[1], i);
745 extName[0] := Char(i);
746 e_WriteLog(Format('EXT: %s', [extName]), TMsgType.Notify);
747 end;
748 end;
749 found := true;
750 for i := 0 to length(name)-1 do
751 begin
752 if (exts[i] = #0) then begin found := false; break; end;
753 if (exts[i] <> name[i+1]) then begin found := false; break; end;
754 end;
755 if found and ((exts[Length(name)] = #0) or (exts[Length(name)] = ' ')) then begin result := true; exit; end;
756 while (exts[0] <> #0) and (exts[0] <> ' ') do Inc(exts);
757 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
758 end;
759 end;
762 function SDLMain (): Integer;
763 var
764 idx: Integer;
765 {$IF not DEFINED(HEADLESS)}
766 ltmp: Integer;
767 {$ENDIF}
768 arg: AnsiString;
769 mdfo: TStream;
770 itmp: Integer;
771 valres: Word;
772 begin
773 {$IFDEF HEADLESS}
774 e_NoGraphics := true;
775 {$ELSE}
776 if (not g_holmes_imfunctional) then
777 begin
778 uiInitialize();
779 uiContext.font := 'win14';
780 end;
781 {$ENDIF}
783 idx := 1;
784 while (idx <= ParamCount) do
785 begin
786 arg := ParamStr(idx);
787 Inc(idx);
788 if arg = '--opengl-dump-exts' then gwin_dump_extensions := true;
789 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
790 if arg = '--jah' then g_profile_history_size := 100;
791 if arg = '--no-particles' then gpart_dbg_enabled := false;
792 if arg = '--no-los' then gmon_dbg_los_enabled := false;
794 if arg = '--profile-render' then g_profile_frame_draw := true;
795 if arg = '--profile-coldet' then g_profile_collision := true;
796 if arg = '--profile-los' then g_profile_los := true;
798 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
799 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
800 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
801 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
802 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
803 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
805 {.$IF DEFINED(D2F_DEBUG)}
806 if arg = '--aimline' then g_dbg_aimline_on := true;
807 {.$ENDIF}
809 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
811 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
812 begin
813 if (idx <= ParamCount) then
814 begin
815 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
816 Inc(idx);
817 end;
818 end;
820 if (arg = '--holmes-font') or (arg = '-holmes-font') then
821 begin
822 if (idx <= ParamCount) then
823 begin
824 itmp := 0;
825 val(ParamStr(idx), itmp, valres);
826 {$IFNDEF HEADLESS}
827 if (valres = 0) and (not g_holmes_imfunctional) then
828 begin
829 case itmp of
830 8: uiContext.font := 'win8';
831 14: uiContext.font := 'win14';
832 16: uiContext.font := 'win16';
833 end;
834 end;
835 {$ELSE}
836 // fuck off, fpc!
837 itmp := itmp;
838 valres := valres;
839 {$ENDIF}
840 Inc(idx);
841 end;
842 end;
844 if (arg = '--game-scale') or (arg = '-game-scale') then
845 begin
846 if (idx <= ParamCount) then
847 begin
848 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
849 Inc(idx);
850 end;
851 end;
853 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
854 begin
855 mdfo := createDiskFile('mapdef.txt');
856 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
857 mdfo.Free();
858 Halt(0);
859 end;
860 end;
862 e_WriteLog('Initializing OpenGL', TMsgType.Notify);
863 InitOpenGL(gVSync);
865 e_WriteLog('Creating GL window', TMsgType.Notify);
866 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION]))) then
867 begin
868 result := 0;
869 exit;
870 end;
872 {EnumDisplayModes();}
874 {$IFDEF HEADLESS}
875 //gwin_k8_enable_light_experiments := false;
876 gwin_has_stencil := false;
877 glLegacyNPOT := false;
878 gwin_dump_extensions := false;
879 {$ELSE}
880 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, @ltmp);
881 e_LogWritefln('stencil buffer size: %s', [ltmp]);
882 gwin_has_stencil := (ltmp > 0);
884 if not glHasExtension('GL_ARB_texture_non_power_of_two') then
885 begin
886 e_WriteLog('NPOT textures: NO', TMsgType.Warning);
887 glLegacyNPOT := true;
888 end
889 else
890 begin
891 e_WriteLog('NPOT textures: YES', TMsgType.Notify);
892 glLegacyNPOT := false;
893 end;
894 gwin_dump_extensions := false;
895 {$ENDIF}
897 Init();
898 Time_Old := GetTimer();
900 // Êîìàíäíàÿ ñòðîêà
901 if (ParamCount > 0) then g_Game_Process_Params();
903 // Çàïðîñ ÿçûêà
904 if (not gGameOn) and gAskLanguage then g_Menu_AskLanguage();
906 e_WriteLog('Entering the main loop', TMsgType.Notify);
908 // main loop
909 while not ProcessMessage() do begin end;
911 Release();
912 KillGLWindow();
914 result := 0;
915 end;
918 end.