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;
60 for Result
:= 0 to 127 do
61 if CP1251
[Result
] = wc
then
63 Result
:= Result
+ 128;
66 function g_Window_SetDisplay(PreserveGL
: Boolean = False): Boolean;
70 e_WriteLog('Setting display mode...', MSG_NOTIFY
);
72 if wWindowCreated
and PreserveGL
then
73 e_SaveGLContext(); // we need this and restore because of a bug in SDL1.2, apparently
75 wFlags
:= SDL_RESIZABLE
or SDL_OPENGL
;
76 if gFullscreen
then wFlags
:= wFlags
or SDL_FULLSCREEN
;
78 h_Wnd
:= SDL_SetVideoMode(gScreenWidth
, gScreenHeight
, gBPP
, wFlags
);
79 SDL_EnableUNICODE(SDL_ENABLE
);
80 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
81 SDL_ShowCursor(SDL_DISABLE
);
83 if wWindowCreated
and PreserveGL
then
86 Result
:= h_Wnd
<> nil;
89 procedure ReShowCursor();
91 // TODO: what was this for?
94 function GetDisplayModes(dBPP
: DWORD
; var SelRes
: DWORD
): SArray
;
101 SetLength(Result
, 0);
102 modesp
:= SDL_ListModes(nil, SDL_FULLSCREEN
or SDL_HWSURFACE
);
103 if modesp
= nil then exit
;
104 if Pointer(-1) = modesp
then exit
;
111 if (tmpr
.w
= gScreenWidth
) and (tmpr
.h
= gScreenHeight
) then
113 SetLength(Result
, Length(Result
) + 1);
114 Result
[i
] := IntToStr(tmpr
.w
) + 'x' + IntToStr(tmpr
.h
);
116 modesp
:= Pointer(Cardinal(modesp
) + SizeOf(PSDL_Rect
));
121 e_WriteLog('SDL: Got ' + IntToStr(Length(Result
)) + ' resolutions.', MSG_NOTIFY
);
124 procedure Sleep(ms
: LongWord);
129 procedure ChangeWindowSize();
131 gWinSizeX
:= gScreenWidth
;
132 gWinSizeY
:= gScreenHeight
;
133 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
134 g_Game_SetupScreenSize();
136 g_Game_ClearLoading();
139 function g_Window_SetSize(W
, H
: Word; FScreen
: Boolean): Boolean;
146 if (gScreenWidth
<> W
) or (gScreenHeight
<> H
) then
153 if gFullscreen
<> FScreen
then
156 gFullscreen
:= FScreen
;
162 g_Window_SetDisplay(Preserve
);
167 function EventHandler(ev
: TSDL_Event
): Boolean;
176 g_Window_SetSize(ev
.resize
.w
, ev
.resize
.h
, gFullscreen
);
182 if (ev
.active
.gain
= 0) then
184 if g_debug_WinMsgs
then
186 g_Console_Add('Inactive');
187 e_WriteLog('[DEBUG] WinMsgs: Inactive', MSG_NOTIFY
);
190 if LongBool(ev
.active
.state
and SDL_APPINPUTFOCUS
) and gWinActive
then
192 e_EnableInput
:= False;
193 e_ClearInputBuffer();
195 if gMuteWhenInactive
then
196 e_MuteChannels(True);
198 if g_debug_WinMsgs
then
200 g_Console_Add('Inactive indeed');
201 e_WriteLog('[DEBUG] WinMsgs: Inactive indeed', MSG_NOTIFY
);
207 if LongBool(ev
.active
.state
and SDL_APPACTIVE
) and (not wMinimized
) then
209 e_ResizeWindow(0, 0);
212 if g_debug_WinMsgs
then
214 g_Console_Add('Minimized indeed');
215 e_WriteLog('[DEBUG] WinMsgs: Minimized indeed', MSG_NOTIFY
);
221 if g_debug_WinMsgs
then
223 g_Console_Add('Active');
224 e_WriteLog('[DEBUG] WinMsgs: Active', MSG_NOTIFY
);
227 // Åñëè îêíî áûëî íåàêòèâíûì:
228 if LongBool(ev
.active
.state
and SDL_APPINPUTFOCUS
) and (not gWinActive
) then
230 e_EnableInput
:= True;
232 if gMuteWhenInactive
then
233 e_MuteChannels(False);
235 if g_debug_WinMsgs
then
237 g_Console_Add('Active indeed');
238 e_WriteLog('[DEBUG] WinMsgs: Active indeed', MSG_NOTIFY
);
244 if LongBool(ev
.active
.state
and SDL_APPACTIVE
) and wMinimized
then
246 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
250 if g_debug_WinMsgs
then
252 g_Console_Add('Restored indeed');
253 e_WriteLog('[DEBUG] WinMsgs: Restored indeed', MSG_NOTIFY
);
261 // TODO: the fuck is this event?
267 if gExit
<> EXIT_QUIT
then
269 if not wLoadingProgress
then
275 wLoadingQuit
:= True;
282 key
:= ev
.key
.keysym
.sym
;
283 keychr
:= ev
.key
.keysym
.unicode
;
285 if (keychr
> 7) and (key
<> IK_BACKSPACE
) then
287 if (keychr
>= 128) then
288 keychr
:= WCharToCP1251(keychr
);
289 CharPress(Chr(keychr
));
293 // key presses and joysticks are handled in e_input
297 procedure SwapBuffers();
299 SDL_GL_SwapBuffers();
302 procedure KillGLWindow();
304 wWindowCreated
:= False;
307 function CreateGLWindow(Title
: PChar): Boolean;
313 gWinSizeX
:= gScreenWidth
;
314 gWinSizeY
:= gScreenHeight
;
316 e_WriteLog('Creating window', MSG_NOTIFY
);
318 if not g_Window_SetDisplay() then
321 e_WriteLog('Window creation error (resolution not supported?)', MSG_FATALERROR
);
325 SDL_WM_SetCaption(Title
, Title
);
326 wWindowCreated
:= True;
328 e_ResizeWindow(gScreenWidth
, gScreenHeight
);
334 function GetTimer(): Int64;
336 Result
:= SDL_GetTicks() * 1000; // TODO: do we really need microseconds here?
339 procedure ResetTimer();
341 wNeedTimeReset
:= True;
344 procedure PushExitEvent();
348 ev
.type_
:= SDL_QUITEV
;
352 procedure ProcessLoading();
357 //wNeedFree := False;
358 wLoadingProgress
:= True;
359 while SDL_PollEvent(@ev
) > 0 do
361 if (ev
.type_
= SDL_QUITEV
) then
366 if (ev
.type_
= SDL_QUITEV
) or (gExit
= EXIT_QUIT
) then
368 wLoadingProgress
:= False;
372 if not wMinimized
then
374 if g_Texture_Get('INTER', ID
) then
375 e_DrawSize(ID
, 0, 0, 0, False, False, gScreenWidth
, gScreenHeight
)
377 e_Clear(GL_COLOR_BUFFER_BIT
, 0, 0, 0);
387 if NetMode
= NET_SERVER
then
390 if (NetMode
= NET_CLIENT
) and (NetState
<> NET_STATE_AUTH
) then
391 g_Net_Client_UpdateWhileLoading
;
392 wLoadingProgress
:= False;
395 function ProcessMessage(): Boolean;
402 while SDL_PollEvent(@ev
) > 0 do
404 Result
:= EventHandler(ev
);
405 if ev
.type_
= SDL_QUITEV
then exit
;
409 Time_Delta
:= Time
- Time_Old
;
413 if wNeedTimeReset
then
416 wNeedTimeReset
:= False;
419 t
:= Time_Delta
div 27777;
425 if NetMode
= NET_SERVER
then g_Net_Host_Update()
426 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
432 if NetMode
= NET_SERVER
then g_Net_Host_Update()
433 else if NetMode
= NET_CLIENT
then g_Net_Client_Update();
442 if gExit
= EXIT_QUIT
then
448 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ:
451 Time_Old
:= Time
- (Time_Delta
mod 27777);
452 if (not wMinimized
) then
465 procedure ReDrawWindow
;
471 procedure InitOpenGL(VSync
: Boolean);
475 if VSync
then v
:= 1 else v
:= 0;
476 SDL_GL_SetAttribute(SDL_GL_RED_SIZE
, 8);
477 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE
, 8);
478 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE
, 8);
479 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE
, 16);
480 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
481 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL
, v
);
484 function SDLMain(): Integer;
486 e_WriteLog('Creating GL window', MSG_NOTIFY
);
487 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION
]))) then
493 e_WriteLog('Initializing OpenGL', MSG_NOTIFY
);
496 {EnumDisplayModes();}
499 Time_Old
:= GetTimer();
502 if ParamCount
> 0 then
503 g_Game_Process_Params();
506 if (not gGameOn
) and gAskLanguage
then
507 g_Menu_AskLanguage();
509 e_WriteLog('Entering the main loop', MSG_NOTIFY
);
511 while not ProcessMessage() do