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;
74 e_WriteLog('Setting display mode...', MSG_NOTIFY
);
76 // if wWindowCreated and PreserveGL then
77 // e_SaveGLContext(); // we need this and restore because of a bug in SDL1.2, apparently
79 wFlags
:= SDL_WINDOW_OPENGL
or SDL_WINDOW_RESIZABLE
;
80 if gFullscreen
then wFlags
:= wFlags
or SDL_WINDOW_FULLSCREEN
;
81 if gWinMaximized
then wFlags
:= wFlags
or SDL_WINDOW_MAXIMIZED
;
85 SDL_DestroyWindow(h_Wnd
);
89 h_Wnd
:= SDL_CreateWindow(PChar(wTitle
), gWinRealPosX
, gWinRealPosY
, gScreenWidth
, gScreenHeight
, wFlags
);
90 if h_Wnd
= nil then Exit
;
92 SDL_GL_MakeCurrent(h_Wnd
, h_GL
);
93 SDL_ShowCursor(SDL_DISABLE
);
95 // if wWindowCreated and PreserveGL then
96 // e_RestoreGLContext();
101 procedure ReShowCursor();
103 // TODO: what was this for?
106 function GetDisplayModes(dBPP
: DWORD
; var SelRes
: DWORD
): SArray
;
108 mode
: TSDL_DisplayMode
;
111 SetLength(Result
, 0);
114 for i
:= 0 to SDL_GetNumDisplayModes(0) do
116 res
:= SDL_GetDisplayMode(0, i
, @mode
);
117 if res
< 0 then continue
;
118 if (mode
.w
= gScreenWidth
) and (mode
.h
= gScreenHeight
) then
121 SetLength(Result
, k
);
122 Result
[k
-1] := IntToStr(mode
.w
) + 'x' + IntToStr(mode
.h
);
125 e_WriteLog('SDL: Got ' + IntToStr(k
) + ' resolutions.', MSG_NOTIFY
);
128 procedure Sleep(ms
: LongWord);
133 procedure ChangeWindowSize();
135 gWinSizeX
:= gScreenWidth
;
136 gWinSizeY
:= gScreenHeight
;
137 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
138 g_Game_SetupScreenSize();
140 g_Game_ClearLoading();
143 function g_Window_SetSize(W
, H
: Word; FScreen
: Boolean): Boolean;
150 if (gScreenWidth
<> W
) or (gScreenHeight
<> H
) then
157 if gFullscreen
<> FScreen
then
160 gFullscreen
:= FScreen
;
166 g_Window_SetDisplay(Preserve
);
171 function WindowEventHandler(ev
: TSDL_WindowEvent
): Boolean;
173 wActivate
, wDeactivate
: Boolean;
177 wDeactivate
:= False;
180 SDL_WINDOWEVENT_MOVED
:
182 if not (gFullscreen
or gWinMaximized
) then
184 gWinRealPosX
:= ev
.data1
;
185 gWinRealPosY
:= ev
.data2
;
189 SDL_WINDOWEVENT_MINIMIZED
:
191 if not wMinimized
then
193 e_ResizeWindow(0, 0);
196 if g_debug_WinMsgs
then
198 g_Console_Add('Now minimized');
199 e_WriteLog('[DEBUG] WinMsgs: Now minimized', MSG_NOTIFY
);
205 SDL_WINDOWEVENT_RESIZED
:
207 gScreenWidth
:= ev
.data1
;
208 gScreenHeight
:= ev
.data2
;
211 if g_debug_WinMsgs
then
213 g_Console_Add('Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
));
214 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev
.data1
) + 'x' + IntToStr(ev
.data2
), MSG_NOTIFY
);
218 SDL_WINDOWEVENT_EXPOSED
:
221 SDL_WINDOWEVENT_MAXIMIZED
:
225 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
229 if not gWinMaximized
then
231 gWinMaximized
:= True;
232 if g_debug_WinMsgs
then
234 g_Console_Add('Now maximized');
235 e_WriteLog('[DEBUG] WinMsgs: Now maximized', MSG_NOTIFY
);
240 SDL_WINDOWEVENT_RESTORED
:
244 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
248 if gWinMaximized
then
249 gWinMaximized
:= False;
250 if g_debug_WinMsgs
then
252 g_Console_Add('Now restored');
253 e_WriteLog('[DEBUG] WinMsgs: Now restored', MSG_NOTIFY
);
257 SDL_WINDOWEVENT_FOCUS_GAINED
:
260 SDL_WINDOWEVENT_FOCUS_LOST
:
268 e_EnableInput
:= False;
269 e_ClearInputBuffer();
271 if gMuteWhenInactive
then
272 e_MuteChannels(True);
274 if g_debug_WinMsgs
then
276 g_Console_Add('Now inactive');
277 e_WriteLog('[DEBUG] WinMsgs: Now inactive', MSG_NOTIFY
);
283 else if wActivate
then
285 if not gWinActive
then
287 e_EnableInput
:= True;
289 if gMuteWhenInactive
then
290 e_MuteChannels(False);
292 if g_debug_WinMsgs
then
294 g_Console_Add('Now active');
295 e_WriteLog('[DEBUG] WinMsgs: Now active', MSG_NOTIFY
);
303 function EventHandler(ev
: TSDL_Event
): Boolean;
312 Result
:= WindowEventHandler(ev
.window
);
316 if gExit
<> EXIT_QUIT
then
318 if not wLoadingProgress
then
324 wLoadingQuit
:= True;
331 key
:= ev
.key
.keysym
.scancode
;
337 Utf8ToUnicode(@uc
, PChar(ev
.text.text), 1);
339 if (keychr
> 127) then
340 keychr
:= WCharToCP1251(keychr
);
341 CharPress(Chr(keychr
));
344 // other key presses and joysticks are handled in e_input
348 procedure SwapBuffers();
350 SDL_GL_SwapWindow(h_Wnd
);
353 procedure KillGLWindow();
355 if h_Wnd
<> nil then SDL_DestroyWindow(h_Wnd
);
356 if h_GL
<> nil then SDL_GL_DeleteContext(h_GL
);
359 wWindowCreated
:= False;
362 function CreateGLWindow(Title
: PChar): Boolean;
368 gWinSizeX
:= gScreenWidth
;
369 gWinSizeY
:= gScreenHeight
;
372 e_WriteLog('Creating window', MSG_NOTIFY
);
374 if not g_Window_SetDisplay() then
377 e_WriteLog('Window creation error (resolution not supported?)', MSG_FATALERROR
);
381 h_Gl
:= SDL_GL_CreateContext(h_Wnd
);
382 if h_Gl
= nil then Exit
;
384 wWindowCreated
:= True;
386 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
392 function GetTimer(): Int64;
394 Result
:= SDL_GetTicks() * 1000; // TODO: do we really need microseconds here?
397 procedure ResetTimer();
399 wNeedTimeReset
:= True;
402 procedure PushExitEvent();
406 ev
.type_
:= SDL_QUITEV
;
410 procedure ProcessLoading();
415 FillChar(ev
, SizeOf(ev
), 0);
416 //wNeedFree := False;
417 wLoadingProgress
:= True;
418 while SDL_PollEvent(@ev
) > 0 do
420 if (ev
.type_
= SDL_QUITEV
) then
425 if (ev
.type_
= SDL_QUITEV
) or (gExit
= EXIT_QUIT
) then
427 wLoadingProgress
:= False;
431 if not wMinimized
then
433 if g_Texture_Get('INTER', ID
) then
434 e_DrawSize(ID
, 0, 0, 0, False, False, gScreenWidth
, gScreenHeight
)
436 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
446 if NetMode
= NET_SERVER
then
449 if (NetMode
= NET_CLIENT
) and (NetState
<> NET_STATE_AUTH
) then
450 g_Net_Client_UpdateWhileLoading
;
451 wLoadingProgress
:= False;
454 function ProcessMessage(): Boolean;
460 FillChar(ev
, SizeOf(ev
), 0);
462 while SDL_PollEvent(@ev
) > 0 do
464 Result
:= EventHandler(ev
);
465 if ev
.type_
= SDL_QUITEV
then exit
;
469 Time_Delta
:= Time
- Time_Old
;
473 if wNeedTimeReset
then
476 wNeedTimeReset
:= False;
479 t
:= Time_Delta
div 27777;
485 if NetMode
= NET_SERVER
then g_Net_Host_Update()
486 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
492 if NetMode
= NET_SERVER
then g_Net_Host_Update()
493 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
502 if gExit
= EXIT_QUIT
then
508 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ:
511 Time_Old
:= Time
- (Time_Delta
mod 27777);
512 if (not wMinimized
) then
525 procedure ReDrawWindow
;
531 procedure InitOpenGL(VSync
: Boolean);
535 if VSync
then v
:= 1 else v
:= 0;
536 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
537 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
538 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
539 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
540 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
541 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
542 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
543 SDL_GL_SetSwapInterval(v
);
546 function SDLMain(): Integer;
548 e_WriteLog('Creating GL window', MSG_NOTIFY
);
549 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION
]))) then
555 e_WriteLog('Initializing OpenGL', MSG_NOTIFY
);
558 {EnumDisplayModes();}
561 Time_Old
:= GetTimer();
564 if ParamCount
> 0 then
565 g_Game_Process_Params();
568 if (not gGameOn
) and gAskLanguage
then
569 g_Menu_AskLanguage();
571 e_WriteLog('Entering the main loop', MSG_NOTIFY
);
573 while not ProcessMessage() do