8 function SDLMain(): Integer;
9 function GetTimer(): Int64;
10 procedure ResetTimer();
11 function CreateGLWindow(Title
: PChar): Boolean;
12 procedure KillGLWindow();
13 procedure PushExitEvent();
14 function ProcessMessage(): Boolean;
15 procedure ProcessLoading();
16 procedure ReDrawWindow();
17 procedure SwapBuffers();
18 procedure Sleep(ms
: LongWord);
19 function GetDisplayModes(dBPP
: DWORD
; var SelRes
: DWORD
): SArray
;
20 function g_Window_SetDisplay(PreserveGL
: Boolean = False): Boolean;
21 function g_Window_SetSize(W
, H
: Word; FScreen
: Boolean): Boolean;
26 SDL2
, GL
, GLExt
, e_graphics
, e_log
, g_main
,
27 g_console
, SysUtils
, e_input
, g_options
, g_game
,
28 g_basic
, g_textures
, e_sound
, g_sound
, g_menu
, ENet
, g_net
;
34 Time
, Time_Delta
, Time_Old
: Int64;
37 wNeedTimeReset
: Boolean = False;
38 //wWindowCreated: Boolean = False;
39 //wCursorShown: Boolean = False;
40 wMinimized
: Boolean = False;
41 //wNeedFree: Boolean = True;
42 wLoadingProgress
: Boolean = False;
43 wLoadingQuit
: Boolean = False;
44 {wWinPause: Byte = 0;}
45 ticksOverflow
: Int64 = -1;
46 lastTicks
: Uint32
= 0; // to detect overflow
49 // TODO: move this to a separate file
50 CP1251
: array [0..127] of Word = (
51 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
52 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
53 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
54 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
55 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
56 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
57 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
58 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
61 // TODO: make a transition table or something
62 function WCharToCP1251(wc
: Word): Word;
68 if CP1251
[n
] = wc
then begin Result
:= n
; break
end;
69 Result
:= Result
+ 128;
72 function g_Window_SetDisplay(PreserveGL
: Boolean = False): Boolean;
74 mode
, cmode
: TSDL_DisplayMode
;
78 e_WriteLog('Setting display mode...', MSG_NOTIFY
);
80 wFlags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
81 if gFullscreen
then wFlags
:= wFlags
or SDL_WINDOW_FULLSCREEN
;
82 if gWinMaximized
then wFlags
:= wFlags
or SDL_WINDOW_MAXIMIZED
;
86 SDL_DestroyWindow(h_Wnd
);
92 mode
.w
:= gScreenWidth
;
93 mode
.h
:= gScreenHeight
;
95 mode
.refresh_rate
:= 0;
96 mode
.driverdata
:= nil;
97 if SDL_GetClosestDisplayMode(0, @mode
, @cmode
) = nil then
100 gScreenHeight
:= 600;
104 gScreenWidth
:= cmode
.w
;
105 gScreenHeight
:= cmode
.h
;
109 h_Wnd
:= SDL_CreateWindow(PChar(wTitle
), gWinRealPosX
, gWinRealPosY
, gScreenWidth
, gScreenHeight
, wFlags
);
110 if h_Wnd
= nil then Exit
;
112 SDL_GL_MakeCurrent(h_Wnd
, h_GL
);
113 SDL_ShowCursor(SDL_DISABLE
);
118 procedure ReShowCursor();
120 // TODO: what was this for?
123 function GetDisplayModes(dBPP
: DWORD
; var SelRes
: DWORD
): SArray
;
125 mode
: TSDL_DisplayMode
;
126 res
, i
, k
, n
, pw
, ph
: Integer;
128 SetLength(Result
, 0);
131 n
:= SDL_GetNumDisplayModes(0);
135 res
:= SDL_GetDisplayMode(0, i
, @mode
);
136 if res
< 0 then continue
;
137 if SDL_BITSPERPIXEL(mode
.format
) = gBPP
then continue
;
138 if (mode
.w
= pw
) and (mode
.h
= ph
) then continue
;
139 if (mode
.w
= gScreenWidth
) and (mode
.h
= gScreenHeight
) then
142 SetLength(Result
, k
);
143 Result
[k
-1] := IntToStr(mode
.w
) + 'x' + IntToStr(mode
.h
);
144 pw
:= mode
.w
; ph
:= mode
.h
147 e_WriteLog('SDL: Got ' + IntToStr(k
) + ' resolutions.', MSG_NOTIFY
);
150 procedure Sleep(ms
: LongWord);
155 procedure ChangeWindowSize();
157 gWinSizeX
:= gScreenWidth
;
158 gWinSizeY
:= gScreenHeight
;
159 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
160 g_Game_SetupScreenSize();
162 g_Game_ClearLoading();
165 function g_Window_SetSize(W
, H
: Word; FScreen
: Boolean): Boolean;
172 if (gScreenWidth
<> W
) or (gScreenHeight
<> H
) then
179 if gFullscreen
<> FScreen
then
182 gFullscreen
:= FScreen
;
188 g_Window_SetDisplay(Preserve
);
193 function WindowEventHandler(ev
: TSDL_WindowEvent
): Boolean;
195 wActivate
, wDeactivate
: Boolean;
199 wDeactivate
:= False;
202 SDL_WINDOWEVENT_MOVED
:
204 if not (gFullscreen
or gWinMaximized
) then
206 gWinRealPosX
:= ev
.data1
;
207 gWinRealPosY
:= ev
.data2
;
211 SDL_WINDOWEVENT_MINIMIZED
:
213 if not wMinimized
then
215 e_ResizeWindow(0, 0);
218 if g_debug_WinMsgs
then
220 g_Console_Add('Now minimized');
221 e_WriteLog('[DEBUG] WinMsgs: Now minimized', MSG_NOTIFY
);
227 SDL_WINDOWEVENT_RESIZED
:
229 gScreenWidth
:= ev
.data1
;
230 gScreenHeight
:= ev
.data2
;
233 if g_debug_WinMsgs
then
235 g_Console_Add('Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
));
236 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
), MSG_NOTIFY
);
240 SDL_WINDOWEVENT_EXPOSED
:
243 SDL_WINDOWEVENT_MAXIMIZED
:
247 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
251 if not gWinMaximized
then
253 gWinMaximized
:= True;
254 if g_debug_WinMsgs
then
256 g_Console_Add('Now maximized');
257 e_WriteLog('[DEBUG] WinMsgs: Now maximized', MSG_NOTIFY
);
262 SDL_WINDOWEVENT_RESTORED
:
266 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
270 if gWinMaximized
then
271 gWinMaximized
:= False;
272 if g_debug_WinMsgs
then
274 g_Console_Add('Now restored');
275 e_WriteLog('[DEBUG] WinMsgs: Now restored', MSG_NOTIFY
);
279 SDL_WINDOWEVENT_FOCUS_GAINED
:
282 //e_WriteLog('window gained focus!', MSG_NOTIFY);
285 SDL_WINDOWEVENT_FOCUS_LOST
:
288 //e_WriteLog('window lost focus!', MSG_NOTIFY);
296 e_WriteLog('deactivating window', MSG_NOTIFY
);
297 e_EnableInput
:= False;
298 e_ClearInputBuffer();
300 if gMuteWhenInactive
then
302 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
303 e_MuteChannels(True);
306 if g_debug_WinMsgs
then
308 g_Console_Add('Now inactive');
309 e_WriteLog('[DEBUG] WinMsgs: Now inactive', MSG_NOTIFY
);
315 else if wActivate
then
317 if not gWinActive
then
319 //e_WriteLog('activating window', MSG_NOTIFY);
320 e_EnableInput
:= True;
322 if gMuteWhenInactive
then
324 //e_WriteLog('activating sounds', MSG_NOTIFY);
325 e_MuteChannels(False);
328 if g_debug_WinMsgs
then
330 g_Console_Add('Now active');
331 e_WriteLog('[DEBUG] WinMsgs: Now active', MSG_NOTIFY
);
339 function EventHandler(ev
: TSDL_Event
): Boolean;
348 Result
:= WindowEventHandler(ev
.window
);
352 if gExit
<> EXIT_QUIT
then
354 if not wLoadingProgress
then
360 wLoadingQuit
:= True;
367 key
:= ev
.key
.keysym
.scancode
;
373 Utf8ToUnicode(@uc
, PChar(ev
.text.text), 1);
375 if (keychr
> 127) then
376 keychr
:= WCharToCP1251(keychr
);
377 CharPress(Chr(keychr
));
380 // other key presses and joysticks are handled in e_input
384 procedure SwapBuffers();
386 SDL_GL_SwapWindow(h_Wnd
);
389 procedure KillGLWindow();
391 if h_Wnd
<> nil then SDL_DestroyWindow(h_Wnd
);
392 if h_GL
<> nil then SDL_GL_DeleteContext(h_GL
);
395 //wWindowCreated := False;
398 function CreateGLWindow(Title
: PChar): Boolean;
404 gWinSizeX
:= gScreenWidth
;
405 gWinSizeY
:= gScreenHeight
;
408 e_WriteLog('Creating window', MSG_NOTIFY
);
410 if not g_Window_SetDisplay() then
413 e_WriteLog('Window creation error (resolution not supported?)', MSG_FATALERROR
);
417 h_Gl
:= SDL_GL_CreateContext(h_Wnd
);
418 if h_Gl
= nil then Exit
;
420 //wWindowCreated := True;
422 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
428 function GetTimer(): Int64;
433 t
:= SDL_GetTicks() {* 1000}; // TODO: do we really need microseconds here? k8: NOPE!
434 if ticksOverflow
= -1 then
441 if lastTicks
> t
then
443 // overflow, increment overflow ;-)
444 ticksOverflow
:= ticksOverflow
+(Int64($ffffffff)+Int64(1));
445 tt
:= (Int64($ffffffff)+Int64(1))+Int64(t
);
446 t
:= Uint32(tt
-lastTicks
);
450 result
:= ticksOverflow
+Int64(t
);
453 procedure ResetTimer();
455 wNeedTimeReset
:= True;
458 procedure PushExitEvent();
462 ev
.type_
:= SDL_QUITEV
;
466 procedure ProcessLoading();
471 FillChar(ev
, SizeOf(ev
), 0);
472 //wNeedFree := False;
473 wLoadingProgress
:= True;
474 while SDL_PollEvent(@ev
) > 0 do
476 if (ev
.type_
= SDL_QUITEV
) then
481 if (ev
.type_
= SDL_QUITEV
) or (gExit
= EXIT_QUIT
) then
483 wLoadingProgress
:= False;
487 if not wMinimized
then
489 if g_Texture_Get('INTER', ID
) then
490 e_DrawSize(ID
, 0, 0, 0, False, False, gScreenWidth
, gScreenHeight
)
492 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
502 if NetMode
= NET_SERVER
then
505 if (NetMode
= NET_CLIENT
) and (NetState
<> NET_STATE_AUTH
) then
506 g_Net_Client_UpdateWhileLoading
;
507 wLoadingProgress
:= False;
510 function ProcessMessage(): Boolean;
516 FillChar(ev
, SizeOf(ev
), 0);
518 while SDL_PollEvent(@ev
) > 0 do
520 Result
:= EventHandler(ev
);
521 if ev
.type_
= SDL_QUITEV
then exit
;
525 Time_Delta
:= Time
- Time_Old
;
529 if wNeedTimeReset
then
531 Time_Delta
:= (27777 div 1000);
532 wNeedTimeReset
:= False;
535 t
:= Time_Delta
div (27777 div 1000);
541 if NetMode
= NET_SERVER
then g_Net_Host_Update()
542 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
548 if NetMode
= NET_SERVER
then g_Net_Host_Update()
549 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
558 if gExit
= EXIT_QUIT
then
564 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ:
567 Time_Old
:= Time
- (Time_Delta
mod (27777 div 1000));
568 if (not wMinimized
) then
581 procedure ReDrawWindow
;
587 procedure InitOpenGL(VSync
: Boolean);
591 if VSync
then v
:= 1 else v
:= 0;
592 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
593 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
594 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
595 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
596 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
597 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
598 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
599 SDL_GL_SetSwapInterval(v
);
602 function SDLMain(): Integer;
604 e_WriteLog('Creating GL window', MSG_NOTIFY
);
605 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION
]))) then
611 e_WriteLog('Initializing OpenGL', MSG_NOTIFY
);
614 {EnumDisplayModes();}
617 Time_Old
:= GetTimer();
620 if ParamCount
> 0 then
621 g_Game_Process_Params();
624 if (not gGameOn
) and gAskLanguage
then
625 g_Menu_AskLanguage();
627 e_WriteLog('Entering the main loop', MSG_NOTIFY
);
629 while not ProcessMessage() do