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;}
47 // TODO: move this to a separate file
48 CP1251
: array [0..127] of Word = (
49 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
50 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
51 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
52 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
53 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
54 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
55 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
56 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
59 // TODO: make a transition table or something
60 function WCharToCP1251(wc
: Word): Word;
66 if CP1251
[n
] = wc
then begin Result
:= n
; break
end;
67 Result
:= Result
+ 128;
70 function g_Window_SetDisplay(PreserveGL
: Boolean = False): Boolean;
72 mode
, cmode
: TSDL_DisplayMode
;
76 e_WriteLog('Setting display mode...', MSG_NOTIFY
);
78 wFlags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
79 if gFullscreen
then wFlags
:= wFlags
or SDL_WINDOW_FULLSCREEN
;
80 if gWinMaximized
then wFlags
:= wFlags
or SDL_WINDOW_MAXIMIZED
;
84 SDL_DestroyWindow(h_Wnd
);
90 mode
.w
:= gScreenWidth
;
91 mode
.h
:= gScreenHeight
;
93 mode
.refresh_rate
:= 0;
94 mode
.driverdata
:= nil;
95 if SDL_GetClosestDisplayMode(0, @mode
, @cmode
) = nil then
102 gScreenWidth
:= cmode
.w
;
103 gScreenHeight
:= cmode
.h
;
107 h_Wnd
:= SDL_CreateWindow(PChar(wTitle
), gWinRealPosX
, gWinRealPosY
, gScreenWidth
, gScreenHeight
, wFlags
);
108 if h_Wnd
= nil then Exit
;
110 SDL_GL_MakeCurrent(h_Wnd
, h_GL
);
111 SDL_ShowCursor(SDL_DISABLE
);
116 procedure ReShowCursor();
118 // TODO: what was this for?
121 function GetDisplayModes(dBPP
: DWORD
; var SelRes
: DWORD
): SArray
;
123 mode
: TSDL_DisplayMode
;
124 res
, i
, k
, n
, pw
, ph
: Integer;
126 SetLength(Result
, 0);
129 n
:= SDL_GetNumDisplayModes(0);
133 res
:= SDL_GetDisplayMode(0, i
, @mode
);
134 if res
< 0 then continue
;
135 if SDL_BITSPERPIXEL(mode
.format
) = gBPP
then continue
;
136 if (mode
.w
= pw
) and (mode
.h
= ph
) then continue
;
137 if (mode
.w
= gScreenWidth
) and (mode
.h
= gScreenHeight
) then
140 SetLength(Result
, k
);
141 Result
[k
-1] := IntToStr(mode
.w
) + 'x' + IntToStr(mode
.h
);
142 pw
:= mode
.w
; ph
:= mode
.h
145 e_WriteLog('SDL: Got ' + IntToStr(k
) + ' resolutions.', MSG_NOTIFY
);
148 procedure Sleep(ms
: LongWord);
153 procedure ChangeWindowSize();
155 gWinSizeX
:= gScreenWidth
;
156 gWinSizeY
:= gScreenHeight
;
157 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
158 g_Game_SetupScreenSize();
160 g_Game_ClearLoading();
163 function g_Window_SetSize(W
, H
: Word; FScreen
: Boolean): Boolean;
170 if (gScreenWidth
<> W
) or (gScreenHeight
<> H
) then
177 if gFullscreen
<> FScreen
then
180 gFullscreen
:= FScreen
;
186 g_Window_SetDisplay(Preserve
);
191 function WindowEventHandler(ev
: TSDL_WindowEvent
): Boolean;
193 wActivate
, wDeactivate
: Boolean;
197 wDeactivate
:= False;
200 SDL_WINDOWEVENT_MOVED
:
202 if not (gFullscreen
or gWinMaximized
) then
204 gWinRealPosX
:= ev
.data1
;
205 gWinRealPosY
:= ev
.data2
;
209 SDL_WINDOWEVENT_MINIMIZED
:
211 if not wMinimized
then
213 e_ResizeWindow(0, 0);
216 if g_debug_WinMsgs
then
218 g_Console_Add('Now minimized');
219 e_WriteLog('[DEBUG] WinMsgs: Now minimized', MSG_NOTIFY
);
225 SDL_WINDOWEVENT_RESIZED
:
227 gScreenWidth
:= ev
.data1
;
228 gScreenHeight
:= ev
.data2
;
231 if g_debug_WinMsgs
then
233 g_Console_Add('Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
));
234 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
), MSG_NOTIFY
);
238 SDL_WINDOWEVENT_EXPOSED
:
241 SDL_WINDOWEVENT_MAXIMIZED
:
245 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
249 if not gWinMaximized
then
251 gWinMaximized
:= True;
252 if g_debug_WinMsgs
then
254 g_Console_Add('Now maximized');
255 e_WriteLog('[DEBUG] WinMsgs: Now maximized', MSG_NOTIFY
);
260 SDL_WINDOWEVENT_RESTORED
:
264 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
268 if gWinMaximized
then
269 gWinMaximized
:= False;
270 if g_debug_WinMsgs
then
272 g_Console_Add('Now restored');
273 e_WriteLog('[DEBUG] WinMsgs: Now restored', MSG_NOTIFY
);
277 SDL_WINDOWEVENT_FOCUS_GAINED
:
280 SDL_WINDOWEVENT_FOCUS_LOST
:
288 e_EnableInput
:= False;
289 e_ClearInputBuffer();
291 if gMuteWhenInactive
then
292 e_MuteChannels(True);
294 if g_debug_WinMsgs
then
296 g_Console_Add('Now inactive');
297 e_WriteLog('[DEBUG] WinMsgs: Now inactive', MSG_NOTIFY
);
303 else if wActivate
then
305 if not gWinActive
then
307 e_EnableInput
:= True;
309 if gMuteWhenInactive
then
310 e_MuteChannels(False);
312 if g_debug_WinMsgs
then
314 g_Console_Add('Now active');
315 e_WriteLog('[DEBUG] WinMsgs: Now active', MSG_NOTIFY
);
323 function EventHandler(ev
: TSDL_Event
): Boolean;
332 Result
:= WindowEventHandler(ev
.window
);
336 if gExit
<> EXIT_QUIT
then
338 if not wLoadingProgress
then
344 wLoadingQuit
:= True;
351 key
:= ev
.key
.keysym
.scancode
;
357 Utf8ToUnicode(@uc
, PChar(ev
.text.text), 1);
359 if (keychr
> 127) then
360 keychr
:= WCharToCP1251(keychr
);
361 CharPress(Chr(keychr
));
364 // other key presses and joysticks are handled in e_input
368 procedure SwapBuffers();
370 SDL_GL_SwapWindow(h_Wnd
);
373 procedure KillGLWindow();
375 if h_Wnd
<> nil then SDL_DestroyWindow(h_Wnd
);
376 if h_GL
<> nil then SDL_GL_DeleteContext(h_GL
);
379 wWindowCreated
:= False;
382 function CreateGLWindow(Title
: PChar): Boolean;
388 gWinSizeX
:= gScreenWidth
;
389 gWinSizeY
:= gScreenHeight
;
392 e_WriteLog('Creating window', MSG_NOTIFY
);
394 if not g_Window_SetDisplay() then
397 e_WriteLog('Window creation error (resolution not supported?)', MSG_FATALERROR
);
401 h_Gl
:= SDL_GL_CreateContext(h_Wnd
);
402 if h_Gl
= nil then Exit
;
404 wWindowCreated
:= True;
406 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
412 function GetTimer(): Int64;
414 Result
:= SDL_GetTicks() * 1000; // TODO: do we really need microseconds here?
417 procedure ResetTimer();
419 wNeedTimeReset
:= True;
422 procedure PushExitEvent();
426 ev
.type_
:= SDL_QUITEV
;
430 procedure ProcessLoading();
435 FillChar(ev
, SizeOf(ev
), 0);
436 //wNeedFree := False;
437 wLoadingProgress
:= True;
438 while SDL_PollEvent(@ev
) > 0 do
440 if (ev
.type_
= SDL_QUITEV
) then
445 if (ev
.type_
= SDL_QUITEV
) or (gExit
= EXIT_QUIT
) then
447 wLoadingProgress
:= False;
451 if not wMinimized
then
453 if g_Texture_Get('INTER', ID
) then
454 e_DrawSize(ID
, 0, 0, 0, False, False, gScreenWidth
, gScreenHeight
)
456 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
466 if NetMode
= NET_SERVER
then
469 if (NetMode
= NET_CLIENT
) and (NetState
<> NET_STATE_AUTH
) then
470 g_Net_Client_UpdateWhileLoading
;
471 wLoadingProgress
:= False;
474 function ProcessMessage(): Boolean;
480 FillChar(ev
, SizeOf(ev
), 0);
482 while SDL_PollEvent(@ev
) > 0 do
484 Result
:= EventHandler(ev
);
485 if ev
.type_
= SDL_QUITEV
then exit
;
489 Time_Delta
:= Time
- Time_Old
;
493 if wNeedTimeReset
then
496 wNeedTimeReset
:= False;
499 t
:= Time_Delta
div 27777;
505 if NetMode
= NET_SERVER
then g_Net_Host_Update()
506 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
512 if NetMode
= NET_SERVER
then g_Net_Host_Update()
513 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
522 if gExit
= EXIT_QUIT
then
528 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ:
531 Time_Old
:= Time
- (Time_Delta
mod 27777);
532 if (not wMinimized
) then
545 procedure ReDrawWindow
;
551 procedure InitOpenGL(VSync
: Boolean);
555 if VSync
then v
:= 1 else v
:= 0;
556 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
557 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
558 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
559 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
560 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
561 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
562 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
563 SDL_GL_SetSwapInterval(v
);
566 function SDLMain(): Integer;
568 e_WriteLog('Creating GL window', MSG_NOTIFY
);
569 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION
]))) then
575 e_WriteLog('Initializing OpenGL', MSG_NOTIFY
);
578 {EnumDisplayModes();}
581 Time_Old
:= GetTimer();
584 if ParamCount
> 0 then
585 g_Game_Process_Params();
588 if (not gGameOn
) and gAskLanguage
then
589 g_Menu_AskLanguage();
591 e_WriteLog('Entering the main loop', MSG_NOTIFY
);
593 while not ProcessMessage() do