DEADSOFTWARE

419e135299da48568c51fe8974a8a2389016c0c6
[d2df-sdl.git] / src / game / g_window.pas
1 unit g_window;
3 interface
5 uses
6 WADEDITOR;
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;
23 implementation
25 uses
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;
30 var
31 h_Wnd: PSDL_Window;
32 h_GL: TSDL_GLContext;
33 wFlags: LongWord = 0;
34 Time, Time_Delta, Time_Old: Int64;
35 flag: Boolean;
36 wTitle: PChar = nil;
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;}
46 const
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
57 );
59 // TODO: make a transition table or something
60 function WCharToCP1251(wc: Word): Word;
61 var
62 n: Word;
63 begin
64 Result := 0;
65 for n := 0 to 127 do
66 if CP1251[n] = wc then begin Result := n; break end;
67 Result := Result + 128;
68 end;
70 function g_Window_SetDisplay(PreserveGL: Boolean = False): Boolean;
71 var
72 mode, cmode: TSDL_DisplayMode;
73 begin
74 Result := False;
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;
82 if h_Wnd <> nil then
83 begin
84 SDL_DestroyWindow(h_Wnd);
85 h_Wnd := nil;
86 end;
88 if gFullscreen then
89 begin
90 mode.w := gScreenWidth;
91 mode.h := gScreenHeight;
92 mode.format := 0;
93 mode.refresh_rate := 0;
94 mode.driverdata := nil;
95 if SDL_GetClosestDisplayMode(0, @mode, @cmode) = nil then
96 begin
97 gScreenWidth := 800;
98 gScreenHeight := 600;
99 end
100 else
101 begin
102 gScreenWidth := cmode.w;
103 gScreenHeight := cmode.h;
104 end;
105 end;
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);
113 Result := True;
114 end;
116 procedure ReShowCursor();
117 begin
118 // TODO: what was this for?
119 end;
121 function GetDisplayModes(dBPP: DWORD; var SelRes: DWORD): SArray;
122 var
123 mode: TSDL_DisplayMode;
124 res, i, k, n, pw, ph: Integer;
125 begin
126 SetLength(Result, 0);
128 k := 0; SelRes := 0;
129 n := SDL_GetNumDisplayModes(0);
130 pw := 0; ph := 0;
131 for i := 0 to n do
132 begin
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
138 SelRes := k;
139 Inc(k);
140 SetLength(Result, k);
141 Result[k-1] := IntToStr(mode.w) + 'x' + IntToStr(mode.h);
142 pw := mode.w; ph := mode.h
143 end;
145 e_WriteLog('SDL: Got ' + IntToStr(k) + ' resolutions.', MSG_NOTIFY);
146 end;
148 procedure Sleep(ms: LongWord);
149 begin
150 SDL_Delay(ms);
151 end;
153 procedure ChangeWindowSize();
154 begin
155 gWinSizeX := gScreenWidth;
156 gWinSizeY := gScreenHeight;
157 e_ResizeWindow(gScreenWidth, gScreenHeight);
158 g_Game_SetupScreenSize();
159 g_Menu_Reset();
160 g_Game_ClearLoading();
161 end;
163 function g_Window_SetSize(W, H: Word; FScreen: Boolean): Boolean;
164 var
165 Preserve: Boolean;
166 begin
167 Result := False;
168 Preserve := False;
170 if (gScreenWidth <> W) or (gScreenHeight <> H) then
171 begin
172 Result := True;
173 gScreenWidth := W;
174 gScreenHeight := H;
175 end;
177 if gFullscreen <> FScreen then
178 begin
179 Result := True;
180 gFullscreen := FScreen;
181 Preserve := True;
182 end;
184 if Result then
185 begin
186 g_Window_SetDisplay(Preserve);
187 ChangeWindowSize();
188 end;
189 end;
191 function WindowEventHandler(ev: TSDL_WindowEvent): Boolean;
192 var
193 wActivate, wDeactivate: Boolean;
194 begin
195 Result := False;
196 wActivate := False;
197 wDeactivate := False;
199 case ev.event of
200 SDL_WINDOWEVENT_MOVED:
201 begin
202 if not (gFullscreen or gWinMaximized) then
203 begin
204 gWinRealPosX := ev.data1;
205 gWinRealPosY := ev.data2;
206 end;
207 end;
209 SDL_WINDOWEVENT_MINIMIZED:
210 begin
211 if not wMinimized then
212 begin
213 e_ResizeWindow(0, 0);
214 wMinimized := True;
216 if g_debug_WinMsgs then
217 begin
218 g_Console_Add('Now minimized');
219 e_WriteLog('[DEBUG] WinMsgs: Now minimized', MSG_NOTIFY);
220 end;
221 wDeactivate := True;
222 end;
223 end;
225 SDL_WINDOWEVENT_RESIZED:
226 begin
227 gScreenWidth := ev.data1;
228 gScreenHeight := ev.data2;
229 ChangeWindowSize();
230 SwapBuffers();
231 if g_debug_WinMsgs then
232 begin
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);
235 end;
236 end;
238 SDL_WINDOWEVENT_EXPOSED:
239 SwapBuffers();
241 SDL_WINDOWEVENT_MAXIMIZED:
242 begin
243 if wMinimized then
244 begin
245 e_ResizeWindow(gScreenWidth, gScreenHeight);
246 wMinimized := False;
247 wActivate := True;
248 end;
249 if not gWinMaximized then
250 begin
251 gWinMaximized := True;
252 if g_debug_WinMsgs then
253 begin
254 g_Console_Add('Now maximized');
255 e_WriteLog('[DEBUG] WinMsgs: Now maximized', MSG_NOTIFY);
256 end;
257 end;
258 end;
260 SDL_WINDOWEVENT_RESTORED:
261 begin
262 if wMinimized then
263 begin
264 e_ResizeWindow(gScreenWidth, gScreenHeight);
265 wMinimized := False;
266 wActivate := True;
267 end;
268 if gWinMaximized then
269 gWinMaximized := False;
270 if g_debug_WinMsgs then
271 begin
272 g_Console_Add('Now restored');
273 e_WriteLog('[DEBUG] WinMsgs: Now restored', MSG_NOTIFY);
274 end;
275 end;
277 SDL_WINDOWEVENT_FOCUS_GAINED:
278 begin
279 wActivate := True;
280 //e_WriteLog('window gained focus!', MSG_NOTIFY);
281 end;
283 SDL_WINDOWEVENT_FOCUS_LOST:
284 begin
285 wDeactivate := True;
286 //e_WriteLog('window lost focus!', MSG_NOTIFY);
287 end;
288 end;
290 if wDeactivate then
291 begin
292 if gWinActive then
293 begin
294 e_WriteLog('deactivating window', MSG_NOTIFY);
295 e_EnableInput := False;
296 e_ClearInputBuffer();
298 if gMuteWhenInactive then
299 begin
300 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
301 e_MuteChannels(True);
302 end;
304 if g_debug_WinMsgs then
305 begin
306 g_Console_Add('Now inactive');
307 e_WriteLog('[DEBUG] WinMsgs: Now inactive', MSG_NOTIFY);
308 end;
310 gWinActive := False;
311 end;
312 end
313 else if wActivate then
314 begin
315 if not gWinActive then
316 begin
317 //e_WriteLog('activating window', MSG_NOTIFY);
318 e_EnableInput := True;
320 if gMuteWhenInactive then
321 begin
322 //e_WriteLog('activating sounds', MSG_NOTIFY);
323 e_MuteChannels(False);
324 end;
326 if g_debug_WinMsgs then
327 begin
328 g_Console_Add('Now active');
329 e_WriteLog('[DEBUG] WinMsgs: Now active', MSG_NOTIFY);
330 end;
332 gWinActive := True;
333 end;
334 end;
335 end;
337 function EventHandler(ev: TSDL_Event): Boolean;
338 var
339 key, keychr: Word;
340 uc: UnicodeChar;
341 //joy: Integer;
342 begin
343 Result := False;
344 case ev.type_ of
345 SDL_WINDOWEVENT:
346 Result := WindowEventHandler(ev.window);
348 SDL_QUITEV:
349 begin
350 if gExit <> EXIT_QUIT then
351 begin
352 if not wLoadingProgress then
353 begin
354 g_Game_Free();
355 g_Game_Quit();
356 end
357 else
358 wLoadingQuit := True;
359 end;
360 Result := True;
361 end;
363 SDL_KEYDOWN:
364 begin
365 key := ev.key.keysym.scancode;
366 KeyPress(key);
367 end;
369 SDL_TEXTINPUT:
370 begin
371 Utf8ToUnicode(@uc, PChar(ev.text.text), 1);
372 keychr := Word(uc);
373 if (keychr > 127) then
374 keychr := WCharToCP1251(keychr);
375 CharPress(Chr(keychr));
376 end;
378 // other key presses and joysticks are handled in e_input
379 end;
380 end;
382 procedure SwapBuffers();
383 begin
384 SDL_GL_SwapWindow(h_Wnd);
385 end;
387 procedure KillGLWindow();
388 begin
389 if h_Wnd <> nil then SDL_DestroyWindow(h_Wnd);
390 if h_GL <> nil then SDL_GL_DeleteContext(h_GL);
391 h_Wnd := nil;
392 h_GL := nil;
393 //wWindowCreated := False;
394 end;
396 function CreateGLWindow(Title: PChar): Boolean;
397 //var
398 // flags: LongWord;
399 begin
400 Result := False;
402 gWinSizeX := gScreenWidth;
403 gWinSizeY := gScreenHeight;
405 wTitle := Title;
406 e_WriteLog('Creating window', MSG_NOTIFY);
408 if not g_Window_SetDisplay() then
409 begin
410 KillGLWindow();
411 e_WriteLog('Window creation error (resolution not supported?)', MSG_FATALERROR);
412 exit;
413 end;
415 h_Gl := SDL_GL_CreateContext(h_Wnd);
416 if h_Gl = nil then Exit;
418 //wWindowCreated := True;
420 e_ResizeWindow(gScreenWidth, gScreenHeight);
421 e_InitGL();
423 Result := True;
424 end;
426 function GetTimer(): Int64;
427 begin
428 Result := SDL_GetTicks() {* 1000}; // TODO: do we really need microseconds here? k8: NOPE!
429 end;
431 procedure ResetTimer();
432 begin
433 wNeedTimeReset := True;
434 end;
436 procedure PushExitEvent();
437 var
438 ev: TSDL_Event;
439 begin
440 ev.type_ := SDL_QUITEV;
441 SDL_PushEvent(@ev);
442 end;
444 procedure ProcessLoading();
445 var
446 ev: TSDL_Event;
447 ID: DWORD;
448 begin
449 FillChar(ev, SizeOf(ev), 0);
450 //wNeedFree := False;
451 wLoadingProgress := True;
452 while SDL_PollEvent(@ev) > 0 do
453 begin
454 if (ev.type_ = SDL_QUITEV) then
455 break;
456 end;
457 //wNeedFree := True;
459 if (ev.type_ = SDL_QUITEV) or (gExit = EXIT_QUIT) then
460 begin
461 wLoadingProgress := False;
462 exit;
463 end;
465 if not wMinimized then
466 begin
467 if g_Texture_Get('INTER', ID) then
468 e_DrawSize(ID, 0, 0, 0, False, False, gScreenWidth, gScreenHeight)
469 else
470 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
472 DrawLoadingStat();
473 SwapBuffers();
475 ReShowCursor();
476 end;
478 e_SoundUpdate();
480 if NetMode = NET_SERVER then
481 g_Net_Host_Update
482 else
483 if (NetMode = NET_CLIENT) and (NetState <> NET_STATE_AUTH) then
484 g_Net_Client_UpdateWhileLoading;
485 wLoadingProgress := False;
486 end;
488 function ProcessMessage(): Boolean;
489 var
490 i, t: Integer;
491 ev: TSDL_Event;
492 begin
493 Result := False;
494 FillChar(ev, SizeOf(ev), 0);
496 while SDL_PollEvent(@ev) > 0 do
497 begin
498 Result := EventHandler(ev);
499 if ev.type_ = SDL_QUITEV then exit;
500 end;
502 Time := GetTimer();
503 Time_Delta := Time - Time_Old;
505 flag := False;
507 if wNeedTimeReset then
508 begin
509 Time_Delta := (27777 div 1000);
510 wNeedTimeReset := False;
511 end;
513 t := Time_Delta div (27777 div 1000);
514 if t > 0 then
515 begin
516 flag := True;
517 for i := 1 to t do
518 begin
519 if NetMode = NET_SERVER then g_Net_Host_Update()
520 else if NetMode = NET_CLIENT then g_Net_Client_Update();
521 Update();
522 end;
523 end
524 else
525 begin
526 if NetMode = NET_SERVER then g_Net_Host_Update()
527 else if NetMode = NET_CLIENT then g_Net_Client_Update();
528 end;
530 if wLoadingQuit then
531 begin
532 g_Game_Free();
533 g_Game_Quit();
534 end;
536 if gExit = EXIT_QUIT then
537 begin
538 Result := True;
539 Exit;
540 end;
542 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ:
543 if flag then
544 begin
545 Time_Old := Time - (Time_Delta mod (27777 div 1000));
546 if (not wMinimized) then
547 begin
548 Draw();
549 SwapBuffers();
550 ReShowCursor();
551 end;
552 end
553 else
554 Sleep(1);
556 e_SoundUpdate();
557 end;
559 procedure ReDrawWindow;
560 begin
561 SwapBuffers();
562 ReShowCursor();
563 end;
565 procedure InitOpenGL(VSync: Boolean);
566 var
567 v: Byte;
568 begin
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);
578 end;
580 function SDLMain(): Integer;
581 begin
582 e_WriteLog('Creating GL window', MSG_NOTIFY);
583 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION]))) then
584 begin
585 Result := 0;
586 exit;
587 end;
589 e_WriteLog('Initializing OpenGL', MSG_NOTIFY);
590 InitOpenGL(gVSync);
592 {EnumDisplayModes();}
594 Init();
595 Time_Old := GetTimer();
597 // Êîìàíäíàÿ ñòðîêà:
598 if ParamCount > 0 then
599 g_Game_Process_Params();
601 // Çàïðîñ ÿçûêà:
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
608 { Main Loop } ;
610 Release();
611 KillGLWindow();
613 Result := 0;
614 end;
616 end.