DEADSOFTWARE

fixed keyboard polling (no more); ESC should work in several places where it should...
[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, gh_ui;
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_Wnd <> nil) then
87 begin
88 if assigned(oglDeinitCB) then oglDeinitCB();
89 end;
90 if (h_Wnd <> nil) then SDL_DestroyWindow(h_Wnd);
91 if (h_GL <> nil) then SDL_GL_DeleteContext(h_GL);
92 h_Wnd := nil;
93 h_GL := nil;
94 end;
97 function g_Window_SetDisplay (preserveGL: Boolean = false): Boolean;
98 {$IF not DEFINED(HEADLESS)}
99 var
100 mode, cmode: TSDL_DisplayMode;
101 wFlags: LongWord = 0;
102 {$ENDIF}
103 begin
104 {$IF not DEFINED(HEADLESS)}
105 result := false;
107 e_WriteLog('Setting display mode...', TMsgType.Notify);
109 wFlags := SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE;
110 if gFullscreen then wFlags := wFlags or SDL_WINDOW_FULLSCREEN;
111 if gWinMaximized then wFlags := wFlags or SDL_WINDOW_MAXIMIZED;
113 KillGLWindow();
115 if gFullscreen then
116 begin
117 mode.w := gScreenWidth;
118 mode.h := gScreenHeight;
119 mode.format := 0;
120 mode.refresh_rate := 0;
121 mode.driverdata := nil;
122 if (SDL_GetClosestDisplayMode(0, @mode, @cmode) = nil) then
123 begin
124 gScreenWidth := 800;
125 gScreenHeight := 600;
126 end
127 else
128 begin
129 gScreenWidth := cmode.w;
130 gScreenHeight := cmode.h;
131 end;
132 end;
134 h_Wnd := SDL_CreateWindow(PChar(wTitle), gWinRealPosX, gWinRealPosY, gScreenWidth, gScreenHeight, wFlags);
135 if (h_Wnd = nil) then exit;
137 SDL_GL_MakeCurrent(h_Wnd, h_GL);
138 SDL_ShowCursor(SDL_DISABLE);
139 if (h_GL <> nil) then
140 begin
141 if assigned(oglInitCB) then oglInitCB();
142 end;
143 {$ENDIF}
145 result := true;
146 end;
149 function GetDisplayModes (dbpp: LongWord; var selres: LongWord): SSArray;
150 var
151 mode: TSDL_DisplayMode;
152 res, i, k, n, pw, ph: Integer;
153 begin
154 SetLength(result, 0);
155 {$IFDEF HEADLESS}exit;{$ENDIF}
156 k := 0; selres := 0;
157 n := SDL_GetNumDisplayModes(0);
158 pw := 0; ph := 0;
159 for i := 0 to n do
160 begin
161 res := SDL_GetDisplayMode(0, i, @mode);
162 if res < 0 then continue;
163 if SDL_BITSPERPIXEL(mode.format) = gBPP then continue;
164 if (mode.w = pw) and (mode.h = ph) then continue;
165 if (mode.w = gScreenWidth) and (mode.h = gScreenHeight) then
166 selres := k;
167 Inc(k);
168 SetLength(result, k);
169 result[k-1] := IntToStr(mode.w) + 'x' + IntToStr(mode.h);
170 pw := mode.w; ph := mode.h
171 end;
173 e_WriteLog('SDL: Got ' + IntToStr(k) + ' resolutions.', TMsgType.Notify);
174 end;
177 procedure Sleep (ms: LongWord);
178 begin
179 SDL_Delay(ms);
180 end;
183 procedure ChangeWindowSize ();
184 begin
185 gWinSizeX := gScreenWidth;
186 gWinSizeY := gScreenHeight;
187 {$IF not DEFINED(HEADLESS)}
188 e_ResizeWindow(gScreenWidth, gScreenHeight);
189 g_Game_SetupScreenSize();
190 g_Menu_Reset();
191 g_Game_ClearLoading();
192 {$ENDIF}
193 end;
196 function g_Window_SetSize (w, h: Word; fullscreen: Boolean): Boolean;
197 {$IF not DEFINED(HEADLESS)}
198 var
199 preserve: Boolean;
200 {$ENDIF}
201 begin
202 result := false;
203 {$IF not DEFINED(HEADLESS)}
204 preserve := false;
206 if (gScreenWidth <> w) or (gScreenHeight <> h) then
207 begin
208 result := true;
209 gScreenWidth := w;
210 gScreenHeight := h;
211 end;
213 if (gFullscreen <> fullscreen) then
214 begin
215 result := true;
216 gFullscreen := fullscreen;
217 preserve := true;
218 end;
220 if result then
221 begin
222 g_Window_SetDisplay(preserve);
223 ChangeWindowSize();
224 end;
225 {$ENDIF}
226 end;
229 function WindowEventHandler (constref ev: TSDL_WindowEvent): Boolean;
230 var
231 wActivate, wDeactivate: Boolean;
232 begin
233 result := false;
234 wActivate := false;
235 wDeactivate := false;
237 case ev.event of
238 SDL_WINDOWEVENT_MOVED:
239 begin
240 if not (gFullscreen or gWinMaximized) then
241 begin
242 gWinRealPosX := ev.data1;
243 gWinRealPosY := ev.data2;
244 end;
245 end;
247 SDL_WINDOWEVENT_MINIMIZED:
248 begin
249 e_UnpressAllKeys();
250 if not wMinimized then
251 begin
252 e_ResizeWindow(0, 0);
253 wMinimized := true;
254 if g_debug_WinMsgs then
255 begin
256 g_Console_Add('Now minimized');
257 e_WriteLog('[DEBUG] WinMsgs: Now minimized', TMsgType.Notify);
258 end;
259 wDeactivate := true;
260 end;
261 end;
263 SDL_WINDOWEVENT_RESIZED:
264 begin
265 gScreenWidth := ev.data1;
266 gScreenHeight := ev.data2;
267 ChangeWindowSize();
268 SwapBuffers();
269 if g_debug_WinMsgs then
270 begin
271 g_Console_Add('Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2));
272 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2), TMsgType.Notify);
273 end;
274 end;
276 SDL_WINDOWEVENT_EXPOSED:
277 SwapBuffers();
279 SDL_WINDOWEVENT_MAXIMIZED:
280 begin
281 if wMinimized then
282 begin
283 e_ResizeWindow(gScreenWidth, gScreenHeight);
284 wMinimized := false;
285 wActivate := true;
286 end;
287 if not gWinMaximized then
288 begin
289 gWinMaximized := true;
290 if g_debug_WinMsgs then
291 begin
292 g_Console_Add('Now maximized');
293 e_WriteLog('[DEBUG] WinMsgs: Now maximized', TMsgType.Notify);
294 end;
295 end;
296 end;
298 SDL_WINDOWEVENT_RESTORED:
299 begin
300 if wMinimized then
301 begin
302 e_ResizeWindow(gScreenWidth, gScreenHeight);
303 wMinimized := false;
304 wActivate := true;
305 end;
306 if gWinMaximized then gWinMaximized := false;
307 if g_debug_WinMsgs then
308 begin
309 g_Console_Add('Now restored');
310 e_WriteLog('[DEBUG] WinMsgs: Now restored', TMsgType.Notify);
311 end;
312 end;
314 SDL_WINDOWEVENT_FOCUS_GAINED:
315 begin
316 wActivate := true;
317 //e_WriteLog('window gained focus!', MSG_NOTIFY);
318 end;
320 SDL_WINDOWEVENT_FOCUS_LOST:
321 begin
322 wDeactivate := true;
323 e_UnpressAllKeys();
324 //e_WriteLog('window lost focus!', MSG_NOTIFY);
325 end;
326 end;
328 if wDeactivate then
329 begin
330 if gWinActive then
331 begin
332 e_WriteLog('deactivating window', TMsgType.Notify);
333 e_EnableInput := false;
334 e_ClearInputBuffer();
336 if gMuteWhenInactive then
337 begin
338 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
339 e_MuteChannels(true);
340 end;
342 if g_debug_WinMsgs then
343 begin
344 g_Console_Add('Now inactive');
345 e_WriteLog('[DEBUG] WinMsgs: Now inactive', TMsgType.Notify);
346 end;
348 gWinActive := false;
350 if assigned(winBlurCB) then winBlurCB();
351 end;
352 end
353 else if wActivate then
354 begin
355 if not gWinActive then
356 begin
357 //e_WriteLog('activating window', MSG_NOTIFY);
358 e_EnableInput := true;
360 if gMuteWhenInactive then
361 begin
362 //e_WriteLog('activating sounds', MSG_NOTIFY);
363 e_MuteChannels(false);
364 end;
366 if g_debug_WinMsgs then
367 begin
368 g_Console_Add('Now active');
369 e_WriteLog('[DEBUG] WinMsgs: Now active', TMsgType.Notify);
370 end;
372 gWinActive := true;
373 if assigned(winFocusCB) then winFocusCB();
374 end;
375 end;
376 end;
379 function EventHandler (var ev: TSDL_Event): Boolean;
380 var
381 key, keychr: Word;
382 uc: UnicodeChar;
383 down: Boolean;
384 begin
385 result := false;
387 case ev.type_ of
388 SDL_WINDOWEVENT:
389 result := WindowEventHandler(ev.window);
391 SDL_QUITEV:
392 begin
393 if (gExit <> EXIT_QUIT) then
394 begin
395 if not wLoadingProgress then
396 begin
397 g_Game_Free();
398 g_Game_Quit();
399 end
400 else
401 begin
402 wLoadingQuit := true;
403 end;
404 end;
405 result := true;
406 end;
408 SDL_KEYDOWN, SDL_KEYUP:
409 begin
410 key := ev.key.keysym.scancode;
411 down := (ev.type_ = SDL_KEYDOWN);
412 {$IF not DEFINED(HEADLESS)}
413 if evSDLCB(ev) then
414 begin
415 // event eaten, but...
416 if not down then e_KeyUpDown(key, false);
417 exit;
418 end;
419 {$ENDIF}
420 if down then KeyPress(key);
421 e_KeyUpDown(key, down);
422 end;
424 {$IF not DEFINED(HEADLESS)}
425 SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION:
426 evSDLCB(ev);
427 {$ENDIF}
429 SDL_TEXTINPUT:
430 begin
431 Utf8ToUnicode(@uc, PChar(ev.text.text), 1);
432 keychr := Word(uc);
433 if (keychr > 127) then keychr := Word(wchar2win(WideChar(keychr)));
434 CharPress(AnsiChar(keychr));
435 end;
437 // other key presses and joysticks are handled in e_input
438 end;
439 end;
442 procedure SwapBuffers ();
443 begin
444 {$IF not DEFINED(HEADLESS)}
445 SDL_GL_SwapWindow(h_Wnd);
446 {$ENDIF}
447 end;
450 function CreateGLWindow (Title: PChar): Boolean;
451 begin
452 result := false;
454 gWinSizeX := gScreenWidth;
455 gWinSizeY := gScreenHeight;
457 {$IF not DEFINED(HEADLESS)}
458 wTitle := Title;
459 {$ENDIF}
460 e_WriteLog('Creating window', TMsgType.Notify);
462 if not g_Window_SetDisplay() then
463 begin
464 KillGLWindow();
465 e_WriteLog('Window creation error (resolution not supported?)', TMsgType.Fatal);
466 exit;
467 end;
469 {$IF not DEFINED(HEADLESS)}
470 h_Gl := SDL_GL_CreateContext(h_Wnd);
471 if (h_Gl = nil) then exit;
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 begin
771 {$IFDEF HEADLESS}
772 e_NoGraphics := true;
773 {$ENDIF}
775 idx := 1;
776 while (idx <= ParamCount) do
777 begin
778 arg := ParamStr(idx);
779 Inc(idx);
780 if arg = '--opengl-dump-exts' then gwin_dump_extensions := true;
781 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
782 if arg = '--jah' then g_profile_history_size := 100;
783 if arg = '--no-particles' then gpart_dbg_enabled := false;
784 if arg = '--no-los' then gmon_dbg_los_enabled := false;
786 if arg = '--profile-render' then g_profile_frame_draw := true;
787 if arg = '--profile-coldet' then g_profile_collision := true;
788 if arg = '--profile-los' then g_profile_los := true;
790 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
791 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
792 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
793 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
794 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
795 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
797 {.$IF DEFINED(D2F_DEBUG)}
798 if arg = '--aimline' then g_dbg_aimline_on := true;
799 {.$ENDIF}
801 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(gh_ui_scale, ParamStr(idx)) then gh_ui_scale := 1.0;
807 Inc(idx);
808 end;
809 end;
811 if (arg = '--game-scale') or (arg = '-game-scale') then
812 begin
813 if (idx <= ParamCount) then
814 begin
815 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
816 Inc(idx);
817 end;
818 end;
820 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
821 begin
822 mdfo := createDiskFile('mapdef.txt');
823 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
824 mdfo.Free();
825 Halt(0);
826 end;
827 end;
829 e_WriteLog('Initializing OpenGL', TMsgType.Notify);
830 InitOpenGL(gVSync);
832 e_WriteLog('Creating GL window', TMsgType.Notify);
833 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION]))) then
834 begin
835 result := 0;
836 exit;
837 end;
839 {EnumDisplayModes();}
841 {$IFDEF HEADLESS}
842 //gwin_k8_enable_light_experiments := false;
843 gwin_has_stencil := false;
844 glLegacyNPOT := false;
845 gwin_dump_extensions := false;
846 {$ELSE}
847 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, @ltmp);
848 e_LogWritefln('stencil buffer size: %s', [ltmp]);
849 gwin_has_stencil := (ltmp > 0);
851 if not glHasExtension('GL_ARB_texture_non_power_of_two') then
852 begin
853 e_WriteLog('NPOT textures: NO', TMsgType.Warning);
854 glLegacyNPOT := true;
855 end
856 else
857 begin
858 e_WriteLog('NPOT textures: YES', TMsgType.Notify);
859 glLegacyNPOT := false;
860 end;
861 gwin_dump_extensions := false;
862 {$ENDIF}
864 Init();
865 Time_Old := GetTimer();
867 // Êîìàíäíàÿ ñòðîêà
868 if (ParamCount > 0) then g_Game_Process_Params();
870 // Çàïðîñ ÿçûêà
871 if (not gGameOn) and gAskLanguage then g_Menu_AskLanguage();
873 e_WriteLog('Entering the main loop', TMsgType.Notify);
875 // main loop
876 while not ProcessMessage() do begin end;
878 Release();
879 KillGLWindow();
881 result := 0;
882 end;
885 end.