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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(ww3d2): scale fullscreen below native instead of changing the mode
Olcay Seygan8e89e228245c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 23f648fc3..e91cb2137 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -412,11 +412,12 @@ found and fixed — EA's own, not port damage.**- The game draws its picture through Direct3D 11 now, and falls back to the old renderer on a machine that cannot make a Direct3D 11 device. Eight views over five maps come out within one step of one colour channel of the old picture, which is the smallest difference a screen can show.- The Direct3D 11 picture comes with its finishing passes on, no switch needed. Explosions glow past their own edges, because the battlefield is kept brighter than white until the glow is worked out; jagged edges are smoothed; and a light sharpening brings the 2003 textures back up after the smoothing. All three run over the battlefield only, before the health bars and the command bar go on top, so the lettering is untouched. In a screen of thirty inferno cannons firing the frame averaged 11.89ms with them against 11.82ms without. The antialiasing setting on the display page is for the old renderer; `-dx11post off` on the command line gives you the plain picture, and `-dx11post fxaa` picks effects one by one. The old renderer's rolling-wave sea never went missing in a real match either: both of the game's settings files turn it off, so no map ever drew it.-- 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.+- 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 the game's window over the monitor: alt-tab drops you on the desktop at its own 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.- Moving textures move in the Direct3D 11 picture. Fountain water and tank treads stood still in it, and the fountains came out a dull grey-cyan where the old renderer drew them bright. They match the old renderer now.- 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.+- 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. 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.+- Fullscreen below your monitor's own resolution fills the screen. Picking 1280x720 or 1600x900 on a 1080p monitor switched the monitor to that size, and on some PCs the switch either did not happen or came out unscaled, so the game sat in the top left corner of the desktop like a window. The monitor now stays at its own resolution and the game's picture is scaled up to cover it, with the pointer following the picture. Your second monitor stays where it was too: on a two-screen desk the old switch moved it 640 pixels to the left, along with every window on it.- Hotkeys work again after Alt+Tab, and the game's own pointer comes back with them. Since 2.4.0, returning to a fullscreen game left Windows thinking no window was listening to the keyboard: every key you pressed made the Windows error sound and did nothing in the game, and the plain Windows arrow stood in for the game's cursor. The game takes the keyboard back as it comes up from the taskbar now.- 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.diff --git a/GeneralsMD/Code/BuildFingerprint.manifest b/GeneralsMD/Code/BuildFingerprint.manifestindex 3328d5d80..02a3fbaa7 100644--- a/GeneralsMD/Code/BuildFingerprint.manifest+++ b/GeneralsMD/Code/BuildFingerprint.manifest@@ -1964,6 +1964,7 @@ Libraries/Source/WWVegas/WW3D2/font3d.hLibraries/Source/WWVegas/WW3D2/formconv.cppLibraries/Source/WWVegas/WW3D2/formconv.hLibraries/Source/WWVegas/WW3D2/framgrab.h+Libraries/Source/WWVegas/WW3D2/fullscreenfit.hLibraries/Source/WWVegas/WW3D2/gdifontmetrics.hLibraries/Source/WWVegas/WW3D2/glyphrasteriser.cppLibraries/Source/WWVegas/WW3D2/glyphrasteriser.hdiff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cppindex 9b2a80a90..c6320d0d9 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp@@ -681,13 +681,12 @@ void Reset_D3D_Device(bool active){if (TheDisplay && WW3D::Is_Initted() && !TheDisplay->getWindowed()){- // Under the Direct3D 11 picture nothing owns the display exclusively, so leaving loses no- // device: the mode and the window covering it go, and come back with the game.- if (Direct3D11_Present_Is_Enabled())- {- DX8Wrapper::Apply_Fullscreen_Display(active);- return;- }+ // On Windows nothing owns the display exclusively under either renderer, so leaving loses no+ // device: the window covering the monitor goes, and comes back with the game.+#if defined(_WIN32)+ DX8Wrapper::Apply_Fullscreen_Display(active);+ return;+#endifif (active){//switch back to desired mode when user alt-tabs back into gamediff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DMouse.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DMouse.cppindex e37e6e56f..5cddee3a1 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DMouse.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DMouse.cpp@@ -520,10 +520,12 @@ void W3DMouse::draw(void)if (TheDisplay && !TheDisplay->getWindowed()){ //if we're full-screen, need to manually move cursor image#if defined(_WIN32)+ // The device is a windowed one on Windows, fullscreen included, and a windowed device+ // takes desktop coordinates here. The window's own are only the same thing when it+ // sits at the desktop's origin, which a scaled picture with bars does not.POINT ptCursor;GetCursorPos( &ptCursor );- ScreenToClient( ApplicationHWnd, &ptCursor );m_pDev->SetCursorPosition( ptCursor.x, ptCursor.y, D3DCURSOR_IMMEDIATE_UPDATE);#else// where the pointer is over the window, in the game's pixels, as GetCursorPos anddiff --git a/GeneralsMD/Code/GameEngineDevice/Source/Win32Device/GameClient/Win32Mouse.cpp b/GeneralsMD/Code/GameEngineDevice/Source/Win32Device/GameClient/Win32Mouse.cppindex 07dd62ac9..99aa15a97 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/Win32Device/GameClient/Win32Mouse.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/Win32Device/GameClient/Win32Mouse.cpp@@ -83,6 +83,38 @@ UnsignedByte Win32Mouse::getMouseEvent( MouseIO *result, Bool flush )} // end getMouseEvent+//-------------------------------------------------------------------------------------------------+/** Windows reports the pointer in the window's pixels and the game works in its own. They are the+ * same thing until a fullscreen picture smaller than the monitor is scaled up onto it, when the+ * window is the monitor's size (or the largest part of it of the picture's shape) and the picture+ * is not. Scale by the two sizes, so every mode that does not scale comes out unchanged. */+//-------------------------------------------------------------------------------------------------+static void windowToGame( Int &x, Int &y )+{+ RECT client;+ if( TheDisplay == NULL || !::GetClientRect( ApplicationHWnd, &client ) || client.right <= 0 || client.bottom <= 0 )+ return;+ const Int width = TheDisplay->getWidth();+ const Int height = TheDisplay->getHeight();+ if( client.right != width )+ x = (Int)( ( (Int64)x * width ) / client.right );+ if( client.bottom != height )+ y = (Int)( ( (Int64)y * height ) / client.bottom );+}++static void gameToWindow( Int &x, Int &y )+{+ RECT client;+ if( TheDisplay == NULL || !::GetClientRect( ApplicationHWnd, &client ) )+ return;+ const Int width = TheDisplay->getWidth();+ const Int height = TheDisplay->getHeight();+ if( width > 0 && client.right != width )+ x = (Int)( ( (Int64)x * client.right ) / width );+ if( height > 0 && client.bottom != height )+ y = (Int)( ( (Int64)y * client.bottom ) / height );+}+//-------------------------------------------------------------------------------------------------/** Translate a win32 mouse event to our own event info *///-------------------------------------------------------------------------------------------------@@ -262,6 +294,8 @@ void Win32Mouse::translateEvent( UnsignedInt eventIndex, MouseIO *result )} // end switch on message at event index in buffer+ windowToGame( result->pos.x, result->pos.y );+} // end translateEvent///////////////////////////////////////////////////////////////////////////////////////////////////@@ -359,7 +393,11 @@ void Win32Mouse::update( void )// until the first window message tells us where the pointer is, Windows is the only one who// knows, and the (0,0) we were given at init is a corner nobody put the mouse in.if( m_positionReported == FALSE && m_cursorInWindow )- setPosition( clientPos.x, clientPos.y );+ {+ Int x = clientPos.x, y = clientPos.y;+ windowToGame( x, y );+ setPosition( x, y );+ }//// A window that covers a screen, borderless or fullscreen, keeps the pointer while it has the@@ -443,7 +481,9 @@ Bool Win32Mouse::warpCursor( Int x, Int y ){if( ::GetForegroundWindow() != ApplicationHWnd )return FALSE;- POINT screen = { x, y };+ Int windowX = x, windowY = y;+ gameToWindow( windowX, windowY );+ POINT screen = { windowX, windowY };if( !::ClientToScreen( ApplicationHWnd, &screen ) || !::SetCursorPos( screen.x, screen.y ) )return FALSE;setPosition( x, y );diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cppindex 63271d9a1..f0b98d9ce 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp@@ -56,6 +56,7 @@#include "dx8webbrowser.h" // the embedded browser: Windows only#endif#include "dx8fvf.h"+#include "fullscreenfit.h"#include "dx8vertexbuffer.h"#include "dx8indexbuffer.h"#include "dx8renderer.h"@@ -932,9 +933,8 @@ bool DX8Wrapper::Create_Device(void)}#if defined(_WIN32)-// What the fullscreen display under the Direct3D 11 picture has changed on the desktop, so leaving-// the game can put it back. The gamma is the desktop's own ramp, read before the game's first one.-static bool DisplayModeChanged = false;+// What the fullscreen display has changed on the desktop, so leaving the game can put it back: only+// the gamma now, the desktop's own ramp, read before the game's first one.static bool DesktopGammaSaved = false;static bool GameGammaSet = false;static D3DGAMMARAMP DesktopGammaRamp;@@ -977,10 +977,44 @@ static void restore_desktop_display()if (DesktopGammaSaved) {set_desktop_gamma(&DesktopGammaRamp);}- if (DisplayModeChanged) {- ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL);- DisplayModeChanged = false;+}++// Black over the parts of the monitor a picture of another shape leaves bare. It never takes the+// focus and has no taskbar button; the game's window sits on top of it.+static HWND FullscreenBackdrop = NULL;+static bool FullscreenKeepAspect = false;++static void hide_backdrop()+{+ if (FullscreenBackdrop != NULL) {+ ::ShowWindow(FullscreenBackdrop, SW_HIDE);+ }+}++static void show_backdrop(int x, int y, int width, int height)+{+ if (FullscreenBackdrop == NULL) {+ WNDCLASSA backdrop_class;+ ZeroMemory(&backdrop_class, sizeof(backdrop_class));+ backdrop_class.lpfnWndProc = DefWindowProcA;+ backdrop_class.hInstance = ::GetModuleHandleA(NULL);+ backdrop_class.hbrBackground = (HBRUSH)::GetStockObject(BLACK_BRUSH);+ backdrop_class.hCursor = ::LoadCursor(NULL, IDC_ARROW);+ backdrop_class.lpszClassName = "ZHFullscreenBackdrop";+ ::RegisterClassA(&backdrop_class);+ FullscreenBackdrop = ::CreateWindowExA(WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE | WS_EX_TOPMOST,+ backdrop_class.lpszClassName, "", WS_POPUP, x, y, width, height, NULL, NULL,+ backdrop_class.hInstance, NULL);+ if (FullscreenBackdrop == NULL) {+ return;+ }}+ ::SetWindowPos(FullscreenBackdrop, HWND_TOPMOST, x, y, width, height, SWP_SHOWWINDOW | SWP_NOACTIVATE);+}++void DX8Wrapper::Set_Requested_Fullscreen_Keep_Aspect(bool keep)+{+ FullscreenKeepAspect = keep;}void DX8Wrapper::Set_Requested_Monitor(const char * device)@@ -1002,9 +1036,19 @@ void DX8Wrapper::Apply_Fullscreen_Display(bool shown)// 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();+ //+ // No display mode is changed, whatever the resolution. The game used to set the monitor to the+ // picture's size and lay its window over it, and that is what put a fullscreen game below the+ // monitor's own size in a window: wherever the change was refused, or the driver or Windows'+ // display scaling did not stretch the new mode to the panel, a window of the picture's size sat+ // in the corner of the desktop. Measured on 1920x1080 here: 1280x720 also moved the second+ // monitor from x 1920 to x 1280 and every window on it with it. The picture is scaled instead:+ // the swap chain's buffers keep the game's size and Present stretches them to the window.+ //+ const bool owns_display = !IsWindowed;if (!owns_display || !shown) {restore_desktop_display();+ hide_backdrop();if (owns_display) {::ShowWindow(_Hwnd, SW_SHOWMINNOACTIVE);}@@ -1015,39 +1059,9 @@ void DX8Wrapper::Apply_Fullscreen_Display(bool shown)DEVMODEA current;memset(¤t,0, sizeof(current));current.dmSize = sizeof(current);- EnumDisplaySettingsExA(monitor, ENUM_CURRENT_SETTINGS, ¤t, 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, ¤t, 0);+ if (!EnumDisplaySettingsExA(monitor, ENUM_CURRENT_SETTINGS, ¤t, 0)) {+ current.dmPelsWidth = ResolutionWidth;+ current.dmPelsHeight = ResolutionHeight;}// Only the primary starts at the desktop's origin.@@ -1057,10 +1071,19 @@ void DX8Wrapper::Apply_Fullscreen_Display(bool shown)origin.y = current.dmPosition.y;}+ const FullscreenFitRect fit = Fullscreen_Fit(current.dmPelsWidth, current.dmPelsHeight,+ ResolutionWidth, ResolutionHeight, FullscreenKeepAspect);+ if (fit.width != (int)current.dmPelsWidth || fit.height != (int)current.dmPelsHeight) {+ show_backdrop(origin.x, origin.y, current.dmPelsWidth, current.dmPelsHeight);+ } else {+ hide_backdrop();+ }+if (::IsIconic(_Hwnd)) {::ShowWindow(_Hwnd, SW_SHOWNOACTIVATE);}- ::SetWindowPos(_Hwnd, HWND_TOPMOST, origin.x, origin.y, ResolutionWidth, ResolutionHeight,+ // after the backdrop, so the game is the topmost of the two+ ::SetWindowPos(_Hwnd, HWND_TOPMOST, origin.x + fit.x, origin.y + fit.y, fit.width, fit.height,SWP_SHOWWINDOW | SWP_NOACTIVATE);if (GameGammaSet) {@@ -1079,6 +1102,7 @@ static void save_desktop_gamma() {}static void restore_desktop_display() {}void DX8Wrapper::Set_Requested_Monitor(const char *) {}void DX8Wrapper::Apply_Fullscreen_Display(bool) {}+void DX8Wrapper::Set_Requested_Fullscreen_Keep_Aspect(bool) {}#endifbool DX8Wrapper::Reset_Device(bool reload_assets)@@ -1465,13 +1489,17 @@ bool DX8Wrapper::Set_Render_Device(int dev, int width, int height, int bits, intWWDEBUG_SAY(("-dx11: Direct3D 11 device %s\n", created ? "created" : "refused"));}- // While Direct3D 11 presents, the Direct3D 9 device is windowed even in a fullscreen game. A- // 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.+ // The Direct3D 9 device is windowed even in a fullscreen game. One that owns the display refuses+ // the Direct3D 11 swap chain its window, which kept every fullscreen game on the old picture, and+ // under -d3d9 it would change the monitor's mode, which is what fullscreen no longer does (see+ // Apply_Fullscreen_Display). A windowed device's Present stretches its back buffer to the window.// 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.+#if defined(_WIN32)+ const bool device_windowed = true;+#elseconst bool device_windowed = IsWindowed || Direct3D11_Present_Is_Enabled();+#endif#if defined(_WIN32)if (::GetForegroundWindow() == _Hwnd) {Apply_Fullscreen_Display(true);diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.hindex 376bcb4eb..2c15ce2a1 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h@@ -831,11 +831,15 @@ public:static WW3DFormat getBackBufferFormat( void );static bool Reset_Device(bool reload_assets=true);- // While Direct3D 11 presents, the Direct3D 9 device is always windowed and a fullscreen game is a- // display mode plus a window covering it, both set here. shown false gives the desktop its own- // mode and gamma back and minimises the window, which is what leaving the game does; true puts- // the game's back. A windowed game, or one Direct3D 9 presents, only gets the desktop restored.+ // The Direct3D 9 device is always windowed, and a fullscreen game is a topmost window over its+ // monitor, placed here. The monitor keeps its own mode: a picture smaller than it is scaled up+ // onto all of it, or onto the largest part of its own shape with black either side. shown false+ // gives the desktop its gamma back and minimises the window, which is what leaving the game does;+ // true puts the game's back. A windowed game only gets the desktop's gamma restored.static void Apply_Fullscreen_Display(bool shown);+ // Stretch a fullscreen picture whose shape is not the monitor's (false), or keep its shape with+ // black bars (true). Pushed in from the app layer like the monitor below.+ static void Set_Requested_Fullscreen_Keep_Aspect(bool keep);// The monitor that display mode and window go on, as its GDI device name. WW3D2 cannot see// GlobalData, so the app layer pushes it in before the device is made or reset, like the vsync.static void Set_Requested_Monitor(const char * device);diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/fullscreenfit.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/fullscreenfit.hnew file mode 100644index 000000000..5e5654f51--- /dev/null+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/fullscreenfit.h@@ -0,0 +1,50 @@+/*+** Command & Conquer Generals Zero Hour(tm)+** Copyright 2025 Electronic Arts Inc.+**+** 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+** the Free Software Foundation, either version 3 of the License, or+** (at your option) any later version.+**+** This program is distributed in the hope that it will be useful,+** but WITHOUT ANY WARRANTY; without even the implied warranty of+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+** GNU General Public License for more details.+**+** You should have received a copy of the GNU General Public License+** along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++// fullscreenfit.h+//+// Where a fullscreen picture of one size goes on a monitor of another. The monitor keeps its own+// mode and the picture is scaled onto it, so this is the whole of the decision: all of the monitor,+// or the largest rectangle of the picture's own shape centred on it, with black either side.++#pragma once++struct FullscreenFitRect+{+ int x, y, width, height; // relative to the monitor's top left corner+};++inline FullscreenFitRect Fullscreen_Fit(int monitor_width, int monitor_height, int width, int height,+ bool keep_aspect)+{+ FullscreenFitRect fit = { 0, 0, monitor_width, monitor_height };+ if (!keep_aspect || width <= 0 || height <= 0 || monitor_width <= 0 || monitor_height <= 0) {+ return fit;+ }+ // Compared as products so 1920x1080 on 1920x1200 is decided without rounding.+ const long long wide = (long long)width * monitor_height;+ const long long tall = (long long)height * monitor_width;+ if (wide > tall) {+ fit.height = (int)(((long long)monitor_width * height + width / 2) / width);+ } else if (tall > wide) {+ fit.width = (int)(((long long)monitor_height * width + height / 2) / height);+ }+ fit.x = (monitor_width - fit.width) / 2;+ fit.y = (monitor_height - fit.height) / 2;+ return fit;+}diff --git a/GeneralsMD/Code/Tests/test_ww3d2.cpp b/GeneralsMD/Code/Tests/test_ww3d2.cppindex 6f922d8bc..7373ae31b 100644--- a/GeneralsMD/Code/Tests/test_ww3d2.cpp+++ b/GeneralsMD/Code/Tests/test_ww3d2.cpp@@ -29,6 +29,7 @@#include "vector4.h"#include "quat.h"#include "render2dsentence.h"+#include "fullscreenfit.h"/** W3DMPO_GLUE gives every pooled class an operator new that routes through@@ -78,6 +79,32 @@ void freeFromW3DMemPool(void * /*pool*/, void *p)::operator delete(raw);}+/*=========================================================================+ fullscreenfit - where a scaled fullscreen picture goes on its monitor+ =========================================================================*/++TEST(fullscreen_fit_stretch_covers_the_whole_monitor)+{+ const FullscreenFitRect fit = Fullscreen_Fit(1920, 1200, 1280, 720, false);+ CHECK_EQ(fit.x, 0); CHECK_EQ(fit.y, 0);+ CHECK_EQ(fit.width, 1920); CHECK_EQ(fit.height, 1200);+}++TEST(fullscreen_fit_keeps_the_pictures_shape_with_bars)+{+ /* 16:9 on 16:10: bars above and below, 60 each */+ FullscreenFitRect fit = Fullscreen_Fit(1920, 1200, 1920, 1080, true);+ CHECK_EQ(fit.x, 0); CHECK_EQ(fit.y, 60); CHECK_EQ(fit.width, 1920); CHECK_EQ(fit.height, 1080);+ /* 4:3 on 16:9: bars left and right */+ fit = Fullscreen_Fit(1920, 1080, 1024, 768, true);+ CHECK_EQ(fit.x, 240); CHECK_EQ(fit.y, 0); CHECK_EQ(fit.width, 1440); CHECK_EQ(fit.height, 1080);+ /* the monitor's own shape at a smaller size needs no bars */+ fit = Fullscreen_Fit(1920, 1200, 1680, 1050, true);+ CHECK_EQ(fit.x, 0); CHECK_EQ(fit.y, 0); CHECK_EQ(fit.width, 1920); CHECK_EQ(fit.height, 1200);+ fit = Fullscreen_Fit(1920, 1080, 1280, 720, true);+ CHECK_EQ(fit.width, 1920); CHECK_EQ(fit.height, 1080);+}+/*=========================================================================ww3dformat - format property tables=========================================================================*/