DEADSOFTWARE

c159ddd236d40221480036aa03394f51e41b51f9
[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;}
45 ticksOverflow: Int64 = -1;
46 lastTicks: Uint32 = 0; // to detect overflow
48 const
49 // TODO: move this to a separate file
50 CP1251: array [0..127] of Word = (
51 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
52 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
53 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
54 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
55 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
56 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
57 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
58 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
59 );
61 // TODO: make a transition table or something
62 function WCharToCP1251(wc: Word): Word;
63 var
64 n: Word;
65 begin
66 Result := 0;
67 for n := 0 to 127 do
68 if CP1251[n] = wc then begin Result := n; break end;
69 Result := Result + 128;
70 end;
72 function g_Window_SetDisplay(PreserveGL: Boolean = False): Boolean;
73 var
74 mode, cmode: TSDL_DisplayMode;
75 begin
76 {$IFDEF HEADLESS}
77 Result := True;
78 Exit;
79 {$ENDIF}
81 Result := False;
83 e_WriteLog('Setting display mode...', MSG_NOTIFY);
85 wFlags := SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE;
86 if gFullscreen then wFlags := wFlags or SDL_WINDOW_FULLSCREEN;
87 if gWinMaximized then wFlags := wFlags or SDL_WINDOW_MAXIMIZED;
89 if h_Wnd <> nil then
90 begin
91 SDL_DestroyWindow(h_Wnd);
92 h_Wnd := nil;
93 end;
95 if gFullscreen then
96 begin
97 mode.w := gScreenWidth;
98 mode.h := gScreenHeight;
99 mode.format := 0;
100 mode.refresh_rate := 0;
101 mode.driverdata := nil;
102 if SDL_GetClosestDisplayMode(0, @mode, @cmode) = nil then
103 begin
104 gScreenWidth := 800;
105 gScreenHeight := 600;
106 end
107 else
108 begin
109 gScreenWidth := cmode.w;
110 gScreenHeight := cmode.h;
111 end;
112 end;
114 h_Wnd := SDL_CreateWindow(PChar(wTitle), gWinRealPosX, gWinRealPosY, gScreenWidth, gScreenHeight, wFlags);
115 if h_Wnd = nil then Exit;
117 SDL_GL_MakeCurrent(h_Wnd, h_GL);
118 SDL_ShowCursor(SDL_DISABLE);
120 Result := True;
121 end;
123 procedure ReShowCursor();
124 begin
125 // TODO: what was this for?
126 end;
128 function GetDisplayModes(dBPP: DWORD; var SelRes: DWORD): SArray;
129 var
130 mode: TSDL_DisplayMode;
131 res, i, k, n, pw, ph: Integer;
132 begin
133 SetLength(Result, 0);
134 {$IFDEF HEADLESS}Exit;{$ENDIF}
135 k := 0; SelRes := 0;
136 n := SDL_GetNumDisplayModes(0);
137 pw := 0; ph := 0;
138 for i := 0 to n do
139 begin
140 res := SDL_GetDisplayMode(0, i, @mode);
141 if res < 0 then continue;
142 if SDL_BITSPERPIXEL(mode.format) = gBPP then continue;
143 if (mode.w = pw) and (mode.h = ph) then continue;
144 if (mode.w = gScreenWidth) and (mode.h = gScreenHeight) then
145 SelRes := k;
146 Inc(k);
147 SetLength(Result, k);
148 Result[k-1] := IntToStr(mode.w) + 'x' + IntToStr(mode.h);
149 pw := mode.w; ph := mode.h
150 end;
152 e_WriteLog('SDL: Got ' + IntToStr(k) + ' resolutions.', MSG_NOTIFY);
153 end;
155 procedure Sleep(ms: LongWord);
156 begin
157 SDL_Delay(ms);
158 end;
160 procedure ChangeWindowSize();
161 begin
162 gWinSizeX := gScreenWidth;
163 gWinSizeY := gScreenHeight;
164 {$IFDEF HEADLESS}Exit;{$ENDIF}
165 e_ResizeWindow(gScreenWidth, gScreenHeight);
166 g_Game_SetupScreenSize();
167 g_Menu_Reset();
168 g_Game_ClearLoading();
169 end;
171 function g_Window_SetSize(W, H: Word; FScreen: Boolean): Boolean;
172 var
173 Preserve: Boolean;
174 begin
175 Result := False;
176 {$IFDEF HEADLESS}Exit;{$ENDIF}
177 Preserve := False;
179 if (gScreenWidth <> W) or (gScreenHeight <> H) then
180 begin
181 Result := True;
182 gScreenWidth := W;
183 gScreenHeight := H;
184 end;
186 if gFullscreen <> FScreen then
187 begin
188 Result := True;
189 gFullscreen := FScreen;
190 Preserve := True;
191 end;
193 if Result then
194 begin
195 g_Window_SetDisplay(Preserve);
196 ChangeWindowSize();
197 end;
198 end;
200 function WindowEventHandler(ev: TSDL_WindowEvent): Boolean;
201 var
202 wActivate, wDeactivate: Boolean;
203 begin
204 Result := False;
205 wActivate := False;
206 wDeactivate := False;
208 case ev.event of
209 SDL_WINDOWEVENT_MOVED:
210 begin
211 if not (gFullscreen or gWinMaximized) then
212 begin
213 gWinRealPosX := ev.data1;
214 gWinRealPosY := ev.data2;
215 end;
216 end;
218 SDL_WINDOWEVENT_MINIMIZED:
219 begin
220 if not wMinimized then
221 begin
222 e_ResizeWindow(0, 0);
223 wMinimized := True;
225 if g_debug_WinMsgs then
226 begin
227 g_Console_Add('Now minimized');
228 e_WriteLog('[DEBUG] WinMsgs: Now minimized', MSG_NOTIFY);
229 end;
230 wDeactivate := True;
231 end;
232 end;
234 SDL_WINDOWEVENT_RESIZED:
235 begin
236 gScreenWidth := ev.data1;
237 gScreenHeight := ev.data2;
238 ChangeWindowSize();
239 SwapBuffers();
240 if g_debug_WinMsgs then
241 begin
242 g_Console_Add('Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2));
243 e_WriteLog('[DEBUG] WinMsgs: Resized to ' + IntToStr(ev.data1) + 'x' + IntToStr(ev.data2), MSG_NOTIFY);
244 end;
245 end;
247 SDL_WINDOWEVENT_EXPOSED:
248 SwapBuffers();
250 SDL_WINDOWEVENT_MAXIMIZED:
251 begin
252 if wMinimized then
253 begin
254 e_ResizeWindow(gScreenWidth, gScreenHeight);
255 wMinimized := False;
256 wActivate := True;
257 end;
258 if not gWinMaximized then
259 begin
260 gWinMaximized := True;
261 if g_debug_WinMsgs then
262 begin
263 g_Console_Add('Now maximized');
264 e_WriteLog('[DEBUG] WinMsgs: Now maximized', MSG_NOTIFY);
265 end;
266 end;
267 end;
269 SDL_WINDOWEVENT_RESTORED:
270 begin
271 if wMinimized then
272 begin
273 e_ResizeWindow(gScreenWidth, gScreenHeight);
274 wMinimized := False;
275 wActivate := True;
276 end;
277 if gWinMaximized then
278 gWinMaximized := False;
279 if g_debug_WinMsgs then
280 begin
281 g_Console_Add('Now restored');
282 e_WriteLog('[DEBUG] WinMsgs: Now restored', MSG_NOTIFY);
283 end;
284 end;
286 SDL_WINDOWEVENT_FOCUS_GAINED:
287 begin
288 wActivate := True;
289 //e_WriteLog('window gained focus!', MSG_NOTIFY);
290 end;
292 SDL_WINDOWEVENT_FOCUS_LOST:
293 begin
294 wDeactivate := True;
295 //e_WriteLog('window lost focus!', MSG_NOTIFY);
296 end;
297 end;
299 if wDeactivate then
300 begin
301 if gWinActive then
302 begin
303 e_WriteLog('deactivating window', MSG_NOTIFY);
304 e_EnableInput := False;
305 e_ClearInputBuffer();
307 if gMuteWhenInactive then
308 begin
309 //e_WriteLog('deactivating sounds', MSG_NOTIFY);
310 e_MuteChannels(True);
311 end;
313 if g_debug_WinMsgs then
314 begin
315 g_Console_Add('Now inactive');
316 e_WriteLog('[DEBUG] WinMsgs: Now inactive', MSG_NOTIFY);
317 end;
319 gWinActive := False;
320 end;
321 end
322 else if wActivate then
323 begin
324 if not gWinActive then
325 begin
326 //e_WriteLog('activating window', MSG_NOTIFY);
327 e_EnableInput := True;
329 if gMuteWhenInactive then
330 begin
331 //e_WriteLog('activating sounds', MSG_NOTIFY);
332 e_MuteChannels(False);
333 end;
335 if g_debug_WinMsgs then
336 begin
337 g_Console_Add('Now active');
338 e_WriteLog('[DEBUG] WinMsgs: Now active', MSG_NOTIFY);
339 end;
341 gWinActive := True;
342 end;
343 end;
344 end;
346 function EventHandler(ev: TSDL_Event): Boolean;
347 var
348 key, keychr: Word;
349 uc: UnicodeChar;
350 //joy: Integer;
351 begin
352 Result := False;
353 case ev.type_ of
354 SDL_WINDOWEVENT:
355 Result := WindowEventHandler(ev.window);
357 SDL_QUITEV:
358 begin
359 if gExit <> EXIT_QUIT then
360 begin
361 if not wLoadingProgress then
362 begin
363 g_Game_Free();
364 g_Game_Quit();
365 end
366 else
367 wLoadingQuit := True;
368 end;
369 Result := True;
370 end;
372 SDL_KEYDOWN:
373 begin
374 key := ev.key.keysym.scancode;
375 KeyPress(key);
376 end;
378 SDL_TEXTINPUT:
379 begin
380 Utf8ToUnicode(@uc, PChar(ev.text.text), 1);
381 keychr := Word(uc);
382 if (keychr > 127) then
383 keychr := WCharToCP1251(keychr);
384 CharPress(Chr(keychr));
385 end;
387 // other key presses and joysticks are handled in e_input
388 end;
389 end;
391 procedure SwapBuffers();
392 begin
393 {$IFDEF HEADLESS}Exit;{$ENDIF}
394 SDL_GL_SwapWindow(h_Wnd);
395 end;
397 procedure KillGLWindow();
398 begin
399 if h_Wnd <> nil then SDL_DestroyWindow(h_Wnd);
400 if h_GL <> nil then SDL_GL_DeleteContext(h_GL);
401 h_Wnd := nil;
402 h_GL := nil;
403 //wWindowCreated := False;
404 end;
406 function CreateGLWindow(Title: PChar): Boolean;
407 //var
408 // flags: LongWord;
409 begin
410 Result := False;
412 gWinSizeX := gScreenWidth;
413 gWinSizeY := gScreenHeight;
415 wTitle := Title;
416 e_WriteLog('Creating window', MSG_NOTIFY);
418 if not g_Window_SetDisplay() then
419 begin
420 KillGLWindow();
421 e_WriteLog('Window creation error (resolution not supported?)', MSG_FATALERROR);
422 exit;
423 end;
425 {$IFNDEF HEADLESS}
426 h_Gl := SDL_GL_CreateContext(h_Wnd);
427 if h_Gl = nil then Exit;
428 {$ENDIF}
429 //wWindowCreated := True;
431 e_ResizeWindow(gScreenWidth, gScreenHeight);
432 e_InitGL();
434 Result := True;
435 end;
437 function GetTimer(): Int64;
438 var
439 t: Uint32;
440 tt: Int64;
441 begin
442 t := SDL_GetTicks() {* 1000}; // TODO: do we really need microseconds here? k8: NOPE!
443 if ticksOverflow = -1 then
444 begin
445 ticksOverflow := 0;
446 lastTicks := t;
447 end
448 else
449 begin
450 if lastTicks > t then
451 begin
452 // overflow, increment overflow ;-)
453 ticksOverflow := ticksOverflow+(Int64($ffffffff)+Int64(1));
454 tt := (Int64($ffffffff)+Int64(1))+Int64(t);
455 t := Uint32(tt-lastTicks);
456 end;
457 end;
458 lastTicks := t;
459 result := ticksOverflow+Int64(t);
460 end;
462 procedure ResetTimer();
463 begin
464 wNeedTimeReset := True;
465 end;
467 procedure PushExitEvent();
468 var
469 ev: TSDL_Event;
470 begin
471 ev.type_ := SDL_QUITEV;
472 SDL_PushEvent(@ev);
473 end;
475 procedure ProcessLoading();
476 var
477 ev: TSDL_Event;
478 ID: DWORD;
479 begin
480 FillChar(ev, SizeOf(ev), 0);
481 //wNeedFree := False;
482 wLoadingProgress := True;
483 while SDL_PollEvent(@ev) > 0 do
484 begin
485 if (ev.type_ = SDL_QUITEV) then
486 break;
487 end;
488 //wNeedFree := True;
490 if (ev.type_ = SDL_QUITEV) or (gExit = EXIT_QUIT) then
491 begin
492 wLoadingProgress := False;
493 exit;
494 end;
496 if not wMinimized then
497 begin
498 if g_Texture_Get('INTER', ID) then
499 e_DrawSize(ID, 0, 0, 0, False, False, gScreenWidth, gScreenHeight)
500 else
501 e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0);
503 DrawLoadingStat();
504 SwapBuffers();
506 ReShowCursor();
507 end;
509 e_SoundUpdate();
511 if NetMode = NET_SERVER then
512 g_Net_Host_Update
513 else
514 if (NetMode = NET_CLIENT) and (NetState <> NET_STATE_AUTH) then
515 g_Net_Client_UpdateWhileLoading;
516 wLoadingProgress := False;
517 end;
519 function ProcessMessage(): Boolean;
520 var
521 i, t: Integer;
522 ev: TSDL_Event;
523 begin
524 Result := False;
525 FillChar(ev, SizeOf(ev), 0);
527 while SDL_PollEvent(@ev) > 0 do
528 begin
529 Result := EventHandler(ev);
530 if ev.type_ = SDL_QUITEV then exit;
531 end;
533 Time := GetTimer();
534 Time_Delta := Time - Time_Old;
536 flag := False;
538 if wNeedTimeReset then
539 begin
540 Time_Delta := (27777 div 1000);
541 wNeedTimeReset := False;
542 end;
544 t := Time_Delta div (27777 div 1000);
545 if t > 0 then
546 begin
547 flag := True;
548 for i := 1 to t do
549 begin
550 if NetMode = NET_SERVER then g_Net_Host_Update()
551 else if NetMode = NET_CLIENT then g_Net_Client_Update();
552 Update();
553 end;
554 end
555 else
556 begin
557 if NetMode = NET_SERVER then g_Net_Host_Update()
558 else if NetMode = NET_CLIENT then g_Net_Client_Update();
559 end;
561 if wLoadingQuit then
562 begin
563 g_Game_Free();
564 g_Game_Quit();
565 end;
567 if gExit = EXIT_QUIT then
568 begin
569 Result := True;
570 Exit;
571 end;
573 // Âðåìÿ ïðåäûäóùåãî îáíîâëåíèÿ:
574 if flag then
575 begin
576 Time_Old := Time - (Time_Delta mod (27777 div 1000));
577 if (not wMinimized) then
578 begin
579 Draw();
580 SwapBuffers();
581 ReShowCursor();
582 end;
583 end
584 else
585 Sleep(1);
587 e_SoundUpdate();
588 end;
590 procedure ReDrawWindow;
591 begin
592 SwapBuffers();
593 ReShowCursor();
594 end;
596 procedure InitOpenGL(VSync: Boolean);
597 var
598 v: Byte;
599 begin
600 {$IFDEF HEADLESS}Exit;{$ENDIF}
601 if VSync then v := 1 else v := 0;
602 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
603 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
604 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
605 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
606 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
607 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
608 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
609 SDL_GL_SetSwapInterval(v);
610 end;
612 function SDLMain(): Integer;
613 begin
614 {$IFDEF HEADLESS}
615 e_NoGraphics := True;
616 {$ENDIF}
618 e_WriteLog('Creating GL window', MSG_NOTIFY);
619 if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION]))) then
620 begin
621 Result := 0;
622 exit;
623 end;
625 e_WriteLog('Initializing OpenGL', MSG_NOTIFY);
626 InitOpenGL(gVSync);
628 {EnumDisplayModes();}
630 Init();
631 Time_Old := GetTimer();
633 // Êîìàíäíàÿ ñòðîêà:
634 if ParamCount > 0 then
635 g_Game_Process_Params();
637 // Çàïðîñ ÿçûêà:
638 if (not gGameOn) and gAskLanguage then
639 g_Menu_AskLanguage();
641 e_WriteLog('Entering the main loop', MSG_NOTIFY);
643 while not ProcessMessage() do
644 { Main Loop } ;
646 Release();
647 KillGLWindow();
649 Result := 0;
650 end;
652 end.