DEADSOFTWARE

mplat fixes
[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 wadreader;
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 ProcessLoading (forceUpdate: Boolean=false);
32 procedure ReDrawWindow();
33 procedure SwapBuffers();
34 procedure Sleep(ms: LongWord);
35 function GetDisplayModes(dBPP: DWORD; var SelRes: DWORD): SArray;
36 function g_Window_SetDisplay(PreserveGL: Boolean = False): Boolean;
37 function g_Window_SetSize(W, H: Word; FScreen: Boolean): Boolean;
39 var
40 gwin_dump_extensions: Boolean = false;
41 gwin_has_stencil: Boolean = false;
42 gwin_k8_enable_light_experiments: Boolean = false;
43 g_dbg_aimline_on: Boolean = false;
46 implementation
48 uses
49 {$IFDEF WINDOWS}Windows,{$ENDIF}
50 SDL2, GL, GLExt, e_graphics, e_log, g_main,
51 g_console, SysUtils, e_input, g_options, g_game,
52 g_basic, g_textures, e_sound, g_sound, g_menu, ENet, g_net,
53 g_map, g_gfx, g_monsters, g_holmes, xprofiler;
55 var
56 h_Wnd: PSDL_Window;
57 h_GL: TSDL_GLContext;
58 wFlags: LongWord = 0;
59 Time, Time_Delta, Time_Old: Int64;
60 flag: Boolean;
61 wTitle: PChar = nil;
62 wNeedTimeReset: Boolean = False;
63 //wWindowCreated: Boolean = False;
64 //wCursorShown: Boolean = False;
65 wMinimized: Boolean = False;
66 //wNeedFree: Boolean = True;
67 wLoadingProgress: Boolean = False;
68 wLoadingQuit: Boolean = False;
69 {wWinPause: Byte = 0;}
70 {$IFNDEF WINDOWS}
71 ticksOverflow: Int64 = -1;
72 lastTicks: Uint32 = 0; // to detect overflow
73 {$ENDIF}
74 curMsButState: Word = 0;
75 curKbState: Word = 0;
76 curMsX: Integer = 0;
77 curMsY: Integer = 0;
79 const
80 // TODO: move this to a separate file
81 CP1251: array [0..127] of Word = (
82 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
83 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
84 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
85 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
86 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
87 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
88 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
89 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
90 );
92 // TODO: make a transition table or something
93 function WCharToCP1251(wc: Word): Word;
94 var
95 n: Word;
96 begin
97 Result := 0;
98 for n := 0 to 127 do
99 if CP1251[n] = wc then begin Result := n; break end;
100 Result := Result + 128;
101 end;
103 function g_Window_SetDisplay(PreserveGL: Boolean = False): Boolean;
104 var
105 mode, cmode: TSDL_DisplayMode;
106 begin
107 {$IFDEF HEADLESS}
108 Result := True;
109 Exit;
110 {$ENDIF}
112 Result := False;
114 e_WriteLog('Setting display mode...', MSG_NOTIFY);
116 wFlags := SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE;
117 if gFullscreen then wFlags := wFlags or SDL_WINDOW_FULLSCREEN;
118 if gWinMaximized then wFlags := wFlags or SDL_WINDOW_MAXIMIZED;
120 if h_Wnd <> nil then
121 begin
122 SDL_DestroyWindow(h_Wnd);
123 h_Wnd := nil;
124 end;
126 if gFullscreen then
127 begin
128 mode.w := gScreenWidth;
129 mode.h := gScreenHeight;
130 mode.format := 0;
131 mode.refresh_rate := 0;
132 mode.driverdata := nil;
133 if SDL_GetClosestDisplayMode(0, @mode, @cmode) = nil then
134 begin
135 gScreenWidth := 800;
136 gScreenHeight := 600;
137 end
138 else
139 begin
140 gScreenWidth := cmode.w;
141 gScreenHeight := cmode.h;
142 end;
143 end;
145 h_Wnd := SDL_CreateWindow(PChar(wTitle), gWinRealPosX, gWinRealPosY, gScreenWidth, gScreenHeight, wFlags);
146 if h_Wnd = nil then Exit;
148 SDL_GL_MakeCurrent(h_Wnd, h_GL);
149 SDL_ShowCursor(SDL_DISABLE);
151 Result := True;
152 end;
154 procedure ReShowCursor();
155 begin
156 // TODO: what was this for?
157 end;
159 function GetDisplayModes(dBPP: DWORD; var SelRes: DWORD): SArray;
160 var
161 mode: TSDL_DisplayMode;
162 res, i, k, n, pw, ph: Integer;
163 begin
164 SetLength(Result, 0);
165 {$IFDEF HEADLESS}Exit;{$ENDIF}
166 k := 0; SelRes := 0;
167 n := SDL_GetNumDisplayModes(0);
168 pw := 0; ph := 0;
169 for i := 0 to n do
170 begin
171 res := SDL_GetDisplayMode(0, i, @mode);
172 if res < 0 then continue;
173 if SDL_BITSPERPIXEL(mode.format) = gBPP then continue;
174 if (mode.w = pw) and (mode.h = ph) then continue;
175 if (mode.w = gScreenWidth) and (mode.h = gScreenHeight) then
176 SelRes := k;
177 Inc(k);
178 SetLength(Result, k);
179 Result[k-1] := IntToStr(mode.w) + 'x' + IntToStr(mode.h);
180 pw := mode.w; ph := mode.h
181 end;
183 e_WriteLog('SDL: Got ' + IntToStr(k) + ' resolutions.', MSG_NOTIFY);
184 end;
186 procedure Sleep(ms: LongWord);
187 begin
188 SDL_Delay(ms);
189 end;
191 procedure ChangeWindowSize();
192 begin
193 gWinSizeX := gScreenWidth;
194 gWinSizeY := gScreenHeight;
195 {$IFDEF HEADLESS}Exit;{$ENDIF}
196 e_ResizeWindow(gScreenWidth, gScreenHeight);
197 g_Game_SetupScreenSize();
198 g_Menu_Reset();
199 g_Game_ClearLoading();
200 g_Holmes_VidModeChanged();
201 end;
203 function g_Window_SetSize(W, H: Word; FScreen: Boolean): Boolean;
204 var
205 Preserve: Boolean;
206 begin
207 Result := False;
208 {$IFDEF HEADLESS}Exit;{$ENDIF}
209 Preserve := False;
211 if (gScreenWidth <> W) or (gScreenHeight <> H) then
212 begin
213 Result := True;
214 gScreenWidth := W;
215 gScreenHeight := H;
216 end;
218 if gFullscreen <> FScreen then
219 begin
220 Result := True;
221 gFullscreen := FScreen;
222 Preserve := True;
223 end;
225 if Result then
226 begin
227 g_Window_SetDisplay(Preserve);
228 ChangeWindowSize();
229 end;
230 end;
232 function WindowEventHandler(ev: TSDL_WindowEvent): Boolean;
233 var
234 wActivate, wDeactivate: Boolean;
235 begin
236 Result := False;
237 wActivate := False;
238 wDeactivate := False;
240 case ev.event of
241 SDL_WINDOWEVENT_MOVED:
242 begin
243 if not (gFullscreen or gWinMaximized) then
244 begin
245 gWinRealPosX := ev.data1;
246 gWinRealPosY := ev.data2;
247 end;
248 end;
250 SDL_WINDOWEVENT_MINIMIZED:
251 begin
252 curMsButState := 0;
253 curKbState := 0;
254 e_UnpressAllKeys();
255 if not wMinimized then
256 begin
257 e_ResizeWindow(0, 0);
258 wMinimized := True;
260 if g_debug_WinMsgs then
261 begin
262 g_Console_Add('Now minimized');
263 e_WriteLog('[DEBUG] WinMsgs: Now minimized', MSG_NOTIFY);
264 end;
265 wDeactivate := True;
266 end;
267 end;
269 SDL_WINDOWEVENT_RESIZED:
270 begin
271 curMsButState := 0;
272 curKbState := 0;
273 gScreenWidth := ev.data1;
274 gScreenHeight := ev.data2;
275 ChangeWindowSize();
276 SwapBuffers();
277 if g_debug_WinMsgs then
278 begin
279 g_Console_Add('Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2));
280 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2), MSG_NOTIFY);
281 end;
282 end;
284 SDL_WINDOWEVENT_EXPOSED:
285 SwapBuffers();
287 SDL_WINDOWEVENT_MAXIMIZED:
288 begin
289 curMsButState := 0;
290 curKbState := 0;
291 if wMinimized then
292 begin
293 e_ResizeWindow(gScreenWidth, gScreenHeight);
294 wMinimized := False;
295 wActivate := True;
296 end;
297 if not gWinMaximized then
298 begin
299 gWinMaximized := True;
300 if g_debug_WinMsgs then
301 begin
302 g_Console_Add('Now maximized');
303 e_WriteLog('[DEBUG] WinMsgs: Now maximized', MSG_NOTIFY);
304 end;
305 end;
306 end;
308 SDL_WINDOWEVENT_RESTORED:
309 begin
310 curMsButState := 0;
311 curKbState := 0;
312 if wMinimized then
313 begin
314 e_ResizeWindow(gScreenWidth, gScreenHeight);
315 wMinimized := False;
316 wActivate := True;
317 end;
318 if gWinMaximized then
319 gWinMaximized := False;
320 if g_debug_WinMsgs then
321 begin
322 g_Console_Add('Now restored');
323 e_WriteLog('[DEBUG] WinMsgs: Now restored', MSG_NOTIFY);
324 end;
325 end;
327 SDL_WINDOWEVENT_FOCUS_GAINED:
328 begin
329 curMsButState := 0;
330 curKbState := 0;
331 wActivate := True;
332 //e_WriteLog('window gained focus!', MSG_NOTIFY);
333 g_Holmes_WindowFocused();
334 end;
336 SDL_WINDOWEVENT_FOCUS_LOST:
337 begin
338 curMsButState := 0;
339 curKbState := 0;
340 wDeactivate := True;
341 e_UnpressAllKeys();
342 //e_WriteLog('window lost focus!', MSG_NOTIFY);
343 g_Holmes_WindowBlured();
344 end;
345 end;
347 if wDeactivate then
348 begin
349 if gWinActive then
350 begin
351 e_WriteLog('deactivating window', MSG_NOTIFY);
352 e_EnableInput := False;
353 e_ClearInputBuffer();
355 if gMuteWhenInactive then
356 begin
357 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
358 e_MuteChannels(True);
359 end;
361 if g_debug_WinMsgs then
362 begin
363 g_Console_Add('Now inactive');
364 e_WriteLog('[DEBUG] WinMsgs: Now inactive', MSG_NOTIFY);
365 end;
367 gWinActive := False;
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', MSG_NOTIFY);
387 end;
389 gWinActive := True;
390 end;
391 end;
392 end;
394 function EventHandler(ev: TSDL_Event): Boolean;
395 var
396 key, keychr: Word;
397 uc: UnicodeChar;
398 //joy: Integer;
399 msev: THMouseEvent;
400 kbev: THKeyEvent;
402 function buildBut (b: Byte): Word;
403 begin
404 result := 0;
405 case b of
406 SDL_BUTTON_LEFT: result := result or THMouseEvent.Left;
407 SDL_BUTTON_MIDDLE: result := result or THMouseEvent.Middle;
408 SDL_BUTTON_RIGHT: result := result or THMouseEvent.Right;
409 end;
410 end;
412 procedure updateKBState ();
413 var
414 kbstate: PUint8;
415 begin
416 curKbState := 0;
417 kbstate := SDL_GetKeyboardState(nil);
418 if (kbstate[SDL_SCANCODE_LCTRL] <> 0) or (kbstate[SDL_SCANCODE_RCTRL] <> 0) then curKbState := curKbState or THKeyEvent.ModCtrl;
419 if (kbstate[SDL_SCANCODE_LALT] <> 0) or (kbstate[SDL_SCANCODE_RALT] <> 0) then curKbState := curKbState or THKeyEvent.ModAlt;
420 if (kbstate[SDL_SCANCODE_LSHIFT] <> 0) or (kbstate[SDL_SCANCODE_RSHIFT] <> 0) then curKbState := curKbState or THKeyEvent.ModShift;
421 end;
423 begin
424 Result := False;
425 updateKBState();
427 case ev.type_ of
428 SDL_WINDOWEVENT:
429 Result := WindowEventHandler(ev.window);
431 SDL_QUITEV:
432 begin
433 if gExit <> EXIT_QUIT then
434 begin
435 if not wLoadingProgress then
436 begin
437 g_Game_Free();
438 g_Game_Quit();
439 end
440 else
441 wLoadingQuit := True;
442 end;
443 Result := True;
444 end;
446 SDL_KEYDOWN, SDL_KEYUP:
447 begin
448 key := ev.key.keysym.scancode;
449 if (g_holmes_enabled) then
450 begin
451 if (ev.type_ = SDL_KEYDOWN) then kbev.kind := THKeyEvent.Press else kbev.kind := THKeyEvent.Release;
452 kbev.scan := ev.key.keysym.scancode;
453 kbev.sym := ev.key.keysym.sym;
454 kbev.bstate := curMsButState;
455 kbev.kstate := curKbState;
456 {$IF not DEFINED(HEADLESS)}
457 if g_Holmes_keyEvent(kbev) then
458 begin
459 if (ev.type_ <> SDL_KEYDOWN) then e_KeyUpDown(ev.key.keysym.scancode, false);
460 exit;
461 end;
462 {$ENDIF}
463 end;
464 if (ev.type_ = SDL_KEYDOWN) then KeyPress(key);
465 e_KeyUpDown(ev.key.keysym.scancode, (ev.type_ = SDL_KEYDOWN));
466 end;
468 {$IF not DEFINED(HEADLESS)}
469 SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP:
470 begin
471 msev.dx := ev.button.x-curMsX;
472 msev.dy := ev.button.y-curMsY;
473 curMsX := ev.button.x;
474 curMsY := ev.button.y;
475 if (ev.type_ = SDL_MOUSEBUTTONDOWN) then msev.kind := THMouseEvent.Press else msev.kind := THMouseEvent.Release;
476 msev.but := buildBut(ev.button.button);
477 msev.x := curMsX;
478 msev.y := curMsY;
479 if (msev.but <> 0) then
480 begin
481 // ev.button.clicks: Byte
482 if (ev.type_ = SDL_MOUSEBUTTONDOWN) then curMsButState := curMsButState or msev.but else curMsButState := curMsButState and (not msev.but);
483 msev.bstate := curMsButState;
484 msev.kstate := curKbState;
485 if (g_holmes_enabled) then g_Holmes_mouseEvent(msev);
486 end;
487 end;
488 SDL_MOUSEWHEEL:
489 begin
490 if (ev.wheel.y <> 0) then
491 begin
492 msev.dx := 0;
493 msev.dy := ev.wheel.y;
494 msev.kind := THMouseEvent.Press;
495 if (ev.wheel.y < 0) then msev.but := THMouseEvent.WheelUp else msev.but := THMouseEvent.WheelDown;
496 msev.x := curMsX;
497 msev.y := curMsY;
498 msev.bstate := curMsButState;
499 msev.kstate := curKbState;
500 if (g_holmes_enabled) then g_Holmes_mouseEvent(msev);
501 end;
502 end;
503 SDL_MOUSEMOTION:
504 begin
505 msev.dx := ev.button.x-curMsX;
506 msev.dy := ev.button.y-curMsY;
507 curMsX := ev.button.x;
508 curMsY := ev.button.y;
509 msev.kind := THMouseEvent.Motion;
510 msev.but := 0;
511 msev.x := curMsX;
512 msev.y := curMsY;
513 msev.bstate := curMsButState;
514 msev.kstate := curKbState;
515 if (g_holmes_enabled) then g_Holmes_mouseEvent(msev);
516 end;
517 {$ENDIF}
519 SDL_TEXTINPUT:
520 begin
521 Utf8ToUnicode(@uc, PChar(ev.text.text), 1);
522 keychr := Word(uc);
523 if (keychr > 127) then keychr := WCharToCP1251(keychr);
524 CharPress(Chr(keychr));
525 end;
527 // other key presses and joysticks are handled in e_input
528 end;
529 end;
531 procedure SwapBuffers();
532 begin
533 {$IFDEF HEADLESS}Exit;{$ENDIF}
534 SDL_GL_SwapWindow(h_Wnd);
535 end;
537 procedure KillGLWindow();
538 begin
539 if h_Wnd <> nil then SDL_DestroyWindow(h_Wnd);
540 if h_GL <> nil then SDL_GL_DeleteContext(h_GL);
541 h_Wnd := nil;
542 h_GL := nil;
543 //wWindowCreated := False;
544 end;
546 function CreateGLWindow(Title: PChar): Boolean;
547 //var
548 // flags: LongWord;
549 begin
550 Result := False;
552 gWinSizeX := gScreenWidth;
553 gWinSizeY := gScreenHeight;
555 wTitle := Title;
556 e_WriteLog('Creating window', MSG_NOTIFY);
558 if not g_Window_SetDisplay() then
559 begin
560 KillGLWindow();
561 e_WriteLog('Window creation error (resolution not supported?)', MSG_FATALERROR);
562 exit;
563 end;
565 {$IFNDEF HEADLESS}
566 h_Gl := SDL_GL_CreateContext(h_Wnd);
567 if h_Gl = nil then Exit;
568 {$ENDIF}
569 //wWindowCreated := True;
571 e_ResizeWindow(gScreenWidth, gScreenHeight);
572 e_InitGL();
574 Result := True;
575 end;
577 {$IFDEF WINDOWS}
578 // windoze sux; in headless mode `GetTickCount()` (and SDL) returns shit
579 function GetTimer(): Int64;
580 var
581 F, C: Int64;
582 begin
583 QueryPerformanceFrequency(F);
584 QueryPerformanceCounter(C);
585 Result := Round(C/F*1000{000});
586 end;
587 {$ELSE}
588 function GetTimer(): Int64;
589 var
590 t: Uint32;
591 tt: Int64;
592 begin
593 t := SDL_GetTicks() {* 1000}; // TODO: do we really need microseconds here? k8: NOPE!
594 if ticksOverflow = -1 then
595 begin
596 ticksOverflow := 0;
597 lastTicks := t;
598 end
599 else
600 begin
601 if lastTicks > t then
602 begin
603 // overflow, increment overflow ;-)
604 ticksOverflow := ticksOverflow+(Int64($ffffffff)+Int64(1));
605 tt := (Int64($ffffffff)+Int64(1))+Int64(t);
606 t := Uint32(tt-lastTicks);
607 end;
608 end;
609 lastTicks := t;
610 result := ticksOverflow+Int64(t);
611 end;
612 {$ENDIF}
614 procedure ResetTimer();
615 begin
616 wNeedTimeReset := True;
617 end;
619 procedure PushExitEvent();
620 var
621 ev: TSDL_Event;
622 begin
623 ev.type_ := SDL_QUITEV;
624 SDL_PushEvent(@ev);
625 end;
628 var
629 prevLoadingUpdateTime: UInt64 = 0;
631 procedure ProcessLoading (forceUpdate: Boolean=false);
632 var
633 ev: TSDL_Event;
634 ID: DWORD;
635 stt: UInt64;
636 begin
637 FillChar(ev, SizeOf(ev), 0);
638 //wNeedFree := False;
639 wLoadingProgress := True;
640 while SDL_PollEvent(@ev) > 0 do
641 begin
642 if (ev.type_ = SDL_QUITEV) then
643 break;
644 end;
645 //wNeedFree := True;
647 if (ev.type_ = SDL_QUITEV) or (gExit = EXIT_QUIT) then
648 begin
649 wLoadingProgress := False;
650 exit;
651 end;
653 if not wMinimized then
654 begin
655 if forceUpdate then
656 begin
657 prevLoadingUpdateTime := curTimeMilli();
658 end
659 else
660 begin
661 stt := curTimeMilli();
662 if (stt < prevLoadingUpdateTime) or (stt-prevLoadingUpdateTime >= 400) then
663 begin
664 prevLoadingUpdateTime := stt;
665 forceUpdate := true;
666 end;
667 end;
669 if forceUpdate then
670 begin
671 if g_Texture_Get('INTER', ID) then
672 begin
673 e_DrawSize(ID, 0, 0, 0, False, False, gScreenWidth, gScreenHeight)
674 end
675 else
676 begin
677 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
678 end;
680 DrawLoadingStat();
681 SwapBuffers();
683 ReShowCursor();
684 end;
685 end;
687 e_SoundUpdate();
689 if NetMode = NET_SERVER then
690 begin
691 g_Net_Host_Update();
692 end
693 else
694 begin
695 if (NetMode = NET_CLIENT) and (NetState <> NET_STATE_AUTH) then g_Net_Client_UpdateWhileLoading();
696 end;
697 wLoadingProgress := False;
698 end;
700 function ProcessMessage(): Boolean;
701 var
702 i, t: Integer;
703 ev: TSDL_Event;
704 begin
705 Result := False;
706 FillChar(ev, SizeOf(ev), 0);
708 while SDL_PollEvent(@ev) > 0 do
709 begin
710 Result := EventHandler(ev);
711 if ev.type_ = SDL_QUITEV then exit;
712 end;
714 Time := GetTimer();
715 Time_Delta := Time - Time_Old;
717 flag := False;
719 if wNeedTimeReset then
720 begin
721 Time_Delta := 28{(27777 div 1000)};
722 wNeedTimeReset := False;
723 end;
725 g_Map_ProfilersBegin();
726 g_Mons_ProfilersBegin();
728 t := Time_Delta div 28{(27777 div 1000)};
729 if t > 0 then
730 begin
731 flag := True;
732 for i := 1 to t do
733 begin
734 if NetMode = NET_SERVER then g_Net_Host_Update()
735 else if NetMode = NET_CLIENT then g_Net_Client_Update();
736 Update();
737 end;
738 end
739 else
740 begin
741 if NetMode = NET_SERVER then g_Net_Host_Update()
742 else if NetMode = NET_CLIENT then g_Net_Client_Update();
743 end;
745 g_Map_ProfilersEnd();
746 g_Mons_ProfilersEnd();
748 if wLoadingQuit then
749 begin
750 g_Game_Free();
751 g_Game_Quit();
752 end;
754 if gExit = EXIT_QUIT then
755 begin
756 Result := True;
757 Exit;
758 end;
760 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ:
761 if flag then
762 begin
763 Time_Old := Time - (Time_Delta mod 28{(27777 div 1000)});
764 if (not wMinimized) then
765 begin
766 Draw();
767 SwapBuffers();
768 ReShowCursor();
769 end;
770 end
771 else
772 Sleep(1);
774 e_SoundUpdate();
775 end;
777 procedure ReDrawWindow;
778 begin
779 SwapBuffers();
780 ReShowCursor();
781 end;
783 procedure InitOpenGL(VSync: Boolean);
784 var
785 v: Byte;
786 begin
787 {$IFDEF HEADLESS}Exit;{$ENDIF}
788 if VSync then v := 1 else v := 0;
789 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
790 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
791 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
792 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
793 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
794 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
795 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
796 if gwin_k8_enable_light_experiments then
797 begin
798 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1); // lights; it is enough to have 1-bit stencil buffer for lighting
799 end;
800 SDL_GL_SetSwapInterval(v);
801 end;
803 function glHasExtension (name: AnsiString): Boolean;
804 var
805 exts: PChar;
806 i: Integer;
807 found: Boolean;
808 extName: ShortString;
809 begin
810 result := false;
811 if length(name) = 0 then exit;
812 exts := glGetString(GL_EXTENSIONS);
813 if exts = nil then exit;
814 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
815 while exts[0] <> #0 do
816 begin
817 if gwin_dump_extensions then
818 begin
819 i := 0;
820 while (exts[i] <> #0) and (exts[i] <> ' ') do Inc(i);
821 if i > 255 then
822 begin
823 e_WriteLog('FUUUUUUUUUUUUU', MSG_WARNING);
824 end
825 else
826 begin
827 Move(exts^, extName[1], i);
828 extName[0] := Char(i);
829 e_WriteLog(Format('EXT: %s', [extName]), MSG_NOTIFY);
830 end;
831 end;
832 found := true;
833 for i := 0 to length(name)-1 do
834 begin
835 if exts[i] = #0 then begin found := false; break; end;
836 if exts[i] <> name[i+1] then begin found := false; break; end;
837 end;
838 if found and ((exts[length(name)] = #0) or (exts[length(name)] = ' ')) then begin result := true; exit; end;
839 while (exts[0] <> #0) and (exts[0] <> ' ') do Inc(exts);
840 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
841 end;
842 end;
844 function SDLMain(): Integer;
845 var
846 idx: Integer;
847 ltmp: Integer;
848 arg: AnsiString;
849 begin
850 {$IFDEF HEADLESS}
851 e_NoGraphics := True;
852 {$ENDIF}
854 idx := 1;
855 while (idx <= ParamCount) do
856 begin
857 arg := ParamStr(idx);
858 Inc(idx);
859 if arg = '--opengl-dump-exts' then gwin_dump_extensions := true;
860 if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
861 if arg = '--jah' then g_profile_history_size := 100;
862 if arg = '--no-particles' then gpart_dbg_enabled := false;
863 if arg = '--no-los' then gmon_dbg_los_enabled := false;
865 if arg = '--profile-render' then g_profile_frame_draw := true;
866 if arg = '--profile-coldet' then g_profile_collision := true;
867 if arg = '--profile-los' then g_profile_los := true;
869 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
870 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
871 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
872 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
873 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
874 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
876 {$IF DEFINED(D2F_DEBUG)}
877 if arg = '--aimline' then g_dbg_aimline_on := false;
878 {$ENDIF}
880 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
881 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
882 begin
883 if (idx <= ParamCount) then
884 begin
885 if not conParseFloat(g_holmes_ui_scale, ParamStr(idx)) then g_holmes_ui_scale := 1.0;
886 Inc(idx);
887 end;
888 end;
890 {$IF DEFINED(D2F_DEBUG)}
891 if (arg = '--game-scale') or (arg = '-game-scale') then
892 begin
893 if (idx <= ParamCount) then
894 begin
895 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
896 Inc(idx);
897 end;
898 end;
899 {$ENDIF}
900 end;
902 e_WriteLog('Initializing OpenGL', MSG_NOTIFY);
903 InitOpenGL(gVSync);
905 e_WriteLog('Creating GL window', MSG_NOTIFY);
906 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION]))) then
907 begin
908 Result := 0;
909 exit;
910 end;
912 {EnumDisplayModes();}
914 if gwin_k8_enable_light_experiments then
915 begin
916 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, @ltmp);
917 e_WriteLog(Format('stencil buffer size: %d', [ltmp]), MSG_WARNING);
918 gwin_has_stencil := (ltmp > 0);
919 end
920 else
921 begin
922 gwin_has_stencil := false;
923 end;
925 if not glHasExtension('GL_ARB_texture_non_power_of_two') then
926 begin
927 e_WriteLog('Driver DID''T advertised NPOT textures support', MSG_WARNING);
928 glLegacyNPOT := true;
929 end
930 else
931 begin
932 e_WriteLog('Driver advertised NPOT textures support', MSG_NOTIFY);
933 glLegacyNPOT := false;
934 end;
935 gwin_dump_extensions := false;
937 Init();
938 Time_Old := GetTimer();
940 // Êîìàíäíàÿ ñòðîêà:
941 if ParamCount > 0 then
942 g_Game_Process_Params();
944 // Çàïðîñ ÿçûêà:
945 if (not gGameOn) and gAskLanguage then
946 g_Menu_AskLanguage();
948 e_WriteLog('Entering the main loop', MSG_NOTIFY);
950 while not ProcessMessage() do
951 { Main Loop } ;
953 Release();
954 KillGLWindow();
956 Result := 0;
957 end;
959 end.