浏览代码

Remove main menu global. Now accessed via GameInstance

Ivan Savenko 9 月之前
父节点
当前提交
e5a095a237

+ 1 - 1
client/CPlayerInterface.cpp

@@ -1801,7 +1801,7 @@ void CPlayerInterface::proposeLoadingGame()
 		[]()
 		{
 			GAME->server().endGameplay();
-			CMM->menu->switchToTab("load");
+			GAME->mainmenu()->menu->switchToTab("load");
 		},
 		nullptr
 	);

+ 12 - 18
client/CServerHandler.cpp

@@ -15,6 +15,7 @@
 #include "GameChatHandler.h"
 #include "CPlayerInterface.h"
 #include "GameEngine.h"
+#include "GameInstance.h"
 #include "gui/WindowHandler.h"
 
 #include "globalLobby/GlobalLobbyClient.h"
@@ -610,8 +611,8 @@ void CServerHandler::startMapAfterConnection(std::shared_ptr<CMapInfo> to)
 
 void CServerHandler::startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameState)
 {
-	if(CMM)
-		CMM->disable();
+	if(GAME->mainmenu())
+		GAME->mainmenu()->disable();
 
 	switch(si->mode)
 	{
@@ -649,7 +650,7 @@ void CServerHandler::showHighScoresAndEndGameplay(PlayerColor player, bool victo
 		scenarioHighScores.isCampaign = false;
 
 		endGameplay();
-		CMM->menu->switchToTab("main");
+		GAME->mainmenu()->menu->switchToTab("main");
 		ENGINE->windows().createAndPushWindow<CHighScoreInputScreen>(victory, scenarioHighScores, statistic);
 	}
 }
@@ -664,17 +665,11 @@ void CServerHandler::endGameplay()
 	client->endGame();
 	client.reset();
 
-	if(CMM)
+	if (GAME->mainmenu())
 	{
-		CMM->enable();
-		CMM->playMusic();
-		CMM->makeActiveInterface();
-	}
-	else
-	{
-		auto mainMenu = CMainMenu::create();
-		mainMenu->playMusic();
-		mainMenu->makeActiveInterface();
+		GAME->mainmenu()->enable();
+		GAME->mainmenu()->playMusic();
+		GAME->mainmenu()->makeActiveInterface();
 	}
 }
 
@@ -710,14 +705,13 @@ void CServerHandler::startCampaignScenario(HighScoreParameter param, std::shared
 			entry->Bool() = true;
 		}
 
-		ENGINE->windows().pushWindow(CMM);
-		ENGINE->windows().pushWindow(CMM->menu);
+		GAME->mainmenu()->makeActiveInterface();
 
 		if(!ourCampaign->isCampaignFinished())
-			CMM->openCampaignLobby(ourCampaign);
+			GAME->mainmenu()->openCampaignLobby(ourCampaign);
 		else
 		{
-			CMM->openCampaignScreen(ourCampaign->campaignSet);
+			GAME->mainmenu()->openCampaignScreen(ourCampaign->campaignSet);
 			if(!ourCampaign->getOutroVideo().empty() && ENGINE->video().open(ourCampaign->getOutroVideo(), 1))
 			{
 				ENGINE->music().stopMusic();
@@ -890,7 +884,7 @@ void CServerHandler::onDisconnected(const std::shared_ptr<INetworkConnection> &
 	if(client)
 	{
 		endGameplay();
-		CMM->menu->switchToTab("main");
+		GAME->mainmenu()->menu->switchToTab("main");
 		showServerError(LIBRARY->generaltexth->translate("vcmi.server.errors.disconnected"));
 	}
 	else

+ 15 - 1
client/GameInstance.cpp

@@ -14,6 +14,9 @@
 #include "CServerHandler.h"
 #include "mapView/mapHandler.h"
 #include "globalLobby/GlobalLobbyClient.h"
+#include "mainmenu/CMainMenu.h"
+
+#include "../lib/CConfigHandler.h"
 
 std::unique_ptr<GameInstance> GAME = nullptr;
 
@@ -41,6 +44,17 @@ CMapHandler & GameInstance::map()
 	return *mapInstance;
 }
 
+std::shared_ptr<CMainMenu> GameInstance::mainmenu()
+{
+	if(settings["session"]["headless"].Bool())
+		return nullptr;
+
+	if (!mainMenuInstance)
+		mainMenuInstance = std::shared_ptr<CMainMenu>(new CMainMenu());
+
+	return mainMenuInstance;
+}
+
 CPlayerInterface * GameInstance::interface()
 {
 	return interfaceInstance;
@@ -58,7 +72,7 @@ void GameInstance::setMapInstance(std::unique_ptr<CMapHandler> ptr)
 
 void GameInstance::setInterfaceInstance(CPlayerInterface * ptr)
 {
-	interfaceInstance = std::move(ptr);
+	interfaceInstance = ptr;
 }
 
 void GameInstance::onGlobalLobbyInterfaceActivated()

+ 3 - 0
client/GameInstance.h

@@ -15,6 +15,7 @@ class CServerHandler;
 class GlobalLobbyClient;
 class CPlayerInterface;
 class CMapHandler;
+class CMainMenu;
 
 VCMI_LIB_NAMESPACE_BEGIN
 class INetworkHandler;
@@ -24,6 +25,7 @@ class GameInstance final : boost::noncopyable, public IGameEngineUser
 {
 	std::unique_ptr<CServerHandler> serverInstance;
 	std::unique_ptr<CMapHandler> mapInstance;
+	std::shared_ptr<CMainMenu> mainMenuInstance;
 	CPlayerInterface * interfaceInstance;
 
 public:
@@ -33,6 +35,7 @@ public:
 	CServerHandler & server();
 	CMapHandler & map();
 
+	std::shared_ptr<CMainMenu> mainmenu();
 	CPlayerInterface * interface();
 
 	void setServerInstance(std::unique_ptr<CServerHandler> ptr);

+ 2 - 2
client/adventureMap/AdventureMapShortcuts.cpp

@@ -331,7 +331,7 @@ void AdventureMapShortcuts::toMainMenu()
 		[]()
 		{
 			GAME->server().endGameplay();
-			CMM->menu->switchToTab("main");
+			GAME->mainmenu()->menu->switchToTab("main");
 		},
 		0
 		);
@@ -344,7 +344,7 @@ void AdventureMapShortcuts::newGame()
 		[]()
 		{
 			GAME->server().endGameplay();
-			CMM->menu->switchToTab("new");
+			GAME->mainmenu()->menu->switchToTab("new");
 		},
 		nullptr
 		);

+ 1 - 1
client/lobby/CBonusSelection.cpp

@@ -398,7 +398,7 @@ void CBonusSelection::goBack()
 	if(GAME->server().getState() != EClientState::GAMEPLAY)
 	{
 		ENGINE->windows().popWindows(2);
-		CMM->playMusic();
+		GAME->mainmenu()->playMusic();
 	}
 	else
 	{

+ 3 - 2
client/mainmenu/CHighScoreScreen.cpp

@@ -14,6 +14,7 @@
 #include "CStatisticScreen.h"
 #include "CMainMenu.h"
 #include "../GameEngine.h"
+#include "../GameInstance.h"
 #include "../gui/WindowHandler.h"
 #include "../gui/Shortcut.h"
 #include "../media/IMusicPlayer.h"
@@ -169,7 +170,7 @@ void CHighScoreScreen::buttonResetClick()
 void CHighScoreScreen::buttonExitClick()
 {
 	close();
-	CMM->playMusic();
+	GAME->mainmenu()->playMusic();
 }
 
 void CHighScoreScreen::showAll(Canvas & to)
@@ -221,7 +222,7 @@ CHighScoreInputScreen::CHighScoreInputScreen(bool won, HighScoreCalculation calc
 
 void CHighScoreInputScreen::stopMusicAndClose()
 {
-	CMM->playMusic();
+	GAME->mainmenu()->playMusic();
 	close();
 }
 

+ 2 - 11
client/mainmenu/CMainMenu.cpp

@@ -61,7 +61,6 @@
 #include "../../lib/CRandomGenerator.h"
 #include "../../lib/GameLibrary.h"
 
-std::shared_ptr<CMainMenu> CMM;
 ISelectionScreenInfo * SEL = nullptr;
 
 static void do_quit()
@@ -171,7 +170,7 @@ static std::function<void()> genCommand(CMenuScreen * menu, std::vector<std::str
 			}
 			case 1: //open campaign selection window
 			{
-				return std::bind(&CMainMenu::openCampaignScreen, CMM, commands.front());
+				return std::bind(&CMainMenu::openCampaignScreen, GAME->mainmenu(), commands.front());
 				break;
 			}
 			case 2: //start
@@ -384,7 +383,7 @@ void CMainMenu::onScreenResize()
 
 void CMainMenu::makeActiveInterface()
 {
-	ENGINE->windows().pushWindow(CMM);
+	ENGINE->windows().pushWindow(GAME->mainmenu());
 	ENGINE->windows().pushWindow(menu);
 	menu->switchToTab(menu->getActiveTab());
 }
@@ -461,14 +460,6 @@ void CMainMenu::openHighScoreScreen()
 	return;
 }
 
-std::shared_ptr<CMainMenu> CMainMenu::create()
-{
-	if(!CMM)
-		CMM = std::shared_ptr<CMainMenu>(new CMainMenu());
-
-	return CMM;
-}
-
 std::shared_ptr<CPicture> CMainMenu::createPicture(const JsonNode & config)
 {
 	return std::make_shared<CPicture>(ImagePath::fromJson(config["name"]), (int)config["x"].Float(), (int)config["y"].Float());

+ 4 - 6
client/mainmenu/CMainMenu.h

@@ -139,15 +139,15 @@ private:
 };
 
 /// Handles background screen, loads graphics for victory/loss condition and random town or hero selection
-class CMainMenu : public CIntObject, public std::enable_shared_from_this<CMainMenu>
+class CMainMenu final : public CIntObject, public std::enable_shared_from_this<CMainMenu>
 {
 	std::shared_ptr<CFilledTexture> backgroundAroundMenu;
 
 	std::vector<VideoPath> videoPlayList;
 
-	CMainMenu(); //Use CMainMenu::create
-
 public:
+	CMainMenu();
+
 	std::shared_ptr<CMenuScreen> menu;
 
 	~CMainMenu();
@@ -161,8 +161,6 @@ public:
 	static void openHighScoreScreen();
 	void openCampaignScreen(std::string name);
 
-	static std::shared_ptr<CMainMenu> create();
-
 	static std::shared_ptr<CPicture> createPicture(const JsonNode & config);
 
 	void playIntroVideos();
@@ -203,4 +201,4 @@ public:
 	void tick(uint32_t msPassed) override;
 };
 
-extern std::shared_ptr<CMainMenu> CMM;
+

+ 1 - 1
client/windows/settings/SettingsMainWindow.cpp

@@ -147,7 +147,7 @@ void SettingsMainWindow::mainMenuButtonCallback()
 		{
 			close();
 			GAME->server().endGameplay();
-			CMM->menu->switchToTab("main");
+			GAME->mainmenu()->menu->switchToTab("main");
 		},
 		0
 	);

+ 6 - 7
clientapp/EntryPoint.cpp

@@ -371,16 +371,15 @@ int main(int argc, char * argv[])
 		session["onlyai"].Bool() = true;
 		boost::thread(&CServerHandler::debugStartTest, &GAME->server(), session["testsave"].String(), true);
 	}
-	else
+	else if (!settings["session"]["headless"].Bool())
 	{
-		auto mmenu = CMainMenu::create();
-		mmenu->makeActiveInterface();
+		GAME->mainmenu()->makeActiveInterface();
 
-		bool playIntroVideo = !settings["session"]["headless"].Bool() && !vm.count("battle") && !vm.count("nointro") && settings["video"]["showIntro"].Bool();
+		bool playIntroVideo = !vm.count("battle") && !vm.count("nointro") && settings["video"]["showIntro"].Bool();
 		if(playIntroVideo)
-			mmenu->playIntroVideos();
+			GAME->mainmenu()->playIntroVideos();
 		else
-			mmenu->playMusic();
+			GAME->mainmenu()->playMusic();
 	}
 	
 	std::vector<std::string> names;
@@ -444,7 +443,7 @@ static void mainLoop()
 	}
 
 	GAME.reset();
-	CMM.reset();
+	GAME->mainmenu().reset();
 
 	if(!settings["session"]["headless"].Bool())
 	{