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;}
48 ticksOverflow
: Int64 = -1;
49 lastTicks
: Uint32
= 0; // to detect overflow
53 // TODO: move this to a separate file
54 CP1251
: array [0..127] of Word = (
55 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
56 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
57 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
58 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
59 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
60 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
61 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
62 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
65 // TODO: make a transition table or something
66 function WCharToCP1251(wc
: Word): Word;
72 if CP1251
[n
] = wc
then begin Result
:= n
; break
end;
73 Result
:= Result
+ 128;
76 function g_Window_SetDisplay(PreserveGL
: Boolean = False): Boolean;
78 mode
, cmode
: TSDL_DisplayMode
;
87 e_WriteLog('Setting display mode...', MSG_NOTIFY
);
89 wFlags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
90 if gFullscreen
then wFlags
:= wFlags
or SDL_WINDOW_FULLSCREEN
;
91 if gWinMaximized
then wFlags
:= wFlags
or SDL_WINDOW_MAXIMIZED
;
95 SDL_DestroyWindow(h_Wnd
);
101 mode
.w
:= gScreenWidth
;
102 mode
.h
:= gScreenHeight
;
104 mode
.refresh_rate
:= 0;
105 mode
.driverdata
:= nil;
106 if SDL_GetClosestDisplayMode(0, @mode
, @cmode
) = nil then
109 gScreenHeight
:= 600;
113 gScreenWidth
:= cmode
.w
;
114 gScreenHeight
:= cmode
.h
;
118 h_Wnd
:= SDL_CreateWindow(PChar(wTitle
), gWinRealPosX
, gWinRealPosY
, gScreenWidth
, gScreenHeight
, wFlags
);
119 if h_Wnd
= nil then Exit
;
121 SDL_GL_MakeCurrent(h_Wnd
, h_GL
);
122 SDL_ShowCursor(SDL_DISABLE
);
127 procedure ReShowCursor();
129 // TODO: what was this for?
132 function GetDisplayModes(dBPP
: DWORD
; var SelRes
: DWORD
): SArray
;
134 mode
: TSDL_DisplayMode
;
135 res
, i
, k
, n
, pw
, ph
: Integer;
137 SetLength(Result
, 0);
138 {$IFDEF HEADLESS}Exit
;{$ENDIF}
140 n
:= SDL_GetNumDisplayModes(0);
144 res
:= SDL_GetDisplayMode(0, i
, @mode
);
145 if res
< 0 then continue
;
146 if SDL_BITSPERPIXEL(mode
.format
) = gBPP
then continue
;
147 if (mode
.w
= pw
) and (mode
.h
= ph
) then continue
;
148 if (mode
.w
= gScreenWidth
) and (mode
.h
= gScreenHeight
) then
151 SetLength(Result
, k
);
152 Result
[k
-1] := IntToStr(mode
.w
) + 'x' + IntToStr(mode
.h
);
153 pw
:= mode
.w
; ph
:= mode
.h
156 e_WriteLog('SDL: Got ' + IntToStr(k
) + ' resolutions.', MSG_NOTIFY
);
159 procedure Sleep(ms
: LongWord);
164 procedure ChangeWindowSize();
166 gWinSizeX
:= gScreenWidth
;
167 gWinSizeY
:= gScreenHeight
;
168 {$IFDEF HEADLESS}Exit
;{$ENDIF}
169 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
170 g_Game_SetupScreenSize();
172 g_Game_ClearLoading();
175 function g_Window_SetSize(W
, H
: Word; FScreen
: Boolean): Boolean;
180 {$IFDEF HEADLESS}Exit
;{$ENDIF}
183 if (gScreenWidth
<> W
) or (gScreenHeight
<> H
) then
190 if gFullscreen
<> FScreen
then
193 gFullscreen
:= FScreen
;
199 g_Window_SetDisplay(Preserve
);
204 function WindowEventHandler(ev
: TSDL_WindowEvent
): Boolean;
206 wActivate
, wDeactivate
: Boolean;
210 wDeactivate
:= False;
213 SDL_WINDOWEVENT_MOVED
:
215 if not (gFullscreen
or gWinMaximized
) then
217 gWinRealPosX
:= ev
.data1
;
218 gWinRealPosY
:= ev
.data2
;
222 SDL_WINDOWEVENT_MINIMIZED
:
224 if not wMinimized
then
226 e_ResizeWindow(0, 0);
229 if g_debug_WinMsgs
then
231 g_Console_Add('Now minimized');
232 e_WriteLog('[DEBUG] WinMsgs: Now minimized', MSG_NOTIFY
);
238 SDL_WINDOWEVENT_RESIZED
:
240 gScreenWidth
:= ev
.data1
;
241 gScreenHeight
:= ev
.data2
;
244 if g_debug_WinMsgs
then
246 g_Console_Add('Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
));
247 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
), MSG_NOTIFY
);
251 SDL_WINDOWEVENT_EXPOSED
:
254 SDL_WINDOWEVENT_MAXIMIZED
:
258 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
262 if not gWinMaximized
then
264 gWinMaximized
:= True;
265 if g_debug_WinMsgs
then
267 g_Console_Add('Now maximized');
268 e_WriteLog('[DEBUG] WinMsgs: Now maximized', MSG_NOTIFY
);
273 SDL_WINDOWEVENT_RESTORED
:
277 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
281 if gWinMaximized
then
282 gWinMaximized
:= False;
283 if g_debug_WinMsgs
then
285 g_Console_Add('Now restored');
286 e_WriteLog('[DEBUG] WinMsgs: Now restored', MSG_NOTIFY
);
290 SDL_WINDOWEVENT_FOCUS_GAINED
:
293 //e_WriteLog('window gained focus!', MSG_NOTIFY);
296 SDL_WINDOWEVENT_FOCUS_LOST
:
299 //e_WriteLog('window lost focus!', MSG_NOTIFY);
307 e_WriteLog('deactivating window', MSG_NOTIFY
);
308 e_EnableInput
:= False;
309 e_ClearInputBuffer();
311 if gMuteWhenInactive
then
313 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
314 e_MuteChannels(True);
317 if g_debug_WinMsgs
then
319 g_Console_Add('Now inactive');
320 e_WriteLog('[DEBUG] WinMsgs: Now inactive', MSG_NOTIFY
);
326 else if wActivate
then
328 if not gWinActive
then
330 //e_WriteLog('activating window', MSG_NOTIFY);
331 e_EnableInput
:= True;
333 if gMuteWhenInactive
then
335 //e_WriteLog('activating sounds', MSG_NOTIFY);
336 e_MuteChannels(False);
339 if g_debug_WinMsgs
then
341 g_Console_Add('Now active');
342 e_WriteLog('[DEBUG] WinMsgs: Now active', MSG_NOTIFY
);
350 function EventHandler(ev
: TSDL_Event
): Boolean;
359 Result
:= WindowEventHandler(ev
.window
);
363 if gExit
<> EXIT_QUIT
then
365 if not wLoadingProgress
then
371 wLoadingQuit
:= True;
378 key
:= ev
.key
.keysym
.scancode
;
384 Utf8ToUnicode(@uc
, PChar(ev
.text.text), 1);
386 if (keychr
> 127) then
387 keychr
:= WCharToCP1251(keychr
);
388 CharPress(Chr(keychr
));
391 // other key presses and joysticks are handled in e_input
395 procedure SwapBuffers();
397 {$IFDEF HEADLESS}Exit
;{$ENDIF}
398 SDL_GL_SwapWindow(h_Wnd
);
401 procedure KillGLWindow();
403 if h_Wnd
<> nil then SDL_DestroyWindow(h_Wnd
);
404 if h_GL
<> nil then SDL_GL_DeleteContext(h_GL
);
407 //wWindowCreated := False;
410 function CreateGLWindow(Title
: PChar): Boolean;
416 gWinSizeX
:= gScreenWidth
;
417 gWinSizeY
:= gScreenHeight
;
420 e_WriteLog('Creating window', MSG_NOTIFY
);
422 if not g_Window_SetDisplay() then
425 e_WriteLog('Window creation error (resolution not supported?)', MSG_FATALERROR
);
430 h_Gl
:= SDL_GL_CreateContext(h_Wnd
);
431 if h_Gl
= nil then Exit
;
433 //wWindowCreated := True;
435 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
442 // windoze sux; in headless mode `GetTickCount()` (and SDL) returns shit
443 function GetTimer(): Int64;
447 QueryPerformanceFrequency(F
);
448 QueryPerformanceCounter(C
);
449 Result
:= Round(C
/F
*1000{000});
452 function GetTimer(): Int64;
457 t
:= SDL_GetTicks() {* 1000}; // TODO: do we really need microseconds here? k8: NOPE!
458 if ticksOverflow
= -1 then
465 if lastTicks
> t
then
467 // overflow, increment overflow ;-)
468 ticksOverflow
:= ticksOverflow
+(Int64($ffffffff)+Int64(1));
469 tt
:= (Int64($ffffffff)+Int64(1))+Int64(t
);
470 t
:= Uint32(tt
-lastTicks
);
474 result
:= ticksOverflow
+Int64(t
);
478 procedure ResetTimer();
480 wNeedTimeReset
:= True;
483 procedure PushExitEvent();
487 ev
.type_
:= SDL_QUITEV
;
491 procedure ProcessLoading();
496 FillChar(ev
, SizeOf(ev
), 0);
497 //wNeedFree := False;
498 wLoadingProgress
:= True;
499 while SDL_PollEvent(@ev
) > 0 do
501 if (ev
.type_
= SDL_QUITEV
) then
506 if (ev
.type_
= SDL_QUITEV
) or (gExit
= EXIT_QUIT
) then
508 wLoadingProgress
:= False;
512 if not wMinimized
then
514 if g_Texture_Get('INTER', ID
) then
515 e_DrawSize(ID
, 0, 0, 0, False, False, gScreenWidth
, gScreenHeight
)
517 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
527 if NetMode
= NET_SERVER
then
530 if (NetMode
= NET_CLIENT
) and (NetState
<> NET_STATE_AUTH
) then
531 g_Net_Client_UpdateWhileLoading
;
532 wLoadingProgress
:= False;
535 function ProcessMessage(): Boolean;
541 FillChar(ev
, SizeOf(ev
), 0);
543 while SDL_PollEvent(@ev
) > 0 do
545 Result
:= EventHandler(ev
);
546 if ev
.type_
= SDL_QUITEV
then exit
;
550 Time_Delta
:= Time
- Time_Old
;
554 if wNeedTimeReset
then
556 Time_Delta
:= (27777 div 1000);
557 wNeedTimeReset
:= False;
560 t
:= Time_Delta
div (27777 div 1000);
566 if NetMode
= NET_SERVER
then g_Net_Host_Update()
567 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
573 if NetMode
= NET_SERVER
then g_Net_Host_Update()
574 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
583 if gExit
= EXIT_QUIT
then
589 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ:
592 Time_Old
:= Time
- (Time_Delta
mod (27777 div 1000));
593 if (not wMinimized
) then
606 procedure ReDrawWindow
;
612 procedure InitOpenGL(VSync
: Boolean);
616 {$IFDEF HEADLESS}Exit
;{$ENDIF}
617 if VSync
then v
:= 1 else v
:= 0;
618 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
619 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
620 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
621 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
622 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
623 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
624 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
625 SDL_GL_SetSwapInterval(v
);
628 function SDLMain(): Integer;
631 e_NoGraphics
:= True;
634 e_WriteLog('Creating GL window', MSG_NOTIFY
);
635 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION
]))) then
641 e_WriteLog('Initializing OpenGL', MSG_NOTIFY
);
644 {EnumDisplayModes();}
647 Time_Old
:= GetTimer();
650 if ParamCount
> 0 then
651 g_Game_Process_Params();
654 if (not gGameOn
) and gAskLanguage
then
655 g_Menu_AskLanguage();
657 e_WriteLog('Entering the main loop', MSG_NOTIFY
);
659 while not ProcessMessage() do