DEADSOFTWARE

sound muting should work on first window deactivation now
[d2df-sdl.git] / src / game / g_window.pas
index d4cd928673a591bd2b16dbf0d7156cf708cf2be9..ef1d362bd77362b5d5045ea3ffcea44b9ed24da7 100644 (file)
@@ -35,7 +35,7 @@ var
   flag: Boolean;
   wTitle: PChar = nil;
   wNeedTimeReset: Boolean = False;
-  wWindowCreated: Boolean = False;
+  //wWindowCreated: Boolean = False;
   //wCursorShown: Boolean = False;
   wMinimized: Boolean = False;
   //wNeedFree: Boolean = True;
@@ -68,14 +68,13 @@ begin
 end;
 
 function g_Window_SetDisplay(PreserveGL: Boolean = False): Boolean;
+var
+  mode, cmode: TSDL_DisplayMode;
 begin
   Result := False;
 
   e_WriteLog('Setting display mode...', MSG_NOTIFY);
 
-  // if wWindowCreated and PreserveGL then
-  //   e_SaveGLContext(); // we need this and restore because of a bug in SDL1.2, apparently
-
   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;
@@ -85,6 +84,25 @@ begin
     SDL_DestroyWindow(h_Wnd);
     h_Wnd := nil;
   end;
+  
+  if gFullscreen then
+  begin
+    mode.w := gScreenWidth;
+    mode.h := gScreenHeight;
+    mode.format := 0;
+    mode.refresh_rate := 0;
+    mode.driverdata := nil;
+    if SDL_GetClosestDisplayMode(0, @mode, @cmode) = nil then
+    begin
+      gScreenWidth := 800;
+      gScreenHeight := 600;
+    end
+    else
+    begin
+      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;
@@ -92,9 +110,6 @@ begin
   SDL_GL_MakeCurrent(h_Wnd, h_GL);
   SDL_ShowCursor(SDL_DISABLE);
 
-  // if wWindowCreated and PreserveGL then
-  //  e_RestoreGLContext();
-
   Result := True;
 end;
 
@@ -106,20 +121,25 @@ end;
 function GetDisplayModes(dBPP: DWORD; var SelRes: DWORD): SArray;
 var
   mode: TSDL_DisplayMode;
-  res, i, k: Integer;
+  res, i, k, n, pw, ph: Integer;
 begin
   SetLength(Result, 0);
 
-  k := 0;
-  for i := 0 to SDL_GetNumDisplayModes(0) do
+  k := 0; SelRes := 0;
+  n := SDL_GetNumDisplayModes(0);
+  pw := 0; ph := 0;
+  for i := 0 to n do
   begin
     res := SDL_GetDisplayMode(0, i, @mode);
     if res < 0 then continue;
+    if SDL_BITSPERPIXEL(mode.format) = gBPP then continue;
+    if (mode.w = pw) and (mode.h = ph) then continue;
     if (mode.w = gScreenWidth) and (mode.h = gScreenHeight) then
       SelRes := k;
     Inc(k);
     SetLength(Result, k);
     Result[k-1] := IntToStr(mode.w) + 'x' + IntToStr(mode.h);
+    pw := mode.w; ph := mode.h
   end;
 
   e_WriteLog('SDL: Got ' + IntToStr(k) + ' resolutions.', MSG_NOTIFY);
@@ -253,23 +273,33 @@ begin
         e_WriteLog('[DEBUG] WinMsgs: Now restored', MSG_NOTIFY);
       end;
     end;
-    
+
     SDL_WINDOWEVENT_FOCUS_GAINED:
+    begin
       wActivate := True;
-    
+      //e_WriteLog('window gained focus!', MSG_NOTIFY);
+    end;
+
     SDL_WINDOWEVENT_FOCUS_LOST:
+    begin
       wDeactivate := True;
+      //e_WriteLog('window lost focus!', MSG_NOTIFY);
+    end;
   end;
-  
+
   if wDeactivate then
   begin
     if gWinActive then
     begin
+      e_WriteLog('deactivating window', MSG_NOTIFY);
       e_EnableInput := False;
       e_ClearInputBuffer();
 
       if gMuteWhenInactive then
+      begin
+        //e_WriteLog('deactivating sounds', MSG_NOTIFY);
         e_MuteChannels(True);
+      end;
 
       if g_debug_WinMsgs then
       begin
@@ -284,10 +314,14 @@ begin
   begin
     if not gWinActive then
     begin
+      //e_WriteLog('activating window', MSG_NOTIFY);
       e_EnableInput := True;
 
       if gMuteWhenInactive then
+      begin
+        //e_WriteLog('activating sounds', MSG_NOTIFY);
         e_MuteChannels(False);
+      end;
 
       if g_debug_WinMsgs then
       begin
@@ -356,7 +390,7 @@ begin
   if h_GL <> nil then SDL_GL_DeleteContext(h_GL);
   h_Wnd := nil;
   h_GL := nil;
-  wWindowCreated := False;
+  //wWindowCreated := False;
 end;
 
 function CreateGLWindow(Title: PChar): Boolean;
@@ -380,8 +414,8 @@ begin
 
   h_Gl := SDL_GL_CreateContext(h_Wnd);
   if h_Gl = nil then Exit;
-  
-  wWindowCreated := True;
+
+  //wWindowCreated := True;
 
   e_ResizeWindow(gScreenWidth, gScreenHeight);
   e_InitGL();