DEADSOFTWARE

3958d24d12df79b518b6dc351942a53d487acc52
[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;
35 procedure g_SetVSync (vsync: Boolean);
37 procedure ProcessLoading (forceUpdate: Boolean=false);
39 // returns `true` if quit event was received
40 function g_ProcessMessages (): Boolean;
43 var
44 gwin_dump_extensions: Boolean = false;
45 gwin_has_stencil: Boolean = false;
46 gwin_k8_enable_light_experiments: Boolean = false;
47 g_dbg_aimline_on: Boolean = false;
50 implementation
52 uses
53 {$IFDEF WINDOWS}Windows,{$ENDIF}
54 {$INCLUDE ../nogl/noGLuses.inc}
55 {$IFDEF ENABLE_HOLMES}
56 g_holmes, sdlcarcass, fui_ctls,
57 {$ENDIF}
58 SysUtils, Classes, MAPDEF,
59 SDL2, e_graphics, e_log, e_texture, g_main,
60 g_console, e_input, g_options, g_game,
61 g_basic, g_textures, e_sound, g_sound, g_menu, ENet, g_net,
62 g_map, g_gfx, g_monsters, xprofiler,
63 g_touch;
66 const
67 ProgressUpdateMSecs = 1;//100;
69 var
70 h_Wnd: PSDL_Window = nil;
71 h_GL: TSDL_GLContext = nil;
72 Time, Time_Delta, Time_Old: Int64;
73 flag: Boolean;
74 {$IF not DEFINED(HEADLESS)}
75 wTitle: PChar = nil;
76 wasFullscreen: Boolean = true; // so we need to recreate the window
77 {$ENDIF}
78 wNeedTimeReset: Boolean = false;
79 wMinimized: Boolean = false;
80 wMaximized: Boolean = false;
81 wLoadingProgress: Boolean = false;
82 wLoadingQuit: Boolean = false;
83 {$IFNDEF WINDOWS}
84 ticksOverflow: Int64 = -1;
85 lastTicks: Uint32 = 0; // to detect overflow
86 {$ENDIF}
89 procedure KillGLWindow (preserveGL: Boolean);
90 begin
91 {$IFDEF ENABLE_HOLMES}
92 if (h_GL <> nil) and (not preserveGL) then begin if (assigned(oglDeinitCB)) then oglDeinitCB(); end;
93 {$ENDIF}
94 if (h_Wnd <> nil) then SDL_DestroyWindow(h_Wnd);
95 if (h_GL <> nil) and (not preserveGL) then
96 begin
98 {$IFDEF USE_NANOGL}
99 nanoGL_Destroy;
100 {$ENDIF}
102 {$IFDEF USE_NOGL}
103 nogl_Quit;
104 {$ENDIF}
106 SDL_GL_DeleteContext(h_GL);
107 end;
108 h_Wnd := nil;
109 if (not preserveGL) then h_GL := nil;
110 end;
113 function g_Window_SetDisplay (preserveGL: Boolean = false): Boolean;
114 {$IF not DEFINED(HEADLESS)}
115 var
116 mode, cmode: TSDL_DisplayMode;
117 wFlags: LongWord = 0;
118 nw, nh: Integer;
119 {$ENDIF}
120 begin
121 {$IF not DEFINED(HEADLESS)}
122 result := false;
124 e_WriteLog('Setting display mode...', TMsgType.Notify);
126 wFlags := SDL_WINDOW_OPENGL {or SDL_WINDOW_RESIZABLE};
127 if gFullscreen then wFlags := wFlags {or SDL_WINDOW_FULLSCREEN} else wFlags := wFlags or SDL_WINDOW_RESIZABLE;
128 if (not gFullscreen) and (not preserveGL) and gWinMaximized then wFlags := wFlags or SDL_WINDOW_MAXIMIZED else gWinMaximized := false;
130 if gFullscreen then
131 begin
132 mode.w := gScreenWidth;
133 mode.h := gScreenHeight;
134 mode.format := 0;
135 mode.refresh_rate := 0;
136 mode.driverdata := nil;
137 if (SDL_GetClosestDisplayMode(0, @mode, @cmode) = nil) then
138 begin
139 e_WriteLog('SDL: cannot find display mode for '+IntToStr(gScreenWidth), TMsgType.Notify);
140 gScreenWidth := 800;
141 gScreenHeight := 600;
142 end
143 else
144 begin
145 e_WriteLog('SDL: found display mode for '+IntToStr(gScreenWidth)+'x'+IntToStr(gScreenHeight)+': '+IntToStr(cmode.w)+'x'+IntToStr(cmode.h), TMsgType.Notify);
146 gScreenWidth := cmode.w;
147 gScreenHeight := cmode.h;
148 end;
149 end;
151 if (preserveGL) and (h_Wnd <> nil) and (not gFullscreen) and (not wasFullscreen) then
152 begin
153 //SDL_SetWindowMaximumSize(h_Wnd, gScreenWidth, gScreenHeight);
154 //SDL_SetWindowDisplayMode(h_Wnd, @cmode);
155 if (wMaximized) then SDL_RestoreWindow(h_Wnd);
156 wMaximized := false;
157 gWinMaximized := false;
158 SDL_SetWindowSize(h_Wnd, gScreenWidth, gScreenHeight);
159 //SDL_SetWindowFullscreen(h_Wnd, SDL_WINDOW_FULLSCREEN);
160 //SDL_SetWindowFullscreen(h_Wnd, 0);
161 end
162 else
163 begin
164 KillGLWindow(preserveGL);
165 h_Wnd := SDL_CreateWindow(PChar(wTitle), gWinRealPosX, gWinRealPosY, gScreenWidth, gScreenHeight, wFlags);
166 if gFullscreen then
167 SDL_SetWindowFullscreen(h_Wnd, SDL_WINDOW_FULLSCREEN);
168 if (h_Wnd = nil) then exit;
169 end;
170 wasFullscreen := gFullscreen;
172 SDL_GL_MakeCurrent(h_Wnd, h_GL);
173 SDL_ShowCursor(SDL_DISABLE);
174 if (h_GL <> nil) then g_SetVSync(gVSync);
175 if (gFullscreen) then
176 begin
177 nw := 0;
178 nh := 0;
179 SDL_GetWindowSize(h_Wnd, @nw, @nh);
180 if (nw > 128) and (nh > 128) then
181 begin
182 e_WriteLog('SDL: fullscreen window got size '+IntToStr(nw)+'x'+IntToStr(nh)+': '+IntToStr(gScreenWidth)+'x'+IntToStr(gScreenHeight), TMsgType.Notify);
183 gScreenWidth := nw;
184 gScreenHeight := nh;
185 end
186 else
187 begin
188 e_WriteLog('SDL: fullscreen window got invalid size: '+IntToStr(nw)+'x'+IntToStr(nh), TMsgType.Notify);
189 end;
190 end;
192 {$IFDEF ENABLE_HOLMES}
193 fuiScrWdt := gScreenWidth;
194 fuiScrHgt := gScreenHeight;
195 if (h_GL <> nil) and (not preserveGL) then begin if (assigned(oglInitCB)) then oglInitCB(); end;
196 {$ENDIF}
197 {$ENDIF}
199 result := true;
200 end;
203 function GetDisplayModes (dbpp: LongWord; var selres: LongWord): SSArray;
204 var
205 mode: TSDL_DisplayMode;
206 res, i, k, n, pw, ph: Integer;
207 begin
208 SetLength(result, 0);
209 {$IFDEF HEADLESS}exit;{$ENDIF}
210 k := 0; selres := 0;
211 n := SDL_GetNumDisplayModes(0);
212 pw := 0; ph := 0;
213 for i := 0 to n do
214 begin
215 res := SDL_GetDisplayMode(0, i, @mode);
216 if res < 0 then continue;
217 if SDL_BITSPERPIXEL(mode.format) = gBPP then continue;
218 if (mode.w = pw) and (mode.h = ph) then continue;
219 if (mode.w = gScreenWidth) and (mode.h = gScreenHeight) then
220 selres := k;
221 Inc(k);
222 SetLength(result, k);
223 result[k-1] := IntToStr(mode.w) + 'x' + IntToStr(mode.h);
224 pw := mode.w; ph := mode.h
225 end;
227 e_WriteLog('SDL: Got ' + IntToStr(k) + ' resolutions.', TMsgType.Notify);
228 end;
231 procedure Sleep (ms: LongWord);
232 begin
233 SDL_Delay(ms);
234 end;
237 procedure ChangeWindowSize (requested: Boolean);
238 begin
239 e_LogWritefln(' ChangeWindowSize: (ws=%dx%d) (ss=%dx%d)', [gWinSizeX, gWinSizeY, gScreenWidth, gScreenHeight]);
240 gWinSizeX := gScreenWidth;
241 gWinSizeY := gScreenHeight;
242 {$IF not DEFINED(HEADLESS)}
243 {$IFDEF ENABLE_HOLMES}
244 fuiScrWdt := gScreenWidth;
245 fuiScrHgt := gScreenHeight;
246 {$ENDIF}
247 e_ResizeWindow(gScreenWidth, gScreenHeight);
248 g_Game_SetupScreenSize();
249 {$IF DEFINED(ANDROID)}
250 (* This will fix menu reset on keyboard showing *)
251 if requested then
252 g_Menu_Reset;
253 {$ELSE}
254 g_Menu_Reset;
255 {$ENDIF}
256 g_Game_ClearLoading();
257 {$ENDIF}
258 end;
261 function g_Window_SetSize (w, h: Word; fullscreen: Boolean): Boolean;
262 {$IF not DEFINED(HEADLESS)}
263 var
264 preserve: Boolean;
265 {$ENDIF}
266 begin
267 result := false;
268 {$IF not DEFINED(HEADLESS)}
269 preserve := false;
271 if (gScreenWidth <> w) or (gScreenHeight <> h) then
272 begin
273 result := true;
274 preserve := true;
275 gScreenWidth := w;
276 gScreenHeight := h;
277 end;
279 if (gFullscreen <> fullscreen) then
280 begin
281 result := true;
282 preserve := true;
283 gFullscreen := fullscreen;
284 preserve := true;
285 end;
287 if result then
288 begin
289 g_Window_SetDisplay(preserve);
290 ChangeWindowSize(true);
291 end;
292 {$ENDIF}
293 end;
296 function WindowEventHandler (constref ev: TSDL_WindowEvent): Boolean;
297 var
298 wActivate, wDeactivate: Boolean;
299 begin
300 result := false;
301 wActivate := false;
302 wDeactivate := false;
304 case ev.event of
305 SDL_WINDOWEVENT_MOVED:
306 begin
307 if not (gFullscreen or gWinMaximized) then
308 begin
309 gWinRealPosX := ev.data1;
310 gWinRealPosY := ev.data2;
311 end;
312 end;
314 SDL_WINDOWEVENT_MINIMIZED:
315 begin
316 e_UnpressAllKeys();
317 if not wMinimized then
318 begin
319 e_ResizeWindow(0, 0);
320 wMinimized := true;
321 if g_debug_WinMsgs then
322 begin
323 g_Console_Add('Now minimized');
324 e_WriteLog('[DEBUG] WinMsgs: Now minimized', TMsgType.Notify);
325 end;
326 wDeactivate := true;
327 end;
328 end;
330 SDL_WINDOWEVENT_RESIZED:
331 begin
332 e_LogWritefln('Resize: (os=%dx%d) (ns=%dx%d)', [gScreenWidth, gScreenHeight, Integer(ev.data1), Integer(ev.data2)]);
333 {if (gFullscreen) then
334 begin
335 e_LogWriteln(' fullscreen fix applied.');
336 if (gScreenWidth <> ev.data1) or (gScreenHeight <> ev.data2) then
337 begin
338 SDL_SetWindowSize(h_Wnd, gScreenWidth, gScreenHeight);
339 end;
340 end
341 else}
342 begin
343 gScreenWidth := ev.data1;
344 gScreenHeight := ev.data2;
345 end;
346 ChangeWindowSize(false);
347 SwapBuffers();
348 if g_debug_WinMsgs then
349 begin
350 g_Console_Add('Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2));
351 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2), TMsgType.Notify);
352 end;
353 end;
355 SDL_WINDOWEVENT_EXPOSED:
356 SwapBuffers();
358 SDL_WINDOWEVENT_MAXIMIZED:
359 begin
360 wMaximized := true;
361 if wMinimized then
362 begin
363 e_ResizeWindow(gScreenWidth, gScreenHeight);
364 wMinimized := false;
365 wActivate := true;
366 end;
367 if (not gWinMaximized) and (not gFullscreen) then
368 begin
369 gWinMaximized := true;
370 if g_debug_WinMsgs then
371 begin
372 g_Console_Add('Now maximized');
373 e_WriteLog('[DEBUG] WinMsgs: Now maximized', TMsgType.Notify);
374 end;
375 end;
376 end;
378 SDL_WINDOWEVENT_RESTORED:
379 begin
380 wMaximized := false;
381 if wMinimized then
382 begin
383 e_ResizeWindow(gScreenWidth, gScreenHeight);
384 wMinimized := false;
385 wActivate := true;
386 end;
387 gWinMaximized := false;
388 if g_debug_WinMsgs then
389 begin
390 g_Console_Add('Now restored');
391 e_WriteLog('[DEBUG] WinMsgs: Now restored', TMsgType.Notify);
392 end;
393 end;
395 SDL_WINDOWEVENT_FOCUS_GAINED:
396 begin
397 wActivate := true;
398 //e_WriteLog('window gained focus!', MSG_NOTIFY);
399 end;
401 SDL_WINDOWEVENT_FOCUS_LOST:
402 begin
403 wDeactivate := true;
404 e_UnpressAllKeys();
405 //e_WriteLog('window lost focus!', MSG_NOTIFY);
406 end;
407 end;
409 if wDeactivate then
410 begin
411 if gWinActive then
412 begin
413 e_WriteLog('deactivating window', TMsgType.Notify);
414 e_EnableInput := false;
415 e_ClearInputBuffer();
417 if gMuteWhenInactive then
418 begin
419 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
420 e_MuteChannels(true);
421 end;
423 if g_debug_WinMsgs then
424 begin
425 g_Console_Add('Now inactive');
426 e_WriteLog('[DEBUG] WinMsgs: Now inactive', TMsgType.Notify);
427 end;
429 gWinActive := false;
431 {$IFDEF ENABLE_HOLMES}
432 if assigned(winBlurCB) then winBlurCB();
433 {$ENDIF}
434 end;
435 end
436 else if wActivate then
437 begin
438 if not gWinActive then
439 begin
440 //e_WriteLog('activating window', MSG_NOTIFY);
441 e_EnableInput := true;
443 if gMuteWhenInactive then
444 begin
445 //e_WriteLog('activating sounds', MSG_NOTIFY);
446 e_MuteChannels(false);
447 end;
449 if g_debug_WinMsgs then
450 begin
451 g_Console_Add('Now active');
452 e_WriteLog('[DEBUG] WinMsgs: Now active', TMsgType.Notify);
453 end;
455 gWinActive := true;
457 {$IFDEF ENABLE_HOLMES}
458 if assigned(winFocusCB) then winFocusCB();
459 {$ENDIF}
460 end;
461 end;
462 end;
465 function EventHandler (var ev: TSDL_Event): Boolean;
466 var
467 key, keychr: Word;
468 uc: UnicodeChar;
469 down: Boolean;
470 begin
471 result := false;
473 case ev.type_ of
474 SDL_WINDOWEVENT:
475 result := WindowEventHandler(ev.window);
477 SDL_QUITEV:
478 begin
479 if (gExit <> EXIT_QUIT) then
480 begin
481 if not wLoadingProgress then
482 begin
483 g_Game_Free();
484 g_Game_Quit();
485 end
486 else
487 begin
488 wLoadingQuit := true;
489 end;
490 end;
491 result := true;
492 end;
494 SDL_KEYDOWN, SDL_KEYUP:
495 begin
496 key := ev.key.keysym.scancode;
497 if key = SDL_SCANCODE_AC_BACK then
498 key := SDL_SCANCODE_ESCAPE;
499 down := (ev.type_ = SDL_KEYDOWN);
500 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
501 if fuiOnSDLEvent(ev) then
502 begin
503 // event eaten, but...
504 if not down then e_KeyUpDown(key, false);
505 exit;
506 end;
507 {$ENDIF}
508 e_KeyUpDown(key, down);
509 if down then KeyPress(key);
510 end;
512 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
513 SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION:
514 fuiOnSDLEvent(ev);
515 {$ENDIF}
517 SDL_TEXTINPUT:
518 begin
519 Utf8ToUnicode(@uc, PChar(ev.text.text), 1);
520 keychr := Word(uc);
521 if (keychr > 127) then keychr := Word(wchar2win(WideChar(keychr)));
522 if (keychr > 0) and (keychr <= 255) then CharPress(AnsiChar(keychr));
523 end;
525 SDL_FINGERMOTION, SDL_FINGERDOWN, SDL_FINGERUP:
526 g_Touch_HandleEvent(ev.tfinger);
528 // other key presses and joysticks are handled in e_input
529 end;
530 end;
533 procedure SwapBuffers ();
534 begin
535 {$IF not DEFINED(HEADLESS)}
536 SDL_GL_SwapWindow(h_Wnd);
537 {$ENDIF}
538 end;
541 function CreateGLWindow (Title: PChar): Boolean;
542 begin
543 result := false;
545 gWinSizeX := gScreenWidth;
546 gWinSizeY := gScreenHeight;
548 {$IF not DEFINED(HEADLESS)}
549 wTitle := Title;
550 {$ENDIF}
551 e_WriteLog('Creating window', TMsgType.Notify);
553 if not g_Window_SetDisplay() then
554 begin
555 KillGLWindow(false);
556 e_WriteLog('Window creation error (resolution not supported?)', TMsgType.Fatal);
557 exit;
558 end;
560 {$IF not DEFINED(HEADLESS)}
561 h_GL := SDL_GL_CreateContext(h_Wnd);
562 if (h_GL = nil) then exit;
563 {$IFDEF ENABLE_HOLMES}
564 fuiScrWdt := gScreenWidth;
565 fuiScrHgt := gScreenHeight;
566 {$ENDIF}
567 SDL_GL_MakeCurrent(h_Wnd, h_GL);
568 {$IFDEF USE_NANOGL}
569 if nanoGL_Init() = 0 then
570 begin
571 KillGLWindow(false);
572 e_WriteLog('nanoGL initialization error', TMsgType.Fatal);
573 exit;
574 end;
575 {$ENDIF}
576 {$IFDEF USE_NOGL}
577 nogl_Init;
578 {$ENDIF}
579 {$IFDEF ENABLE_HOLMES}
580 if (assigned(oglInitCB)) then oglInitCB();
581 {$ENDIF}
582 if (h_GL <> nil) then g_SetVSync(gVSync);
583 {$ENDIF}
585 e_ResizeWindow(gScreenWidth, gScreenHeight);
586 e_InitGL();
588 result := true;
589 end;
592 {$IFDEF WINDOWS}
593 // windoze sux; in headless mode `GetTickCount()` (and SDL) returns shit
594 function GetTimer (): Int64;
595 var
596 F, C: Int64;
597 begin
598 QueryPerformanceFrequency(F);
599 QueryPerformanceCounter(C);
600 result := Round(C/F*1000{000});
601 end;
602 {$ELSE}
603 function GetTimer (): Int64;
604 var
605 t: Uint32;
606 tt: Int64;
607 begin
608 t := SDL_GetTicks();
609 if (ticksOverflow = -1) then
610 begin
611 ticksOverflow := 0;
612 lastTicks := t;
613 end
614 else
615 begin
616 if (lastTicks > t) then
617 begin
618 // overflow, increment overflow ;-)
619 ticksOverflow := ticksOverflow+(Int64($ffffffff)+Int64(1));
620 tt := (Int64($ffffffff)+Int64(1))+Int64(t);
621 t := Uint32(tt-lastTicks);
622 end;
623 end;
624 lastTicks := t;
625 result := ticksOverflow+Int64(t);
626 end;
627 {$ENDIF}
630 procedure ResetTimer ();
631 begin
632 wNeedTimeReset := true;
633 end;
636 procedure PushExitEvent ();
637 var
638 ev: TSDL_Event;
639 begin
640 ev.type_ := SDL_QUITEV;
641 SDL_PushEvent(@ev);
642 end;
645 var
646 prevLoadingUpdateTime: UInt64 = 0;
648 procedure ProcessLoading (forceUpdate: Boolean=false);
649 var
650 ev: TSDL_Event;
651 ID: LongWord;
652 stt: UInt64;
653 begin
654 FillChar(ev, sizeof(ev), 0);
655 wLoadingProgress := true;
657 while (SDL_PollEvent(@ev) > 0) do
658 begin
659 EventHandler(ev);
660 if (ev.type_ = SDL_QUITEV) then break;
661 end;
662 e_PollJoysticks();
664 if (ev.type_ = SDL_QUITEV) or (gExit = EXIT_QUIT) then
665 begin
666 wLoadingProgress := false;
667 exit;
668 end;
670 if not wMinimized then
671 begin
672 if forceUpdate then
673 begin
674 prevLoadingUpdateTime := getTimeMilli();
675 end
676 else
677 begin
678 stt := getTimeMilli();
679 if (stt < prevLoadingUpdateTime) or (stt-prevLoadingUpdateTime >= ProgressUpdateMSecs) then
680 begin
681 prevLoadingUpdateTime := stt;
682 forceUpdate := true;
683 end;
684 end;
686 if forceUpdate then
687 begin
688 if g_Texture_Get('INTER', ID) then
689 begin
690 e_DrawSize(ID, 0, 0, 0, false, false, gScreenWidth, gScreenHeight);
691 e_DarkenQuadWH(0, 0, gScreenWidth, gScreenHeight, 150);
692 end
693 else
694 begin
695 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
696 end;
698 DrawLoadingStat();
699 SwapBuffers();
700 end;
701 end;
703 e_SoundUpdate();
705 if NetMode = NET_SERVER then
706 begin
707 g_Net_Host_Update();
708 end
709 else
710 begin
711 if (NetMode = NET_CLIENT) and (NetState <> NET_STATE_AUTH) then g_Net_Client_UpdateWhileLoading();
712 end;
714 wLoadingProgress := false;
715 end;
718 function g_ProcessMessages (): Boolean;
719 var
720 ev: TSDL_Event;
721 begin
722 result := false;
723 FillChar(ev, SizeOf(ev), 0);
724 while (SDL_PollEvent(@ev) > 0) do
725 begin
726 result := EventHandler(ev);
727 if (ev.type_ = SDL_QUITEV) then exit;
728 end;
729 e_PollJoysticks();
730 end;
733 function ProcessMessage (): Boolean;
734 var
735 i, t: Integer;
736 begin
737 result := g_ProcessMessages();
739 Time := GetTimer();
740 Time_Delta := Time-Time_Old;
742 flag := false;
744 if wNeedTimeReset then
745 begin
746 Time_Delta := 28;
747 wNeedTimeReset := false;
748 end;
750 g_Map_ProfilersBegin();
751 g_Mons_ProfilersBegin();
753 t := Time_Delta div 28;
754 if (t > 0) then
755 begin
756 flag := true;
757 for i := 1 to t do
758 begin
759 if (NetMode = NET_SERVER) then g_Net_Host_Update()
760 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
761 Update();
762 end;
763 end
764 else
765 begin
766 if (NetMode = NET_SERVER) then g_Net_Host_Update()
767 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
768 end;
770 g_Map_ProfilersEnd();
771 g_Mons_ProfilersEnd();
773 if wLoadingQuit then
774 begin
775 g_Game_Free();
776 g_Game_Quit();
777 end;
779 if (gExit = EXIT_QUIT) then
780 begin
781 result := true;
782 exit;
783 end;
785 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
786 if flag then
787 begin
788 Time_Old := Time-(Time_Delta mod 28);
789 if (not wMinimized) then
790 begin
791 Draw();
792 SwapBuffers();
793 end;
794 end
795 else
796 begin
797 Sleep(1); // release time slice, so we won't eat 100% CPU
798 end;
800 e_SoundUpdate();
801 end;
804 procedure ReDrawWindow ();
805 begin
806 SwapBuffers();
807 end;
810 procedure g_SetVSync (vsync: Boolean);
811 {$IF not DEFINED(HEADLESS)}
812 var
813 v: Byte;
814 {$ENDIF}
815 begin
816 {$IF not DEFINED(HEADLESS)}
817 if vsync then v := 1 else v := 0;
818 if (SDL_GL_SetSwapInterval(v) <> 0) then
819 begin
820 e_WriteLog('oops; can''t change vsync option, restart required', TMsgType.Warning);
821 end
822 else
823 begin
824 if vsync then e_WriteLog('VSync: ON', TMsgType.Notify) else e_WriteLog('VSync: OFF', TMsgType.Notify);
825 end;
826 {$ENDIF}
827 end;
830 procedure InitOpenGL ();
831 begin
832 {$IF not DEFINED(HEADLESS)}
833 {$IFDEF USE_GLES1}
834 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
835 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
836 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
837 {$ELSE}
838 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
839 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
840 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
841 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
842 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
843 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
844 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
845 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
846 {$ENDIF}
847 {$ENDIF}
848 end;
851 function glHasExtension (const name: AnsiString): Boolean;
852 var
853 exts: PChar;
854 i: Integer;
855 found: Boolean;
856 extName: ShortString;
857 begin
858 result := false;
859 if (Length(name) = 0) then exit;
860 exts := glGetString(GL_EXTENSIONS);
861 if (exts = nil) then exit;
862 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
863 while (exts[0] <> #0) do
864 begin
865 if gwin_dump_extensions then
866 begin
867 i := 0;
868 while (exts[i] <> #0) and (exts[i] <> ' ') do Inc(i);
869 if i > 255 then
870 begin
871 e_WriteLog('FUUUUUUUUUUUUU', TMsgType.Warning);
872 end
873 else
874 begin
875 Move(exts^, extName[1], i);
876 extName[0] := Char(i);
877 e_WriteLog(Format('EXT: %s', [extName]), TMsgType.Notify);
878 end;
879 end;
880 found := true;
881 for i := 0 to length(name)-1 do
882 begin
883 if (exts[i] = #0) then begin found := false; break; end;
884 if (exts[i] <> name[i+1]) then begin found := false; break; end;
885 end;
886 if found and ((exts[Length(name)] = #0) or (exts[Length(name)] = ' ')) then begin result := true; exit; end;
887 while (exts[0] <> #0) and (exts[0] <> ' ') do Inc(exts);
888 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
889 end;
890 end;
893 function SDLMain (): Integer;
894 var
895 idx: Integer;
896 {$IF not DEFINED(HEADLESS)}
897 ltmp: Integer;
898 {$ENDIF}
899 arg: AnsiString;
900 mdfo: TStream;
901 {$IFDEF ENABLE_HOLMES}
902 itmp: Integer;
903 valres: Word;
904 {$ENDIF}
905 begin
906 {$IFDEF HEADLESS}
907 e_NoGraphics := true;
908 {$ELSE}
909 {$IFDEF ENABLE_HOLMES}
910 if (not g_holmes_imfunctional) then
911 begin
912 uiInitialize();
913 uiContext.font := 'win14';
914 end;
915 {$ENDIF}
916 {$ENDIF}
918 idx := 1;
919 while (idx <= ParamCount) do
920 begin
921 arg := ParamStr(idx);
922 Inc(idx);
923 if arg = '--opengl-dump-exts' then gwin_dump_extensions := true;
924 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
925 if arg = '--jah' then g_profile_history_size := 100;
926 if arg = '--no-particles' then gpart_dbg_enabled := false;
927 if arg = '--no-los' then gmon_dbg_los_enabled := false;
929 if arg = '--profile-render' then g_profile_frame_draw := true;
930 if arg = '--profile-coldet' then g_profile_collision := true;
931 if arg = '--profile-los' then g_profile_los := true;
933 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
934 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
935 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
936 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
937 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
938 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
940 {.$IF DEFINED(D2F_DEBUG)}
941 if arg = '--aimline' then g_dbg_aimline_on := true;
942 {.$ENDIF}
944 {$IFDEF ENABLE_HOLMES}
945 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
947 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
948 begin
949 if (idx <= ParamCount) then
950 begin
951 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
952 Inc(idx);
953 end;
954 end;
956 if (arg = '--holmes-font') or (arg = '-holmes-font') then
957 begin
958 if (idx <= ParamCount) then
959 begin
960 itmp := 0;
961 val(ParamStr(idx), itmp, valres);
962 {$IFNDEF HEADLESS}
963 if (valres = 0) and (not g_holmes_imfunctional) then
964 begin
965 case itmp of
966 8: uiContext.font := 'win8';
967 14: uiContext.font := 'win14';
968 16: uiContext.font := 'win16';
969 end;
970 end;
971 {$ELSE}
972 // fuck off, fpc!
973 itmp := itmp;
974 valres := valres;
975 {$ENDIF}
976 Inc(idx);
977 end;
978 end;
979 {$ENDIF}
981 if (arg = '--game-scale') or (arg = '-game-scale') then
982 begin
983 if (idx <= ParamCount) then
984 begin
985 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
986 Inc(idx);
987 end;
988 end;
990 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
991 begin
992 mdfo := createDiskFile('mapdef.txt');
993 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
994 mdfo.Free();
995 Halt(0);
996 end;
997 end;
999 e_WriteLog('Initializing OpenGL', TMsgType.Notify);
1000 InitOpenGL();
1002 e_WriteLog('Creating GL window', TMsgType.Notify);
1003 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION]))) then
1004 begin
1005 result := 0;
1006 e_WriteLog('Unable to create GL window: ' + SDL_GetError(), TMsgType.Fatal);
1007 exit;
1008 end;
1010 {EnumDisplayModes();}
1012 {$IFDEF HEADLESS}
1013 //gwin_k8_enable_light_experiments := false;
1014 gwin_has_stencil := false;
1015 glLegacyNPOT := false;
1016 gwin_dump_extensions := false;
1017 {$ELSE}
1018 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, @ltmp);
1019 e_LogWritefln('stencil buffer size: %s', [ltmp]);
1020 gwin_has_stencil := (ltmp > 0);
1022 if glHasExtension('GL_ARB_texture_non_power_of_two') or
1023 glHasExtension('GL_OES_texture_npot') then
1024 begin
1025 e_WriteLog('NPOT textures: YES', TMsgType.Notify);
1026 glLegacyNPOT := false;
1027 end
1028 else
1029 begin
1030 e_WriteLog('NPOT textures: NO', TMsgType.Warning);
1031 glLegacyNPOT := true;
1032 end;
1033 gwin_dump_extensions := false;
1034 {$ENDIF}
1036 Init();
1037 Time_Old := GetTimer();
1039 // Êîìàíäíàÿ ñòðîêà
1040 if (ParamCount > 0) then g_Game_Process_Params();
1042 // Çàïðîñ ÿçûêà
1043 if (not gGameOn) and gAskLanguage then g_Menu_AskLanguage();
1045 e_WriteLog('Entering the main loop', TMsgType.Notify);
1047 // main loop
1048 while not ProcessMessage() do begin end;
1050 Release();
1051 KillGLWindow(false);
1053 result := 0;
1054 end;
1057 end.