From c295697abae7d9634b96a198e5527bb8bab713bd Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sun, 12 Sep 2010 05:05:08 +0000 Subject: [PATCH] - added map preview on custom game menu --- source/glest_game/game/game.cpp | 439 ++++++++---------- source/glest_game/game/game.h | 12 + source/glest_game/menu/main_menu.cpp | 3 +- source/glest_game/menu/main_menu.h | 5 + .../menu/menu_state_custom_game.cpp | 390 +++++++++++++++- .../glest_game/menu/menu_state_custom_game.h | 88 +++- source/glest_game/world/minimap.cpp | 12 +- source/glest_game/world/world.cpp | 13 +- source/glest_game/world/world.h | 1 + 9 files changed, 696 insertions(+), 267 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 80f31033..e4f31bc0 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -86,7 +86,7 @@ Game::Game(Program *program, const GameSettings *gameSettings): SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } -Game::~Game(){ +Game::~Game() { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); thisGamePtr = NULL; @@ -297,23 +297,34 @@ string Game::findFactionLogoFile(const GameSettings *settings, Logger *logger,st return result; } -void Game::load(){ +void Game::load() { + load(lgt_All); +} + +void Game::load(LoadGameItem loadTypes) { originalDisplayMsgCallback = NetworkInterface::getDisplayMessageFunction(); NetworkInterface::setDisplayMessageFunction(ErrorDisplayMessage); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] gameSettings = [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->gameSettings.toString().c_str()); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] loadTypes = %d, gameSettings = [%s]\n",__FILE__,__FUNCTION__,__LINE__,loadTypes,this->gameSettings.toString().c_str()); Config &config = Config::getInstance(); Logger &logger= Logger::getInstance(); - Game::findFactionLogoFile(&gameSettings, &logger); string mapName= gameSettings.getMap(); string tilesetName= gameSettings.getTileset(); string techName= gameSettings.getTech(); string scenarioName= gameSettings.getScenario(); + if((loadTypes & lgt_FactionPreview) == lgt_FactionPreview) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + Game::findFactionLogoFile(&gameSettings, &logger); + + SDL_PumpEvents(); + } + string scenarioDir = ""; if(gameSettings.getScenarioDir() != "") { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); scenarioDir = gameSettings.getScenarioDir(); if(EndsWith(scenarioDir, ".xml") == true) { scenarioDir = scenarioDir.erase(scenarioDir.size() - 4, 4); @@ -321,128 +332,15 @@ void Game::load(){ } } -/* - bool loadingImageUsed=false; - - logger.setState(Lang::getInstance().get("Loading")); - - if(scenarioName.empty()){ - logger.setSubtitle(formatString(mapName)+" - "+formatString(tilesetName)+" - "+formatString(techName)); - } - else{ - logger.setSubtitle(formatString(scenarioName)); - } - - Config &config = Config::getInstance(); - //good_fpu_control_registers(NULL,__FILE__,__FUNCTION__,__LINE__); - - //bool skipCustomLoadScreen = true; - bool skipCustomLoadScreen = false; - - string scenarioDir = ""; - if(skipCustomLoadScreen == false && gameSettings.getScenarioDir() != "") { - scenarioDir = gameSettings.getScenarioDir(); - if(EndsWith(scenarioDir, ".xml") == true) { - scenarioDir = scenarioDir.erase(scenarioDir.size() - 4, 4); - scenarioDir = scenarioDir.erase(scenarioDir.size() - gameSettings.getScenario().size(), gameSettings.getScenario().size() + 1); - } - // use a scenario based loading screen - vector loadScreenList; - findAll(scenarioDir + "loading_screen.*", loadScreenList, false, false); - if(loadScreenList.size() > 0) { - //string senarioLogo = scenarioDir + "/" + "loading_screen.jpg"; - string senarioLogo = scenarioDir + loadScreenList[0]; - if(fileExists(senarioLogo) == true) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] found scenario loading screen '%s'\n",__FILE__,__FUNCTION__,senarioLogo.c_str()); - - logger.loadLoadingScreen(senarioLogo); - loadingImageUsed=true; - } - } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s]\n",__FILE__,__FUNCTION__,__LINE__,gameSettings.getScenarioDir().c_str(),gameSettings.getScenario().c_str(),scenarioDir.c_str()); - } - - // give CPU time to update other things to avoid apperance of hanging - sleep(0); - //SDL_PumpEvents(); - - if(skipCustomLoadScreen == false && loadingImageUsed == false){ - // try to use a faction related loading screen - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Searching for faction loading screen\n",__FILE__,__FUNCTION__); - for ( int i=0; i < gameSettings.getFactionCount(); ++i ) { - if( gameSettings.getFactionControl(i) == ctHuman || - (gameSettings.getFactionControl(i) == ctNetwork && gameSettings.getThisFactionIndex() == i)){ - vector pathList=config.getPathListForType(ptTechs,scenarioDir); - for(int idx = 0; idx < pathList.size(); idx++) { - const string path = pathList[idx]+ "/" +techName+ "/"+ "factions"+ "/"+ gameSettings.getFactionTypeName(i); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] possible loading screen dir '%s'\n",__FILE__,__FUNCTION__,path.c_str()); - if(isdir(path.c_str()) == true) { - vector loadScreenList; - findAll(path + "/" + "loading_screen.*", loadScreenList, false, false); - if(loadScreenList.size() > 0) { - //string factionLogo = path + "/" + "loading_screen.jpg"; - string factionLogo = path + "/" + loadScreenList[0]; - - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] looking for loading screen '%s'\n",__FILE__,__FUNCTION__,factionLogo.c_str()); - - if(fileExists(factionLogo) == true) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] found loading screen '%s'\n",__FILE__,__FUNCTION__,factionLogo.c_str()); - - logger.loadLoadingScreen(factionLogo); - loadingImageUsed = true; - break; - } - } - } - - if(loadingImageUsed == true) { - break; - } - } - break; - } - } - } - if(skipCustomLoadScreen == false && loadingImageUsed == false){ - // try to use a tech related loading screen - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Searching for tech loading screen\n",__FILE__,__FUNCTION__); - - vector pathList=config.getPathListForType(ptTechs,scenarioDir); - for(int idx = 0; idx < pathList.size(); idx++) { - const string path = pathList[idx]+ "/" +techName; - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] possible loading screen dir '%s'\n",__FILE__,__FUNCTION__,path.c_str()); - if(isdir(path.c_str()) == true) { - vector loadScreenList; - findAll(path + "/" + "loading_screen.*", loadScreenList, false, false); - if(loadScreenList.size() > 0) { - //string factionLogo = path + "/" + "loading_screen.jpg"; - string factionLogo = path + "/" + loadScreenList[0]; - - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] looking for loading screen '%s'\n",__FILE__,__FUNCTION__,factionLogo.c_str()); - - if(fileExists(factionLogo) == true) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] found loading screen '%s'\n",__FILE__,__FUNCTION__,factionLogo.c_str()); - - logger.loadLoadingScreen(factionLogo); - loadingImageUsed = true; - break; - } - } - } - if(loadingImageUsed == true) { - break; - } - } - } -*/ //throw runtime_error("Test!"); - - SDL_PumpEvents(); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //tileset - world.loadTileset(config.getPathListForType(ptTilesets,scenarioDir), tilesetName, &checksum); + if((loadTypes & lgt_TileSet) == lgt_TileSet) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + world.loadTileset( config.getPathListForType(ptTilesets,scenarioDir), + tilesetName, &checksum); + } // give CPU time to update other things to avoid apperance of hanging sleep(0); @@ -450,31 +348,36 @@ void Game::load(){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - set factions; + set factions; + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + for ( int i=0; i < gameSettings.getFactionCount(); ++i ) { factions.insert(gameSettings.getFactionTypeName(i)); } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + if((loadTypes & lgt_TechTree) == lgt_TechTree) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - //tech, load before map because of resources - world.loadTech(config.getPathListForType(ptTechs,scenarioDir), techName, factions, &checksum); + //tech, load before map because of resources + world.loadTech( config.getPathListForType(ptTechs,scenarioDir), techName, + factions, &checksum); - // Validate the faction setup to ensure we don't have any bad associations - /* - std::vector results = world.validateFactionTypes(); - if(results.size() > 0) { - // Display the validation errors - string errorText = "Errors were detected:\n"; - for(int i = 0; i < results.size(); ++i) { - if(i > 0) { - errorText += "\n"; + // Validate the faction setup to ensure we don't have any bad associations + /* + std::vector results = world.validateFactionTypes(); + if(results.size() > 0) { + // Display the validation errors + string errorText = "Errors were detected:\n"; + for(int i = 0; i < results.size(); ++i) { + if(i > 0) { + errorText += "\n"; + } + errorText += results[i]; } - errorText += results[i]; + throw runtime_error(errorText); } - throw runtime_error(errorText); - } - */ + */ + } // give CPU time to update other things to avoid apperance of hanging sleep(0); @@ -483,7 +386,10 @@ void Game::load(){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //map - world.loadMap(Map::getMapPath(mapName,scenarioDir), &checksum); + if((loadTypes & lgt_Map) == lgt_Map) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + world.loadMap(Map::getMapPath(mapName,scenarioDir), &checksum); + } // give CPU time to update other things to avoid apperance of hanging sleep(0); @@ -492,10 +398,13 @@ void Game::load(){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //scenario - if(!scenarioName.empty()){ - Lang::getInstance().loadScenarioStrings(gameSettings.getScenarioDir(), scenarioName); - world.loadScenario(gameSettings.getScenarioDir(), &checksum); - } + if((loadTypes & lgt_Scenario) == lgt_Scenario) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(scenarioName.empty() == false) { + Lang::getInstance().loadScenarioStrings(gameSettings.getScenarioDir(), scenarioName); + world.loadScenario(gameSettings.getScenarioDir(), &checksum); + } + } // give CPU time to update other things to avoid apperance of hanging sleep(0); @@ -505,9 +414,13 @@ void Game::load(){ //good_fpu_control_registers(NULL,__FILE__,__FUNCTION__,__LINE__); } -void Game::init() +void Game::init() { + init(false); +} + +void Game::init(bool initForPreviewOnly) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] initForPreviewOnly = %d\n",__FILE__,__FUNCTION__,__LINE__,initForPreviewOnly); Lang &lang= Lang::getInstance(); Logger &logger= Logger::getInstance(); @@ -516,141 +429,167 @@ void Game::init() Map *map= world.getMap(); NetworkManager &networkManager= NetworkManager::getInstance(); + if(map == NULL) { + throw runtime_error("map == NULL"); + } + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - logger.setState(lang.get("Initializing")); - //mesage box - mainMessageBox.init(lang.get("Yes"), lang.get("No")); - mainMessageBox.setEnabled(false); + if(initForPreviewOnly == false) { + logger.setState(lang.get("Initializing")); - //mesage box - errorMessageBox.init(lang.get("Ok")); - errorMessageBox.setEnabled(false); - errorMessageBox.setY(mainMessageBox.getY() - mainMessageBox.getH() - 10); + //mesage box + mainMessageBox.init(lang.get("Yes"), lang.get("No")); + mainMessageBox.setEnabled(false); - //init world, and place camera - commander.init(&world); + //mesage box + errorMessageBox.init(lang.get("Ok")); + errorMessageBox.setEnabled(false); + errorMessageBox.setY(mainMessageBox.getY() - mainMessageBox.getH() - 10); - // give CPU time to update other things to avoid apperance of hanging - sleep(0); - SDL_PumpEvents(); + //init world, and place camera + commander.init(&world); + + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + SDL_PumpEvents(); + } world.init(this, gameSettings.getDefaultUnits()); - // give CPU time to update other things to avoid apperance of hanging - sleep(0); - SDL_PumpEvents(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - gui.init(this); + if(initForPreviewOnly == false) { + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + SDL_PumpEvents(); - // give CPU time to update other things to avoid apperance of hanging - sleep(0); - //SDL_PumpEvents(); + gui.init(this); - chatManager.init(&console, world.getThisTeamIndex()); - console.clearStoredLines(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + //SDL_PumpEvents(); + + chatManager.init(&console, world.getThisTeamIndex()); + console.clearStoredLines(); + } - const Vec2i &v= map->getStartLocation(world.getThisFaction()->getStartLocationIndex()); gameCamera.init(map->getW(), map->getH()); - gameCamera.setPos(Vec2f(v.x, v.y)); - // give CPU time to update other things to avoid apperance of hanging - sleep(0); - SDL_PumpEvents(); + if(world.getThisFaction() != NULL) { + const Vec2i &v= map->getStartLocation(world.getThisFaction()->getStartLocationIndex()); + gameCamera.setPos(Vec2f(v.x, v.y)); + } - scriptManager.init(&world, &gameCamera); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - //good_fpu_control_registers(NULL,__FILE__,__FUNCTION__,__LINE__); + if(initForPreviewOnly == false) { + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + SDL_PumpEvents(); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] creating AI's\n",__FILE__,__FUNCTION__); + scriptManager.init(&world, &gameCamera); - //create IAs - aiInterfaces.resize(world.getFactionCount()); - for(int i=0; igetCpuControl()){ - aiInterfaces[i]= new AiInterface(*this, i, faction->getTeam()); - logger.add("Creating AI for faction " + intToStr(i), true); + //good_fpu_control_registers(NULL,__FILE__,__FUNCTION__,__LINE__); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] creating AI's\n",__FILE__,__FUNCTION__,__LINE__); + + //create IAs + aiInterfaces.resize(world.getFactionCount()); + for(int i=0; i < world.getFactionCount(); ++i) { + Faction *faction= world.getFaction(i); + if(faction->getCpuControl()) { + aiInterfaces[i]= new AiInterface(*this, i, faction->getTeam()); + logger.add("Creating AI for faction " + intToStr(i), true); + } + else { + aiInterfaces[i]= NULL; + } } - else{ - aiInterfaces[i]= NULL; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + SDL_PumpEvents(); + + //weather particle systems + if(world.getTileset()->getWeather() == wRainy) { + logger.add("Creating rain particle system", true); + weatherParticleSystem= new RainParticleSystem(); + weatherParticleSystem->setSpeed(12.f/GameConstants::updateFps); + weatherParticleSystem->setPos(gameCamera.getPos()); + renderer.manageParticleSystem(weatherParticleSystem, rsGame); } - } - - // give CPU time to update other things to avoid apperance of hanging - sleep(0); - SDL_PumpEvents(); - - //wheather particle systems - if(world.getTileset()->getWeather() == wRainy){ - logger.add("Creating rain particle system", true); - weatherParticleSystem= new RainParticleSystem(); - weatherParticleSystem->setSpeed(12.f/GameConstants::updateFps); - weatherParticleSystem->setPos(gameCamera.getPos()); - renderer.manageParticleSystem(weatherParticleSystem, rsGame); - } - else if(world.getTileset()->getWeather() == wSnowy){ - logger.add("Creating snow particle system", true); - weatherParticleSystem= new SnowParticleSystem(1200); - weatherParticleSystem->setSpeed(1.5f/GameConstants::updateFps); - weatherParticleSystem->setPos(gameCamera.getPos()); - weatherParticleSystem->setTexture(coreData.getSnowTexture()); - renderer.manageParticleSystem(weatherParticleSystem, rsGame); - } + else if(world.getTileset()->getWeather() == wSnowy) { + logger.add("Creating snow particle system", true); + weatherParticleSystem= new SnowParticleSystem(1200); + weatherParticleSystem->setSpeed(1.5f/GameConstants::updateFps); + weatherParticleSystem->setPos(gameCamera.getPos()); + weatherParticleSystem->setTexture(coreData.getSnowTexture()); + renderer.manageParticleSystem(weatherParticleSystem, rsGame); + } + } //init renderer state SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Initializing renderer\n",__FILE__,__FUNCTION__); logger.add("Initializing renderer", true); renderer.initGame(this); - //good_fpu_control_registers(NULL,__FILE__,__FUNCTION__,__LINE__); + if(initForPreviewOnly == false) { + //good_fpu_control_registers(NULL,__FILE__,__FUNCTION__,__LINE__); - // give CPU time to update other things to avoid apperance of hanging - sleep(0); - SDL_PumpEvents(); + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + SDL_PumpEvents(); - //sounds - SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //sounds + SoundRenderer &soundRenderer= SoundRenderer::getInstance(); - Tileset *tileset= world.getTileset(); - AmbientSounds *ambientSounds= tileset->getAmbientSounds(); + Tileset *tileset= world.getTileset(); + AmbientSounds *ambientSounds= tileset->getAmbientSounds(); - //rain - if(tileset->getWeather()==wRainy && ambientSounds->isEnabledRain()){ - logger.add("Starting ambient stream", true); - soundRenderer.playAmbient(ambientSounds->getRain()); + //rain + if(tileset->getWeather() == wRainy && ambientSounds->isEnabledRain()) { + logger.add("Starting ambient stream", true); + soundRenderer.playAmbient(ambientSounds->getRain()); + } + + //snow + if(tileset->getWeather() == wSnowy && ambientSounds->isEnabledSnow()) { + logger.add("Starting ambient stream", true); + soundRenderer.playAmbient(ambientSounds->getSnow()); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Waiting for network\n",__FILE__,__FUNCTION__); + logger.add("Waiting for network players", true); + networkManager.getGameNetworkInterface()->waitUntilReady(&checksum); + + //std::string worldLog = world.DumpWorldToLog(true); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Starting music stream\n",__FILE__,__FUNCTION__,__LINE__); + logger.add("Starting music stream", true); + + if(world.getThisFaction() == NULL) { + throw runtime_error("world.getThisFaction() == NULL"); + } + if(world.getThisFaction()->getType() == NULL) { + throw runtime_error("world.getThisFaction()->getType() == NULL"); + } + //if(world.getThisFaction()->getType()->getMusic() == NULL) { + // throw runtime_error("world.getThisFaction()->getType()->getMusic() == NULL"); + //} + + StrSound *gameMusic= world.getThisFaction()->getType()->getMusic(); + soundRenderer.playMusic(gameMusic); + + logger.add("Launching game"); } - //snow - if(tileset->getWeather()==wSnowy && ambientSounds->isEnabledSnow()){ - logger.add("Starting ambient stream", true); - soundRenderer.playAmbient(ambientSounds->getSnow()); - } - - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Waiting for network\n",__FILE__,__FUNCTION__); - logger.add("Waiting for network players", true); - networkManager.getGameNetworkInterface()->waitUntilReady(&checksum); - - //std::string worldLog = world.DumpWorldToLog(true); - - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Starting music stream\n",__FILE__,__FUNCTION__,__LINE__); - logger.add("Starting music stream", true); - - if(world.getThisFaction() == NULL) { - throw runtime_error("world.getThisFaction() == NULL"); - } - if(world.getThisFaction()->getType() == NULL) { - throw runtime_error("world.getThisFaction()->getType() == NULL"); - } - //if(world.getThisFaction()->getType()->getMusic() == NULL) { - // throw runtime_error("world.getThisFaction()->getType()->getMusic() == NULL"); - //} - - StrSound *gameMusic= world.getThisFaction()->getType()->getMusic(); - soundRenderer.playMusic(gameMusic); - - logger.add("Launching game"); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"================ STARTING GAME ================\n"); SystemFlags::OutputDebug(SystemFlags::debugPathFinder,"================ STARTING GAME ================\n"); SystemFlags::OutputDebug(SystemFlags::debugPathFinder,"PathFinderType: %s\n", (getGameSettings()->getPathFinderType() ? "RoutePlanner" : "PathFinder")); @@ -661,7 +600,7 @@ void Game::init() // ==================== update ==================== //update -void Game::update(){ +void Game::update() { //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); try { diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index b51bba62..908dce9b 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -32,6 +32,16 @@ namespace Glest{ namespace Game{ class GraphicMessageBox; +enum LoadGameItem { + lgt_FactionPreview = 0x01, + lgt_TileSet = 0x02, + lgt_TechTree = 0x04, + lgt_Map = 0x08, + lgt_Scenario = 0x10, + + lgt_All = (lgt_FactionPreview | lgt_TileSet | lgt_TechTree | lgt_Map | lgt_Scenario) +}; + // ===================================================== // class Game // @@ -117,8 +127,10 @@ public: const int getTotalRenderFps() const {return totalRenderFps;} //init + virtual void load(LoadGameItem loadTypes); virtual void load(); virtual void init(); + virtual void init(bool initForPreviewOnly); virtual void update(); virtual void updateCamera(); virtual void render(); diff --git a/source/glest_game/menu/main_menu.cpp b/source/glest_game/menu/main_menu.cpp index eb3cc226..ff8b900f 100644 --- a/source/glest_game/menu/main_menu.cpp +++ b/source/glest_game/menu/main_menu.cpp @@ -95,10 +95,11 @@ void MainMenu::render(){ fps++; - renderer.clearBuffers(); + renderer.clearBuffers(); //3d renderer.reset3dMenu(); + renderer.clearZBuffer(); renderer.loadCameraMatrix(menuBackground.getCamera()); renderer.renderMenuBackground(&menuBackground); diff --git a/source/glest_game/menu/main_menu.h b/source/glest_game/menu/main_menu.h index c002e61c..ccaf3f1a 100644 --- a/source/glest_game/menu/main_menu.h +++ b/source/glest_game/menu/main_menu.h @@ -105,6 +105,11 @@ public: void setState(MenuState *state); virtual bool isInSpecialKeyCaptureEvent(); + + int getMouseX() const {return mouseX;} + int getMouseY() const {return mouseY;} + int getMouse2dAnim() const {return mouse2dAnim;} + }; diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 22b580e1..087e80ef 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -150,7 +150,7 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b for ( int i = 0 ; irenderProgramMsgBox(); + + if(mapPreview.hasFileLoaded() == true) { + + int mouseX = mainMenu->getMouseX(); + int mouseY = mainMenu->getMouseY(); + int mouse2dAnim = mainMenu->getMouse2dAnim(); + + renderer.renderMouse2d(mouseX, mouseY, mouse2dAnim); + renderMap(&mapPreview, 0, 0, 250, 250, 2, 10, 350); + } } catch(const std::exception &ex) { char szBuf[1024]=""; @@ -1099,6 +1109,170 @@ void MenuStateCustomGame::render() { } } +void MenuStateCustomGame::renderMap(const MapPreview *map, int x, int y, + int clientW, int clientH, int cellSize, + int screenX, int screenY) { + float alt=0; + float showWater=0; + + assertGl(); + + glMatrixMode(GL_PROJECTION); + + glLoadIdentity(); + + glOrtho(0, clientW, 0, clientH, 1, -1); + + glViewport(screenX, screenY, clientW, clientH); + + glMatrixMode(GL_MODELVIEW); + + glPushMatrix(); + + glPushAttrib(GL_CURRENT_BIT); + + glTranslatef(static_cast(x), static_cast(y), 0.0f); + glLineWidth(1); + //glClear(GL_COLOR_BUFFER_BIT); + glColor3f(0, 0, 0); + + + for (int j = 0; j < map->getH(); j++) { + for (int i = 0; i < map->getW(); i++) { + if (i * cellSize + x > -cellSize + && i * cellSize + x < clientW + && clientH - cellSize - j * cellSize + y > -cellSize + && clientH - cellSize - j * cellSize + y < clientH) { + + //surface + alt = map->getHeight(i, j) / 20.f; + showWater = map->getWaterLevel()/ 20.f - alt; + showWater = (showWater > 0)? showWater:0; + Vec3f surfColor; + switch (map->getSurface(i, j)) { + case 1: surfColor = Vec3f(0.0, 0.8f * alt, 0.f + showWater); break; + case 2: surfColor = Vec3f(0.4f * alt, 0.6f * alt, 0.f + showWater); break; + case 3: surfColor = Vec3f(0.6f * alt, 0.3f * alt, 0.f + showWater); break; + case 4: surfColor = Vec3f(0.7f * alt, 0.7f * alt, 0.7f * alt + showWater); break; + case 5: surfColor = Vec3f(0.7f * alt, 0.5f * alt, 0.3f * alt + showWater); break; + } + + glColor3fv(surfColor.ptr()); + + glBegin(GL_TRIANGLE_STRIP); + glVertex2i(i * cellSize, clientH - j * cellSize - cellSize); + glVertex2i(i * cellSize, clientH - j * cellSize); + glVertex2i(i * cellSize + cellSize, clientH - j * cellSize - cellSize); + glVertex2i(i * cellSize + cellSize, clientH - j * cellSize); + glEnd(); + + //objects + switch (map->getObject(i, j)) { + case 0: glColor3f(0.f, 0.f, 0.f); break; + case 1: glColor3f(1.f, 0.f, 0.f); break; + case 2: glColor3f(1.f, 1.f, 1.f); break; + case 3: glColor3f(0.5f, 0.5f, 1.f); break; + case 4: glColor3f(0.f, 0.f, 1.f); break; + case 5: glColor3f(0.5f, 0.5f, 0.5f); break; + case 6: glColor3f(1.f, 0.8f, 0.5f); break; + case 7: glColor3f(0.f, 1.f, 1.f); break; + case 8: glColor3f(0.7f, 0.1f, 0.3f); break; + case 9: glColor3f(0.5f, 1.f, 0.1f); break; + case 10: glColor3f(1.f, 0.2f, 0.8f); break; + } + + if (map->getObject(i, j) != 0) { + glPointSize(cellSize / 2.f); + glBegin(GL_POINTS); + glVertex2i(i * cellSize + cellSize / 2, clientH - j * cellSize - cellSize / 2); + glEnd(); + } + +// bool found = false; + + //height lines +// if (!found) { + glColor3fv((surfColor*0.5f).ptr()); + //left + if (i > 0 && map->getHeight(i - 1, j) > map->getHeight(i, j)) { + glBegin(GL_LINES); + glVertex2i(i * cellSize, clientH - (j + 1) * cellSize); + glVertex2i(i * cellSize, clientH - j * cellSize); + glEnd(); + } + //down + if (j > 0 && map->getHeight(i, j - 1) > map->getHeight(i, j)) { + glBegin(GL_LINES); + glVertex2i(i * cellSize, clientH - j * cellSize); + glVertex2i((i + 1) * cellSize, clientH - j * cellSize); + glEnd(); + } + + glColor3fv((surfColor*2.f).ptr()); + //left + if (i > 0 && map->getHeight(i - 1, j) < map->getHeight(i, j)) { + glBegin(GL_LINES); + glVertex2i(i * cellSize, clientH - (j + 1) * cellSize); + glVertex2i(i * cellSize, clientH - j * cellSize); + glEnd(); + } + if (j > 0 && map->getHeight(i, j - 1) < map->getHeight(i, j)) { + glBegin(GL_LINES); + glVertex2i(i * cellSize, clientH - j * cellSize); + glVertex2i((i + 1) * cellSize, clientH - j * cellSize); + glEnd(); + } +// } + + //resources + switch (map->getResource(i, j)) { + case 1: glColor3f(1.f, 1.f, 0.f); break; + case 2: glColor3f(0.5f, 0.5f, 0.5f); break; + case 3: glColor3f(1.f, 0.f, 0.f); break; + case 4: glColor3f(0.f, 0.f, 1.f); break; + case 5: glColor3f(0.5f, 0.5f, 1.f); break; + } + + if (map->getResource(i, j) != 0) { + glBegin(GL_LINES); + glVertex2i(i * cellSize, clientH - j * cellSize - cellSize); + glVertex2i(i * cellSize + cellSize, clientH - j * cellSize); + glVertex2i(i * cellSize, clientH - j * cellSize); + glVertex2i(i * cellSize + cellSize, clientH - j * cellSize - cellSize); + glEnd(); + } + } + } + } + + + //start locations + glLineWidth(3); + for (int i = 0; i < map->getMaxFactions(); i++) { + switch (i) { + case 0: glColor3f(1.f, 0.f, 0.f); break; + case 1: glColor3f(0.f, 0.f, 1.f); break; + case 2: glColor3f(0.f, 1.f, 0.f); break; + case 3: glColor3f(1.f, 1.f, 0.f); break; + case 4: glColor3f(1.f, 1.f, 1.f); break; + case 5: glColor3f(0.f, 1.f, 0.8f); break; + case 6: glColor3f(1.f, 0.5f, 0.f); break; + case 7: glColor3f(1.f, 0.5f, 1.f); break; + } + glBegin(GL_LINES); + glVertex2i((map->getStartLocationX(i) - 1) * cellSize, clientH - (map->getStartLocationY(i) - 1) * cellSize); + glVertex2i((map->getStartLocationX(i) + 1) * cellSize + cellSize, clientH - (map->getStartLocationY(i) + 1) * cellSize - cellSize); + glVertex2i((map->getStartLocationX(i) - 1) * cellSize, clientH - (map->getStartLocationY(i) + 1) * cellSize - cellSize); + glVertex2i((map->getStartLocationX(i) + 1) * cellSize + cellSize, clientH - (map->getStartLocationY(i) - 1) * cellSize); + glEnd(); + } + + glPopMatrix(); + glPopAttrib(); + + assertGl(); +} + void MenuStateCustomGame::update() { Chrono chrono; chrono.start(); @@ -1907,7 +2081,7 @@ GameSettings MenuStateCustomGame::loadGameSettingsFromFile(std::string fileName) mapFile = formatString(mapFile); listBoxMap.setSelectedItem(mapFile); - loadMapInfo(Map::getMapPath(getCurrentMapFile()), &mapInfo); + loadMapInfo(Map::getMapPath(getCurrentMapFile()), &mapInfo, true); labelMapInfo.setText(mapInfo.desc); string tilesetFile = gameSettings.getTileset(); @@ -2021,7 +2195,7 @@ bool MenuStateCustomGame::hasNetworkGameSettings() { return hasNetworkSlot; } -void MenuStateCustomGame::loadMapInfo(string file, MapInfo *mapInfo){ +void MenuStateCustomGame::loadMapInfo(string file, MapInfo *mapInfo, bool loadMapPreview) { struct MapFileHeader{ int32 version; @@ -2061,9 +2235,14 @@ void MenuStateCustomGame::loadMapInfo(string file, MapInfo *mapInfo){ labelNetStatus[i].setVisible(i+1 <= mapInfo->players); } + // Not painting properly so this is on hold + if(loadMapPreview == true) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + mapPreview.loadFromFile(file.c_str()); + } } catch(exception e){ - throw runtime_error("Error loading map file: "+file+'\n'+e.what()); + throw runtime_error("Error loading map file: [" + file + "] msg: " + e.what()); } } @@ -2436,4 +2615,201 @@ void MenuStateCustomGame::cleanupFactionTexture() { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } + +// ======================================= + +MapPreview::MapPreview() { + altFactor = 3; + waterLevel = 4; + cells = NULL; + startLocations = NULL; + reset(128, 128, 10.f, 1); + resetFactions(8); + title = ""; + desc = ""; + author = ""; + refAlt = 10; +} + +MapPreview::~MapPreview() { + delete [] startLocations; + startLocations = NULL; + + for (int i = 0; i < h; i++) { + delete [] cells[i]; + } + delete [] cells; + cells = NULL; +} + + +float MapPreview::getHeight(int x, int y) const { + return cells[x][y].height; +} + +int MapPreview::getSurface(int x, int y) const { + return cells[x][y].surface; +} + +int MapPreview::getObject(int x, int y) const { + return cells[x][y].object; +} + +int MapPreview::getResource(int x, int y) const { + return cells[x][y].resource; +} + +int MapPreview::getStartLocationX(int index) const { + return startLocations[index].x; +} + +int MapPreview::getStartLocationY(int index) const { + return startLocations[index].y; +} + +bool MapPreview::inside(int x, int y) { + return (x >= 0 && x < w && y >= 0 && y < h); +} + +void MapPreview::reset(int w, int h, float alt, int surf) { + if (w < 16 || h < 16) { + throw runtime_error("Size of map must be at least 16x16"); + return; + } + + if (w > 1024 || h > 1024) { + throw runtime_error("Size of map can be at most 1024x1024"); + return; + } + + if (alt < 0 || alt > 20) { + throw runtime_error("Height must be in the range 0-20"); + return; + } + + if (surf < 1 || surf > 5) { + throw runtime_error("Surface must be in the range 1-5"); + return; + } + + if (cells != NULL) { + for (int i = 0; i < this->w; i++) { + delete [] cells[i]; + } + delete [] cells; + } + + this->w = w; + this->h = h; + this->maxFactions = maxFactions; + + cells = new Cell*[w]; + for (int i = 0; i < w; i++) { + cells[i] = new Cell[h]; + for (int j = 0; j < h; j++) { + cells[i][j].height = alt; + cells[i][j].object = 0; + cells[i][j].resource = 0; + cells[i][j].surface = surf; + } + } +} + +void MapPreview::resetFactions(int maxPlayers) { + if (maxPlayers<1 || maxPlayers>8){ + throw runtime_error("Max Players must be in the range 1-8"); + } + + if (startLocations != NULL) { + delete [] startLocations; + startLocations = NULL; + } + + maxFactions = maxPlayers; + + startLocations = new StartLocation[maxFactions]; + for (int i = 0; i < maxFactions; i++) { + startLocations[i].x = 0; + startLocations[i].y = 0; + } +} + +int MapPreview::getHeightFactor() const { + return altFactor; +} + +int MapPreview::getWaterLevel() const { + return waterLevel; +} + +void MapPreview::loadFromFile(const string &path) { + altFactor = 3; + waterLevel = 4; + cells = NULL; + startLocations = NULL; + reset(128, 128, 10.f, 1); + resetFactions(8); + title = ""; + desc = ""; + author = ""; + refAlt = 10; + + FILE *f1 = fopen(path.c_str(), "rb"); + if (f1 != NULL) { + + //read header + MapFileHeaderPreview header; + size_t bytes = fread(&header, sizeof(MapFileHeaderPreview), 1, f1); + + altFactor = header.altFactor; + waterLevel = header.waterLevel; + title = header.title; + author = header.author; + desc = header.description; + + //read start locations + resetFactions(header.maxFactions); + for (int i = 0; i < maxFactions; ++i) { + bytes = fread(&startLocations[i].x, sizeof(int32), 1, f1); + bytes = fread(&startLocations[i].y, sizeof(int32), 1, f1); + } + + //read Heights + reset(header.width, header.height, 10, 1); + for (int j = 0; j < h; ++j) { + for (int i = 0; i < w; ++i) { + bytes = fread(&cells[i][j].height, sizeof(float), 1, f1); + } + } + + //read surfaces + for (int j = 0; j < h; ++j) { + for (int i = 0; i < w; ++i) { + bytes = fread(&cells[i][j].surface, sizeof(int8), 1, f1); + } + } + + //read objects + for (int j = 0; j < h; ++j) { + for (int i = 0; i < w; ++i) { + int8 obj; + bytes = fread(&obj, sizeof(int8), 1, f1); + if (obj <= 10) { + cells[i][j].object = obj; + } else { + cells[i][j].resource = obj - 10; + } + } + } + + fclose(f1); + + fileLoaded = true; + } else { + throw runtime_error("error opening map file: " + path); + } +} + +// ==================== PRIVATE ==================== + }}//end namespace diff --git a/source/glest_game/menu/menu_state_custom_game.h b/source/glest_game/menu/menu_state_custom_game.h index d0e2d6c9..01d050b9 100644 --- a/source/glest_game/menu/menu_state_custom_game.h +++ b/source/glest_game/menu/menu_state_custom_game.h @@ -15,9 +15,90 @@ #include "main_menu.h" #include "chat_manager.h" #include "simple_threads.h" +#include "game.h" #include "leak_dumper.h" namespace Glest{ namespace Game{ + +struct MapFileHeaderPreview { + int32 version; + int32 maxFactions; + int32 width; + int32 height; + int32 altFactor; + int32 waterLevel; + int8 title[128]; + int8 author[128]; + int8 description[256]; +}; + +// =============================================== +// class Map +// =============================================== + +class MapPreview { +public: + static const int maxHeight = 20; + static const int minHeight = 0; + +private: + struct Cell { + int surface; + int object; + int resource; + float height; + }; + + struct StartLocation { + int x; + int y; + }; + + RandomGen random; + string title; + string author; + string desc; + string recScn; + int type; + int h; + int w; + int altFactor; + int waterLevel; + Cell **cells; + int maxFactions; + StartLocation *startLocations; + int refAlt; + bool fileLoaded; + +public: + MapPreview(); + ~MapPreview(); + float getHeight(int x, int y) const; + int getSurface(int x, int y) const; + int getObject(int x, int y) const; + int getResource(int x, int y) const; + int getStartLocationX(int index) const; + int getStartLocationY(int index) const; + int getHeightFactor() const; + int getWaterLevel() const; + bool inside(int x, int y); + + int getH() const {return h;} + int getW() const {return w;} + int getMaxFactions() const {return maxFactions;} + string getTitle() const {return title;} + string getDesc() const {return desc;} + string getAuthor() const {return author;} + + void reset(int w, int h, float alt, int surf); + void resetFactions(int maxFactions); + + void loadFromFile(const string &path); + bool hasFileLoaded() const { return fileLoaded; } +}; + + + // =============================== // class MenuStateCustomGame // =============================== @@ -126,6 +207,8 @@ private: string currentFactionLogo; Texture2D *factionTexture; + MapPreview mapPreview; + public: MenuStateCustomGame(Program *program, MainMenu *mainMenu ,bool openNetworkSlots= false, bool parentMenuIsMasterserver=false); ~MenuStateCustomGame(); @@ -147,7 +230,7 @@ private: bool hasNetworkGameSettings(); void loadGameSettings(GameSettings *gameSettings); - void loadMapInfo(string file, MapInfo *mapInfo); + void loadMapInfo(string file, MapInfo *mapInfo,bool loadMapPreview); void reloadFactions(); void updateControlers(); void closeUnusedSlots(); @@ -165,6 +248,9 @@ private: void cleanupFactionTexture(); void loadFactionTexture(string filepath); + + void renderMap( const MapPreview *map, int x, int y, int clientW, + int clientH, int cellSize, int screenX, int screenY); }; }}//end namespace diff --git a/source/glest_game/world/minimap.cpp b/source/glest_game/world/minimap.cpp index ddb4b32b..c2cd9c64 100644 --- a/source/glest_game/world/minimap.cpp +++ b/source/glest_game/world/minimap.cpp @@ -30,7 +30,7 @@ namespace Glest{ namespace Game{ const float Minimap::exploredAlpha= 0.5f; -Minimap::Minimap(){ +Minimap::Minimap() { fowPixmap0= NULL; fowPixmap1= NULL; fogOfWar= true;//Config::getInstance().getBool("FogOfWar"); @@ -66,7 +66,7 @@ void Minimap::init(int w, int h, const World *world, bool fogOfWar){ computeTexture(world); } -Minimap::~Minimap(){ +Minimap::~Minimap() { Logger::getInstance().add("Minimap", true); delete fowPixmap0; delete fowPixmap1; @@ -74,7 +74,7 @@ Minimap::~Minimap(){ // ==================== set ==================== -void Minimap::incFowTextureAlphaSurface(const Vec2i &sPos, float alpha){ +void Minimap::incFowTextureAlphaSurface(const Vec2i &sPos, float alpha) { assert(sPos.xgetW() && sPos.ygetH()); @@ -83,7 +83,7 @@ void Minimap::incFowTextureAlphaSurface(const Vec2i &sPos, float alpha){ } } -void Minimap::resetFowTex(){ +void Minimap::resetFowTex() { Pixmap2D *tmpPixmap= fowPixmap0; fowPixmap0= fowPixmap1; fowPixmap1= tmpPixmap; @@ -111,7 +111,7 @@ void Minimap::resetFowTex(){ } } -void Minimap::updateFowTex(float t){ +void Minimap::updateFowTex(float t) { for(int i=0; igetW(); ++i){ for(int j=0; jgetH(); ++j){ float p1= fowPixmap1->getPixelf(i, j); @@ -125,7 +125,7 @@ void Minimap::updateFowTex(float t){ // ==================== PRIVATE ==================== -void Minimap::computeTexture(const World *world){ +void Minimap::computeTexture(const World *world) { Vec3f color; const Map *map= world->getMap(); diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index b7aee08f..7d87353f 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -861,10 +861,15 @@ void World::initFactionTypes(GameSettings *gs){ //create stats stats.init(gs->getFactionCount(), gs->getThisFactionIndex(), gs->getDescription()); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //create factions this->thisFactionIndex= gs->getThisFactionIndex(); factions.resize(gs->getFactionCount()); - for(int i=0; igetType(gs->getFactionTypeName(i)); factions[i].init( ft, gs->getFactionControl(i), techTree, game, i, gs->getTeam(i), @@ -878,7 +883,11 @@ void World::initFactionTypes(GameSettings *gs){ stats.setPlayerColor(i,getFaction(i)->getTexture()->getPixmap()->getPixel3f(0, 0)); } - thisTeamIndex= getFaction(thisFactionIndex)->getTeam(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + if(factions.size() > 0) { + thisTeamIndex= getFaction(thisFactionIndex)->getTeam(); + } SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } diff --git a/source/glest_game/world/world.h b/source/glest_game/world/world.h index 936003a0..07631d90 100644 --- a/source/glest_game/world/world.h +++ b/source/glest_game/world/world.h @@ -157,6 +157,7 @@ public: const Faction *getFaction(int i) const {return &factions[i];} Faction *getFaction(int i) {return &factions[i];} const Minimap *getMinimap() const {return &minimap;} + Minimap *getMiniMapObject() {return &minimap;} const Stats *getStats() const {return &stats;}; Stats *getStats() {return &stats;}; const WaterEffects *getWaterEffects() const {return &waterEffects;}