DEADSOFTWARE

hackfix for video resolution change (still not working right, see commit text)
[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 procedure PushExitEvent ();
28 function ProcessMessage (): Boolean;
29 procedure ReDrawWindow ();
30 procedure SwapBuffers ();
31 procedure Sleep (ms: LongWord);
32 function GetDisplayModes (dbpp: LongWord; var selres: LongWord): SSArray;
33 function g_Window_SetDisplay (preserveGL: Boolean=false): Boolean;
34 function g_Window_SetSize (w, h: Word; fullscreen: Boolean): Boolean;
36 procedure ProcessLoading (forceUpdate: Boolean=false);
38 // returns `true` if quit event was received
39 function g_ProcessMessages (): Boolean;
42 var
43 gwin_dump_extensions: Boolean = false;
44 gwin_has_stencil: Boolean = false;
45 gwin_k8_enable_light_experiments: Boolean = false;
46 g_dbg_aimline_on: Boolean = false;
49 implementation
51 uses
52 {$IFDEF WINDOWS}Windows,{$ENDIF}
53 SysUtils, Classes, MAPDEF,
54 SDL2, GL, GLExt, e_graphics, e_log, e_texture, g_main,
55 g_console, e_input, g_options, g_game,
56 g_basic, g_textures, e_sound, g_sound, g_menu, ENet, g_net,
57 g_map, g_gfx, g_monsters, g_holmes, xprofiler,
58 sdlcarcass, fui_ctls;
61 const
62 ProgressUpdateMSecs = 1;//100;
64 var
65 h_Wnd: PSDL_Window = nil;
66 h_GL: TSDL_GLContext = nil;
67 Time, Time_Delta, Time_Old: Int64;
68 flag: Boolean;
69 {$IF not DEFINED(HEADLESS)}
70 wTitle: PChar = nil;
71 {$ENDIF}
72 wNeedTimeReset: Boolean = false;
73 wMinimized: Boolean = false;
74 wLoadingProgress: Boolean = false;
75 wLoadingQuit: Boolean = false;
76 {$IFNDEF WINDOWS}
77 ticksOverflow: Int64 = -1;
78 lastTicks: Uint32 = 0; // to detect overflow
79 {$ENDIF}
82 procedure KillGLWindow (preserveGL: Boolean);
83 begin
84 if (h_GL <> nil) and (not preserveGL) then begin if (assigned(oglDeinitCB)) then oglDeinitCB(); end;
85 if (h_Wnd <> nil) then SDL_DestroyWindow(h_Wnd);
86 if (h_GL <> nil) and (not preserveGL) then SDL_GL_DeleteContext(h_GL);
87 h_Wnd := nil;
88 if (not preserveGL) then h_GL := nil;
89 end;
92 function g_Window_SetDisplay (preserveGL: Boolean = false): Boolean;
93 {$IF not DEFINED(HEADLESS)}
94 var
95 mode, cmode: TSDL_DisplayMode;
96 wFlags: LongWord = 0;
97 nw, nh: Integer;
98 {$ENDIF}
99 begin
100 {$IF not DEFINED(HEADLESS)}
101 result := false;
103 e_WriteLog('Setting display mode...', TMsgType.Notify);
105 wFlags := SDL_WINDOW_OPENGL; // or SDL_WINDOW_RESIZABLE;
106 if gFullscreen then wFlags := wFlags or SDL_WINDOW_FULLSCREEN or SDL_WINDOW_BORDERLESS else wFlags := wFlags or SDL_WINDOW_RESIZABLE;
107 //if gWinMaximized then wFlags := wFlags or SDL_WINDOW_MAXIMIZED;
109 if gFullscreen then
110 begin
111 mode.w := gScreenWidth;
112 mode.h := gScreenHeight;
113 mode.format := 0;
114 mode.refresh_rate := 0;
115 mode.driverdata := nil;
116 if (SDL_GetClosestDisplayMode(0, @mode, @cmode) = nil) then
117 begin
118 e_WriteLog('SDL: cannot find display mode for '+IntToStr(gScreenWidth), TMsgType.Notify);
119 gScreenWidth := 800;
120 gScreenHeight := 600;
121 end
122 else
123 begin
124 e_WriteLog('SDL: found display mode for '+IntToStr(gScreenWidth)+'x'+IntToStr(gScreenHeight)+': '+IntToStr(cmode.w)+'x'+IntToStr(cmode.h), TMsgType.Notify);
125 gScreenWidth := cmode.w;
126 gScreenHeight := cmode.h;
127 end;
128 end;
130 KillGLWindow(preserveGL);
132 h_Wnd := SDL_CreateWindow(PChar(wTitle), gWinRealPosX, gWinRealPosY, gScreenWidth, gScreenHeight, wFlags);
133 if (h_Wnd = nil) then exit;
135 SDL_GL_MakeCurrent(h_Wnd, h_GL);
136 SDL_ShowCursor(SDL_DISABLE);
137 if (gFullscreen) then
138 begin
139 nw := 0;
140 nh := 0;
141 SDL_GetWindowSize(h_Wnd, @nw, @nh);
142 if (nw > 128) and (nh > 128) then
143 begin
144 e_WriteLog('SDL: fullscreen window got size '+IntToStr(nw)+'x'+IntToStr(nh)+': '+IntToStr(gScreenWidth)+'x'+IntToStr(gScreenHeight), TMsgType.Notify);
145 gScreenWidth := nw;
146 gScreenHeight := nh;
147 end
148 else
149 begin
150 e_WriteLog('SDL: fullscreen window got invalid size: '+IntToStr(nw)+'x'+IntToStr(nh), TMsgType.Notify);
151 end;
152 end;
153 fuiScrWdt := gScreenWidth;
154 fuiScrHgt := gScreenHeight;
155 if (h_GL <> nil) and (not preserveGL) then begin if (assigned(oglInitCB)) then oglInitCB(); end;
156 {$ENDIF}
158 result := true;
159 end;
162 function GetDisplayModes (dbpp: LongWord; var selres: LongWord): SSArray;
163 var
164 mode: TSDL_DisplayMode;
165 res, i, k, n, pw, ph: Integer;
166 begin
167 SetLength(result, 0);
168 {$IFDEF HEADLESS}exit;{$ENDIF}
169 k := 0; selres := 0;
170 n := SDL_GetNumDisplayModes(0);
171 pw := 0; ph := 0;
172 for i := 0 to n do
173 begin
174 res := SDL_GetDisplayMode(0, i, @mode);
175 if res < 0 then continue;
176 if SDL_BITSPERPIXEL(mode.format) = gBPP then continue;
177 if (mode.w = pw) and (mode.h = ph) then continue;
178 if (mode.w = gScreenWidth) and (mode.h = gScreenHeight) then
179 selres := k;
180 Inc(k);
181 SetLength(result, k);
182 result[k-1] := IntToStr(mode.w) + 'x' + IntToStr(mode.h);
183 pw := mode.w; ph := mode.h
184 end;
186 e_WriteLog('SDL: Got ' + IntToStr(k) + ' resolutions.', TMsgType.Notify);
187 end;
190 procedure Sleep (ms: LongWord);
191 begin
192 SDL_Delay(ms);
193 end;
196 procedure ChangeWindowSize ();
197 begin
198 gWinSizeX := gScreenWidth;
199 gWinSizeY := gScreenHeight;
200 {$IF not DEFINED(HEADLESS)}
201 fuiScrWdt := gScreenWidth;
202 fuiScrHgt := gScreenHeight;
203 e_ResizeWindow(gScreenWidth, gScreenHeight);
204 g_Game_SetupScreenSize();
205 g_Menu_Reset();
206 g_Game_ClearLoading();
207 {$ENDIF}
208 end;
211 function g_Window_SetSize (w, h: Word; fullscreen: Boolean): Boolean;
212 {$IF not DEFINED(HEADLESS)}
213 var
214 preserve: Boolean;
215 {$ENDIF}
216 begin
217 result := false;
218 {$IF not DEFINED(HEADLESS)}
219 preserve := false;
221 if (gScreenWidth <> w) or (gScreenHeight <> h) then
222 begin
223 result := true;
224 preserve := true;
225 gScreenWidth := w;
226 gScreenHeight := h;
227 end;
229 if (gFullscreen <> fullscreen) then
230 begin
231 result := true;
232 preserve := true;
233 gFullscreen := fullscreen;
234 preserve := true;
235 end;
237 if result then
238 begin
239 g_Window_SetDisplay(preserve);
240 ChangeWindowSize();
241 end;
242 {$ENDIF}
243 end;
246 function WindowEventHandler (constref ev: TSDL_WindowEvent): Boolean;
247 var
248 wActivate, wDeactivate: Boolean;
249 begin
250 result := false;
251 wActivate := false;
252 wDeactivate := false;
254 case ev.event of
255 SDL_WINDOWEVENT_MOVED:
256 begin
257 if not (gFullscreen or gWinMaximized) then
258 begin
259 gWinRealPosX := ev.data1;
260 gWinRealPosY := ev.data2;
261 end;
262 end;
264 SDL_WINDOWEVENT_MINIMIZED:
265 begin
266 e_UnpressAllKeys();
267 if not wMinimized then
268 begin
269 e_ResizeWindow(0, 0);
270 wMinimized := true;
271 if g_debug_WinMsgs then
272 begin
273 g_Console_Add('Now minimized');
274 e_WriteLog('[DEBUG] WinMsgs: Now minimized', TMsgType.Notify);
275 end;
276 wDeactivate := true;
277 end;
278 end;
280 SDL_WINDOWEVENT_RESIZED:
281 begin
282 gScreenWidth := ev.data1;
283 gScreenHeight := ev.data2;
284 ChangeWindowSize();
285 SwapBuffers();
286 if g_debug_WinMsgs then
287 begin
288 g_Console_Add('Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2));
289 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2), TMsgType.Notify);
290 end;
291 end;
293 SDL_WINDOWEVENT_EXPOSED:
294 SwapBuffers();
296 SDL_WINDOWEVENT_MAXIMIZED:
297 begin
298 if wMinimized then
299 begin
300 e_ResizeWindow(gScreenWidth, gScreenHeight);
301 wMinimized := false;
302 wActivate := true;
303 end;
304 if not gWinMaximized then
305 begin
306 gWinMaximized := true;
307 if g_debug_WinMsgs then
308 begin
309 g_Console_Add('Now maximized');
310 e_WriteLog('[DEBUG] WinMsgs: Now maximized', TMsgType.Notify);
311 end;
312 end;
313 end;
315 SDL_WINDOWEVENT_RESTORED:
316 begin
317 if wMinimized then
318 begin
319 e_ResizeWindow(gScreenWidth, gScreenHeight);
320 wMinimized := false;
321 wActivate := true;
322 end;
323 if gWinMaximized then gWinMaximized := false;
324 if g_debug_WinMsgs then
325 begin
326 g_Console_Add('Now restored');
327 e_WriteLog('[DEBUG] WinMsgs: Now restored', TMsgType.Notify);
328 end;
329 end;
331 SDL_WINDOWEVENT_FOCUS_GAINED:
332 begin
333 wActivate := true;
334 //e_WriteLog('window gained focus!', MSG_NOTIFY);
335 end;
337 SDL_WINDOWEVENT_FOCUS_LOST:
338 begin
339 wDeactivate := true;
340 e_UnpressAllKeys();
341 //e_WriteLog('window lost focus!', MSG_NOTIFY);
342 end;
343 end;
345 if wDeactivate then
346 begin
347 if gWinActive then
348 begin
349 e_WriteLog('deactivating window', TMsgType.Notify);
350 e_EnableInput := false;
351 e_ClearInputBuffer();
353 if gMuteWhenInactive then
354 begin
355 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
356 e_MuteChannels(true);
357 end;
359 if g_debug_WinMsgs then
360 begin
361 g_Console_Add('Now inactive');
362 e_WriteLog('[DEBUG] WinMsgs: Now inactive', TMsgType.Notify);
363 end;
365 gWinActive := false;
367 if assigned(winBlurCB) then winBlurCB();
368 end;
369 end
370 else if wActivate then
371 begin
372 if not gWinActive then
373 begin
374 //e_WriteLog('activating window', MSG_NOTIFY);
375 e_EnableInput := true;
377 if gMuteWhenInactive then
378 begin
379 //e_WriteLog('activating sounds', MSG_NOTIFY);
380 e_MuteChannels(false);
381 end;
383 if g_debug_WinMsgs then
384 begin
385 g_Console_Add('Now active');
386 e_WriteLog('[DEBUG] WinMsgs: Now active', TMsgType.Notify);
387 end;
389 gWinActive := true;
390 if assigned(winFocusCB) then winFocusCB();
391 end;
392 end;
393 end;
396 function EventHandler (var ev: TSDL_Event): Boolean;
397 var
398 key, keychr: Word;
399 uc: UnicodeChar;
400 down: Boolean;
401 begin
402 result := false;
404 case ev.type_ of
405 SDL_WINDOWEVENT:
406 result := WindowEventHandler(ev.window);
408 SDL_QUITEV:
409 begin
410 if (gExit <> EXIT_QUIT) then
411 begin
412 if not wLoadingProgress then
413 begin
414 g_Game_Free();
415 g_Game_Quit();
416 end
417 else
418 begin
419 wLoadingQuit := true;
420 end;
421 end;
422 result := true;
423 end;
425 SDL_KEYDOWN, SDL_KEYUP:
426 begin
427 key := ev.key.keysym.scancode;
428 down := (ev.type_ = SDL_KEYDOWN);
429 {$IF not DEFINED(HEADLESS)}
430 if fuiOnSDLEvent(ev) then
431 begin
432 // event eaten, but...
433 if not down then e_KeyUpDown(key, false);
434 exit;
435 end;
436 {$ENDIF}
437 if down then KeyPress(key);
438 e_KeyUpDown(key, down);
439 end;
441 {$IF not DEFINED(HEADLESS)}
442 SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION:
443 fuiOnSDLEvent(ev);
444 {$ENDIF}
446 SDL_TEXTINPUT:
447 begin
448 Utf8ToUnicode(@uc, PChar(ev.text.text), 1);
449 keychr := Word(uc);
450 if (keychr > 127) then keychr := Word(wchar2win(WideChar(keychr)));
451 if (keychr > 0) and (keychr <= 255) then CharPress(AnsiChar(keychr));
452 end;
454 // other key presses and joysticks are handled in e_input
455 end;
456 end;
459 procedure SwapBuffers ();
460 begin
461 {$IF not DEFINED(HEADLESS)}
462 SDL_GL_SwapWindow(h_Wnd);
463 {$ENDIF}
464 end;
467 function CreateGLWindow (Title: PChar): Boolean;
468 begin
469 result := false;
471 gWinSizeX := gScreenWidth;
472 gWinSizeY := gScreenHeight;
474 {$IF not DEFINED(HEADLESS)}
475 wTitle := Title;
476 {$ENDIF}
477 e_WriteLog('Creating window', TMsgType.Notify);
479 if not g_Window_SetDisplay() then
480 begin
481 KillGLWindow(false);
482 e_WriteLog('Window creation error (resolution not supported?)', TMsgType.Fatal);
483 exit;
484 end;
486 {$IF not DEFINED(HEADLESS)}
487 h_GL := SDL_GL_CreateContext(h_Wnd);
488 if (h_GL = nil) then exit;
489 fuiScrWdt := gScreenWidth;
490 fuiScrHgt := gScreenHeight;
491 if (assigned(oglInitCB)) then oglInitCB();
492 {$ENDIF}
494 e_ResizeWindow(gScreenWidth, gScreenHeight);
495 e_InitGL();
497 result := true;
498 end;
501 {$IFDEF WINDOWS}
502 // windoze sux; in headless mode `GetTickCount()` (and SDL) returns shit
503 function GetTimer (): Int64;
504 var
505 F, C: Int64;
506 begin
507 QueryPerformanceFrequency(F);
508 QueryPerformanceCounter(C);
509 result := Round(C/F*1000{000});
510 end;
511 {$ELSE}
512 function GetTimer (): Int64;
513 var
514 t: Uint32;
515 tt: Int64;
516 begin
517 t := SDL_GetTicks();
518 if (ticksOverflow = -1) then
519 begin
520 ticksOverflow := 0;
521 lastTicks := t;
522 end
523 else
524 begin
525 if (lastTicks > t) then
526 begin
527 // overflow, increment overflow ;-)
528 ticksOverflow := ticksOverflow+(Int64($ffffffff)+Int64(1));
529 tt := (Int64($ffffffff)+Int64(1))+Int64(t);
530 t := Uint32(tt-lastTicks);
531 end;
532 end;
533 lastTicks := t;
534 result := ticksOverflow+Int64(t);
535 end;
536 {$ENDIF}
539 procedure ResetTimer ();
540 begin
541 wNeedTimeReset := true;
542 end;
545 procedure PushExitEvent ();
546 var
547 ev: TSDL_Event;
548 begin
549 ev.type_ := SDL_QUITEV;
550 SDL_PushEvent(@ev);
551 end;
554 var
555 prevLoadingUpdateTime: UInt64 = 0;
557 procedure ProcessLoading (forceUpdate: Boolean=false);
558 var
559 ev: TSDL_Event;
560 ID: LongWord;
561 stt: UInt64;
562 begin
563 FillChar(ev, sizeof(ev), 0);
564 wLoadingProgress := true;
566 while (SDL_PollEvent(@ev) > 0) do
567 begin
568 EventHandler(ev);
569 if (ev.type_ = SDL_QUITEV) then break;
570 end;
571 e_PollJoysticks();
573 if (ev.type_ = SDL_QUITEV) or (gExit = EXIT_QUIT) then
574 begin
575 wLoadingProgress := false;
576 exit;
577 end;
579 if not wMinimized then
580 begin
581 if forceUpdate then
582 begin
583 prevLoadingUpdateTime := getTimeMilli();
584 end
585 else
586 begin
587 stt := getTimeMilli();
588 if (stt < prevLoadingUpdateTime) or (stt-prevLoadingUpdateTime >= ProgressUpdateMSecs) then
589 begin
590 prevLoadingUpdateTime := stt;
591 forceUpdate := true;
592 end;
593 end;
595 if forceUpdate then
596 begin
597 if g_Texture_Get('INTER', ID) then
598 begin
599 e_DrawSize(ID, 0, 0, 0, false, false, gScreenWidth, gScreenHeight);
600 e_DarkenQuadWH(0, 0, gScreenWidth, gScreenHeight, 150);
601 end
602 else
603 begin
604 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
605 end;
607 DrawLoadingStat();
608 SwapBuffers();
609 end;
610 end;
612 e_SoundUpdate();
614 if NetMode = NET_SERVER then
615 begin
616 g_Net_Host_Update();
617 end
618 else
619 begin
620 if (NetMode = NET_CLIENT) and (NetState <> NET_STATE_AUTH) then g_Net_Client_UpdateWhileLoading();
621 end;
623 wLoadingProgress := false;
624 end;
627 function g_ProcessMessages (): Boolean;
628 var
629 ev: TSDL_Event;
630 begin
631 result := false;
632 FillChar(ev, SizeOf(ev), 0);
633 while (SDL_PollEvent(@ev) > 0) do
634 begin
635 result := EventHandler(ev);
636 if (ev.type_ = SDL_QUITEV) then exit;
637 end;
638 e_PollJoysticks();
639 end;
642 function ProcessMessage (): Boolean;
643 var
644 i, t: Integer;
645 begin
646 result := g_ProcessMessages();
648 Time := GetTimer();
649 Time_Delta := Time-Time_Old;
651 flag := false;
653 if wNeedTimeReset then
654 begin
655 Time_Delta := 28;
656 wNeedTimeReset := false;
657 end;
659 g_Map_ProfilersBegin();
660 g_Mons_ProfilersBegin();
662 t := Time_Delta div 28;
663 if (t > 0) then
664 begin
665 flag := true;
666 for i := 1 to t do
667 begin
668 if (NetMode = NET_SERVER) then g_Net_Host_Update()
669 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
670 Update();
671 end;
672 end
673 else
674 begin
675 if (NetMode = NET_SERVER) then g_Net_Host_Update()
676 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
677 end;
679 g_Map_ProfilersEnd();
680 g_Mons_ProfilersEnd();
682 if wLoadingQuit then
683 begin
684 g_Game_Free();
685 g_Game_Quit();
686 end;
688 if (gExit = EXIT_QUIT) then
689 begin
690 result := true;
691 exit;
692 end;
694 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
695 if flag then
696 begin
697 Time_Old := Time-(Time_Delta mod 28);
698 if (not wMinimized) then
699 begin
700 Draw();
701 SwapBuffers();
702 end;
703 end
704 else
705 begin
706 Sleep(1); // release time slice, so we won't eat 100% CPU
707 end;
709 e_SoundUpdate();
710 end;
713 procedure ReDrawWindow ();
714 begin
715 SwapBuffers();
716 end;
719 procedure InitOpenGL (vsync: Boolean);
720 {$IF not DEFINED(HEADLESS)}
721 var
722 v: Byte;
723 {$ENDIF}
724 begin
725 {$IF not DEFINED(HEADLESS)}
726 if vsync then v := 1 else v := 0;
727 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
728 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
729 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
730 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
731 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
732 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
733 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
734 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
735 SDL_GL_SetSwapInterval(v);
736 {$ENDIF}
737 end;
740 function glHasExtension (const name: AnsiString): Boolean;
741 var
742 exts: PChar;
743 i: Integer;
744 found: Boolean;
745 extName: ShortString;
746 begin
747 result := false;
748 if (Length(name) = 0) then exit;
749 exts := glGetString(GL_EXTENSIONS);
750 if (exts = nil) then exit;
751 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
752 while (exts[0] <> #0) do
753 begin
754 if gwin_dump_extensions then
755 begin
756 i := 0;
757 while (exts[i] <> #0) and (exts[i] <> ' ') do Inc(i);
758 if i > 255 then
759 begin
760 e_WriteLog('FUUUUUUUUUUUUU', TMsgType.Warning);
761 end
762 else
763 begin
764 Move(exts^, extName[1], i);
765 extName[0] := Char(i);
766 e_WriteLog(Format('EXT: %s', [extName]), TMsgType.Notify);
767 end;
768 end;
769 found := true;
770 for i := 0 to length(name)-1 do
771 begin
772 if (exts[i] = #0) then begin found := false; break; end;
773 if (exts[i] <> name[i+1]) then begin found := false; break; end;
774 end;
775 if found and ((exts[Length(name)] = #0) or (exts[Length(name)] = ' ')) then begin result := true; exit; end;
776 while (exts[0] <> #0) and (exts[0] <> ' ') do Inc(exts);
777 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
778 end;
779 end;
782 function SDLMain (): Integer;
783 var
784 idx: Integer;
785 {$IF not DEFINED(HEADLESS)}
786 ltmp: Integer;
787 {$ENDIF}
788 arg: AnsiString;
789 mdfo: TStream;
790 itmp: Integer;
791 valres: Word;
792 begin
793 {$IFDEF HEADLESS}
794 e_NoGraphics := true;
795 {$ELSE}
796 if (not g_holmes_imfunctional) then
797 begin
798 uiInitialize();
799 uiContext.font := 'win14';
800 end;
801 {$ENDIF}
803 idx := 1;
804 while (idx <= ParamCount) do
805 begin
806 arg := ParamStr(idx);
807 Inc(idx);
808 if arg = '--opengl-dump-exts' then gwin_dump_extensions := true;
809 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
810 if arg = '--jah' then g_profile_history_size := 100;
811 if arg = '--no-particles' then gpart_dbg_enabled := false;
812 if arg = '--no-los' then gmon_dbg_los_enabled := false;
814 if arg = '--profile-render' then g_profile_frame_draw := true;
815 if arg = '--profile-coldet' then g_profile_collision := true;
816 if arg = '--profile-los' then g_profile_los := true;
818 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
819 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
820 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
821 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
822 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
823 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
825 {.$IF DEFINED(D2F_DEBUG)}
826 if arg = '--aimline' then g_dbg_aimline_on := true;
827 {.$ENDIF}
829 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
831 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
832 begin
833 if (idx <= ParamCount) then
834 begin
835 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
836 Inc(idx);
837 end;
838 end;
840 if (arg = '--holmes-font') or (arg = '-holmes-font') then
841 begin
842 if (idx <= ParamCount) then
843 begin
844 itmp := 0;
845 val(ParamStr(idx), itmp, valres);
846 {$IFNDEF HEADLESS}
847 if (valres = 0) and (not g_holmes_imfunctional) then
848 begin
849 case itmp of
850 8: uiContext.font := 'win8';
851 14: uiContext.font := 'win14';
852 16: uiContext.font := 'win16';
853 end;
854 end;
855 {$ELSE}
856 // fuck off, fpc!
857 itmp := itmp;
858 valres := valres;
859 {$ENDIF}
860 Inc(idx);
861 end;
862 end;
864 if (arg = '--game-scale') or (arg = '-game-scale') then
865 begin
866 if (idx <= ParamCount) then
867 begin
868 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
869 Inc(idx);
870 end;
871 end;
873 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
874 begin
875 mdfo := createDiskFile('mapdef.txt');
876 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
877 mdfo.Free();
878 Halt(0);
879 end;
880 end;
882 e_WriteLog('Initializing OpenGL', TMsgType.Notify);
883 InitOpenGL(gVSync);
885 e_WriteLog('Creating GL window', TMsgType.Notify);
886 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION]))) then
887 begin
888 result := 0;
889 exit;
890 end;
892 {EnumDisplayModes();}
894 {$IFDEF HEADLESS}
895 //gwin_k8_enable_light_experiments := false;
896 gwin_has_stencil := false;
897 glLegacyNPOT := false;
898 gwin_dump_extensions := false;
899 {$ELSE}
900 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, @ltmp);
901 e_LogWritefln('stencil buffer size: %s', [ltmp]);
902 gwin_has_stencil := (ltmp > 0);
904 if not glHasExtension('GL_ARB_texture_non_power_of_two') then
905 begin
906 e_WriteLog('NPOT textures: NO', TMsgType.Warning);
907 glLegacyNPOT := true;
908 end
909 else
910 begin
911 e_WriteLog('NPOT textures: YES', TMsgType.Notify);
912 glLegacyNPOT := false;
913 end;
914 gwin_dump_extensions := false;
915 {$ENDIF}
917 Init();
918 Time_Old := GetTimer();
920 // Êîìàíäíàÿ ñòðîêà
921 if (ParamCount > 0) then g_Game_Process_Params();
923 // Çàïðîñ ÿçûêà
924 if (not gGameOn) and gAskLanguage then g_Menu_AskLanguage();
926 e_WriteLog('Entering the main loop', TMsgType.Notify);
928 // main loop
929 while not ProcessMessage() do begin end;
931 Release();
932 KillGLWindow(false);
934 result := 0;
935 end;
938 end.