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(gameclient): rebuild the in-game interface on a resolution change
Olcay Seygan56334b6ef060
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 75aea0785..23c0c63af 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -498,6 +498,9 @@ found and fixed — EA's own, not port damage.**- The command bar's artwork moves with the command bar. Minimising it slid the buttons off the bottom of the screen and left the metal they sit on painted over the battlefield; the same painting stood still through the slide the bar makes when a match opens. It travels with the bar and goes with it.- Widescreen resolutions are back in the options menu, and the list works during a match. It was greyed out the moment a game started, because changing it rebuilt the menus and then pushed the main menu over whatever you were playing. It rebuilds the menus for later and leaves you on the battlefield with the command bar back up.- Changing the resolution during a match no longer takes the game down. Two things were behind it. The old command bar's windows were handed back while the menu system still had them on its list, so the next thing that painted a button read whatever had since been written over it. And the bar itself was thrown away and built from scratch, which handed out fresh copies of every build button while your factories, your repair crews and the promotion screen went on pointing at the old ones. The bar's windows are rebuilt now and the bar keeps everything that is not a window.+- One command bar, not three. Changing the resolution during a match rebuilt the bar and left the old one behind, still drawn and still taking clicks at the size of the screen it was built for - and the confirmation box you answer afterwards rebuilds it a second time, so two mode changes in a row put three command bars on top of each other across the bottom of the battlefield. The code that was meant to throw the old one away asked the window system for a window called "ControlBar.wnd", and the bar's window is called "ControlBar.wnd:ControlBarParent", so it never found anything to throw away and never had.+- Your money is still your money after a resolution change. The command bar is rebuilt at the new size and its readout comes back holding the placeholder its layout ships with, three dollar signs, and the game only writes a number into it when the amount changes - so until you next sold, spent or collected something, that is what the box said.+- The menus you already had open are rebuilt at the new size too. A panel is stretched to the screen once, when it is built, so everything on screen when the mode changed - the in-game menu, the diplomacy panel, the chat line, the replay controls - carried on wearing the shape of the screen that had gone: drawn a proportion of a screen away from where it belongs, and on a smaller screen partly off the edge. The in-game menu is the one that mattered. It pauses the match and takes input off you until you press Return, and pressing Return is impossible when the button is no longer on the monitor.- The command bar stays on the bottom edge of the screen. Minimising it and bringing it back lifted a few pieces that had never gone down in the first place, and every rebuild of the layout lifted them a little further, so over a long match the bar climbed off its own strip and the build tooltip, which is placed against it, ended up above the top of the screen. Each piece is now put back by exactly the amount it was moved by.- The other half of that had the money readout, the general's tabs and the column of small buttons stranded a couple of hundred pixels above the bar, floating over the battlefield with the bar itself still sitting where it belongs. The bar takes a fifth of a second to slide back up when you un-minimise it, and anything that redrew the panel artwork during that fifth of a second measured every one of those pieces against a bar that had not arrived yet, then wrote the wrong answer down as if it were the right one. Switching general, changing faction colours and a resolution change all redraw it, so in a long match it happened sooner or later. The bar is put where it belongs before any of that is measured. Over six thousand frames of a match that minimises and rebuilds the bar every second, the top edge of everything it owns now reads the same number every single time.- Tooltips stay on the screen. A tooltip is drawn to the right of the pointer, and near the right or bottom edge it jumps to the other side to make room, which was all the original code did about edges - the man who wrote it left a note saying so. Nothing then checked where it had jumped to. A wide one, and the game will wrap a tooltip to the full width of the screen when the text asks for it, jumped clean off the left edge with the first half of the sentence outside the monitor; a long narrow one hovered near the top went off the top. Both are put back on the screen now, and the pointer sits over the corner of the box rather than the words going where nobody can read them.diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/GUICallbacks.h b/GeneralsMD/Code/GameEngine/Include/GameClient/GUICallbacks.hindex 40198439c..20442aacc 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/GUICallbacks.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/GUICallbacks.h@@ -375,6 +375,8 @@ extern WindowMsgHandledType EstablishConnectionsControlInput( GameWindow *windowextern void destroyQuitMenu();extern void ToggleQuitMenu( void );extern void HideQuitMenu( void );+extern void RecreateQuitMenu( void );+extern void QuitMenuLogPlacement( const char *tag );extern WindowMsgHandledType QuitMenuSystem( GameWindow *window, UnsignedInt msg, WindowMsgData mData1, WindowMsgData mData2 );// Message of the Day -----------------------------------------------------------------------------diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.hindex 08f039e85..1182fe3e6 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h@@ -793,6 +793,7 @@ public: // ********************************************************************virtual void selectNextIdleWorker( void );virtual void recreateControlBar( void );+ virtual void notifyResolutionChange( void );virtual void disableTooltipsUntil(UnsignedInt frameNum);virtual void clearTooltipsDisabled();@@ -1028,6 +1029,7 @@ protected:DisplayString * m_peaceTimeDisplayString; ///< the peace time countdown in the cornerDisplayString * m_incomeDisplayString; ///< the "(+N/min)" drawn beside the moneyInt m_lastIncomeDisplayed; ///< so that string is only rebuilt when the rate changes+ Int m_lastMoneyDisplayed; ///< so the money gadget is only written when the amount changesUnsignedInt m_hudDrawCount; ///< rendered frames counted by drawHudOverlay itselfUnsignedInt m_hudLastSampleFrame; ///< m_hudDrawCount the fps sample was last refreshed onUnsignedInt m_hudLastSampleMs; ///< wall clock of that samplediff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cppindex 8c106ebc9..4b3d8740e 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp@@ -1482,25 +1482,31 @@ static void updateResDrill( void )if( TheGameLogic->getFrame() < (UnsignedInt)TheGlobalData->m_resDrillFrame )return;- /* Two stages, sixty frames apart. The first presses Accept, which leaves the "keep this+ /* Two stages, two seconds apart. The first presses Accept, which leaves the "keep thisresolution?" box on screen; the second answers it with Cancel, which is DeclineResolution -the mode goes back and the shell and the command bar are built a second time, from inside thebox's own handler, over what the first rebuild left. Answering in the same breath as Accept- would be a sequence no player can perform. */+ would be a sequence no player can perform.++ The wait is on the wall clock and not on logic frames because stage one opens the quit menu,+ the way a player reaches the options screen in a match, and that menu pauses the game. A+ paused single-player game runs no logic at all, so a frame count here would never come due. */+ const DWORD RES_DRILL_DISMISS_DELAY_MS = 2000;+static Bool applied = FALSE;static Bool dismissed = FALSE;- static UnsignedInt appliedFrame = 0;+ static DWORD appliedTimeMs = 0;if( applied ){- if( dismissed || TheGameLogic->getFrame() < appliedFrame + 60 )+ if( dismissed || timeGetTime() - appliedTimeMs < RES_DRILL_DISMISS_DELAY_MS )return;dismissed = TRUE;ResolutionDrillDismiss( FALSE );return;}applied = TRUE;- appliedFrame = TheGameLogic->getFrame();+ appliedTimeMs = timeGetTime();if( TheGlobalData->m_headless ){diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/MainMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/MainMenu.cppindex 8d244c854..f992ac541 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/MainMenu.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/MainMenu.cpp@@ -776,7 +776,7 @@ void DeclineResolution()if( TheShell )TheShell->init();- TheInGameUI->recreateControlBar();+ TheInGameUI->notifyResolutionChange();//// Same rule as accepting the change: in a match the thing on screen is the battlefield, anddiff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenu.cppindex b6d191442..fb16e61b4 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenu.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenu.cpp@@ -1560,7 +1560,7 @@ static void applyPendingShellRebuild( void )TheShell = MSGNEW("GameClientSubsystem") Shell;TheShell->init();- TheInGameUI->recreateControlBar();+ TheInGameUI->notifyResolutionChange();//// Out of a match the shell is what you are looking at, so it opens on the main menu. In one,@@ -1575,6 +1575,53 @@ static void applyPendingShellRebuild( void )TheShell->push( AsciiString("Menus/MainMenu.wnd") );}+//+// What the money box on the command bar actually says. The bar is thrown away and built again by+// every one of these mode changes, and the new gadget carries the "GUI:$$$" its .wnd ships with+// until something writes a number over it - so this line reading dollar signs a full second after a+// rebuild is the readout that never got written.+//+static void resDrillLogMoney( const char *tag )+{+ GameWindow *moneyWindow = TheWindowManager->winGetWindowFromId(+ NULL, TheNameKeyGenerator->nameToKey( AsciiString( "ControlBar.wnd:MoneyDisplay" ) ) );++ if( moneyWindow == NULL )+ {+ DEBUG_LOG(("RESDRILL: %s, no money readout on the bar\n", tag));+ return;+ }++ AsciiString text;+ text.translate( GadgetStaticTextGetText( moneyWindow ) );+ DEBUG_LOG(("RESDRILL: %s, money readout reads \"%s\"\n", tag, text.str()));+}++//+// Every top-level window the manager is holding. A rebuild that leaves the old copy on the list+// shows up here as the same name twice, which on screen is one panel painted over another.+//+static void resDrillLogWindowRoots( const char *tag )+{+ Int count = 0;++ for( GameWindow *window = TheWindowManager->winGetWindowList(); window;+ window = window->winGetNext() )+ {+ ICoord2D position, size;+ window->winGetScreenPosition( &position.x, &position.y );+ window->winGetSize( &size.x, &size.y );++ DEBUG_LOG(("RESDRILL: %s, root \"%s\" (%d,%d %dx%d)%s\n",+ tag, window->winGetInstanceData()->m_decoratedNameString.str(),+ position.x, position.y, size.x, size.y,+ window->winIsHidden() ? " hidden" : ""));+ count++;+ }++ DEBUG_LOG(("RESDRILL: %s, %d top level windows\n", tag, count));+}+//// -resdrill: the resolution change a player makes in the middle of a match, made from a script.//@@ -1640,13 +1687,21 @@ void ResolutionDrillApply( Int xres, Int yres )wasX, wasY, xres, yres, modeIndex, bitDepth));//- // From here it is the player's own route and nothing else: open the options menu the way the- // in-game quit menu opens it, put the dropdown on the mode we want, and press Accept. Doing the- // device change directly instead - which this used to - skips the part that broke, which is the- // shell being torn down and rebuilt from inside a shell window's own message handler while the- // options layout is still on the stack. QuitMenu.cpp's buttonOptions branch is the three lines- // below.+ // From here it is the player's own route and nothing else: open the quit menu, open the options+ // menu the way that menu's Options button does, put the dropdown on the mode we want, and press+ // Accept. Doing the device change directly instead - which this used to - skips the part that+ // broke, which is the shell being torn down and rebuilt from inside a shell window's own message+ // handler while the options layout is still on the stack. QuitMenu.cpp's buttonOptions branch+ // is the three lines below.+ //+ // The quit menu is not decoration here. It pauses the game and takes input off the player until+ // Return is pressed, and it is stretched to the screen it was built on, so a resolution change+ // under it puts Return somewhere other than where the button now is - off the edge entirely on a+ // smaller screen, which is a paused match with no way out.//+ if( !TheInGameUI->isQuitMenuVisible() )+ ToggleQuitMenu();+WindowLayout *optLayout = TheShell->getOptionsLayout( TRUE );if( optLayout == NULL ){@@ -1675,11 +1730,17 @@ void ResolutionDrillApply( Int xres, Int yres )return;}+ QuitMenuLogPlacement( "before the mode change" );+ resDrillLogMoney( "before the mode change" );+ resDrillLogWindowRoots( "before the mode change" );+DEBUG_LOG(("RESDRILL: pressing Accept\n"));TheWindowManager->winSendSystemMsg( parent, GBM_SELECTED, (WindowMsgData)accept, acceptID );DEBUG_LOG(("RESDRILL: shell and command bar rebuilt at %dx%d\n",TheDisplay->getWidth(), TheDisplay->getHeight()));++ QuitMenuLogPlacement( "after the mode change" );}//@@ -1700,6 +1761,9 @@ void ResolutionDrillDismiss( Bool accept )return;}+ resDrillLogMoney( "after the mode change" );+ resDrillLogWindowRoots( "after the mode change" );+NameKeyType buttonID = TheNameKeyGenerator->nameToKey(AsciiString( accept ? "MessageBox.wnd:ButtonOk" : "MessageBox.wnd:ButtonCancel" ) );GameWindow *button = TheWindowManager->winGetWindowFromId( resAcceptMenu, buttonID );@@ -1714,6 +1778,20 @@ void ResolutionDrillDismiss( Bool accept )DEBUG_LOG(("RESDRILL: after the dialog the display is %dx%d\n",TheDisplay->getWidth(), TheDisplay->getHeight()));++ QuitMenuLogPlacement( "after the dialog" );++ //+ // Press Return the way a player does, then say whether the game came back. Input enabled 0 or+ // paused 1 on this line is the resolution change having stranded the menu that holds the pause.+ //+ if( TheInGameUI->isQuitMenuVisible() )+ ToggleQuitMenu();++ DEBUG_LOG(("RESDRILL: quit menu closed, input enabled %d, game paused %d\n",+ TheInGameUI->getInputEnabled() ? 1 : 0, TheGameLogic->isGamePaused() ? 1 : 0));++ resDrillLogWindowRoots( "after the quit menu closed" );}static void showAdvancedOptions()diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/QuitMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/QuitMenu.cppindex 3df988332..5c563051a 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/QuitMenu.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/QuitMenu.cpp@@ -39,6 +39,7 @@#include "Common/Recorder.h"#include "GameClient/GUICallbacks.h"#include "GameClient/WindowLayout.h"+#include "GameClient/Display.h"#include "GameClient/GameWindowManager.h"#include "GameClient/Gadget.h"#include "GameClient/GadgetPushButton.h"@@ -135,6 +136,52 @@ void destroyQuitMenu()isVisible = FALSE;}+//+// A .wnd is stretched to the screen it is built on, once, when it is built. The quit menu on+// screen when the resolution changes is therefore laid out for the screen that has just gone: on a+// smaller one its buttons sit past the edge, and Return is the only thing that takes the game back+// out of the pause the menu put it into, so a player who goes down a resolution from inside a match+// is left looking at a paused game he cannot click his way out of. Building it again costs a+// dropped animation and nothing else.+//+void RecreateQuitMenu( void )+{+ const Bool wasVisible = isVisible;++ destroyQuitMenu();++ // destroyQuitMenu clears isVisible, so this takes ToggleQuitMenu's open branch. The game is+ // already paused and setGamePaused ignores a pause it is already holding.+ if( wasVisible )+ ToggleQuitMenu();+}++//+// Where Return actually is, for -resdrill. A drill closes the menu by calling ToggleQuitMenu and so+// can never be told that the button is out of reach; the rectangle against the screen size is what+// says it, and a button whose right edge is past the screen width is a paused game with no way out.+//+void QuitMenuLogPlacement( const char *tag )+{+ GameWindow *returnButton = ( quitMenuLayout == NULL )+ ? NULL+ : TheWindowManager->winGetWindowFromId( NULL, buttonReturn );++ if( returnButton == NULL )+ {+ DEBUG_LOG(("QUITDRILL: %s, no quit menu on screen\n", tag));+ return;+ }++ ICoord2D position, size;+ returnButton->winGetScreenPosition( &position.x, &position.y );+ returnButton->winGetSize( &size.x, &size.y );++ DEBUG_LOG(("QUITDRILL: %s, return button (%d,%d %dx%d) screen %dx%d\n",+ tag, position.x, position.y, size.x, size.y,+ TheDisplay->getWidth(), TheDisplay->getHeight()));+}+/*** quits the program*/diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cppindex ec6703655..306e1ed8f 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp@@ -1129,6 +1129,7 @@ InGameUI::InGameUI()m_peaceTimeDisplayString = NULL;m_incomeDisplayString = NULL;m_lastIncomeDisplayed = -1;+ m_lastMoneyDisplayed = -1;m_hudDrawCount = 0;m_hudLastSampleFrame = 0;m_hudLastSampleMs = 0;@@ -2184,9 +2185,14 @@ void InGameUI::update( void )}}- // update the player money window if the money amount has changed- // this seems like as good a place as any to do the power hide/show- static Int lastMoney = -1;+ //+ // Update the player money window if the money amount has changed. The amount last written is+ // a member and not a static inside this function: the gadget is thrown away and built again+ // whenever the command bar is, and a cache that outlives the window it was filled for left the+ // new one showing the dollar signs its .wnd ships with until the player's money next moved.+ //+ // This is also as good a place as any to do the power hide/show.+ //static NameKeyType moneyWindowKey = TheNameKeyGenerator->nameToKey( "ControlBar.wnd:MoneyDisplay" );static NameKeyType powerWindowKey = TheNameKeyGenerator->nameToKey( "ControlBar.wnd:PowerWindow" );@@ -2212,13 +2218,13 @@ void InGameUI::update( void )// the money gadget wraps at its own width and a second line looked brokenupdateIncomeEstimate( moneyPlayer );- if( lastMoney != currentMoney )+ if( m_lastMoneyDisplayed != currentMoney ){UnicodeString buffer;buffer.format( TheGameText->fetch( "GUI:ControlBarMoneyDisplay" ), currentMoney );GadgetStaticTextSetText( moneyWin, buffer );- lastMoney = currentMoney;+ m_lastMoneyDisplayed = currentMoney;} // end if@@ -7851,12 +7857,39 @@ void InGameUI::recreateControlBar( void )// resolution. winDestroy unlinks the whole tree and defers the free to the window manager's own// pass, which is also what keeps the pointers below readable until then.//- GameWindow *win = TheWindowManager->winGetWindowFromId(NULL, TheNameKeyGenerator->nameToKey(AsciiString("ControlBar.wnd")));- if(win)- TheWindowManager->winDestroy(win);+ // A window's id is the key of its full decorated name, and the bar's root is called+ // ControlBar.wnd:ControlBarParent - so a lookup on "ControlBar.wnd" matched nothing and the old+ // bar was never destroyed at all. Every rebuild left its predecessor on the window list, drawn+ // and clickable at the size of the screen it was built for, and a resolution change makes two of+ // them in a row: the command bar stacked two and three deep over the battlefield. Every root+ // with that id goes, not the first, so a run that already collected some is cleaned out.+ //+ // winDestroy rather than deleteInstance: deleteInstance hands the block back to the pool without+ // taking the window off the manager's list and without touching its children, so the freed root+ // stayed in m_windowList and the pool then handed the same block to the root created two lines+ // later. The list was a ring of half-freed windows after that, and the first winRepaint that+ // reached one drew a push button whose overlay image was whatever the block now held. winDestroy+ // unlinks the whole tree and defers the free to the window manager's own pass, which is also what+ // keeps the pointers below readable until then - and it relinks m_next into the destroy list, so+ // the walk has to take the next window before it destroys this one.+ //+ const NameKeyType controlBarRootID =+ TheNameKeyGenerator->nameToKey( AsciiString( "ControlBar.wnd:ControlBarParent" ) );++ GameWindow *nextWindow = NULL;+ for( GameWindow *window = TheWindowManager->winGetWindowList(); window; window = nextWindow )+ {+ nextWindow = window->winGetNext();+ if( window->winGetWindowId() == controlBarRootID )+ TheWindowManager->winDestroy( window );+ }m_idleWorkerWin = NULL;+ // the money gadget goes with those windows, and the new one carries whatever text its .wnd+ // ships with until update() is told the amount it is showing is not the amount the player has+ m_lastMoneyDisplayed = -1;+createControlBar();//@@ -7883,6 +7916,34 @@ void InGameUI::recreateControlBar( void )}+//+// Everything a running match has on screen that is not the shell and not the command bar. Each of+// these was stretched to the resolution it was created at and none of them is rebuilt by the shell+// going away, so after a mode change the diplomacy panel, the chat line and the replay controls+// were still wearing the old screen's geometry - drawn and clicked a proportion of a screen away+// from where they belong, or off the edge of a smaller one. Diplomacy and chat are thrown away and+// come back the next time the player asks for them; the replay controls are put back here because+// nothing else builds them.+//+void InGameUI::notifyResolutionChange( void )+{+ recreateControlBar();++ ResetDiplomacy();+ ResetInGameChat();++ if( m_replayWindow )+ {+ const Bool wasHidden = m_replayWindow->winIsHidden();++ TheWindowManager->winDestroy( m_replayWindow );+ createReplayControl();+ m_replayWindow->winHide( wasHidden );+ }++ RecreateQuitMenu();+}+void InGameUI::disableTooltipsUntil(UnsignedInt frameNum){if (frameNum > m_tooltipsDisabledUntil)