DEADSOFTWARE

set vsync after window creation (it should work better this way)
[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 SysUtils, Classes, MAPDEF,
55 SDL2, GL, GLExt, e_graphics, e_log, e_texture, g_main,
56 g_console, e_input, g_options, g_game,
57 g_basic, g_textures, e_sound, g_sound, g_menu, ENet, g_net,
58 g_map, g_gfx, g_monsters, g_holmes, xprofiler,
59 sdlcarcass, fui_ctls;
62 const
63 ProgressUpdateMSecs = 1;//100;
65 var
66 h_Wnd: PSDL_Window = nil;
67 h_GL: TSDL_GLContext = nil;
68 Time, Time_Delta, Time_Old: Int64;
69 flag: Boolean;
70 {$IF not DEFINED(HEADLESS)}
71 wTitle: PChar = nil;
72 wasFullscreen: Boolean = true; // so we need to recreate the window
73 {$ENDIF}
74 wNeedTimeReset: Boolean = false;
75 wMinimized: Boolean = false;
76 wMaximized: Boolean = false;
77 wLoadingProgress: Boolean = false;
78 wLoadingQuit: Boolean = false;
79 {$IFNDEF WINDOWS}
80 ticksOverflow: Int64 = -1;
81 lastTicks: Uint32 = 0; // to detect overflow
82 {$ENDIF}
85 procedure KillGLWindow (preserveGL: Boolean);
86 begin
87 if (h_GL <> nil) and (not preserveGL) then begin if (assigned(oglDeinitCB)) then oglDeinitCB(); end;
88 if (h_Wnd <> nil) then SDL_DestroyWindow(h_Wnd);
89 if (h_GL <> nil) and (not preserveGL) then SDL_GL_DeleteContext(h_GL);
90 h_Wnd := nil;
91 if (not preserveGL) then h_GL := nil;
92 end;
95 function g_Window_SetDisplay (preserveGL: Boolean = false): Boolean;
96 {$IF not DEFINED(HEADLESS)}
97 var
98 mode, cmode: TSDL_DisplayMode;
99 wFlags: LongWord = 0;
100 nw, nh: Integer;
101 {$ENDIF}
102 begin
103 {$IF not DEFINED(HEADLESS)}
104 result := false;
106 e_WriteLog('Setting display mode...', TMsgType.Notify);
108 wFlags := SDL_WINDOW_OPENGL {or SDL_WINDOW_RESIZABLE};
109 if gFullscreen then wFlags := wFlags {or SDL_WINDOW_FULLSCREEN} else wFlags := wFlags or SDL_WINDOW_RESIZABLE;
110 if (not gFullscreen) and (not preserveGL) and gWinMaximized then wFlags := wFlags or SDL_WINDOW_MAXIMIZED else gWinMaximized := false;
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 e_WriteLog('SDL: cannot find display mode for '+IntToStr(gScreenWidth), TMsgType.Notify);
122 gScreenWidth := 800;
123 gScreenHeight := 600;
124 end
125 else
126 begin
127 e_WriteLog('SDL: found display mode for '+IntToStr(gScreenWidth)+'x'+IntToStr(gScreenHeight)+': '+IntToStr(cmode.w)+'x'+IntToStr(cmode.h), TMsgType.Notify);
128 gScreenWidth := cmode.w;
129 gScreenHeight := cmode.h;
130 end;
131 end;
133 if (preserveGL) and (h_Wnd <> nil) and (not gFullscreen) and (not wasFullscreen) then
134 begin
135 //SDL_SetWindowMaximumSize(h_Wnd, gScreenWidth, gScreenHeight);
136 //SDL_SetWindowDisplayMode(h_Wnd, @cmode);
137 if (wMaximized) then SDL_RestoreWindow(h_Wnd);
138 wMaximized := false;
139 gWinMaximized := false;
140 SDL_SetWindowSize(h_Wnd, gScreenWidth, gScreenHeight);
141 //SDL_SetWindowFullscreen(h_Wnd, SDL_WINDOW_FULLSCREEN);
142 //SDL_SetWindowFullscreen(h_Wnd, 0);
143 end
144 else
145 begin
146 KillGLWindow(preserveGL);
147 h_Wnd := SDL_CreateWindow(PChar(wTitle), gWinRealPosX, gWinRealPosY, gScreenWidth, gScreenHeight, wFlags);
148 if gFullscreen then
149 SDL_SetWindowFullscreen(h_Wnd, SDL_WINDOW_FULLSCREEN);
150 if (h_Wnd = nil) then exit;
151 end;
152 wasFullscreen := gFullscreen;
154 SDL_GL_MakeCurrent(h_Wnd, h_GL);
155 SDL_ShowCursor(SDL_DISABLE);
156 if (gFullscreen) then
157 begin
158 nw := 0;
159 nh := 0;
160 SDL_GetWindowSize(h_Wnd, @nw, @nh);
161 if (nw > 128) and (nh > 128) then
162 begin
163 e_WriteLog('SDL: fullscreen window got size '+IntToStr(nw)+'x'+IntToStr(nh)+': '+IntToStr(gScreenWidth)+'x'+IntToStr(gScreenHeight), TMsgType.Notify);
164 gScreenWidth := nw;
165 gScreenHeight := nh;
166 end
167 else
168 begin
169 e_WriteLog('SDL: fullscreen window got invalid size: '+IntToStr(nw)+'x'+IntToStr(nh), TMsgType.Notify);
170 end;
171 end;
172 fuiScrWdt := gScreenWidth;
173 fuiScrHgt := gScreenHeight;
174 if (h_GL <> nil) and (not preserveGL) then begin if (assigned(oglInitCB)) then oglInitCB(); end;
175 {$ENDIF}
177 result := true;
178 end;
181 function GetDisplayModes (dbpp: LongWord; var selres: LongWord): SSArray;
182 var
183 mode: TSDL_DisplayMode;
184 res, i, k, n, pw, ph: Integer;
185 begin
186 SetLength(result, 0);
187 {$IFDEF HEADLESS}exit;{$ENDIF}
188 k := 0; selres := 0;
189 n := SDL_GetNumDisplayModes(0);
190 pw := 0; ph := 0;
191 for i := 0 to n do
192 begin
193 res := SDL_GetDisplayMode(0, i, @mode);
194 if res < 0 then continue;
195 if SDL_BITSPERPIXEL(mode.format) = gBPP then continue;
196 if (mode.w = pw) and (mode.h = ph) then continue;
197 if (mode.w = gScreenWidth) and (mode.h = gScreenHeight) then
198 selres := k;
199 Inc(k);
200 SetLength(result, k);
201 result[k-1] := IntToStr(mode.w) + 'x' + IntToStr(mode.h);
202 pw := mode.w; ph := mode.h
203 end;
205 e_WriteLog('SDL: Got ' + IntToStr(k) + ' resolutions.', TMsgType.Notify);
206 end;
209 procedure Sleep (ms: LongWord);
210 begin
211 SDL_Delay(ms);
212 end;
215 procedure ChangeWindowSize ();
216 begin
217 e_LogWritefln(' ChangeWindowSize: (ws=%dx%d) (ss=%dx%d)', [gWinSizeX, gWinSizeY, gScreenWidth, gScreenHeight]);
218 gWinSizeX := gScreenWidth;
219 gWinSizeY := gScreenHeight;
220 {$IF not DEFINED(HEADLESS)}
221 fuiScrWdt := gScreenWidth;
222 fuiScrHgt := gScreenHeight;
223 e_ResizeWindow(gScreenWidth, gScreenHeight);
224 g_Game_SetupScreenSize();
225 g_Menu_Reset();
226 g_Game_ClearLoading();
227 {$ENDIF}
228 end;
231 function g_Window_SetSize (w, h: Word; fullscreen: Boolean): Boolean;
232 {$IF not DEFINED(HEADLESS)}
233 var
234 preserve: Boolean;
235 {$ENDIF}
236 begin
237 result := false;
238 {$IF not DEFINED(HEADLESS)}
239 preserve := false;
241 if (gScreenWidth <> w) or (gScreenHeight <> h) then
242 begin
243 result := true;
244 preserve := true;
245 gScreenWidth := w;
246 gScreenHeight := h;
247 end;
249 if (gFullscreen <> fullscreen) then
250 begin
251 result := true;
252 preserve := true;
253 gFullscreen := fullscreen;
254 preserve := true;
255 end;
257 if result then
258 begin
259 g_Window_SetDisplay(preserve);
260 ChangeWindowSize();
261 end;
262 {$ENDIF}
263 end;
266 function WindowEventHandler (constref ev: TSDL_WindowEvent): Boolean;
267 var
268 wActivate, wDeactivate: Boolean;
269 begin
270 result := false;
271 wActivate := false;
272 wDeactivate := false;
274 case ev.event of
275 SDL_WINDOWEVENT_MOVED:
276 begin
277 if not (gFullscreen or gWinMaximized) then
278 begin
279 gWinRealPosX := ev.data1;
280 gWinRealPosY := ev.data2;
281 end;
282 end;
284 SDL_WINDOWEVENT_MINIMIZED:
285 begin
286 e_UnpressAllKeys();
287 if not wMinimized then
288 begin
289 e_ResizeWindow(0, 0);
290 wMinimized := true;
291 if g_debug_WinMsgs then
292 begin
293 g_Console_Add('Now minimized');
294 e_WriteLog('[DEBUG] WinMsgs: Now minimized', TMsgType.Notify);
295 end;
296 wDeactivate := true;
297 end;
298 end;
300 SDL_WINDOWEVENT_RESIZED:
301 begin
302 e_LogWritefln('Resize: (os=%dx%d) (ns=%dx%d)', [gScreenWidth, gScreenHeight, Integer(ev.data1), Integer(ev.data2)]);
303 {if (gFullscreen) then
304 begin
305 e_LogWriteln(' fullscreen fix applied.');
306 if (gScreenWidth <> ev.data1) or (gScreenHeight <> ev.data2) then
307 begin
308 SDL_SetWindowSize(h_Wnd, gScreenWidth, gScreenHeight);
309 end;
310 end
311 else}
312 begin
313 gScreenWidth := ev.data1;
314 gScreenHeight := ev.data2;
315 end;
316 ChangeWindowSize();
317 SwapBuffers();
318 if g_debug_WinMsgs then
319 begin
320 g_Console_Add('Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2));
321 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2), TMsgType.Notify);
322 end;
323 end;
325 SDL_WINDOWEVENT_EXPOSED:
326 SwapBuffers();
328 SDL_WINDOWEVENT_MAXIMIZED:
329 begin
330 wMaximized := true;
331 if wMinimized then
332 begin
333 e_ResizeWindow(gScreenWidth, gScreenHeight);
334 wMinimized := false;
335 wActivate := true;
336 end;
337 if (not gWinMaximized) and (not gFullscreen) then
338 begin
339 gWinMaximized := true;
340 if g_debug_WinMsgs then
341 begin
342 g_Console_Add('Now maximized');
343 e_WriteLog('[DEBUG] WinMsgs: Now maximized', TMsgType.Notify);
344 end;
345 end;
346 end;
348 SDL_WINDOWEVENT_RESTORED:
349 begin
350 wMaximized := false;
351 if wMinimized then
352 begin
353 e_ResizeWindow(gScreenWidth, gScreenHeight);
354 wMinimized := false;
355 wActivate := true;
356 end;
357 gWinMaximized := false;
358 if g_debug_WinMsgs then
359 begin
360 g_Console_Add('Now restored');
361 e_WriteLog('[DEBUG] WinMsgs: Now restored', TMsgType.Notify);
362 end;
363 end;
365 SDL_WINDOWEVENT_FOCUS_GAINED:
366 begin
367 wActivate := true;
368 //e_WriteLog('window gained focus!', MSG_NOTIFY);
369 end;
371 SDL_WINDOWEVENT_FOCUS_LOST:
372 begin
373 wDeactivate := true;
374 e_UnpressAllKeys();
375 //e_WriteLog('window lost focus!', MSG_NOTIFY);
376 end;
377 end;
379 if wDeactivate then
380 begin
381 if gWinActive then
382 begin
383 e_WriteLog('deactivating window', TMsgType.Notify);
384 e_EnableInput := false;
385 e_ClearInputBuffer();
387 if gMuteWhenInactive then
388 begin
389 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
390 e_MuteChannels(true);
391 end;
393 if g_debug_WinMsgs then
394 begin
395 g_Console_Add('Now inactive');
396 e_WriteLog('[DEBUG] WinMsgs: Now inactive', TMsgType.Notify);
397 end;
399 gWinActive := false;
401 if assigned(winBlurCB) then winBlurCB();
402 end;
403 end
404 else if wActivate then
405 begin
406 if not gWinActive then
407 begin
408 //e_WriteLog('activating window', MSG_NOTIFY);
409 e_EnableInput := true;
411 if gMuteWhenInactive then
412 begin
413 //e_WriteLog('activating sounds', MSG_NOTIFY);
414 e_MuteChannels(false);
415 end;
417 if g_debug_WinMsgs then
418 begin
419 g_Console_Add('Now active');
420 e_WriteLog('[DEBUG] WinMsgs: Now active', TMsgType.Notify);
421 end;
423 gWinActive := true;
424 if assigned(winFocusCB) then winFocusCB();
425 end;
426 end;
427 end;
430 function EventHandler (var ev: TSDL_Event): Boolean;
431 var
432 key, keychr: Word;
433 uc: UnicodeChar;
434 down: Boolean;
435 begin
436 result := false;
438 case ev.type_ of
439 SDL_WINDOWEVENT:
440 result := WindowEventHandler(ev.window);
442 SDL_QUITEV:
443 begin
444 if (gExit <> EXIT_QUIT) then
445 begin
446 if not wLoadingProgress then
447 begin
448 g_Game_Free();
449 g_Game_Quit();
450 end
451 else
452 begin
453 wLoadingQuit := true;
454 end;
455 end;
456 result := true;
457 end;
459 SDL_KEYDOWN, SDL_KEYUP:
460 begin
461 key := ev.key.keysym.scancode;
462 down := (ev.type_ = SDL_KEYDOWN);
463 {$IF not DEFINED(HEADLESS)}
464 if fuiOnSDLEvent(ev) then
465 begin
466 // event eaten, but...
467 if not down then e_KeyUpDown(key, false);
468 exit;
469 end;
470 {$ENDIF}
471 if down then KeyPress(key);
472 e_KeyUpDown(key, down);
473 end;
475 {$IF not DEFINED(HEADLESS)}
476 SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION:
477 fuiOnSDLEvent(ev);
478 {$ENDIF}
480 SDL_TEXTINPUT:
481 begin
482 Utf8ToUnicode(@uc, PChar(ev.text.text), 1);
483 keychr := Word(uc);
484 if (keychr > 127) then keychr := Word(wchar2win(WideChar(keychr)));
485 if (keychr > 0) and (keychr <= 255) then CharPress(AnsiChar(keychr));
486 end;
488 // other key presses and joysticks are handled in e_input
489 end;
490 end;
493 procedure SwapBuffers ();
494 begin
495 {$IF not DEFINED(HEADLESS)}
496 SDL_GL_SwapWindow(h_Wnd);
497 {$ENDIF}
498 end;
501 function CreateGLWindow (Title: PChar): Boolean;
502 begin
503 result := false;
505 gWinSizeX := gScreenWidth;
506 gWinSizeY := gScreenHeight;
508 {$IF not DEFINED(HEADLESS)}
509 wTitle := Title;
510 {$ENDIF}
511 e_WriteLog('Creating window', TMsgType.Notify);
513 if not g_Window_SetDisplay() then
514 begin
515 KillGLWindow(false);
516 e_WriteLog('Window creation error (resolution not supported?)', TMsgType.Fatal);
517 exit;
518 end;
520 {$IF not DEFINED(HEADLESS)}
521 h_GL := SDL_GL_CreateContext(h_Wnd);
522 if (h_GL = nil) then exit;
523 fuiScrWdt := gScreenWidth;
524 fuiScrHgt := gScreenHeight;
525 if (assigned(oglInitCB)) then oglInitCB();
526 g_SetVSync(gVSync);
527 {$ENDIF}
529 e_ResizeWindow(gScreenWidth, gScreenHeight);
530 e_InitGL();
532 result := true;
533 end;
536 {$IFDEF WINDOWS}
537 // windoze sux; in headless mode `GetTickCount()` (and SDL) returns shit
538 function GetTimer (): Int64;
539 var
540 F, C: Int64;
541 begin
542 QueryPerformanceFrequency(F);
543 QueryPerformanceCounter(C);
544 result := Round(C/F*1000{000});
545 end;
546 {$ELSE}
547 function GetTimer (): Int64;
548 var
549 t: Uint32;
550 tt: Int64;
551 begin
552 t := SDL_GetTicks();
553 if (ticksOverflow = -1) then
554 begin
555 ticksOverflow := 0;
556 lastTicks := t;
557 end
558 else
559 begin
560 if (lastTicks > t) then
561 begin
562 // overflow, increment overflow ;-)
563 ticksOverflow := ticksOverflow+(Int64($ffffffff)+Int64(1));
564 tt := (Int64($ffffffff)+Int64(1))+Int64(t);
565 t := Uint32(tt-lastTicks);
566 end;
567 end;
568 lastTicks := t;
569 result := ticksOverflow+Int64(t);
570 end;
571 {$ENDIF}
574 procedure ResetTimer ();
575 begin
576 wNeedTimeReset := true;
577 end;
580 procedure PushExitEvent ();
581 var
582 ev: TSDL_Event;
583 begin
584 ev.type_ := SDL_QUITEV;
585 SDL_PushEvent(@ev);
586 end;
589 var
590 prevLoadingUpdateTime: UInt64 = 0;
592 procedure ProcessLoading (forceUpdate: Boolean=false);
593 var
594 ev: TSDL_Event;
595 ID: LongWord;
596 stt: UInt64;
597 begin
598 FillChar(ev, sizeof(ev), 0);
599 wLoadingProgress := true;
601 while (SDL_PollEvent(@ev) > 0) do
602 begin
603 EventHandler(ev);
604 if (ev.type_ = SDL_QUITEV) then break;
605 end;
606 e_PollJoysticks();
608 if (ev.type_ = SDL_QUITEV) or (gExit = EXIT_QUIT) then
609 begin
610 wLoadingProgress := false;
611 exit;
612 end;
614 if not wMinimized then
615 begin
616 if forceUpdate then
617 begin
618 prevLoadingUpdateTime := getTimeMilli();
619 end
620 else
621 begin
622 stt := getTimeMilli();
623 if (stt < prevLoadingUpdateTime) or (stt-prevLoadingUpdateTime >= ProgressUpdateMSecs) then
624 begin
625 prevLoadingUpdateTime := stt;
626 forceUpdate := true;
627 end;
628 end;
630 if forceUpdate then
631 begin
632 if g_Texture_Get('INTER', ID) then
633 begin
634 e_DrawSize(ID, 0, 0, 0, false, false, gScreenWidth, gScreenHeight);
635 e_DarkenQuadWH(0, 0, gScreenWidth, gScreenHeight, 150);
636 end
637 else
638 begin
639 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
640 end;
642 DrawLoadingStat();
643 SwapBuffers();
644 end;
645 end;
647 e_SoundUpdate();
649 if NetMode = NET_SERVER then
650 begin
651 g_Net_Host_Update();
652 end
653 else
654 begin
655 if (NetMode = NET_CLIENT) and (NetState <> NET_STATE_AUTH) then g_Net_Client_UpdateWhileLoading();
656 end;
658 wLoadingProgress := false;
659 end;
662 function g_ProcessMessages (): Boolean;
663 var
664 ev: TSDL_Event;
665 begin
666 result := false;
667 FillChar(ev, SizeOf(ev), 0);
668 while (SDL_PollEvent(@ev) > 0) do
669 begin
670 result := EventHandler(ev);
671 if (ev.type_ = SDL_QUITEV) then exit;
672 end;
673 e_PollJoysticks();
674 end;
677 function ProcessMessage (): Boolean;
678 var
679 i, t: Integer;
680 begin
681 result := g_ProcessMessages();
683 Time := GetTimer();
684 Time_Delta := Time-Time_Old;
686 flag := false;
688 if wNeedTimeReset then
689 begin
690 Time_Delta := 28;
691 wNeedTimeReset := false;
692 end;
694 g_Map_ProfilersBegin();
695 g_Mons_ProfilersBegin();
697 t := Time_Delta div 28;
698 if (t > 0) then
699 begin
700 flag := true;
701 for i := 1 to t do
702 begin
703 if (NetMode = NET_SERVER) then g_Net_Host_Update()
704 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
705 Update();
706 end;
707 end
708 else
709 begin
710 if (NetMode = NET_SERVER) then g_Net_Host_Update()
711 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
712 end;
714 g_Map_ProfilersEnd();
715 g_Mons_ProfilersEnd();
717 if wLoadingQuit then
718 begin
719 g_Game_Free();
720 g_Game_Quit();
721 end;
723 if (gExit = EXIT_QUIT) then
724 begin
725 result := true;
726 exit;
727 end;
729 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ
730 if flag then
731 begin
732 Time_Old := Time-(Time_Delta mod 28);
733 if (not wMinimized) then
734 begin
735 Draw();
736 SwapBuffers();
737 end;
738 end
739 else
740 begin
741 Sleep(1); // release time slice, so we won't eat 100% CPU
742 end;
744 e_SoundUpdate();
745 end;
748 procedure ReDrawWindow ();
749 begin
750 SwapBuffers();
751 end;
754 procedure g_SetVSync (vsync: Boolean);
755 {$IF not DEFINED(HEADLESS)}
756 var
757 v: Byte;
758 {$ENDIF}
759 begin
760 {$IF not DEFINED(HEADLESS)}
761 if vsync then v := 1 else v := 0;
762 if (SDL_GL_SetSwapInterval(v) <> 0) then
763 begin
764 e_WriteLog('oops; can''t change vsync option, restart required', TMsgType.Warning);
765 end
766 else
767 begin
768 if vsync then e_WriteLog('VSync: ON', TMsgType.Notify) else e_WriteLog('VSync: OFF', TMsgType.Notify);
769 end;
770 {$ENDIF}
771 end;
774 procedure InitOpenGL ();
775 begin
776 {$IF not DEFINED(HEADLESS)}
777 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
778 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
779 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
780 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
781 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
782 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
783 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
784 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but...
785 {$ENDIF}
786 end;
789 function glHasExtension (const name: AnsiString): Boolean;
790 var
791 exts: PChar;
792 i: Integer;
793 found: Boolean;
794 extName: ShortString;
795 begin
796 result := false;
797 if (Length(name) = 0) then exit;
798 exts := glGetString(GL_EXTENSIONS);
799 if (exts = nil) then exit;
800 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
801 while (exts[0] <> #0) do
802 begin
803 if gwin_dump_extensions then
804 begin
805 i := 0;
806 while (exts[i] <> #0) and (exts[i] <> ' ') do Inc(i);
807 if i > 255 then
808 begin
809 e_WriteLog('FUUUUUUUUUUUUU', TMsgType.Warning);
810 end
811 else
812 begin
813 Move(exts^, extName[1], i);
814 extName[0] := Char(i);
815 e_WriteLog(Format('EXT: %s', [extName]), TMsgType.Notify);
816 end;
817 end;
818 found := true;
819 for i := 0 to length(name)-1 do
820 begin
821 if (exts[i] = #0) then begin found := false; break; end;
822 if (exts[i] <> name[i+1]) then begin found := false; break; end;
823 end;
824 if found and ((exts[Length(name)] = #0) or (exts[Length(name)] = ' ')) then begin result := true; exit; end;
825 while (exts[0] <> #0) and (exts[0] <> ' ') do Inc(exts);
826 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
827 end;
828 end;
831 function SDLMain (): Integer;
832 var
833 idx: Integer;
834 {$IF not DEFINED(HEADLESS)}
835 ltmp: Integer;
836 {$ENDIF}
837 arg: AnsiString;
838 mdfo: TStream;
839 itmp: Integer;
840 valres: Word;
841 begin
842 {$IFDEF HEADLESS}
843 e_NoGraphics := true;
844 {$ELSE}
845 if (not g_holmes_imfunctional) then
846 begin
847 uiInitialize();
848 uiContext.font := 'win14';
849 end;
850 {$ENDIF}
852 idx := 1;
853 while (idx <= ParamCount) do
854 begin
855 arg := ParamStr(idx);
856 Inc(idx);
857 if arg = '--opengl-dump-exts' then gwin_dump_extensions := true;
858 //if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
859 if arg = '--jah' then g_profile_history_size := 100;
860 if arg = '--no-particles' then gpart_dbg_enabled := false;
861 if arg = '--no-los' then gmon_dbg_los_enabled := false;
863 if arg = '--profile-render' then g_profile_frame_draw := true;
864 if arg = '--profile-coldet' then g_profile_collision := true;
865 if arg = '--profile-los' then g_profile_los := true;
867 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
868 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
869 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
870 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
871 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
872 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
874 {.$IF DEFINED(D2F_DEBUG)}
875 if arg = '--aimline' then g_dbg_aimline_on := true;
876 {.$ENDIF}
878 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
880 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
881 begin
882 if (idx <= ParamCount) then
883 begin
884 if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
885 Inc(idx);
886 end;
887 end;
889 if (arg = '--holmes-font') or (arg = '-holmes-font') then
890 begin
891 if (idx <= ParamCount) then
892 begin
893 itmp := 0;
894 val(ParamStr(idx), itmp, valres);
895 {$IFNDEF HEADLESS}
896 if (valres = 0) and (not g_holmes_imfunctional) then
897 begin
898 case itmp of
899 8: uiContext.font := 'win8';
900 14: uiContext.font := 'win14';
901 16: uiContext.font := 'win16';
902 end;
903 end;
904 {$ELSE}
905 // fuck off, fpc!
906 itmp := itmp;
907 valres := valres;
908 {$ENDIF}
909 Inc(idx);
910 end;
911 end;
913 if (arg = '--game-scale') or (arg = '-game-scale') then
914 begin
915 if (idx <= ParamCount) then
916 begin
917 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
918 Inc(idx);
919 end;
920 end;
922 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
923 begin
924 mdfo := createDiskFile('mapdef.txt');
925 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
926 mdfo.Free();
927 Halt(0);
928 end;
929 end;
931 e_WriteLog('Initializing OpenGL', TMsgType.Notify);
932 InitOpenGL();
934 e_WriteLog('Creating GL window', TMsgType.Notify);
935 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION]))) then
936 begin
937 result := 0;
938 exit;
939 end;
941 {EnumDisplayModes();}
943 {$IFDEF HEADLESS}
944 //gwin_k8_enable_light_experiments := false;
945 gwin_has_stencil := false;
946 glLegacyNPOT := false;
947 gwin_dump_extensions := false;
948 {$ELSE}
949 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, @ltmp);
950 e_LogWritefln('stencil buffer size: %s', [ltmp]);
951 gwin_has_stencil := (ltmp > 0);
953 if not glHasExtension('GL_ARB_texture_non_power_of_two') then
954 begin
955 e_WriteLog('NPOT textures: NO', TMsgType.Warning);
956 glLegacyNPOT := true;
957 end
958 else
959 begin
960 e_WriteLog('NPOT textures: YES', TMsgType.Notify);
961 glLegacyNPOT := false;
962 end;
963 gwin_dump_extensions := false;
964 {$ENDIF}
966 Init();
967 Time_Old := GetTimer();
969 // Êîìàíäíàÿ ñòðîêà
970 if (ParamCount > 0) then g_Game_Process_Params();
972 // Çàïðîñ ÿçûêà
973 if (not gGameOn) and gAskLanguage then g_Menu_AskLanguage();
975 e_WriteLog('Entering the main loop', TMsgType.Notify);
977 // main loop
978 while not ProcessMessage() do begin end;
980 Release();
981 KillGLWindow(false);
983 result := 0;
984 end;
987 end.