DEADSOFTWARE

cosmetix
[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 if (h_GL <> nil) then begin if (assigned(oglInitCB)) then oglInitCB(); end;
137 {$ENDIF}
139 result := true;
140 end;
143 function GetDisplayModes (dbpp: LongWord; var selres: LongWord): SSArray;
144 var
145 mode: TSDL_DisplayMode;
146 res, i, k, n, pw, ph: Integer;
147 begin
148 SetLength(result, 0);
149 {$IFDEF HEADLESS}exit;{$ENDIF}
150 k := 0; selres := 0;
151 n := SDL_GetNumDisplayModes(0);
152 pw := 0; ph := 0;
153 for i := 0 to n do
154 begin
155 res := SDL_GetDisplayMode(0, i, @mode);
156 if res < 0 then continue;
157 if SDL_BITSPERPIXEL(mode.format) = gBPP then continue;
158 if (mode.w = pw) and (mode.h = ph) then continue;
159 if (mode.w = gScreenWidth) and (mode.h = gScreenHeight) then
160 selres := k;
161 Inc(k);
162 SetLength(result, k);
163 result[k-1] := IntToStr(mode.w) + 'x' + IntToStr(mode.h);
164 pw := mode.w; ph := mode.h
165 end;
167 e_WriteLog('SDL: Got ' + IntToStr(k) + ' resolutions.', TMsgType.Notify);
168 end;
171 procedure Sleep (ms: LongWord);
172 begin
173 SDL_Delay(ms);
174 end;
177 procedure ChangeWindowSize ();
178 begin
179 gWinSizeX := gScreenWidth;
180 gWinSizeY := gScreenHeight;
181 {$IF not DEFINED(HEADLESS)}
182 e_ResizeWindow(gScreenWidth, gScreenHeight);
183 g_Game_SetupScreenSize();
184 g_Menu_Reset();
185 g_Game_ClearLoading();
186 {$ENDIF}
187 end;
190 function g_Window_SetSize (w, h: Word; fullscreen: Boolean): Boolean;
191 {$IF not DEFINED(HEADLESS)}
192 var
193 preserve: Boolean;
194 {$ENDIF}
195 begin
196 result := false;
197 {$IF not DEFINED(HEADLESS)}
198 preserve := false;
200 if (gScreenWidth <> w) or (gScreenHeight <> h) then
201 begin
202 result := true;
203 gScreenWidth := w;
204 gScreenHeight := h;
205 end;
207 if (gFullscreen <> fullscreen) then
208 begin
209 result := true;
210 gFullscreen := fullscreen;
211 preserve := true;
212 end;
214 if result then
215 begin
216 g_Window_SetDisplay(preserve);
217 ChangeWindowSize();
218 end;
219 {$ENDIF}
220 end;
223 function WindowEventHandler (constref ev: TSDL_WindowEvent): Boolean;
224 var
225 wActivate, wDeactivate: Boolean;
226 begin
227 result := false;
228 wActivate := false;
229 wDeactivate := false;
231 case ev.event of
232 SDL_WINDOWEVENT_MOVED:
233 begin
234 if not (gFullscreen or gWinMaximized) then
235 begin
236 gWinRealPosX := ev.data1;
237 gWinRealPosY := ev.data2;
238 end;
239 end;
241 SDL_WINDOWEVENT_MINIMIZED:
242 begin
243 e_UnpressAllKeys();
244 if not wMinimized then
245 begin
246 e_ResizeWindow(0, 0);
247 wMinimized := true;
248 if g_debug_WinMsgs then
249 begin
250 g_Console_Add('Now minimized');
251 e_WriteLog('[DEBUG] WinMsgs: Now minimized', TMsgType.Notify);
252 end;
253 wDeactivate := true;
254 end;
255 end;
257 SDL_WINDOWEVENT_RESIZED:
258 begin
259 gScreenWidth := ev.data1;
260 gScreenHeight := ev.data2;
261 ChangeWindowSize();
262 SwapBuffers();
263 if g_debug_WinMsgs then
264 begin
265 g_Console_Add('Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2));
266 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2), TMsgType.Notify);
267 end;
268 end;
270 SDL_WINDOWEVENT_EXPOSED:
271 SwapBuffers();
273 SDL_WINDOWEVENT_MAXIMIZED:
274 begin
275 if wMinimized then
276 begin
277 e_ResizeWindow(gScreenWidth, gScreenHeight);
278 wMinimized := false;
279 wActivate := true;
280 end;
281 if not gWinMaximized then
282 begin
283 gWinMaximized := true;
284 if g_debug_WinMsgs then
285 begin
286 g_Console_Add('Now maximized');
287 e_WriteLog('[DEBUG] WinMsgs: Now maximized', TMsgType.Notify);
288 end;
289 end;
290 end;
292 SDL_WINDOWEVENT_RESTORED:
293 begin
294 if wMinimized then
295 begin
296 e_ResizeWindow(gScreenWidth, gScreenHeight);
297 wMinimized := false;
298 wActivate := true;
299 end;
300 if gWinMaximized then gWinMaximized := false;
301 if g_debug_WinMsgs then
302 begin
303 g_Console_Add('Now restored');
304 e_WriteLog('[DEBUG] WinMsgs: Now restored', TMsgType.Notify);
305 end;
306 end;
308 SDL_WINDOWEVENT_FOCUS_GAINED:
309 begin
310 wActivate := true;
311 //e_WriteLog('window gained focus!', MSG_NOTIFY);
312 end;
314 SDL_WINDOWEVENT_FOCUS_LOST:
315 begin
316 wDeactivate := true;
317 e_UnpressAllKeys();
318 //e_WriteLog('window lost focus!', MSG_NOTIFY);
319 end;
320 end;
322 if wDeactivate then
323 begin
324 if gWinActive then
325 begin
326 e_WriteLog('deactivating window', TMsgType.Notify);
327 e_EnableInput := false;
328 e_ClearInputBuffer();
330 if gMuteWhenInactive then
331 begin
332 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
333 e_MuteChannels(true);
334 end;
336 if g_debug_WinMsgs then
337 begin
338 g_Console_Add('Now inactive');
339 e_WriteLog('[DEBUG] WinMsgs: Now inactive', TMsgType.Notify);
340 end;
342 gWinActive := false;
344 if assigned(winBlurCB) then winBlurCB();
345 end;
346 end
347 else if wActivate then
348 begin
349 if not gWinActive then
350 begin
351 //e_WriteLog('activating window', MSG_NOTIFY);
352 e_EnableInput := true;
354 if gMuteWhenInactive then
355 begin
356 //e_WriteLog('activating sounds', MSG_NOTIFY);
357 e_MuteChannels(false);
358 end;
360 if g_debug_WinMsgs then
361 begin
362 g_Console_Add('Now active');
363 e_WriteLog('[DEBUG] WinMsgs: Now active', TMsgType.Notify);
364 end;
366 gWinActive := true;
367 if assigned(winFocusCB) then winFocusCB();
368 end;
369 end;
370 end;
373 function EventHandler (var ev: TSDL_Event): Boolean;
374 var
375 key, keychr: Word;
376 uc: UnicodeChar;
377 down: Boolean;
378 begin
379 result := false;
381 case ev.type_ of
382 SDL_WINDOWEVENT:
383 result := WindowEventHandler(ev.window);
385 SDL_QUITEV:
386 begin
387 if (gExit <> EXIT_QUIT) then
388 begin
389 if not wLoadingProgress then
390 begin
391 g_Game_Free();
392 g_Game_Quit();
393 end
394 else
395 begin
396 wLoadingQuit := true;
397 end;
398 end;
399 result := true;
400 end;
402 SDL_KEYDOWN, SDL_KEYUP:
403 begin
404 key := ev.key.keysym.scancode;
405 down := (ev.type_ = SDL_KEYDOWN);
406 {$IF not DEFINED(HEADLESS)}
407 if fuiOnSDLEvent(ev) then
408 begin
409 // event eaten, but...
410 if not down then e_KeyUpDown(key, false);
411 exit;
412 end;
413 {$ENDIF}
414 if down then KeyPress(key);
415 e_KeyUpDown(key, down);
416 end;
418 {$IF not DEFINED(HEADLESS)}
419 SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION:
420 fuiOnSDLEvent(ev);
421 {$ENDIF}
423 SDL_TEXTINPUT:
424 begin
425 Utf8ToUnicode(@uc, PChar(ev.text.text), 1);
426 keychr := Word(uc);
427 if (keychr > 127) then keychr := Word(wchar2win(WideChar(keychr)));
428 if (keychr > 0) and (keychr <= 255) then CharPress(AnsiChar(keychr));
429 end;
431 // other key presses and joysticks are handled in e_input
432 end;
433 end;
436 procedure SwapBuffers ();
437 begin
438 {$IF not DEFINED(HEADLESS)}
439 SDL_GL_SwapWindow(h_Wnd);
440 {$ENDIF}
441 end;
444 function CreateGLWindow (Title: PChar): Boolean;
445 begin
446 result := false;
448 gWinSizeX := gScreenWidth;
449 gWinSizeY := gScreenHeight;
451 {$IF not DEFINED(HEADLESS)}
452 wTitle := Title;
453 {$ENDIF}
454 e_WriteLog('Creating window', TMsgType.Notify);
456 if not g_Window_SetDisplay() then
457 begin
458 KillGLWindow();
459 e_WriteLog('Window creation error (resolution not supported?)', TMsgType.Fatal);
460 exit;
461 end;
463 {$IF not DEFINED(HEADLESS)}
464 h_Gl := SDL_GL_CreateContext(h_Wnd);
465 if (h_Gl = nil) then exit;
466 if (assigned(oglInitCB)) then oglInitCB();
467 {$ENDIF}
469 e_ResizeWindow(gScreenWidth, gScreenHeight);
470 e_InitGL();
472 result := true;
473 end;
476 {$IFDEF WINDOWS}
477 // windoze sux; in headless mode `GetTickCount()` (and SDL) returns shit
478 function GetTimer (): Int64;
479 var
480 F, C: Int64;
481 begin
482 QueryPerformanceFrequency(F);
483 QueryPerformanceCounter(C);
484 result := Round(C/F*1000{000});
485 end;
486 {$ELSE}
487 function GetTimer (): Int64;
488 var
489 t: Uint32;
490 tt: Int64;
491 begin
492 t := SDL_GetTicks();
493 if (ticksOverflow = -1) then
494 begin
495 ticksOverflow := 0;
496 lastTicks := t;
497 end
498 else
499 begin
500 if (lastTicks > t) then
501 begin
502 // overflow, increment overflow ;-)
503 ticksOverflow := ticksOverflow+(Int64($ffffffff)+Int64(1));
504 tt := (Int64($ffffffff)+Int64(1))+Int64(t);
505 t := Uint32(tt-lastTicks);
506 end;
507 end;
508 lastTicks := t;
509 result := ticksOverflow+Int64(t);
510 end;
511 {$ENDIF}
514 procedure ResetTimer ();
515 begin
516 wNeedTimeReset := true;
517 end;
520 procedure PushExitEvent ();
521 var
522 ev: TSDL_Event;
523 begin
524 ev.type_ := SDL_QUITEV;
525 SDL_PushEvent(@ev);
526 end;
529 var
530 prevLoadingUpdateTime: UInt64 = 0;
532 procedure ProcessLoading (forceUpdate: Boolean=false);
533 var
534 ev: TSDL_Event;
535 ID: LongWord;
536 stt: UInt64;
537 begin
538 FillChar(ev, sizeof(ev), 0);
539 wLoadingProgress := true;
541 while (SDL_PollEvent(@ev) > 0) do
542 begin
543 EventHandler(ev);
544 if (ev.type_ = SDL_QUITEV) then break;
545 end;
546 e_PollJoysticks();
548 if (ev.type_ = SDL_QUITEV) or (gExit = EXIT_QUIT) then
549 begin
550 wLoadingProgress := false;
551 exit;
552 end;
554 if not wMinimized then
555 begin
556 if forceUpdate then
557 begin
558 prevLoadingUpdateTime := getTimeMilli();
559 end
560 else
561 begin
562 stt := getTimeMilli();
563 if (stt < prevLoadingUpdateTime) or (stt-prevLoadingUpdateTime >= ProgressUpdateMSecs) then
564 begin
565 prevLoadingUpdateTime := stt;
566 forceUpdate := true;
567 end;
568 end;
570 if forceUpdate then
571 begin
572 if g_Texture_Get('INTER', ID) then
573 begin
574 e_DrawSize(ID, 0, 0, 0, false, false, gScreenWidth, gScreenHeight)
575 end
576 else
577 begin
578 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
579 end;
581 DrawLoadingStat();
582 SwapBuffers();
583 end;
584 end;
586 e_SoundUpdate();
588 if NetMode = NET_SERVER then
589 begin
590 g_Net_Host_Update();
591 end
592 else
593 begin
594 if (NetMode = NET_CLIENT) and (NetState <> NET_STATE_AUTH) then g_Net_Client_UpdateWhileLoading();
595 end;
597 wLoadingProgress := false;
598 end;
601 function g_ProcessMessages (): Boolean;
602 var
603 ev: TSDL_Event;
604 begin
605 result := false;
606 FillChar(ev, SizeOf(ev), 0);
607 while (SDL_PollEvent(@ev) > 0) do
608 begin
609 result := EventHandler(ev);
610 if (ev.type_ = SDL_QUITEV) then exit;
611 end;
612 e_PollJoysticks();
613 end;
616 function ProcessMessage (): Boolean;
617 var
618 i, t: Integer;
619 begin
620 result := g_ProcessMessages();
622 Time := GetTimer();
623 Time_Delta := Time-Time_Old;
625 flag := false;
627 if wNeedTimeReset then
628 begin
629 Time_Delta := 28;
630 wNeedTimeReset := false;
631 end;
633 g_Map_ProfilersBegin();
634 g_Mons_ProfilersBegin();
636 t := Time_Delta div 28;
637 if (t > 0) then
638 begin
639 flag := true;
640 for i := 1 to t do
641 begin
642 if (NetMode = NET_SERVER) then g_Net_Host_Update()
643 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
644 Update();
645 end;
646 end
647 else
648 begin
649 if (NetMode = NET_SERVER) then g_Net_Host_Update()
650 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
651 end;
653 g_Map_ProfilersEnd();
654 g_Mons_ProfilersEnd();
656 if wLoadingQuit then
657 begin
658 g_Game_Free();
659 g_Game_Quit();
660 end;
662 if (gExit = EXIT_QUIT) then
663 begin
664 result := true;
665 exit;
666 end;
668 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
669 if flag then
670 begin
671 Time_Old := Time-(Time_Delta mod 28);
672 if (not wMinimized) then
673 begin
674 Draw();
675 SwapBuffers();
676 end;
677 end
678 else
679 begin
680 Sleep(1); // release time slice, so we won't eat 100% CPU
681 end;
683 e_SoundUpdate();
684 end;
687 procedure ReDrawWindow ();
688 begin
689 SwapBuffers();
690 end;
693 procedure InitOpenGL (vsync: Boolean);
694 {$IF not DEFINED(HEADLESS)}
695 var
696 v: Byte;
697 {$ENDIF}
698 begin
699 {$IF not DEFINED(HEADLESS)}
700 if vsync then v := 1 else v := 0;
701 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
702 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
703 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
704 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
705 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
706 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
707 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
708 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
709 SDL_GL_SetSwapInterval(v);
710 {$ENDIF}
711 end;
714 function glHasExtension (const name: AnsiString): Boolean;
715 var
716 exts: PChar;
717 i: Integer;
718 found: Boolean;
719 extName: ShortString;
720 begin
721 result := false;
722 if (Length(name) = 0) then exit;
723 exts := glGetString(GL_EXTENSIONS);
724 if (exts = nil) then exit;
725 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
726 while (exts[0] <> #0) do
727 begin
728 if gwin_dump_extensions then
729 begin
730 i := 0;
731 while (exts[i] <> #0) and (exts[i] <> ' ') do Inc(i);
732 if i > 255 then
733 begin
734 e_WriteLog('FUUUUUUUUUUUUU', TMsgType.Warning);
735 end
736 else
737 begin
738 Move(exts^, extName[1], i);
739 extName[0] := Char(i);
740 e_WriteLog(Format('EXT: %s', [extName]), TMsgType.Notify);
741 end;
742 end;
743 found := true;
744 for i := 0 to length(name)-1 do
745 begin
746 if (exts[i] = #0) then begin found := false; break; end;
747 if (exts[i] <> name[i+1]) then begin found := false; break; end;
748 end;
749 if found and ((exts[Length(name)] = #0) or (exts[Length(name)] = ' ')) then begin result := true; exit; end;
750 while (exts[0] <> #0) and (exts[0] <> ' ') do Inc(exts);
751 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
752 end;
753 end;
756 function SDLMain (): Integer;
757 var
758 idx: Integer;
759 {$IF not DEFINED(HEADLESS)}
760 ltmp: Integer;
761 {$ENDIF}
762 arg: AnsiString;
763 mdfo: TStream;
764 itmp: Integer;
765 valres: Word;
766 begin
767 {$IFDEF HEADLESS}
768 e_NoGraphics := true;
769 {$ELSE}
770 uiInitialize();
771 uiContext.font := 'win14';
772 {$ENDIF}
774 idx := 1;
775 while (idx <= ParamCount) do
776 begin
777 arg := ParamStr(idx);
778 Inc(idx);
779 if arg = '--opengl-dump-exts' then gwin_dump_extensions := true;
780 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
781 if arg = '--jah' then g_profile_history_size := 100;
782 if arg = '--no-particles' then gpart_dbg_enabled := false;
783 if arg = '--no-los' then gmon_dbg_los_enabled := false;
785 if arg = '--profile-render' then g_profile_frame_draw := true;
786 if arg = '--profile-coldet' then g_profile_collision := true;
787 if arg = '--profile-los' then g_profile_los := true;
789 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
790 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
791 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
792 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
793 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
794 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
796 {.$IF DEFINED(D2F_DEBUG)}
797 if arg = '--aimline' then g_dbg_aimline_on := true;
798 {.$ENDIF}
800 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
802 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
803 begin
804 if (idx <= ParamCount) then
805 begin
806 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
807 Inc(idx);
808 end;
809 end;
811 if (arg = '--holmes-font') or (arg = '-holmes-font') then
812 begin
813 if (idx <= ParamCount) then
814 begin
815 itmp := 0;
816 val(ParamStr(idx), itmp, valres);
817 {$IFNDEF HEADLESS}
818 if (valres = 0) and (not g_holmes_imfunctional) then
819 begin
820 case itmp of
821 8: uiContext.font := 'win8';
822 14: uiContext.font := 'win14';
823 16: uiContext.font := 'win16';
824 end;
825 end;
826 {$ELSE}
827 // fuck off, fpc!
828 itmp := itmp;
829 valres := valres;
830 {$ENDIF}
831 Inc(idx);
832 end;
833 end;
835 if (arg = '--game-scale') or (arg = '-game-scale') then
836 begin
837 if (idx <= ParamCount) then
838 begin
839 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
840 Inc(idx);
841 end;
842 end;
844 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
845 begin
846 mdfo := createDiskFile('mapdef.txt');
847 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
848 mdfo.Free();
849 Halt(0);
850 end;
851 end;
853 e_WriteLog('Initializing OpenGL', TMsgType.Notify);
854 InitOpenGL(gVSync);
856 e_WriteLog('Creating GL window', TMsgType.Notify);
857 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION]))) then
858 begin
859 result := 0;
860 exit;
861 end;
863 {EnumDisplayModes();}
865 {$IFDEF HEADLESS}
866 //gwin_k8_enable_light_experiments := false;
867 gwin_has_stencil := false;
868 glLegacyNPOT := false;
869 gwin_dump_extensions := false;
870 {$ELSE}
871 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, @ltmp);
872 e_LogWritefln('stencil buffer size: %s', [ltmp]);
873 gwin_has_stencil := (ltmp > 0);
875 if not glHasExtension('GL_ARB_texture_non_power_of_two') then
876 begin
877 e_WriteLog('NPOT textures: NO', TMsgType.Warning);
878 glLegacyNPOT := true;
879 end
880 else
881 begin
882 e_WriteLog('NPOT textures: YES', TMsgType.Notify);
883 glLegacyNPOT := false;
884 end;
885 gwin_dump_extensions := false;
886 {$ENDIF}
888 Init();
889 Time_Old := GetTimer();
891 // Êîìàíäíàÿ ñòðîêà
892 if (ParamCount > 0) then g_Game_Process_Params();
894 // Çàïðîñ ÿçûêà
895 if (not gGameOn) and gAskLanguage then g_Menu_AskLanguage();
897 e_WriteLog('Entering the main loop', TMsgType.Notify);
899 // main loop
900 while not ProcessMessage() do begin end;
902 Release();
903 KillGLWindow();
905 result := 0;
906 end;
909 end.