DEADSOFTWARE

renamed `SArray` type to `SSArray`, and moved it to "utils.pas"
[d2df-sdl.git] / src / game / g_window.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_window;
19 interface
21 uses
22 utils;
24 function SDLMain(): Integer;
25 function GetTimer(): Int64;
26 procedure ResetTimer();
27 function CreateGLWindow(Title: PChar): Boolean;
28 procedure KillGLWindow();
29 procedure PushExitEvent();
30 function ProcessMessage(): Boolean;
31 procedure ProcessLoading (forceUpdate: Boolean=false);
32 procedure ReDrawWindow();
33 procedure SwapBuffers();
34 procedure Sleep(ms: LongWord);
35 function GetDisplayModes(dBPP: DWORD; var SelRes: DWORD): SSArray;
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 SysUtils, Classes, MAPDEF,
51 SDL2, GL, GLExt, e_graphics, e_log, e_texture, g_main,
52 g_console, e_input, g_options, g_game,
53 g_basic, g_textures, e_sound, g_sound, g_menu, ENet, g_net,
54 g_map, g_gfx, g_monsters, g_holmes, xprofiler;
56 var
57 h_Wnd: PSDL_Window;
58 h_GL: TSDL_GLContext;
59 wFlags: LongWord = 0;
60 Time, Time_Delta, Time_Old: Int64;
61 flag: Boolean;
62 wTitle: PChar = nil;
63 wNeedTimeReset: Boolean = False;
64 wMinimized: Boolean = False;
65 wLoadingProgress: Boolean = False;
66 wLoadingQuit: Boolean = False;
67 {$IFNDEF WINDOWS}
68 ticksOverflow: Int64 = -1;
69 lastTicks: Uint32 = 0; // to detect overflow
70 {$ENDIF}
71 {$IF not DEFINED(HEADLESS)}
72 curMsButState: Word = 0;
73 curKbState: Word = 0;
74 curMsX: Integer = 0;
75 curMsY: Integer = 0;
76 {$ENDIF}
78 function g_Window_SetDisplay(PreserveGL: Boolean = False): Boolean;
79 var
80 mode, cmode: TSDL_DisplayMode;
81 begin
82 {$IFDEF HEADLESS}
83 Result := True;
84 Exit;
85 {$ENDIF}
87 Result := False;
89 e_WriteLog('Setting display mode...', TMsgType.Notify);
91 wFlags := SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE;
92 if gFullscreen then wFlags := wFlags or SDL_WINDOW_FULLSCREEN;
93 if gWinMaximized then wFlags := wFlags or SDL_WINDOW_MAXIMIZED;
95 if h_Wnd <> nil then
96 begin
97 SDL_DestroyWindow(h_Wnd);
98 h_Wnd := nil;
99 end;
101 if gFullscreen then
102 begin
103 mode.w := gScreenWidth;
104 mode.h := gScreenHeight;
105 mode.format := 0;
106 mode.refresh_rate := 0;
107 mode.driverdata := nil;
108 if SDL_GetClosestDisplayMode(0, @mode, @cmode) = nil then
109 begin
110 gScreenWidth := 800;
111 gScreenHeight := 600;
112 end
113 else
114 begin
115 gScreenWidth := cmode.w;
116 gScreenHeight := cmode.h;
117 end;
118 end;
120 h_Wnd := SDL_CreateWindow(PChar(wTitle), gWinRealPosX, gWinRealPosY, gScreenWidth, gScreenHeight, wFlags);
121 if h_Wnd = nil then Exit;
123 SDL_GL_MakeCurrent(h_Wnd, h_GL);
124 SDL_ShowCursor(SDL_DISABLE);
126 Result := True;
127 end;
129 procedure ReShowCursor();
130 begin
131 // TODO: what was this for?
132 end;
134 function GetDisplayModes(dBPP: DWORD; var SelRes: DWORD): SSArray;
135 var
136 mode: TSDL_DisplayMode;
137 res, i, k, n, pw, ph: Integer;
138 begin
139 SetLength(Result, 0);
140 {$IFDEF HEADLESS}Exit;{$ENDIF}
141 k := 0; SelRes := 0;
142 n := SDL_GetNumDisplayModes(0);
143 pw := 0; ph := 0;
144 for i := 0 to n do
145 begin
146 res := SDL_GetDisplayMode(0, i, @mode);
147 if res < 0 then continue;
148 if SDL_BITSPERPIXEL(mode.format) = gBPP then continue;
149 if (mode.w = pw) and (mode.h = ph) then continue;
150 if (mode.w = gScreenWidth) and (mode.h = gScreenHeight) then
151 SelRes := k;
152 Inc(k);
153 SetLength(Result, k);
154 Result[k-1] := IntToStr(mode.w) + 'x' + IntToStr(mode.h);
155 pw := mode.w; ph := mode.h
156 end;
158 e_WriteLog('SDL: Got ' + IntToStr(k) + ' resolutions.', TMsgType.Notify);
159 end;
161 procedure Sleep(ms: LongWord);
162 begin
163 SDL_Delay(ms);
164 end;
166 procedure ChangeWindowSize();
167 begin
168 gWinSizeX := gScreenWidth;
169 gWinSizeY := gScreenHeight;
170 {$IFDEF HEADLESS}Exit;{$ENDIF}
171 e_ResizeWindow(gScreenWidth, gScreenHeight);
172 g_Game_SetupScreenSize();
173 g_Menu_Reset();
174 g_Game_ClearLoading();
175 g_Holmes_VidModeChanged();
176 end;
178 function g_Window_SetSize(W, H: Word; FScreen: Boolean): Boolean;
179 var
180 Preserve: Boolean;
181 begin
182 Result := False;
183 {$IFDEF HEADLESS}Exit;{$ENDIF}
184 Preserve := False;
186 if (gScreenWidth <> W) or (gScreenHeight <> H) then
187 begin
188 Result := True;
189 gScreenWidth := W;
190 gScreenHeight := H;
191 end;
193 if gFullscreen <> FScreen then
194 begin
195 Result := True;
196 gFullscreen := FScreen;
197 Preserve := True;
198 end;
200 if Result then
201 begin
202 g_Window_SetDisplay(Preserve);
203 ChangeWindowSize();
204 end;
205 end;
207 procedure resetKMState ();
208 begin
209 {$IF not DEFINED(HEADLESS)}
210 curMsButState := 0;
211 curKbState := 0;
212 {$ENDIF}
213 end;
215 function WindowEventHandler(ev: TSDL_WindowEvent): Boolean;
216 var
217 wActivate, wDeactivate: Boolean;
218 begin
219 Result := False;
220 wActivate := False;
221 wDeactivate := False;
223 case ev.event of
224 SDL_WINDOWEVENT_MOVED:
225 begin
226 if not (gFullscreen or gWinMaximized) then
227 begin
228 gWinRealPosX := ev.data1;
229 gWinRealPosY := ev.data2;
230 end;
231 end;
233 SDL_WINDOWEVENT_MINIMIZED:
234 begin
235 resetKMState();
236 e_UnpressAllKeys();
237 if not wMinimized then
238 begin
239 e_ResizeWindow(0, 0);
240 wMinimized := True;
242 if g_debug_WinMsgs then
243 begin
244 g_Console_Add('Now minimized');
245 e_WriteLog('[DEBUG] WinMsgs: Now minimized', TMsgType.Notify);
246 end;
247 wDeactivate := True;
248 end;
249 end;
251 SDL_WINDOWEVENT_RESIZED:
252 begin
253 resetKMState();
254 gScreenWidth := ev.data1;
255 gScreenHeight := ev.data2;
256 ChangeWindowSize();
257 SwapBuffers();
258 if g_debug_WinMsgs then
259 begin
260 g_Console_Add('Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2));
261 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2), TMsgType.Notify);
262 end;
263 end;
265 SDL_WINDOWEVENT_EXPOSED:
266 SwapBuffers();
268 SDL_WINDOWEVENT_MAXIMIZED:
269 begin
270 resetKMState();
271 if wMinimized then
272 begin
273 e_ResizeWindow(gScreenWidth, gScreenHeight);
274 wMinimized := False;
275 wActivate := True;
276 end;
277 if not gWinMaximized then
278 begin
279 gWinMaximized := True;
280 if g_debug_WinMsgs then
281 begin
282 g_Console_Add('Now maximized');
283 e_WriteLog('[DEBUG] WinMsgs: Now maximized', TMsgType.Notify);
284 end;
285 end;
286 end;
288 SDL_WINDOWEVENT_RESTORED:
289 begin
290 resetKMState();
291 if wMinimized then
292 begin
293 e_ResizeWindow(gScreenWidth, gScreenHeight);
294 wMinimized := False;
295 wActivate := True;
296 end;
297 if gWinMaximized then
298 gWinMaximized := False;
299 if g_debug_WinMsgs then
300 begin
301 g_Console_Add('Now restored');
302 e_WriteLog('[DEBUG] WinMsgs: Now restored', TMsgType.Notify);
303 end;
304 end;
306 SDL_WINDOWEVENT_FOCUS_GAINED:
307 begin
308 resetKMState();
309 wActivate := True;
310 //e_WriteLog('window gained focus!', MSG_NOTIFY);
311 g_Holmes_WindowFocused();
312 end;
314 SDL_WINDOWEVENT_FOCUS_LOST:
315 begin
316 resetKMState();
317 wDeactivate := True;
318 e_UnpressAllKeys();
319 //e_WriteLog('window lost focus!', MSG_NOTIFY);
320 g_Holmes_WindowBlured();
321 end;
322 end;
324 if wDeactivate then
325 begin
326 if gWinActive then
327 begin
328 e_WriteLog('deactivating window', TMsgType.Notify);
329 e_EnableInput := False;
330 e_ClearInputBuffer();
332 if gMuteWhenInactive then
333 begin
334 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
335 e_MuteChannels(True);
336 end;
338 if g_debug_WinMsgs then
339 begin
340 g_Console_Add('Now inactive');
341 e_WriteLog('[DEBUG] WinMsgs: Now inactive', TMsgType.Notify);
342 end;
344 gWinActive := False;
345 end;
346 end
347 else if wActivate then
348 begin
349 if not gWinActive then
350 begin
351 //e_WriteLog('activating window', MSG_NOTIFY);
352 e_EnableInput := True;
354 if gMuteWhenInactive then
355 begin
356 //e_WriteLog('activating sounds', MSG_NOTIFY);
357 e_MuteChannels(False);
358 end;
360 if g_debug_WinMsgs then
361 begin
362 g_Console_Add('Now active');
363 e_WriteLog('[DEBUG] WinMsgs: Now active', TMsgType.Notify);
364 end;
366 gWinActive := True;
367 end;
368 end;
369 end;
371 function EventHandler(ev: TSDL_Event): Boolean;
372 var
373 key, keychr: Word;
374 uc: UnicodeChar;
375 //joy: Integer;
376 {$IF not DEFINED(HEADLESS)}
377 msev: THMouseEvent;
378 kbev: THKeyEvent;
379 {$ENDIF}
381 function buildBut (b: Byte): Word;
382 begin
383 result := 0;
384 case b of
385 SDL_BUTTON_LEFT: result := result or THMouseEvent.Left;
386 SDL_BUTTON_MIDDLE: result := result or THMouseEvent.Middle;
387 SDL_BUTTON_RIGHT: result := result or THMouseEvent.Right;
388 end;
389 end;
391 {$IF not DEFINED(HEADLESS)}
392 procedure updateKBState ();
393 var
394 kbstate: PUint8;
395 begin
396 curKbState := 0;
397 kbstate := SDL_GetKeyboardState(nil);
398 if (kbstate[SDL_SCANCODE_LCTRL] <> 0) or (kbstate[SDL_SCANCODE_RCTRL] <> 0) then curKbState := curKbState or THKeyEvent.ModCtrl;
399 if (kbstate[SDL_SCANCODE_LALT] <> 0) or (kbstate[SDL_SCANCODE_RALT] <> 0) then curKbState := curKbState or THKeyEvent.ModAlt;
400 if (kbstate[SDL_SCANCODE_LSHIFT] <> 0) or (kbstate[SDL_SCANCODE_RSHIFT] <> 0) then curKbState := curKbState or THKeyEvent.ModShift;
401 end;
402 {$ENDIF}
404 begin
405 Result := False;
406 {$IF not DEFINED(HEADLESS)}
407 updateKBState();
408 {$ENDIF}
410 case ev.type_ of
411 SDL_WINDOWEVENT:
412 Result := WindowEventHandler(ev.window);
414 SDL_QUITEV:
415 begin
416 if gExit <> EXIT_QUIT then
417 begin
418 if not wLoadingProgress then
419 begin
420 g_Game_Free();
421 g_Game_Quit();
422 end
423 else
424 wLoadingQuit := True;
425 end;
426 Result := True;
427 end;
429 SDL_KEYDOWN, SDL_KEYUP:
430 begin
431 key := ev.key.keysym.scancode;
432 {$IF not DEFINED(HEADLESS)}
433 if (g_holmes_enabled) then
434 begin
435 if (ev.type_ = SDL_KEYDOWN) then kbev.kind := THKeyEvent.Press else kbev.kind := THKeyEvent.Release;
436 kbev.scan := ev.key.keysym.scancode;
437 kbev.sym := ev.key.keysym.sym;
438 kbev.bstate := curMsButState;
439 kbev.kstate := curKbState;
440 if g_Holmes_keyEvent(kbev) then
441 begin
442 if (ev.type_ <> SDL_KEYDOWN) then e_KeyUpDown(ev.key.keysym.scancode, false);
443 exit;
444 end;
445 end;
446 {$ENDIF}
447 if (ev.type_ = SDL_KEYDOWN) then KeyPress(key);
448 e_KeyUpDown(ev.key.keysym.scancode, (ev.type_ = SDL_KEYDOWN));
449 end;
451 {$IF not DEFINED(HEADLESS)}
452 SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP:
453 begin
454 msev.dx := ev.button.x-curMsX;
455 msev.dy := ev.button.y-curMsY;
456 curMsX := ev.button.x;
457 curMsY := ev.button.y;
458 if (ev.type_ = SDL_MOUSEBUTTONDOWN) then msev.kind := THMouseEvent.Press else msev.kind := THMouseEvent.Release;
459 msev.but := buildBut(ev.button.button);
460 msev.x := curMsX;
461 msev.y := curMsY;
462 if (msev.but <> 0) then
463 begin
464 // ev.button.clicks: Byte
465 if (ev.type_ = SDL_MOUSEBUTTONDOWN) then curMsButState := curMsButState or msev.but else curMsButState := curMsButState and (not msev.but);
466 msev.bstate := curMsButState;
467 msev.kstate := curKbState;
468 if (g_holmes_enabled) then g_Holmes_mouseEvent(msev);
469 end;
470 end;
471 SDL_MOUSEWHEEL:
472 begin
473 if (ev.wheel.y <> 0) then
474 begin
475 msev.dx := 0;
476 msev.dy := ev.wheel.y;
477 msev.kind := THMouseEvent.Press;
478 if (ev.wheel.y < 0) then msev.but := THMouseEvent.WheelUp else msev.but := THMouseEvent.WheelDown;
479 msev.x := curMsX;
480 msev.y := curMsY;
481 msev.bstate := curMsButState;
482 msev.kstate := curKbState;
483 if (g_holmes_enabled) then g_Holmes_mouseEvent(msev);
484 end;
485 end;
486 SDL_MOUSEMOTION:
487 begin
488 msev.dx := ev.button.x-curMsX;
489 msev.dy := ev.button.y-curMsY;
490 curMsX := ev.button.x;
491 curMsY := ev.button.y;
492 msev.kind := THMouseEvent.Motion;
493 msev.but := 0;
494 msev.x := curMsX;
495 msev.y := curMsY;
496 msev.bstate := curMsButState;
497 msev.kstate := curKbState;
498 if (g_holmes_enabled) then g_Holmes_mouseEvent(msev);
499 end;
500 {$ENDIF}
502 SDL_TEXTINPUT:
503 begin
504 Utf8ToUnicode(@uc, PChar(ev.text.text), 1);
505 keychr := Word(uc);
506 if (keychr > 127) then keychr := Word(wchar2win(WideChar(keychr)));
507 CharPress(AnsiChar(keychr));
508 end;
510 // other key presses and joysticks are handled in e_input
511 end;
512 end;
514 procedure SwapBuffers();
515 begin
516 {$IF not DEFINED(HEADLESS)}
517 SDL_GL_SwapWindow(h_Wnd);
518 {$ENDIF}
519 end;
521 procedure KillGLWindow();
522 begin
523 if h_Wnd <> nil then SDL_DestroyWindow(h_Wnd);
524 if h_GL <> nil then SDL_GL_DeleteContext(h_GL);
525 h_Wnd := nil;
526 h_GL := nil;
527 end;
529 function CreateGLWindow(Title: PChar): Boolean;
530 //var
531 // flags: LongWord;
532 begin
533 Result := False;
535 gWinSizeX := gScreenWidth;
536 gWinSizeY := gScreenHeight;
538 wTitle := Title;
539 e_WriteLog('Creating window', TMsgType.Notify);
541 if not g_Window_SetDisplay() then
542 begin
543 KillGLWindow();
544 e_WriteLog('Window creation error (resolution not supported?)', TMsgType.Fatal);
545 exit;
546 end;
548 {$IFNDEF HEADLESS}
549 h_Gl := SDL_GL_CreateContext(h_Wnd);
550 if h_Gl = nil then Exit;
551 {$ENDIF}
553 e_ResizeWindow(gScreenWidth, gScreenHeight);
554 e_InitGL();
556 Result := True;
557 end;
559 {$IFDEF WINDOWS}
560 // windoze sux; in headless mode `GetTickCount()` (and SDL) returns shit
561 function GetTimer(): Int64;
562 var
563 F, C: Int64;
564 begin
565 QueryPerformanceFrequency(F);
566 QueryPerformanceCounter(C);
567 Result := Round(C/F*1000{000});
568 end;
569 {$ELSE}
570 function GetTimer(): Int64;
571 var
572 t: Uint32;
573 tt: Int64;
574 begin
575 t := SDL_GetTicks() {* 1000}; // TODO: do we really need microseconds here? k8: NOPE!
576 if ticksOverflow = -1 then
577 begin
578 ticksOverflow := 0;
579 lastTicks := t;
580 end
581 else
582 begin
583 if lastTicks > t then
584 begin
585 // overflow, increment overflow ;-)
586 ticksOverflow := ticksOverflow+(Int64($ffffffff)+Int64(1));
587 tt := (Int64($ffffffff)+Int64(1))+Int64(t);
588 t := Uint32(tt-lastTicks);
589 end;
590 end;
591 lastTicks := t;
592 result := ticksOverflow+Int64(t);
593 end;
594 {$ENDIF}
596 procedure ResetTimer();
597 begin
598 wNeedTimeReset := True;
599 end;
601 procedure PushExitEvent();
602 var
603 ev: TSDL_Event;
604 begin
605 ev.type_ := SDL_QUITEV;
606 SDL_PushEvent(@ev);
607 end;
610 var
611 prevLoadingUpdateTime: UInt64 = 0;
613 procedure ProcessLoading (forceUpdate: Boolean=false);
614 var
615 ev: TSDL_Event;
616 ID: DWORD;
617 stt: UInt64;
618 begin
619 FillChar(ev, SizeOf(ev), 0);
620 wLoadingProgress := True;
621 while SDL_PollEvent(@ev) > 0 do
622 begin
623 if (ev.type_ = SDL_QUITEV) then
624 break;
625 end;
627 if (ev.type_ = SDL_QUITEV) or (gExit = EXIT_QUIT) then
628 begin
629 wLoadingProgress := False;
630 exit;
631 end;
633 if not wMinimized then
634 begin
635 if forceUpdate then
636 begin
637 prevLoadingUpdateTime := getTimeMilli();
638 end
639 else
640 begin
641 stt := getTimeMilli();
642 if (stt < prevLoadingUpdateTime) or (stt-prevLoadingUpdateTime >= 400) then
643 begin
644 prevLoadingUpdateTime := stt;
645 forceUpdate := true;
646 end;
647 end;
649 if forceUpdate then
650 begin
651 if g_Texture_Get('INTER', ID) then
652 begin
653 e_DrawSize(ID, 0, 0, 0, False, False, gScreenWidth, gScreenHeight)
654 end
655 else
656 begin
657 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
658 end;
660 DrawLoadingStat();
661 SwapBuffers();
663 ReShowCursor();
664 end;
665 end;
667 e_SoundUpdate();
669 if NetMode = NET_SERVER then
670 begin
671 g_Net_Host_Update();
672 end
673 else
674 begin
675 if (NetMode = NET_CLIENT) and (NetState <> NET_STATE_AUTH) then g_Net_Client_UpdateWhileLoading();
676 end;
677 wLoadingProgress := False;
678 end;
680 function ProcessMessage(): Boolean;
681 var
682 i, t: Integer;
683 ev: TSDL_Event;
684 begin
685 Result := False;
686 FillChar(ev, SizeOf(ev), 0);
688 while SDL_PollEvent(@ev) > 0 do
689 begin
690 Result := EventHandler(ev);
691 if ev.type_ = SDL_QUITEV then exit;
692 end;
694 Time := GetTimer();
695 Time_Delta := Time - Time_Old;
697 flag := False;
699 if wNeedTimeReset then
700 begin
701 Time_Delta := 28;
702 wNeedTimeReset := False;
703 end;
705 g_Map_ProfilersBegin();
706 g_Mons_ProfilersBegin();
708 t := Time_Delta div 28;
709 if t > 0 then
710 begin
711 flag := True;
712 for i := 1 to t do
713 begin
714 if NetMode = NET_SERVER then g_Net_Host_Update()
715 else if NetMode = NET_CLIENT then g_Net_Client_Update();
716 Update();
717 end;
718 end
719 else
720 begin
721 if NetMode = NET_SERVER then g_Net_Host_Update()
722 else if NetMode = NET_CLIENT then g_Net_Client_Update();
723 end;
725 g_Map_ProfilersEnd();
726 g_Mons_ProfilersEnd();
728 if wLoadingQuit then
729 begin
730 g_Game_Free();
731 g_Game_Quit();
732 end;
734 if gExit = EXIT_QUIT then
735 begin
736 Result := True;
737 Exit;
738 end;
740 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ:
741 if flag then
742 begin
743 Time_Old := Time-(Time_Delta mod 28);
744 if (not wMinimized) then
745 begin
746 Draw();
747 SwapBuffers();
748 ReShowCursor();
749 end;
750 end
751 else
752 Sleep(1);
754 e_SoundUpdate();
755 end;
757 procedure ReDrawWindow;
758 begin
759 SwapBuffers();
760 ReShowCursor();
761 end;
763 procedure InitOpenGL(VSync: Boolean);
764 var
765 v: Byte;
766 begin
767 {$IFDEF HEADLESS}Exit;{$ENDIF}
768 if VSync then v := 1 else v := 0;
769 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
770 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
771 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
772 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
773 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
774 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
775 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
776 if gwin_k8_enable_light_experiments then
777 begin
778 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1); // lights; it is enough to have 1-bit stencil buffer for lighting
779 end;
780 SDL_GL_SetSwapInterval(v);
781 end;
783 function glHasExtension (name: AnsiString): Boolean;
784 var
785 exts: PChar;
786 i: Integer;
787 found: Boolean;
788 extName: ShortString;
789 begin
790 result := false;
791 if length(name) = 0 then exit;
792 exts := glGetString(GL_EXTENSIONS);
793 if exts = nil then exit;
794 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
795 while exts[0] <> #0 do
796 begin
797 if gwin_dump_extensions then
798 begin
799 i := 0;
800 while (exts[i] <> #0) and (exts[i] <> ' ') do Inc(i);
801 if i > 255 then
802 begin
803 e_WriteLog('FUUUUUUUUUUUUU', TMsgType.Warning);
804 end
805 else
806 begin
807 Move(exts^, extName[1], i);
808 extName[0] := Char(i);
809 e_WriteLog(Format('EXT: %s', [extName]), TMsgType.Notify);
810 end;
811 end;
812 found := true;
813 for i := 0 to length(name)-1 do
814 begin
815 if exts[i] = #0 then begin found := false; break; end;
816 if exts[i] <> name[i+1] then begin found := false; break; end;
817 end;
818 if found and ((exts[length(name)] = #0) or (exts[length(name)] = ' ')) then begin result := true; exit; end;
819 while (exts[0] <> #0) and (exts[0] <> ' ') do Inc(exts);
820 while (exts[0] <> #0) and (exts[0] = ' ') do Inc(exts);
821 end;
822 end;
824 function SDLMain(): Integer;
825 var
826 idx: Integer;
827 {$IF not DEFINED(HEADLESS)}
828 ltmp: Integer;
829 {$ENDIF}
830 arg: AnsiString;
831 mdfo: TStream;
832 begin
833 {$IFDEF HEADLESS}
834 e_NoGraphics := True;
835 {$ENDIF}
837 idx := 1;
838 while (idx <= ParamCount) do
839 begin
840 arg := ParamStr(idx);
841 Inc(idx);
842 if arg = '--opengl-dump-exts' then gwin_dump_extensions := true;
843 if arg = '--twinkletwinkle' then gwin_k8_enable_light_experiments := true;
844 if arg = '--jah' then g_profile_history_size := 100;
845 if arg = '--no-particles' then gpart_dbg_enabled := false;
846 if arg = '--no-los' then gmon_dbg_los_enabled := false;
848 if arg = '--profile-render' then g_profile_frame_draw := true;
849 if arg = '--profile-coldet' then g_profile_collision := true;
850 if arg = '--profile-los' then g_profile_los := true;
852 if arg = '--no-part-phys' then gpart_dbg_phys_enabled := false;
853 if arg = '--no-part-physics' then gpart_dbg_phys_enabled := false;
854 if arg = '--no-particles-phys' then gpart_dbg_phys_enabled := false;
855 if arg = '--no-particles-physics' then gpart_dbg_phys_enabled := false;
856 if arg = '--no-particle-phys' then gpart_dbg_phys_enabled := false;
857 if arg = '--no-particle-physics' then gpart_dbg_phys_enabled := false;
859 {.$IF DEFINED(D2F_DEBUG)}
860 if arg = '--aimline' then g_dbg_aimline_on := true;
861 {.$ENDIF}
863 if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
864 if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
865 begin
866 if (idx <= ParamCount) then
867 begin
868 if not conParseFloat(g_holmes_ui_scale, ParamStr(idx)) then g_holmes_ui_scale := 1.0;
869 Inc(idx);
870 end;
871 end;
873 if (arg = '--game-scale') or (arg = '-game-scale') then
874 begin
875 if (idx <= ParamCount) then
876 begin
877 if not conParseFloat(g_dbg_scale, ParamStr(idx)) then g_dbg_scale := 1.0;
878 Inc(idx);
879 end;
880 end;
882 if (arg = '--write-mapdef') or (arg = '-write-mapdef') then
883 begin
884 mdfo := createDiskFile('mapdef.txt');
885 mdfo.WriteBuffer(defaultMapDef[1], Length(defaultMapDef));
886 mdfo.Free();
887 Halt(0);
888 end;
889 end;
891 e_WriteLog('Initializing OpenGL', TMsgType.Notify);
892 InitOpenGL(gVSync);
894 e_WriteLog('Creating GL window', TMsgType.Notify);
895 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION]))) then
896 begin
897 Result := 0;
898 exit;
899 end;
901 {EnumDisplayModes();}
903 {$IFDEF HEADLESS}
904 gwin_k8_enable_light_experiments := false;
905 gwin_has_stencil := false;
906 glLegacyNPOT := false;
907 gwin_dump_extensions := false;
908 {$ELSE}
909 if gwin_k8_enable_light_experiments then
910 begin
911 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, @ltmp);
912 e_WriteLog(Format('stencil buffer size: %d', [ltmp]), TMsgType.Warning);
913 gwin_has_stencil := (ltmp > 0);
914 end
915 else
916 begin
917 gwin_has_stencil := false;
918 end;
920 if not glHasExtension('GL_ARB_texture_non_power_of_two') then
921 begin
922 e_WriteLog('Driver DID''T advertised NPOT textures support', TMsgType.Warning);
923 glLegacyNPOT := true;
924 end
925 else
926 begin
927 e_WriteLog('Driver advertised NPOT textures support', TMsgType.Notify);
928 glLegacyNPOT := false;
929 end;
930 gwin_dump_extensions := false;
931 {$ENDIF}
933 Init();
934 Time_Old := GetTimer();
936 // Êîìàíäíàÿ ñòðîêà:
937 if ParamCount > 0 then
938 g_Game_Process_Params();
940 // Çàïðîñ ÿçûêà:
941 if (not gGameOn) and gAskLanguage then
942 g_Menu_AskLanguage();
944 e_WriteLog('Entering the main loop', TMsgType.Notify);
946 while not ProcessMessage() do
947 { Main Loop } ;
949 Release();
950 KillGLWindow();
952 Result := 0;
953 end;
955 end.