Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(ww3d2): apply fullscreen display after activation, never re-activate

Olcay Seygan8c6ac1a68174
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 708c5586c..5ca0af930 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -328,6 +328,7 @@ found and fixed — EA's own, not port damage.**
- Fullscreen gets the Direct3D 11 picture too. The old renderer took the whole display for itself and refused the new one its window, so every fullscreen game quietly ran on Direct3D 9, and the DX9 in the corner readout was the only sign. Fullscreen is now your chosen resolution set on the monitor with the game's window over it: alt-tab drops you on the desktop at its own resolution and brightness, and coming back puts the game's on again. A 1280x720 match on a 1920x1080 screen drew 40.7 million times without one draw refused, before and after an alt-tab.
- The game fits the screen on a PC with Windows display scaling turned up. The game never told Windows it counts real pixels, so at 125% Windows blew the whole window up by a quarter: a 1920x1080 game on a 1920x1080 screen came out 2400x1350 and the right and bottom of it hung off the edge. Now the window is the size you picked.
- Alt-tabbing out of a fullscreen match and back no longer leaves every building already on the map as a dark shadow with nothing standing in it. Coming back from the desktop switched the game over to Direct3D 11 halfway through the match, and the new picture only knew about what was built after the switch, so older buildings drew as their shadow alone while a new one beside them looked fine. A match keeps the picture it started with now.
+- Alt-tabbing back into a fullscreen game brings the game back, once. It used to flicker between the game and the desktop several times and could end as a minimised window that clicking on the taskbar would not open. Putting the screen back was itself switching windows, and every switch set off another round of it. The game now waits until the switch is over before it touches the screen, and nothing it does on the way in or out takes focus from anyone. When the game runs at your desktop's resolution, coming back changes no display mode at all, so a 144 or 165 Hz monitor no longer drops to 60 and goes black for a moment. At any other resolution it keeps your monitor's refresh rate if the monitor can run it there.
- 481 base-game textures at four times the resolution now beat Zero Hour's downscaled copies.
- The ground is drawn at twice the detail. Every map square used to get a 64-pixel patch of its ground texture, squeezed into sixteen-bit colour; it gets 128 pixels now in full colour, so sand, gravel and tracks stop turning to blocks and bands when you zoom in, and a ground texture that ships at twice the size is shown at that size.
- The ground has relief. Every grain of sand, pebble and crack catches the map's own sun on one side and falls into shade on the other, worked out from the ground's own picture when the map loads, and it follows the sun on a dusk map or a night one. The light the ground already had from its hills is kept; the relief goes on top. It is part of the new picture, so a machine on the old renderer keeps the flat ground.
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
index 2b0ced4a1..49213a26b 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
@@ -1105,13 +1105,11 @@ void W3DDisplay::init( void )
} // end if
- // Which runtime the device really landed on. The renderer creates an IDirect3DDevice9 itself
- // now, so there is one answer here and no translating dll to name; -d3d12 was d3d8to9's opt-in
- // and does nothing until RENDERER-ROADMAP.md's phase 5 puts a real Direct3D 12 backend behind
- // the same seam.
+ // Which backend draws what is on screen. d3d9.dll is loaded either way, because the Direct3D 9
+ // device is always made and the Direct3D 11 one mirrors it, so the loaded dll says nothing.
DEBUG_LOG(("W3DDisplay::init - renderer runtime: %s\n",
- GetModuleHandleA("d3d9.dll") ? "Direct3D 9 (native)"
- : "no Direct3D 9 runtime loaded"));
+ Direct3D11_Present_Is_Enabled() ? "Direct3D 11"
+ : "Direct3D 9"));
// multisampling is opt-in with "-msaa" / "-msaa N" and silently degrades to whatever the
// device supports, so log what was actually granted
DEBUG_LOG(("W3DDisplay::init - multisampling: %ux\n", DX8Wrapper::Get_MultiSample_Level()));
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
index aeed616da..0dce8d8e0 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
@@ -935,42 +935,70 @@ void DX8Wrapper::Set_Requested_Monitor(const char * device)
void DX8Wrapper::Apply_Fullscreen_Display(bool shown)
{
+ // Nothing here activates a window. This runs when the game is activated or deactivated, and a
+ // window activated from inside that bounced focus between the game and the desktop until it
+ // ended minimized for good (issue #45): SW_MINIMIZE hands activation to whatever is next in the
+ // z-order, SW_RESTORE and a SetWindowPos without SWP_NOACTIVATE take it back.
const bool owns_display = !IsWindowed && Direct3D11_Present_Is_Enabled();
if (!owns_display || !shown) {
restore_desktop_display();
if (owns_display) {
- ::ShowWindow(_Hwnd, SW_MINIMIZE);
+ ::ShowWindow(_Hwnd, SW_SHOWMINNOACTIVE);
}
return;
}
const char * monitor = RequestedMonitor[0] ? RequestedMonitor : NULL;
- DEVMODEA mode;
- ZeroMemory(&mode, sizeof(mode));
- mode.dmSize = sizeof(mode);
- mode.dmPelsWidth = ResolutionWidth;
- mode.dmPelsHeight = ResolutionHeight;
- mode.dmBitsPerPel = BitDepth;
- mode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
- if (ChangeDisplaySettingsExA(monitor, &mode, NULL, CDS_FULLSCREEN, NULL) == DISP_CHANGE_SUCCESSFUL) {
- DisplayModeChanged = true;
- }
-
- // Only the primary starts at the desktop's origin, and a monitor can move when its mode changes,
- // so where it starts is asked after the change.
DEVMODEA current;
ZeroMemory(&current, sizeof(current));
current.dmSize = sizeof(current);
+ EnumDisplaySettingsExA(monitor, ENUM_CURRENT_SETTINGS, &current, 0);
+
+ // A monitor already in the game's mode is left alone. Asking for it again with no refresh rate
+ // is a real mode change on a 144 or 165Hz desktop, which drops to 60 and back on every return.
+ // Any other mode is asked for at the desktop's own rate first, and the driver's default after.
+ if (current.dmPelsWidth != (DWORD)ResolutionWidth || current.dmPelsHeight != (DWORD)ResolutionHeight ||
+ current.dmBitsPerPel != (DWORD)BitDepth) {
+ DEVMODEA desktop;
+ ZeroMemory(&desktop, sizeof(desktop));
+ desktop.dmSize = sizeof(desktop);
+ DEVMODEA mode;
+ ZeroMemory(&mode, sizeof(mode));
+ mode.dmSize = sizeof(mode);
+ mode.dmPelsWidth = ResolutionWidth;
+ mode.dmPelsHeight = ResolutionHeight;
+ mode.dmBitsPerPel = BitDepth;
+ mode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
+ // 0 and 1 are the hardware default, not a rate.
+ if (EnumDisplaySettingsExA(monitor, ENUM_REGISTRY_SETTINGS, &desktop, 0) &&
+ desktop.dmDisplayFrequency > 1) {
+ mode.dmDisplayFrequency = desktop.dmDisplayFrequency;
+ mode.dmFields |= DM_DISPLAYFREQUENCY;
+ }
+ LONG result = ChangeDisplaySettingsExA(monitor, &mode, NULL, CDS_FULLSCREEN, NULL);
+ if (result != DISP_CHANGE_SUCCESSFUL && (mode.dmFields & DM_DISPLAYFREQUENCY)) {
+ mode.dmFields &= ~DM_DISPLAYFREQUENCY;
+ result = ChangeDisplaySettingsExA(monitor, &mode, NULL, CDS_FULLSCREEN, NULL);
+ }
+ if (result == DISP_CHANGE_SUCCESSFUL) {
+ DisplayModeChanged = true;
+ }
+ // A monitor can move when its mode changes, so where it starts is asked after the change.
+ EnumDisplaySettingsExA(monitor, ENUM_CURRENT_SETTINGS, &current, 0);
+ }
+
+ // Only the primary starts at the desktop's origin.
POINT origin = { 0, 0 };
- if (monitor && EnumDisplaySettingsExA(monitor, ENUM_CURRENT_SETTINGS, &current, 0)) {
+ if (monitor) {
origin.x = current.dmPosition.x;
origin.y = current.dmPosition.y;
}
if (::IsIconic(_Hwnd)) {
- ::ShowWindow(_Hwnd, SW_RESTORE);
+ ::ShowWindow(_Hwnd, SW_SHOWNOACTIVATE);
}
- ::SetWindowPos(_Hwnd, HWND_TOPMOST, origin.x, origin.y, ResolutionWidth, ResolutionHeight, SWP_SHOWWINDOW);
+ ::SetWindowPos(_Hwnd, HWND_TOPMOST, origin.x, origin.y, ResolutionWidth, ResolutionHeight,
+ SWP_SHOWWINDOW | SWP_NOACTIVATE);
if (GameGammaSet) {
save_desktop_gamma(); // the monitor may have changed since the game's ramp was last set
@@ -1348,8 +1376,12 @@ bool DX8Wrapper::Set_Render_Device(int dev, int width, int height, int bits, int
// Direct3D 9 device that owns the display refuses the Direct3D 11 swap chain its window, which
// kept every fullscreen game on the old picture. The mode goes on before the device is made or
// reset, so it is made in the mode it will run in.
+ // A player who alt-tabbed away during the splash is not handed a topmost window over whatever he
+ // went to. The game's activation applies the display when he comes back.
const bool device_windowed = IsWindowed || Direct3D11_Present_Is_Enabled();
- Apply_Fullscreen_Display(true);
+ if (::GetForegroundWindow() == _Hwnd) {
+ Apply_Fullscreen_Display(true);
+ }
/*
** Initialize values for D3DPRESENT_PARAMETERS members.
diff --git a/GeneralsMD/Code/Main/WinMain.cpp b/GeneralsMD/Code/Main/WinMain.cpp
index b24987eae..b035027a1 100644
--- a/GeneralsMD/Code/Main/WinMain.cpp
+++ b/GeneralsMD/Code/Main/WinMain.cpp
@@ -116,6 +116,17 @@ static Bool gInitializing = false;
static Bool gDoPaint = true;
static Bool isWinMainActive = false;
+// The fullscreen display follows activation from a posted message rather than from inside
+// WM_ACTIVATEAPP. Changing the mode and the window there let the change itself activate or
+// deactivate the game again, nested inside the handler, and Alt+Tab back flipped between the game
+// and the desktop until the window stayed minimized (issue #45). The posted message applies
+// whatever state the last activation left, once. It starts out not shown: the device only puts
+// the display on when the window is in the foreground, so a game started or left behind another
+// window during the splash gets it from its first activation. When the game was in front, that
+// first apply finds the mode already set and changes nothing.
+static const UINT WM_APP_APPLY_ACTIVATION = WM_APP + 1;
+static Bool isDisplayShownActive = false;
+
// Whether the cursor is currently clipped to the window. Activating the game by clicking the
// taskbar leaves the pointer wherever it was, and clipping right then snatched it into the window
// from across the desktop; the clip waits for the pointer to arrive on its own now.
@@ -549,7 +560,7 @@ LRESULT CALLBACK WndProc( HWND hWnd, UINT message,
if (TheGameEngine)
TheGameEngine->setIsActive(isWinMainActive);
- Reset_D3D_Device(isWinMainActive);
+ ::PostMessage(hWnd, WM_APP_APPLY_ACTIVATION, 0, 0);
if (isWinMainActive)
{ //restore mouse cursor to our custom version.
if (TheWin32Mouse)
@@ -558,6 +569,16 @@ LRESULT CALLBACK WndProc( HWND hWnd, UINT message,
}
return 0;
}
+
+ case WM_APP_APPLY_ACTIVATION:
+ {
+ if (isDisplayShownActive != isWinMainActive)
+ {
+ isDisplayShownActive = isWinMainActive;
+ Reset_D3D_Device(isWinMainActive);
+ }
+ return 0;
+ }
//-------------------------------------------------------------------------
case WM_ACTIVATE:
{