9 function SDLMain(): Integer;
10 function GetTimer(): Int64;
11 procedure ResetTimer();
12 function CreateGLWindow(Title
: PChar): Boolean;
13 procedure KillGLWindow();
14 procedure PushExitEvent();
15 function ProcessMessage(): Boolean;
16 procedure ProcessLoading();
17 procedure ReDrawWindow();
18 procedure SwapBuffers();
19 procedure Sleep(ms
: LongWord);
20 function GetDisplayModes(dBPP
: DWORD
; var SelRes
: DWORD
): SArray
;
21 function g_Window_SetDisplay(PreserveGL
: Boolean = False): Boolean;
22 function g_Window_SetSize(W
, H
: Word; FScreen
: Boolean): Boolean;
27 {$IFDEF WINDOWS}Windows
,{$ENDIF}
28 SDL2
, GL
, GLExt
, e_graphics
, e_log
, g_main
,
29 g_console
, SysUtils
, e_input
, g_options
, g_game
,
30 g_basic
, g_textures
, e_sound
, g_sound
, g_menu
, ENet
, g_net
;
36 Time
, Time_Delta
, Time_Old
: Int64;
39 wNeedTimeReset
: Boolean = False;
40 //wWindowCreated: Boolean = False;
41 //wCursorShown: Boolean = False;
42 wMinimized
: Boolean = False;
43 //wNeedFree: Boolean = True;
44 wLoadingProgress
: Boolean = False;
45 wLoadingQuit
: Boolean = False;
46 {wWinPause: Byte = 0;}
47 ticksOverflow
: Int64 = -1;
48 lastTicks
: Uint32
= 0; // to detect overflow
51 // TODO: move this to a separate file
52 CP1251
: array [0..127] of Word = (
53 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
54 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
55 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
56 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
57 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
58 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
59 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
60 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
63 // TODO: make a transition table or something
64 function WCharToCP1251(wc
: Word): Word;
70 if CP1251
[n
] = wc
then begin Result
:= n
; break
end;
71 Result
:= Result
+ 128;
74 function g_Window_SetDisplay(PreserveGL
: Boolean = False): Boolean;
76 mode
, cmode
: TSDL_DisplayMode
;
85 e_WriteLog('Setting display mode...', MSG_NOTIFY
);
87 wFlags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
88 if gFullscreen
then wFlags
:= wFlags
or SDL_WINDOW_FULLSCREEN
;
89 if gWinMaximized
then wFlags
:= wFlags
or SDL_WINDOW_MAXIMIZED
;
93 SDL_DestroyWindow(h_Wnd
);
99 mode
.w
:= gScreenWidth
;
100 mode
.h
:= gScreenHeight
;
102 mode
.refresh_rate
:= 0;
103 mode
.driverdata
:= nil;
104 if SDL_GetClosestDisplayMode(0, @mode
, @cmode
) = nil then
107 gScreenHeight
:= 600;
111 gScreenWidth
:= cmode
.w
;
112 gScreenHeight
:= cmode
.h
;
116 h_Wnd
:= SDL_CreateWindow(PChar(wTitle
), gWinRealPosX
, gWinRealPosY
, gScreenWidth
, gScreenHeight
, wFlags
);
117 if h_Wnd
= nil then Exit
;
119 SDL_GL_MakeCurrent(h_Wnd
, h_GL
);
120 SDL_ShowCursor(SDL_DISABLE
);
125 procedure ReShowCursor();
127 // TODO: what was this for?
130 function GetDisplayModes(dBPP
: DWORD
; var SelRes
: DWORD
): SArray
;
132 mode
: TSDL_DisplayMode
;
133 res
, i
, k
, n
, pw
, ph
: Integer;
135 SetLength(Result
, 0);
136 {$IFDEF HEADLESS}Exit
;{$ENDIF}
138 n
:= SDL_GetNumDisplayModes(0);
142 res
:= SDL_GetDisplayMode(0, i
, @mode
);
143 if res
< 0 then continue
;
144 if SDL_BITSPERPIXEL(mode
.format
) = gBPP
then continue
;
145 if (mode
.w
= pw
) and (mode
.h
= ph
) then continue
;
146 if (mode
.w
= gScreenWidth
) and (mode
.h
= gScreenHeight
) then
149 SetLength(Result
, k
);
150 Result
[k
-1] := IntToStr(mode
.w
) + 'x' + IntToStr(mode
.h
);
151 pw
:= mode
.w
; ph
:= mode
.h
154 e_WriteLog('SDL: Got ' + IntToStr(k
) + ' resolutions.', MSG_NOTIFY
);
157 procedure Sleep(ms
: LongWord);
162 procedure ChangeWindowSize();
164 gWinSizeX
:= gScreenWidth
;
165 gWinSizeY
:= gScreenHeight
;
166 {$IFDEF HEADLESS}Exit
;{$ENDIF}
167 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
168 g_Game_SetupScreenSize();
170 g_Game_ClearLoading();
173 function g_Window_SetSize(W
, H
: Word; FScreen
: Boolean): Boolean;
178 {$IFDEF HEADLESS}Exit
;{$ENDIF}
181 if (gScreenWidth
<> W
) or (gScreenHeight
<> H
) then
188 if gFullscreen
<> FScreen
then
191 gFullscreen
:= FScreen
;
197 g_Window_SetDisplay(Preserve
);
202 function WindowEventHandler(ev
: TSDL_WindowEvent
): Boolean;
204 wActivate
, wDeactivate
: Boolean;
208 wDeactivate
:= False;
211 SDL_WINDOWEVENT_MOVED
:
213 if not (gFullscreen
or gWinMaximized
) then
215 gWinRealPosX
:= ev
.data1
;
216 gWinRealPosY
:= ev
.data2
;
220 SDL_WINDOWEVENT_MINIMIZED
:
222 if not wMinimized
then
224 e_ResizeWindow(0, 0);
227 if g_debug_WinMsgs
then
229 g_Console_Add('Now minimized');
230 e_WriteLog('[DEBUG] WinMsgs: Now minimized', MSG_NOTIFY
);
236 SDL_WINDOWEVENT_RESIZED
:
238 gScreenWidth
:= ev
.data1
;
239 gScreenHeight
:= ev
.data2
;
242 if g_debug_WinMsgs
then
244 g_Console_Add('Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
));
245 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
), MSG_NOTIFY
);
249 SDL_WINDOWEVENT_EXPOSED
:
252 SDL_WINDOWEVENT_MAXIMIZED
:
256 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
260 if not gWinMaximized
then
262 gWinMaximized
:= True;
263 if g_debug_WinMsgs
then
265 g_Console_Add('Now maximized');
266 e_WriteLog('[DEBUG] WinMsgs: Now maximized', MSG_NOTIFY
);
271 SDL_WINDOWEVENT_RESTORED
:
275 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
279 if gWinMaximized
then
280 gWinMaximized
:= False;
281 if g_debug_WinMsgs
then
283 g_Console_Add('Now restored');
284 e_WriteLog('[DEBUG] WinMsgs: Now restored', MSG_NOTIFY
);
288 SDL_WINDOWEVENT_FOCUS_GAINED
:
291 //e_WriteLog('window gained focus!', MSG_NOTIFY);
294 SDL_WINDOWEVENT_FOCUS_LOST
:
297 //e_WriteLog('window lost focus!', MSG_NOTIFY);
305 e_WriteLog('deactivating window', MSG_NOTIFY
);
306 e_EnableInput
:= False;
307 e_ClearInputBuffer();
309 if gMuteWhenInactive
then
311 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
312 e_MuteChannels(True);
315 if g_debug_WinMsgs
then
317 g_Console_Add('Now inactive');
318 e_WriteLog('[DEBUG] WinMsgs: Now inactive', MSG_NOTIFY
);
324 else if wActivate
then
326 if not gWinActive
then
328 //e_WriteLog('activating window', MSG_NOTIFY);
329 e_EnableInput
:= True;
331 if gMuteWhenInactive
then
333 //e_WriteLog('activating sounds', MSG_NOTIFY);
334 e_MuteChannels(False);
337 if g_debug_WinMsgs
then
339 g_Console_Add('Now active');
340 e_WriteLog('[DEBUG] WinMsgs: Now active', MSG_NOTIFY
);
348 function EventHandler(ev
: TSDL_Event
): Boolean;
357 Result
:= WindowEventHandler(ev
.window
);
361 if gExit
<> EXIT_QUIT
then
363 if not wLoadingProgress
then
369 wLoadingQuit
:= True;
376 key
:= ev
.key
.keysym
.scancode
;
382 Utf8ToUnicode(@uc
, PChar(ev
.text.text), 1);
384 if (keychr
> 127) then
385 keychr
:= WCharToCP1251(keychr
);
386 CharPress(Chr(keychr
));
389 // other key presses and joysticks are handled in e_input
393 procedure SwapBuffers();
395 {$IFDEF HEADLESS}Exit
;{$ENDIF}
396 SDL_GL_SwapWindow(h_Wnd
);
399 procedure KillGLWindow();
401 if h_Wnd
<> nil then SDL_DestroyWindow(h_Wnd
);
402 if h_GL
<> nil then SDL_GL_DeleteContext(h_GL
);
405 //wWindowCreated := False;
408 function CreateGLWindow(Title
: PChar): Boolean;
414 gWinSizeX
:= gScreenWidth
;
415 gWinSizeY
:= gScreenHeight
;
418 e_WriteLog('Creating window', MSG_NOTIFY
);
420 if not g_Window_SetDisplay() then
423 e_WriteLog('Window creation error (resolution not supported?)', MSG_FATALERROR
);
428 h_Gl
:= SDL_GL_CreateContext(h_Wnd
);
429 if h_Gl
= nil then Exit
;
431 //wWindowCreated := True;
433 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
440 // windoze sux; in headless mode `GetTickCount()` (and SDL) returns shit
441 function GetTimer(): Int64;
445 QueryPerformanceFrequency(F
);
446 QueryPerformanceCounter(C
);
447 Result
:= Round(C
/F
*1000{000});
450 function GetTimer(): Int64;
455 t
:= SDL_GetTicks() {* 1000}; // TODO: do we really need microseconds here? k8: NOPE!
456 if ticksOverflow
= -1 then
463 if lastTicks
> t
then
465 // overflow, increment overflow ;-)
466 ticksOverflow
:= ticksOverflow
+(Int64($ffffffff)+Int64(1));
467 tt
:= (Int64($ffffffff)+Int64(1))+Int64(t
);
468 t
:= Uint32(tt
-lastTicks
);
472 result
:= ticksOverflow
+Int64(t
);
476 procedure ResetTimer();
478 wNeedTimeReset
:= True;
481 procedure PushExitEvent();
485 ev
.type_
:= SDL_QUITEV
;
489 procedure ProcessLoading();
494 FillChar(ev
, SizeOf(ev
), 0);
495 //wNeedFree := False;
496 wLoadingProgress
:= True;
497 while SDL_PollEvent(@ev
) > 0 do
499 if (ev
.type_
= SDL_QUITEV
) then
504 if (ev
.type_
= SDL_QUITEV
) or (gExit
= EXIT_QUIT
) then
506 wLoadingProgress
:= False;
510 if not wMinimized
then
512 if g_Texture_Get('INTER', ID
) then
513 e_DrawSize(ID
, 0, 0, 0, False, False, gScreenWidth
, gScreenHeight
)
515 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
525 if NetMode
= NET_SERVER
then
528 if (NetMode
= NET_CLIENT
) and (NetState
<> NET_STATE_AUTH
) then
529 g_Net_Client_UpdateWhileLoading
;
530 wLoadingProgress
:= False;
533 function ProcessMessage(): Boolean;
539 FillChar(ev
, SizeOf(ev
), 0);
541 while SDL_PollEvent(@ev
) > 0 do
543 Result
:= EventHandler(ev
);
544 if ev
.type_
= SDL_QUITEV
then exit
;
548 Time_Delta
:= Time
- Time_Old
;
552 if wNeedTimeReset
then
554 Time_Delta
:= (27777 div 1000);
555 wNeedTimeReset
:= False;
558 t
:= Time_Delta
div (27777 div 1000);
564 if NetMode
= NET_SERVER
then g_Net_Host_Update()
565 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
571 if NetMode
= NET_SERVER
then g_Net_Host_Update()
572 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
581 if gExit
= EXIT_QUIT
then
587 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ:
590 Time_Old
:= Time
- (Time_Delta
mod (27777 div 1000));
591 if (not wMinimized
) then
604 procedure ReDrawWindow
;
610 procedure InitOpenGL(VSync
: Boolean);
614 {$IFDEF HEADLESS}Exit
;{$ENDIF}
615 if VSync
then v
:= 1 else v
:= 0;
616 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
617 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
618 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
619 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
620 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
621 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
622 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
623 SDL_GL_SetSwapInterval(v
);
626 function SDLMain(): Integer;
629 e_NoGraphics
:= True;
632 e_WriteLog('Creating GL window', MSG_NOTIFY
);
633 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION
]))) then
639 e_WriteLog('Initializing OpenGL', MSG_NOTIFY
);
642 {EnumDisplayModes();}
645 Time_Old
:= GetTimer();
648 if ParamCount
> 0 then
649 g_Game_Process_Params();
652 if (not gGameOn
) and gAskLanguage
then
653 g_Menu_AskLanguage();
655 e_WriteLog('Entering the main loop', MSG_NOTIFY
);
657 while not ProcessMessage() do