DEADSOFTWARE

experiments with fullscreen switching -- failed, but i left the commented code for...
[d2df-sdl.git] / src / game / g_window.pas
index 8a23d00c53283fdd5bc98233fcf1a5630e6de042..ab8df098b0be9534e5cec7279e0aa9533c9d0539 100644 (file)
@@ -1,4 +1,4 @@
-(* Copyright (C)  DooM 2D:Forever Developers
+(* Copyright (C)  Doom 2D: Forever Developers
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -24,8 +24,6 @@ uses
 function SDLMain (): Integer;
 function GetTimer (): Int64;
 procedure ResetTimer ();
-function CreateGLWindow (Title: PChar): Boolean;
-procedure KillGLWindow ();
 procedure PushExitEvent ();
 function ProcessMessage (): Boolean;
 procedure ReDrawWindow ();
@@ -37,6 +35,9 @@ function g_Window_SetSize (w, h: Word; fullscreen: Boolean): Boolean;
 
 procedure ProcessLoading (forceUpdate: Boolean=false);
 
+// returns `true` if quit event was received
+function g_ProcessMessages (): Boolean;
+
 
 var
   gwin_dump_extensions: Boolean = false;
@@ -54,11 +55,11 @@ uses
   g_console, e_input, g_options, g_game,
   g_basic, g_textures, e_sound, g_sound, g_menu, ENet, g_net,
   g_map, g_gfx, g_monsters, g_holmes, xprofiler,
-  sdlcarcass, gh_ui;
+  sdlcarcass, fui_ctls;
 
 
 const
-  ProgressUpdateMSecs = 100;
+  ProgressUpdateMSecs = 1;//100;
 
 var
   h_Wnd: PSDL_Window = nil;
@@ -78,16 +79,13 @@ var
 {$ENDIF}
 
 
-procedure KillGLWindow ();
+procedure KillGLWindow (preserveGL: Boolean);
 begin
-  if (h_Wnd <> nil) then
-  begin
-    if assigned(oglDeinitCB) then oglDeinitCB();
-  end;
+  if (h_GL <> nil) and (not preserveGL) then begin if (assigned(oglDeinitCB)) then oglDeinitCB(); end;
   if (h_Wnd <> nil) then SDL_DestroyWindow(h_Wnd);
-  if (h_GL <> nil) then SDL_GL_DeleteContext(h_GL);
+  if (h_GL <> nil) and (not preserveGL) then SDL_GL_DeleteContext(h_GL);
   h_Wnd := nil;
-  h_GL := nil;
+  if (not preserveGL) then h_GL := nil;
 end;
 
 
@@ -96,6 +94,7 @@ function g_Window_SetDisplay (preserveGL: Boolean = false): Boolean;
 var
   mode, cmode: TSDL_DisplayMode;
   wFlags: LongWord = 0;
+  nw, nh: Integer;
 {$ENDIF}
 begin
 {$IF not DEFINED(HEADLESS)}
@@ -103,11 +102,9 @@ begin
 
   e_WriteLog('Setting display mode...', TMsgType.Notify);
 
-  wFlags := SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE;
-  if gFullscreen then wFlags := wFlags or SDL_WINDOW_FULLSCREEN;
-  if gWinMaximized then wFlags := wFlags or SDL_WINDOW_MAXIMIZED;
-
-  KillGLWindow();
+  wFlags := SDL_WINDOW_OPENGL {or SDL_WINDOW_RESIZABLE};
+  if gFullscreen then wFlags := wFlags or SDL_WINDOW_FULLSCREEN else wFlags := wFlags or SDL_WINDOW_RESIZABLE;
+  if (not gFullscreen) and gWinMaximized then wFlags := wFlags or SDL_WINDOW_MAXIMIZED;
 
   if gFullscreen then
   begin
@@ -118,25 +115,61 @@ begin
     mode.driverdata := nil;
     if (SDL_GetClosestDisplayMode(0, @mode, @cmode) = nil) then
     begin
+      e_WriteLog('SDL: cannot find display mode for '+IntToStr(gScreenWidth), TMsgType.Notify);
       gScreenWidth := 800;
       gScreenHeight := 600;
     end
     else
     begin
+      e_WriteLog('SDL: found display mode for '+IntToStr(gScreenWidth)+'x'+IntToStr(gScreenHeight)+': '+IntToStr(cmode.w)+'x'+IntToStr(cmode.h), TMsgType.Notify);
       gScreenWidth := cmode.w;
       gScreenHeight := cmode.h;
     end;
   end;
 
-  h_Wnd := SDL_CreateWindow(PChar(wTitle), gWinRealPosX, gWinRealPosY, gScreenWidth, gScreenHeight, wFlags);
-  if (h_Wnd = nil) then exit;
+  (*if (preserveGL) and (h_Wnd <> nil) {and (gFullscreen)} then
+  begin
+    e_WriteLog('SDL: going fullscreen...', TMsgType.Notify);
+    //SDL_SetWindowMaximumSize(h_Wnd, gScreenWidth, gScreenHeight);
+    SDL_SetWindowDisplayMode(h_Wnd, @cmode);
+    SDL_SetWindowSize(h_Wnd, gScreenWidth, gScreenHeight);
+    if (gFullscreen) then
+    begin
+      SDL_SetWindowFullscreen(h_Wnd, SDL_WINDOW_FULLSCREEN);
+    end
+    else
+    begin
+      SDL_SetWindowFullscreen(h_Wnd, 0);
+    end;
+  end
+  else*)
+  begin
+    KillGLWindow(preserveGL);
+    h_Wnd := SDL_CreateWindow(PChar(wTitle), gWinRealPosX, gWinRealPosY, gScreenWidth, gScreenHeight, wFlags);
+    if (h_Wnd = nil) then exit;
+  end;
 
   SDL_GL_MakeCurrent(h_Wnd, h_GL);
   SDL_ShowCursor(SDL_DISABLE);
-  if (h_GL <> nil) then
+  if (gFullscreen) then
   begin
-    if assigned(oglInitCB) then oglInitCB();
+    nw := 0;
+    nh := 0;
+    SDL_GetWindowSize(h_Wnd, @nw, @nh);
+    if (nw > 128) and (nh > 128) then
+    begin
+      e_WriteLog('SDL: fullscreen window got size '+IntToStr(nw)+'x'+IntToStr(nh)+': '+IntToStr(gScreenWidth)+'x'+IntToStr(gScreenHeight), TMsgType.Notify);
+      gScreenWidth := nw;
+      gScreenHeight := nh;
+    end
+    else
+    begin
+      e_WriteLog('SDL: fullscreen window got invalid size: '+IntToStr(nw)+'x'+IntToStr(nh), TMsgType.Notify);
+    end;
   end;
+  fuiScrWdt := gScreenWidth;
+  fuiScrHgt := gScreenHeight;
+  if (h_GL <> nil) and (not preserveGL) then begin if (assigned(oglInitCB)) then oglInitCB(); end;
 {$ENDIF}
 
   result := true;
@@ -179,9 +212,12 @@ end;
 
 procedure ChangeWindowSize ();
 begin
+  e_LogWritefln('  ChangeWindowSize: (ws=%dx%d) (ss=%dx%d)', [gWinSizeX, gWinSizeY, gScreenWidth, gScreenHeight]);
   gWinSizeX := gScreenWidth;
   gWinSizeY := gScreenHeight;
 {$IF not DEFINED(HEADLESS)}
+  fuiScrWdt := gScreenWidth;
+  fuiScrHgt := gScreenHeight;
   e_ResizeWindow(gScreenWidth, gScreenHeight);
   g_Game_SetupScreenSize();
   g_Menu_Reset();
@@ -203,6 +239,7 @@ begin
   if (gScreenWidth <> w) or (gScreenHeight <> h) then
   begin
     result := true;
+    preserve := true;
     gScreenWidth := w;
     gScreenHeight := h;
   end;
@@ -210,6 +247,7 @@ begin
   if (gFullscreen <> fullscreen) then
   begin
     result := true;
+    preserve := true;
     gFullscreen := fullscreen;
     preserve := true;
   end;
@@ -259,8 +297,20 @@ begin
 
     SDL_WINDOWEVENT_RESIZED:
     begin
-      gScreenWidth := ev.data1;
-      gScreenHeight := ev.data2;
+      e_LogWritefln('Resize: (os=%dx%d) (ns=%dx%d)', [gScreenWidth, gScreenHeight, Integer(ev.data1), Integer(ev.data2)]);
+      {if (gFullscreen) then
+      begin
+        e_LogWriteln('  fullscreen fix applied.');
+        if (gScreenWidth <> ev.data1) or (gScreenHeight <> ev.data2) then
+        begin
+          SDL_SetWindowSize(h_Wnd, gScreenWidth, gScreenHeight);
+        end;
+      end
+      else}
+      begin
+        gScreenWidth := ev.data1;
+        gScreenHeight := ev.data2;
+      end;
       ChangeWindowSize();
       SwapBuffers();
       if g_debug_WinMsgs then
@@ -281,7 +331,7 @@ begin
         wMinimized := false;
         wActivate := true;
       end;
-      if not gWinMaximized then
+      if (not gWinMaximized) and (not gFullscreen) then
       begin
         gWinMaximized := true;
         if g_debug_WinMsgs then
@@ -300,7 +350,7 @@ begin
         wMinimized := false;
         wActivate := true;
       end;
-      if gWinMaximized then gWinMaximized := false;
+      gWinMaximized := false;
       if g_debug_WinMsgs then
       begin
         g_Console_Add('Now restored');
@@ -407,7 +457,7 @@ begin
         key := ev.key.keysym.scancode;
         down := (ev.type_ = SDL_KEYDOWN);
         {$IF not DEFINED(HEADLESS)}
-        if evSDLCB(ev) then
+        if fuiOnSDLEvent(ev) then
         begin
           // event eaten, but...
           if not down then e_KeyUpDown(key, false);
@@ -420,7 +470,7 @@ begin
 
     {$IF not DEFINED(HEADLESS)}
     SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION:
-      evSDLCB(ev);
+      fuiOnSDLEvent(ev);
     {$ENDIF}
 
     SDL_TEXTINPUT:
@@ -428,7 +478,7 @@ begin
         Utf8ToUnicode(@uc, PChar(ev.text.text), 1);
         keychr := Word(uc);
         if (keychr > 127) then keychr := Word(wchar2win(WideChar(keychr)));
-        CharPress(AnsiChar(keychr));
+        if (keychr > 0) and (keychr <= 255) then CharPress(AnsiChar(keychr));
       end;
 
     // other key presses and joysticks are handled in e_input
@@ -458,15 +508,17 @@ begin
 
   if not g_Window_SetDisplay() then
   begin
-    KillGLWindow();
+    KillGLWindow(false);
     e_WriteLog('Window creation error (resolution not supported?)', TMsgType.Fatal);
     exit;
   end;
 
 {$IF not DEFINED(HEADLESS)}
-  h_Gl := SDL_GL_CreateContext(h_Wnd);
-  if (h_Gl = nil) then exit;
-  if assigned(oglInitCB) then oglInitCB();
+  h_GL := SDL_GL_CreateContext(h_Wnd);
+  if (h_GL = nil) then exit;
+  fuiScrWdt := gScreenWidth;
+  fuiScrHgt := gScreenHeight;
+  if (assigned(oglInitCB)) then oglInitCB();
 {$ENDIF}
 
   e_ResizeWindow(gScreenWidth, gScreenHeight);
@@ -543,8 +595,10 @@ begin
 
   while (SDL_PollEvent(@ev) > 0) do
   begin
+    EventHandler(ev);
     if (ev.type_ = SDL_QUITEV) then break;
   end;
+  e_PollJoysticks();
 
   if (ev.type_ = SDL_QUITEV) or (gExit = EXIT_QUIT) then
   begin
@@ -572,7 +626,8 @@ begin
     begin
       if g_Texture_Get('INTER', ID) then
       begin
-        e_DrawSize(ID, 0, 0, 0, false, false, gScreenWidth, gScreenHeight)
+        e_DrawSize(ID, 0, 0, 0, false, false, gScreenWidth, gScreenHeight);
+        e_DarkenQuadWH(0, 0, gScreenWidth, gScreenHeight, 150);
       end
       else
       begin
@@ -599,19 +654,26 @@ begin
 end;
 
 
-function ProcessMessage (): Boolean;
+function g_ProcessMessages (): Boolean;
 var
-  i, t: Integer;
   ev: TSDL_Event;
 begin
   result := false;
   FillChar(ev, SizeOf(ev), 0);
-
   while (SDL_PollEvent(@ev) > 0) do
   begin
     result := EventHandler(ev);
     if (ev.type_ = SDL_QUITEV) then exit;
   end;
+  e_PollJoysticks();
+end;
+
+
+function ProcessMessage (): Boolean;
+var
+  i, t: Integer;
+begin
+  result := g_ProcessMessages();
 
   Time := GetTimer();
   Time_Delta := Time-Time_Old;
@@ -755,9 +817,17 @@ var
   {$ENDIF}
   arg: AnsiString;
   mdfo: TStream;
+  itmp: Integer;
+  valres: Word;
 begin
 {$IFDEF HEADLESS}
   e_NoGraphics := true;
+{$ELSE}
+  if (not g_holmes_imfunctional) then
+  begin
+    uiInitialize();
+    uiContext.font := 'win14';
+  end;
 {$ENDIF}
 
   idx := 1;
@@ -787,11 +857,36 @@ begin
     {.$ENDIF}
 
     if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end;
+
     if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then
     begin
       if (idx <= ParamCount) then
       begin
-        if not conParseFloat(gh_ui_scale, ParamStr(idx)) then gh_ui_scale := 1.0;
+        if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0;
+        Inc(idx);
+      end;
+    end;
+
+    if (arg = '--holmes-font') or (arg = '-holmes-font') then
+    begin
+      if (idx <= ParamCount) then
+      begin
+        itmp := 0;
+        val(ParamStr(idx), itmp, valres);
+        {$IFNDEF HEADLESS}
+        if (valres = 0) and (not g_holmes_imfunctional) then
+        begin
+          case itmp of
+            8: uiContext.font := 'win8';
+            14: uiContext.font := 'win14';
+            16: uiContext.font := 'win16';
+          end;
+        end;
+        {$ELSE}
+        // fuck off, fpc!
+        itmp := itmp;
+        valres := valres;
+        {$ENDIF}
         Inc(idx);
       end;
     end;
@@ -838,12 +933,12 @@ begin
 
   if not glHasExtension('GL_ARB_texture_non_power_of_two') then
   begin
-    e_WriteLog('Driver DID''T advertised NPOT textures support', TMsgType.Warning);
+    e_WriteLog('NPOT textures: NO', TMsgType.Warning);
     glLegacyNPOT := true;
   end
   else
   begin
-    e_WriteLog('Driver advertised NPOT textures support', TMsgType.Notify);
+    e_WriteLog('NPOT textures: YES', TMsgType.Notify);
     glLegacyNPOT := false;
   end;
   gwin_dump_extensions := false;
@@ -864,7 +959,7 @@ begin
   while not ProcessMessage() do begin end;
 
   Release();
-  KillGLWindow();
+  KillGLWindow(false);
 
   result := 0;
 end;