From 5a3fad8c2d2846f33821507930b7a7f833b66eff Mon Sep 17 00:00:00 2001 From: Björn Steinbrink Date: Mon, 20 Mar 2006 22:42:08 +0000 Subject: Fixed a crash on map change. --- src/engine.cpp | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'src/engine.cpp') diff --git a/src/engine.cpp b/src/engine.cpp index 8d78a078..724c0930 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -116,42 +116,47 @@ Engine::~Engine() void Engine::changeMap(const std::string &mapPath) { - // Clear floor items and beings + // Clean up floor items floorItemManager->clear(); - beingManager->clear(); - std::string oldMusic; - // Remove old map - if (mCurrentMap) { - oldMusic = mCurrentMap->getProperty("music"); - delete mCurrentMap; - } + beingManager->clear(); - // Generate full map path + // Store full map path in global var map_path = "maps/" + mapPath.substr(0, mapPath.rfind(".")) + ".tmx.gz"; // Attempt to load the new map - if (!(mCurrentMap = MapReader::readMap(map_path))) { + Map *newMap = MapReader::readMap(map_path); + + if (!newMap) { logger->error("Could not find map file"); } // Notify the minimap and beingManager about the map change Image *mapImage = NULL; - if (mCurrentMap->hasProperty("minimap")) { + if (newMap->hasProperty("minimap")) { ResourceManager *resman = ResourceManager::getInstance(); - mapImage = resman->getImage(mCurrentMap->getProperty("minimap")); + mapImage = resman->getImage(newMap->getProperty("minimap")); } minimap->setMapImage(mapImage); - beingManager->setMap(mCurrentMap); + beingManager->setMap(newMap); - // Change the music, if necessary - std::string newMusic = mCurrentMap->getProperty("music"); + // Start playing new music file when necessary + std::string oldMusic = ""; + + if (mCurrentMap) { + oldMusic = mCurrentMap->getProperty("music"); + delete mCurrentMap; + } + + std::string newMusic = newMap->getProperty("music"); if (newMusic != oldMusic) { - newMusic = std::string(TMW_DATADIR "data/music/") + newMusic; + newMusic = std::string(TMW_DATADIR) + "data/music/" + newMusic; sound.playMusic(newMusic.c_str(), -1); } + mCurrentMap = newMap; + // Send "map loaded" MessageOut outMsg(mNetwork); outMsg.writeInt16(CMSG_MAP_LOADED); -- cgit v1.2.3-70-g09d2