DEADSOFTWARE

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