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
;
83 e_WriteLog('Setting display mode...', MSG_NOTIFY
);
85 wFlags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
86 if gFullscreen
then wFlags
:= wFlags
or SDL_WINDOW_FULLSCREEN
;
87 if gWinMaximized
then wFlags
:= wFlags
or SDL_WINDOW_MAXIMIZED
;
91 SDL_DestroyWindow(h_Wnd
);
97 mode
.w
:= gScreenWidth
;
98 mode
.h
:= gScreenHeight
;
100 mode
.refresh_rate
:= 0;
101 mode
.driverdata
:= nil;
102 if SDL_GetClosestDisplayMode(0, @mode
, @cmode
) = nil then
105 gScreenHeight
:= 600;
109 gScreenWidth
:= cmode
.w
;
110 gScreenHeight
:= cmode
.h
;
114 h_Wnd
:= SDL_CreateWindow(PChar(wTitle
), gWinRealPosX
, gWinRealPosY
, gScreenWidth
, gScreenHeight
, wFlags
);
115 if h_Wnd
= nil then Exit
;
117 SDL_GL_MakeCurrent(h_Wnd
, h_GL
);
118 SDL_ShowCursor(SDL_DISABLE
);
123 procedure ReShowCursor();
125 // TODO: what was this for?
128 function GetDisplayModes(dBPP
: DWORD
; var SelRes
: DWORD
): SArray
;
130 mode
: TSDL_DisplayMode
;
131 res
, i
, k
, n
, pw
, ph
: Integer;
133 SetLength(Result
, 0);
134 {$IFDEF HEADLESS}Exit
;{$ENDIF}
136 n
:= SDL_GetNumDisplayModes(0);
140 res
:= SDL_GetDisplayMode(0, i
, @mode
);
141 if res
< 0 then continue
;
142 if SDL_BITSPERPIXEL(mode
.format
) = gBPP
then continue
;
143 if (mode
.w
= pw
) and (mode
.h
= ph
) then continue
;
144 if (mode
.w
= gScreenWidth
) and (mode
.h
= gScreenHeight
) then
147 SetLength(Result
, k
);
148 Result
[k
-1] := IntToStr(mode
.w
) + 'x' + IntToStr(mode
.h
);
149 pw
:= mode
.w
; ph
:= mode
.h
152 e_WriteLog('SDL: Got ' + IntToStr(k
) + ' resolutions.', MSG_NOTIFY
);
155 procedure Sleep(ms
: LongWord);
160 procedure ChangeWindowSize();
162 gWinSizeX
:= gScreenWidth
;
163 gWinSizeY
:= gScreenHeight
;
164 {$IFDEF HEADLESS}Exit
;{$ENDIF}
165 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
166 g_Game_SetupScreenSize();
168 g_Game_ClearLoading();
171 function g_Window_SetSize(W
, H
: Word; FScreen
: Boolean): Boolean;
176 {$IFDEF HEADLESS}Exit
;{$ENDIF}
179 if (gScreenWidth
<> W
) or (gScreenHeight
<> H
) then
186 if gFullscreen
<> FScreen
then
189 gFullscreen
:= FScreen
;
195 g_Window_SetDisplay(Preserve
);
200 function WindowEventHandler(ev
: TSDL_WindowEvent
): Boolean;
202 wActivate
, wDeactivate
: Boolean;
206 wDeactivate
:= False;
209 SDL_WINDOWEVENT_MOVED
:
211 if not (gFullscreen
or gWinMaximized
) then
213 gWinRealPosX
:= ev
.data1
;
214 gWinRealPosY
:= ev
.data2
;
218 SDL_WINDOWEVENT_MINIMIZED
:
220 if not wMinimized
then
222 e_ResizeWindow(0, 0);
225 if g_debug_WinMsgs
then
227 g_Console_Add('Now minimized');
228 e_WriteLog('[DEBUG] WinMsgs: Now minimized', MSG_NOTIFY
);
234 SDL_WINDOWEVENT_RESIZED
:
236 gScreenWidth
:= ev
.data1
;
237 gScreenHeight
:= ev
.data2
;
240 if g_debug_WinMsgs
then
242 g_Console_Add('Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
));
243 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
), MSG_NOTIFY
);
247 SDL_WINDOWEVENT_EXPOSED
:
250 SDL_WINDOWEVENT_MAXIMIZED
:
254 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
258 if not gWinMaximized
then
260 gWinMaximized
:= True;
261 if g_debug_WinMsgs
then
263 g_Console_Add('Now maximized');
264 e_WriteLog('[DEBUG] WinMsgs: Now maximized', MSG_NOTIFY
);
269 SDL_WINDOWEVENT_RESTORED
:
273 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
277 if gWinMaximized
then
278 gWinMaximized
:= False;
279 if g_debug_WinMsgs
then
281 g_Console_Add('Now restored');
282 e_WriteLog('[DEBUG] WinMsgs: Now restored', MSG_NOTIFY
);
286 SDL_WINDOWEVENT_FOCUS_GAINED
:
289 //e_WriteLog('window gained focus!', MSG_NOTIFY);
292 SDL_WINDOWEVENT_FOCUS_LOST
:
295 //e_WriteLog('window lost focus!', MSG_NOTIFY);
303 e_WriteLog('deactivating window', MSG_NOTIFY
);
304 e_EnableInput
:= False;
305 e_ClearInputBuffer();
307 if gMuteWhenInactive
then
309 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
310 e_MuteChannels(True);
313 if g_debug_WinMsgs
then
315 g_Console_Add('Now inactive');
316 e_WriteLog('[DEBUG] WinMsgs: Now inactive', MSG_NOTIFY
);
322 else if wActivate
then
324 if not gWinActive
then
326 //e_WriteLog('activating window', MSG_NOTIFY);
327 e_EnableInput
:= True;
329 if gMuteWhenInactive
then
331 //e_WriteLog('activating sounds', MSG_NOTIFY);
332 e_MuteChannels(False);
335 if g_debug_WinMsgs
then
337 g_Console_Add('Now active');
338 e_WriteLog('[DEBUG] WinMsgs: Now active', MSG_NOTIFY
);
346 function EventHandler(ev
: TSDL_Event
): Boolean;
355 Result
:= WindowEventHandler(ev
.window
);
359 if gExit
<> EXIT_QUIT
then
361 if not wLoadingProgress
then
367 wLoadingQuit
:= True;
374 key
:= ev
.key
.keysym
.scancode
;
380 Utf8ToUnicode(@uc
, PChar(ev
.text.text), 1);
382 if (keychr
> 127) then
383 keychr
:= WCharToCP1251(keychr
);
384 CharPress(Chr(keychr
));
387 // other key presses and joysticks are handled in e_input
391 procedure SwapBuffers();
393 {$IFDEF HEADLESS}Exit
;{$ENDIF}
394 SDL_GL_SwapWindow(h_Wnd
);
397 procedure KillGLWindow();
399 if h_Wnd
<> nil then SDL_DestroyWindow(h_Wnd
);
400 if h_GL
<> nil then SDL_GL_DeleteContext(h_GL
);
403 //wWindowCreated := False;
406 function CreateGLWindow(Title
: PChar): Boolean;
412 gWinSizeX
:= gScreenWidth
;
413 gWinSizeY
:= gScreenHeight
;
416 e_WriteLog('Creating window', MSG_NOTIFY
);
418 if not g_Window_SetDisplay() then
421 e_WriteLog('Window creation error (resolution not supported?)', MSG_FATALERROR
);
426 h_Gl
:= SDL_GL_CreateContext(h_Wnd
);
427 if h_Gl
= nil then Exit
;
429 //wWindowCreated := True;
431 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
437 function GetTimer(): Int64;
442 t
:= SDL_GetTicks() {* 1000}; // TODO: do we really need microseconds here? k8: NOPE!
443 if ticksOverflow
= -1 then
450 if lastTicks
> t
then
452 // overflow, increment overflow ;-)
453 ticksOverflow
:= ticksOverflow
+(Int64($ffffffff)+Int64(1));
454 tt
:= (Int64($ffffffff)+Int64(1))+Int64(t
);
455 t
:= Uint32(tt
-lastTicks
);
459 result
:= ticksOverflow
+Int64(t
);
462 procedure ResetTimer();
464 wNeedTimeReset
:= True;
467 procedure PushExitEvent();
471 ev
.type_
:= SDL_QUITEV
;
475 procedure ProcessLoading();
480 FillChar(ev
, SizeOf(ev
), 0);
481 //wNeedFree := False;
482 wLoadingProgress
:= True;
483 while SDL_PollEvent(@ev
) > 0 do
485 if (ev
.type_
= SDL_QUITEV
) then
490 if (ev
.type_
= SDL_QUITEV
) or (gExit
= EXIT_QUIT
) then
492 wLoadingProgress
:= False;
496 if not wMinimized
then
498 if g_Texture_Get('INTER', ID
) then
499 e_DrawSize(ID
, 0, 0, 0, False, False, gScreenWidth
, gScreenHeight
)
501 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
511 if NetMode
= NET_SERVER
then
514 if (NetMode
= NET_CLIENT
) and (NetState
<> NET_STATE_AUTH
) then
515 g_Net_Client_UpdateWhileLoading
;
516 wLoadingProgress
:= False;
519 function ProcessMessage(): Boolean;
525 FillChar(ev
, SizeOf(ev
), 0);
527 while SDL_PollEvent(@ev
) > 0 do
529 Result
:= EventHandler(ev
);
530 if ev
.type_
= SDL_QUITEV
then exit
;
534 Time_Delta
:= Time
- Time_Old
;
538 if wNeedTimeReset
then
540 Time_Delta
:= (27777 div 1000);
541 wNeedTimeReset
:= False;
544 t
:= Time_Delta
div (27777 div 1000);
550 if NetMode
= NET_SERVER
then g_Net_Host_Update()
551 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
557 if NetMode
= NET_SERVER
then g_Net_Host_Update()
558 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
567 if gExit
= EXIT_QUIT
then
573 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ:
576 Time_Old
:= Time
- (Time_Delta
mod (27777 div 1000));
577 if (not wMinimized
) then
590 procedure ReDrawWindow
;
596 procedure InitOpenGL(VSync
: Boolean);
600 {$IFDEF HEADLESS}Exit
;{$ENDIF}
601 if VSync
then v
:= 1 else v
:= 0;
602 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
603 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
604 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
605 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
606 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
607 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
608 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
609 SDL_GL_SetSwapInterval(v
);
612 function SDLMain(): Integer;
615 e_NoGraphics
:= True;
618 e_WriteLog('Creating GL window', MSG_NOTIFY
);
619 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION
]))) then
625 e_WriteLog('Initializing OpenGL', MSG_NOTIFY
);
628 {EnumDisplayModes();}
631 Time_Old
:= GetTimer();
634 if ParamCount
> 0 then
635 g_Game_Process_Params();
638 if (not gGameOn
) and gAskLanguage
then
639 g_Menu_AskLanguage();
641 e_WriteLog('Entering the main loop', MSG_NOTIFY
);
643 while not ProcessMessage() do