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 //e_WriteLog('window gained focus!', MSG_NOTIFY);
283 SDL_WINDOWEVENT_FOCUS_LOST
:
286 //e_WriteLog('window lost focus!', MSG_NOTIFY);
294 e_WriteLog('deactivating window', MSG_NOTIFY
);
295 e_EnableInput
:= False;
296 e_ClearInputBuffer();
298 if gMuteWhenInactive
then
300 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
301 e_MuteChannels(True);
304 if g_debug_WinMsgs
then
306 g_Console_Add('Now inactive');
307 e_WriteLog('[DEBUG] WinMsgs: Now inactive', MSG_NOTIFY
);
313 else if wActivate
then
315 if not gWinActive
then
317 //e_WriteLog('activating window', MSG_NOTIFY);
318 e_EnableInput
:= True;
320 if gMuteWhenInactive
then
322 //e_WriteLog('activating sounds', MSG_NOTIFY);
323 e_MuteChannels(False);
326 if g_debug_WinMsgs
then
328 g_Console_Add('Now active');
329 e_WriteLog('[DEBUG] WinMsgs: Now active', MSG_NOTIFY
);
337 function EventHandler(ev
: TSDL_Event
): Boolean;
346 Result
:= WindowEventHandler(ev
.window
);
350 if gExit
<> EXIT_QUIT
then
352 if not wLoadingProgress
then
358 wLoadingQuit
:= True;
365 key
:= ev
.key
.keysym
.scancode
;
371 Utf8ToUnicode(@uc
, PChar(ev
.text.text), 1);
373 if (keychr
> 127) then
374 keychr
:= WCharToCP1251(keychr
);
375 CharPress(Chr(keychr
));
378 // other key presses and joysticks are handled in e_input
382 procedure SwapBuffers();
384 SDL_GL_SwapWindow(h_Wnd
);
387 procedure KillGLWindow();
389 if h_Wnd
<> nil then SDL_DestroyWindow(h_Wnd
);
390 if h_GL
<> nil then SDL_GL_DeleteContext(h_GL
);
393 //wWindowCreated := False;
396 function CreateGLWindow(Title
: PChar): Boolean;
402 gWinSizeX
:= gScreenWidth
;
403 gWinSizeY
:= gScreenHeight
;
406 e_WriteLog('Creating window', MSG_NOTIFY
);
408 if not g_Window_SetDisplay() then
411 e_WriteLog('Window creation error (resolution not supported?)', MSG_FATALERROR
);
415 h_Gl
:= SDL_GL_CreateContext(h_Wnd
);
416 if h_Gl
= nil then Exit
;
418 //wWindowCreated := True;
420 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
426 function GetTimer(): Int64;
428 Result
:= SDL_GetTicks() {* 1000}; // TODO: do we really need microseconds here? k8: NOPE!
431 procedure ResetTimer();
433 wNeedTimeReset
:= True;
436 procedure PushExitEvent();
440 ev
.type_
:= SDL_QUITEV
;
444 procedure ProcessLoading();
449 FillChar(ev
, SizeOf(ev
), 0);
450 //wNeedFree := False;
451 wLoadingProgress
:= True;
452 while SDL_PollEvent(@ev
) > 0 do
454 if (ev
.type_
= SDL_QUITEV
) then
459 if (ev
.type_
= SDL_QUITEV
) or (gExit
= EXIT_QUIT
) then
461 wLoadingProgress
:= False;
465 if not wMinimized
then
467 if g_Texture_Get('INTER', ID
) then
468 e_DrawSize(ID
, 0, 0, 0, False, False, gScreenWidth
, gScreenHeight
)
470 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
480 if NetMode
= NET_SERVER
then
483 if (NetMode
= NET_CLIENT
) and (NetState
<> NET_STATE_AUTH
) then
484 g_Net_Client_UpdateWhileLoading
;
485 wLoadingProgress
:= False;
488 function ProcessMessage(): Boolean;
494 FillChar(ev
, SizeOf(ev
), 0);
496 while SDL_PollEvent(@ev
) > 0 do
498 Result
:= EventHandler(ev
);
499 if ev
.type_
= SDL_QUITEV
then exit
;
503 Time_Delta
:= Time
- Time_Old
;
507 if wNeedTimeReset
then
509 Time_Delta
:= (27777 div 1000);
510 wNeedTimeReset
:= False;
513 t
:= Time_Delta
div (27777 div 1000);
519 if NetMode
= NET_SERVER
then g_Net_Host_Update()
520 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
526 if NetMode
= NET_SERVER
then g_Net_Host_Update()
527 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
536 if gExit
= EXIT_QUIT
then
542 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ:
545 Time_Old
:= Time
- (Time_Delta
mod (27777 div 1000));
546 if (not wMinimized
) then
559 procedure ReDrawWindow
;
565 procedure InitOpenGL(VSync
: Boolean);
569 if VSync
then v
:= 1 else v
:= 0;
570 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
571 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
572 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
573 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
574 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
575 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
576 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
577 SDL_GL_SetSwapInterval(v
);
580 function SDLMain(): Integer;
582 e_WriteLog('Creating GL window', MSG_NOTIFY
);
583 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION
]))) then
589 e_WriteLog('Initializing OpenGL', MSG_NOTIFY
);
592 {EnumDisplayModes();}
595 Time_Old
:= GetTimer();
598 if ParamCount
> 0 then
599 g_Game_Process_Params();
602 if (not gGameOn
) and gAskLanguage
then
603 g_Menu_AskLanguage();
605 e_WriteLog('Entering the main loop', MSG_NOTIFY
);
607 while not ProcessMessage() do