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 SDL
, 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
;
33 Time
, Time_Delta
, Time_Old
: Int64;
35 wNeedTimeReset
: Boolean = False;
36 wWindowCreated
: Boolean = False;
37 //wCursorShown: Boolean = False;
38 wMinimized
: Boolean = False;
39 //wNeedFree: Boolean = True;
40 wLoadingProgress
: Boolean = False;
41 wLoadingQuit
: Boolean = False;
42 {wWinPause: Byte = 0;}
45 // TODO: move this to a separate file
46 CP1251
: array [0..127] of Word = (
47 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
48 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
49 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
50 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
51 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
52 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
53 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
54 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
57 // TODO: make a transition table or something
58 function WCharToCP1251(wc
: Word): Word;
64 if CP1251
[n
] = wc
then begin Result
:= n
; break
end;
65 Result
:= Result
+ 128;
68 function g_Window_SetDisplay(PreserveGL
: Boolean = False): Boolean;
72 e_WriteLog('Setting display mode...', MSG_NOTIFY
);
74 if wWindowCreated
and PreserveGL
then
75 e_SaveGLContext(); // we need this and restore because of a bug in SDL1.2, apparently
77 wFlags
:= SDL_RESIZABLE
or SDL_OPENGL
;
78 if gFullscreen
then wFlags
:= wFlags
or SDL_FULLSCREEN
;
80 h_Wnd
:= SDL_SetVideoMode(gScreenWidth
, gScreenHeight
, gBPP
, wFlags
);
81 SDL_EnableUNICODE(SDL_ENABLE
);
82 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
83 SDL_ShowCursor(SDL_DISABLE
);
85 if wWindowCreated
and PreserveGL
then
88 Result
:= h_Wnd
<> nil;
91 procedure ReShowCursor();
93 // TODO: what was this for?
96 function GetDisplayModes(dBPP
: DWORD
; var SelRes
: DWORD
): SArray
;
103 SetLength(Result
, 0);
104 modesp
:= SDL_ListModes(nil, SDL_FULLSCREEN
or SDL_HWSURFACE
);
105 if modesp
= nil then exit
;
106 if Pointer(-1) = modesp
then exit
;
113 if (tmpr
.w
= gScreenWidth
) and (tmpr
.h
= gScreenHeight
) then
115 SetLength(Result
, Length(Result
) + 1);
116 Result
[i
] := IntToStr(tmpr
.w
) + 'x' + IntToStr(tmpr
.h
);
118 modesp
:= Pointer(Cardinal(modesp
) + SizeOf(PSDL_Rect
));
123 e_WriteLog('SDL: Got ' + IntToStr(Length(Result
)) + ' resolutions.', MSG_NOTIFY
);
126 procedure Sleep(ms
: LongWord);
131 procedure ChangeWindowSize();
133 gWinSizeX
:= gScreenWidth
;
134 gWinSizeY
:= gScreenHeight
;
135 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
136 g_Game_SetupScreenSize();
138 g_Game_ClearLoading();
141 function g_Window_SetSize(W
, H
: Word; FScreen
: Boolean): Boolean;
148 if (gScreenWidth
<> W
) or (gScreenHeight
<> H
) then
155 if gFullscreen
<> FScreen
then
158 gFullscreen
:= FScreen
;
164 g_Window_SetDisplay(Preserve
);
169 function EventHandler(ev
: TSDL_Event
): Boolean;
178 g_Window_SetSize(ev
.resize
.w
, ev
.resize
.h
, gFullscreen
);
184 if (ev
.active
.gain
= 0) then
186 if g_debug_WinMsgs
then
188 g_Console_Add('Inactive');
189 e_WriteLog('[DEBUG] WinMsgs: Inactive', MSG_NOTIFY
);
192 if LongBool(ev
.active
.state
and SDL_APPINPUTFOCUS
) and gWinActive
then
194 e_EnableInput
:= False;
195 e_ClearInputBuffer();
197 if gMuteWhenInactive
then
198 e_MuteChannels(True);
200 if g_debug_WinMsgs
then
202 g_Console_Add('Inactive indeed');
203 e_WriteLog('[DEBUG] WinMsgs: Inactive indeed', MSG_NOTIFY
);
209 if LongBool(ev
.active
.state
and SDL_APPACTIVE
) and (not wMinimized
) then
211 e_ResizeWindow(0, 0);
214 if g_debug_WinMsgs
then
216 g_Console_Add('Minimized indeed');
217 e_WriteLog('[DEBUG] WinMsgs: Minimized indeed', MSG_NOTIFY
);
223 if g_debug_WinMsgs
then
225 g_Console_Add('Active');
226 e_WriteLog('[DEBUG] WinMsgs: Active', MSG_NOTIFY
);
229 // Åñëè îêíî áûëî íåàêòèâíûì:
230 if LongBool(ev
.active
.state
and SDL_APPINPUTFOCUS
) and (not gWinActive
) then
232 e_EnableInput
:= True;
234 if gMuteWhenInactive
then
235 e_MuteChannels(False);
237 if g_debug_WinMsgs
then
239 g_Console_Add('Active indeed');
240 e_WriteLog('[DEBUG] WinMsgs: Active indeed', MSG_NOTIFY
);
246 if LongBool(ev
.active
.state
and SDL_APPACTIVE
) and wMinimized
then
248 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
252 if g_debug_WinMsgs
then
254 g_Console_Add('Restored indeed');
255 e_WriteLog('[DEBUG] WinMsgs: Restored indeed', MSG_NOTIFY
);
263 // TODO: the fuck is this event?
269 if gExit
<> EXIT_QUIT
then
271 if not wLoadingProgress
then
277 wLoadingQuit
:= True;
284 key
:= ev
.key
.keysym
.sym
;
285 keychr
:= ev
.key
.keysym
.unicode
;
287 if (keychr
> 7) and (key
<> IK_BACKSPACE
) then
289 if (keychr
>= 128) then
290 keychr
:= WCharToCP1251(keychr
);
291 CharPress(Chr(keychr
));
295 // key presses and joysticks are handled in e_input
299 procedure SwapBuffers();
301 SDL_GL_SwapBuffers();
304 procedure KillGLWindow();
306 wWindowCreated
:= False;
309 function CreateGLWindow(Title
: PChar): Boolean;
315 gWinSizeX
:= gScreenWidth
;
316 gWinSizeY
:= gScreenHeight
;
318 e_WriteLog('Creating window', MSG_NOTIFY
);
320 if not g_Window_SetDisplay() then
323 e_WriteLog('Window creation error (resolution not supported?)', MSG_FATALERROR
);
327 SDL_WM_SetCaption(Title
, Title
);
328 wWindowCreated
:= True;
330 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
336 function GetTimer(): Int64;
338 Result
:= SDL_GetTicks() * 1000; // TODO: do we really need microseconds here?
341 procedure ResetTimer();
343 wNeedTimeReset
:= True;
346 procedure PushExitEvent();
350 ev
.type_
:= SDL_QUITEV
;
354 procedure ProcessLoading();
359 FillChar(ev
, SizeOf(ev
), 0);
360 //wNeedFree := False;
361 wLoadingProgress
:= True;
362 while SDL_PollEvent(@ev
) > 0 do
364 if (ev
.type_
= SDL_QUITEV
) then
369 if (ev
.type_
= SDL_QUITEV
) or (gExit
= EXIT_QUIT
) then
371 wLoadingProgress
:= False;
375 if not wMinimized
then
377 if g_Texture_Get('INTER', ID
) then
378 e_DrawSize(ID
, 0, 0, 0, False, False, gScreenWidth
, gScreenHeight
)
380 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
390 if NetMode
= NET_SERVER
then
393 if (NetMode
= NET_CLIENT
) and (NetState
<> NET_STATE_AUTH
) then
394 g_Net_Client_UpdateWhileLoading
;
395 wLoadingProgress
:= False;
398 function ProcessMessage(): Boolean;
404 FillChar(ev
, SizeOf(ev
), 0);
406 while SDL_PollEvent(@ev
) > 0 do
408 Result
:= EventHandler(ev
);
409 if ev
.type_
= SDL_QUITEV
then exit
;
413 Time_Delta
:= Time
- Time_Old
;
417 if wNeedTimeReset
then
420 wNeedTimeReset
:= False;
423 t
:= Time_Delta
div 27777;
429 if NetMode
= NET_SERVER
then g_Net_Host_Update()
430 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
436 if NetMode
= NET_SERVER
then g_Net_Host_Update()
437 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
446 if gExit
= EXIT_QUIT
then
452 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ:
455 Time_Old
:= Time
- (Time_Delta
mod 27777);
456 if (not wMinimized
) then
469 procedure ReDrawWindow
;
475 procedure InitOpenGL(VSync
: Boolean);
479 if VSync
then v
:= 1 else v
:= 0;
480 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
481 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
482 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
483 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
484 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
485 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL
, v
);
488 function SDLMain(): Integer;
490 e_WriteLog('Creating GL window', MSG_NOTIFY
);
491 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION
]))) then
497 e_WriteLog('Initializing OpenGL', MSG_NOTIFY
);
500 {EnumDisplayModes();}
503 Time_Old
:= GetTimer();
506 if ParamCount
> 0 then
507 g_Game_Process_Params();
510 if (not gGameOn
) and gAskLanguage
then
511 g_Menu_AskLanguage();
513 e_WriteLog('Entering the main loop', MSG_NOTIFY
);
515 while not ProcessMessage() do