diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 781052ee..4cdc89a3 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -46,6 +46,8 @@ const float PHOTO_MODE_MAXHEIGHT = 500.0; const int CREATE_NEW_TEAM = -100; const int CANCEL_SWITCH_TEAM = -1; +const int fadeMusicMilliseconds = 3500; + Game::Game() : ProgramState(NULL) { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -192,6 +194,67 @@ Game::Game(Program *program, const GameSettings *gameSettings,bool masterserverM if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } +void Game::endGame() { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + Object::setStateCallback(NULL); + thisGamePtr = NULL; + if(originalDisplayMsgCallback != NULL) { + NetworkInterface::setDisplayMessageFunction(originalDisplayMsgCallback); + } + + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + Logger &logger= Logger::getInstance(); + Renderer &renderer= Renderer::getInstance(); + + logger.loadLoadingScreen(""); + logger.setState(Lang::getInstance().get("Deleting")); + //logger.add("Game", true); + logger.add("Game", false); + logger.hideProgress(); + + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + // Cannot Fade because sound files will be deleted below + SoundRenderer::getInstance().stopAllSounds(fadeMusicMilliseconds); + + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + +// deleteValues(aiInterfaces.begin(), aiInterfaces.end()); +// aiInterfaces.clear(); + + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + +// gui.end(); //selection must be cleared before deleting units + + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + +// world.end(); //must die before selection because of referencers + + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] aiInterfaces.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,aiInterfaces.size()); + + // MUST DO THIS LAST!!!! Because objects above have pointers to things like + // unit particles and fade them out etc and this end method deletes the original + // object pointers. + renderer.endGame(); + + GameConstants::updateFps = original_updateFps; + GameConstants::cameraFps = original_cameraFps; + + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + Unit::setGame(NULL); + + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] ==== END GAME ==== getCurrentPixelByteCount() = %llu\n",__FILE__,__FUNCTION__,__LINE__,(long long unsigned int)renderer.getCurrentPixelByteCount()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled) SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"==== END GAME ====\n"); + + //this->program->reInitGl(); + //renderer.reinitAll(); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); +} + Game::~Game() { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -215,6 +278,7 @@ Game::~Game() { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + // Cannot Fade because sound files will be deleted below SoundRenderer::getInstance().stopAllSounds(); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -591,7 +655,7 @@ void Game::load(int loadTypes) { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] loadTypes = %d, gameSettings = [%s]\n",__FILE__,__FUNCTION__,__LINE__,loadTypes,this->gameSettings.toString().c_str()); SoundRenderer &soundRenderer= SoundRenderer::getInstance(); - soundRenderer.stopAllSounds(); + soundRenderer.stopAllSounds(fadeMusicMilliseconds); Config &config = Config::getInstance(); Logger &logger= Logger::getInstance(); @@ -894,7 +958,7 @@ void Game::init(bool initForPreviewOnly) { //sounds SoundRenderer &soundRenderer= SoundRenderer::getInstance(); - soundRenderer.stopAllSounds(); + soundRenderer.stopAllSounds(fadeMusicMilliseconds); soundRenderer= SoundRenderer::getInstance(); Tileset *tileset= world.getTileset(); @@ -1208,7 +1272,7 @@ void Game::update() { //sounds SoundRenderer &soundRenderer= SoundRenderer::getInstance(); - soundRenderer.stopAllSounds(); + soundRenderer.stopAllSounds(fadeMusicMilliseconds); soundRenderer= SoundRenderer::getInstance(); Tileset *tileset= world.getTileset(); @@ -2435,11 +2499,16 @@ Stats Game::quitGame() { void Game::exitGameState(Program *program, Stats &endStats) { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - ProgramState *newState = new BattleEnd(program, &endStats); + Game *game = dynamic_cast(program->getState()); + if(game) { + game->endGame(); + } + + ProgramState *newState = new BattleEnd(program, &endStats, game); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - program->setState(newState); + program->setState(newState, false); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index bede98a6..15c44de1 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -218,6 +218,8 @@ public: virtual vector processTech(string techName); virtual void consoleAddLine(string line); + void endGame(); + private: //render void render3d(); diff --git a/source/glest_game/main/battle_end.cpp b/source/glest_game/main/battle_end.cpp index c9b6c251..0c474cb8 100644 --- a/source/glest_game/main/battle_end.cpp +++ b/source/glest_game/main/battle_end.cpp @@ -34,8 +34,10 @@ namespace Glest{ namespace Game{ // class BattleEnd // ===================================================== -BattleEnd::BattleEnd(Program *program, const Stats *stats): ProgramState(program) { +BattleEnd::BattleEnd(Program *program, const Stats *stats,ProgramState *originState): ProgramState(program) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d] stats = %p\n",__FILE__,__FUNCTION__,__LINE__,stats); + + this->originState = originState; if(stats != NULL) { this->stats= *stats; } @@ -61,6 +63,9 @@ BattleEnd::BattleEnd(Program *program, const Stats *stats): ProgramState(program BattleEnd::~BattleEnd() { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + delete originState; + originState = NULL; + SoundRenderer::getInstance().playMusic(CoreData::getInstance().getMenuMusic()); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/glest_game/main/battle_end.h b/source/glest_game/main/battle_end.h index ac1b861d..d65305e2 100644 --- a/source/glest_game/main/battle_end.h +++ b/source/glest_game/main/battle_end.h @@ -34,11 +34,12 @@ private: int mouse2d; GraphicMessageBox mainMessageBox; Texture2D *renderToTexture; + ProgramState *originState; void showMessageBox(const string &text, const string &header, bool toggle); public: - BattleEnd(Program *program, const Stats *stats); + BattleEnd(Program *program, const Stats *stats, ProgramState *originState); ~BattleEnd(); virtual void update(); diff --git a/source/glest_game/sound/sound_renderer.cpp b/source/glest_game/sound/sound_renderer.cpp index 8ca52b6e..a8fe829e 100644 --- a/source/glest_game/sound/sound_renderer.cpp +++ b/source/glest_game/sound/sound_renderer.cpp @@ -240,14 +240,14 @@ void SoundRenderer::stopAmbient(StrSound *strSound) { // ======================= Misc ============================ -void SoundRenderer::stopAllSounds() { +void SoundRenderer::stopAllSounds(int64 fadeOff) { if(soundPlayer != NULL) { MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); if(runThreadSafe == true) { safeMutex.setMutex(&mutex); } - soundPlayer->stopAllSounds(); + soundPlayer->stopAllSounds(fadeOff); } } diff --git a/source/glest_game/sound/sound_renderer.h b/source/glest_game/sound/sound_renderer.h index c2292314..e34f2a0b 100644 --- a/source/glest_game/sound/sound_renderer.h +++ b/source/glest_game/sound/sound_renderer.h @@ -77,7 +77,7 @@ public: void stopAmbient(StrSound *strSound); //misc - void stopAllSounds(); + void stopAllSounds(int64 fadeOff=0); void loadConfig(); bool wasInitOk() const; diff --git a/source/shared_lib/include/sound/ds8/sound_player_ds8.h b/source/shared_lib/include/sound/ds8/sound_player_ds8.h index a143a9c6..13ce1038 100644 --- a/source/shared_lib/include/sound/ds8/sound_player_ds8.h +++ b/source/shared_lib/include/sound/ds8/sound_player_ds8.h @@ -117,7 +117,7 @@ public: virtual void play(StaticSound *staticSound); virtual void play(StrSound *strSound, int64 fadeOn=0); virtual void stop(StrSound *strSound, int64 fadeOff=0); - virtual void stopAllSounds(); + virtual void stopAllSounds(int64 fadeOff=0); virtual void updateStreams(); //updates str buffers if needed private: diff --git a/source/shared_lib/include/sound/openal/sound_player_openal.h b/source/shared_lib/include/sound/openal/sound_player_openal.h index 40a87317..7b712224 100644 --- a/source/shared_lib/include/sound/openal/sound_player_openal.h +++ b/source/shared_lib/include/sound/openal/sound_player_openal.h @@ -100,7 +100,7 @@ public: virtual void play(StaticSound *staticSound); virtual void play(StrSound *strSound, int64 fadeOn=0); virtual void stop(StrSound *strSound, int64 fadeOff=0); - virtual void stopAllSounds(); + virtual void stopAllSounds(int64 fadeOff=0); virtual void updateStreams(); //updates str buffers if needed private: diff --git a/source/shared_lib/include/sound/sound_player.h b/source/shared_lib/include/sound/sound_player.h index 06ad86c7..13c6957d 100644 --- a/source/shared_lib/include/sound/sound_player.h +++ b/source/shared_lib/include/sound/sound_player.h @@ -54,7 +54,7 @@ public: virtual void play(StaticSound *staticSound)= 0; virtual void play(StrSound *strSound, int64 fadeOn=0)= 0; //delay and fade in miliseconds virtual void stop(StrSound *strSound, int64 fadeOff=0)= 0; - virtual void stopAllSounds()= 0; + virtual void stopAllSounds(int64 fadeOff=0)= 0; virtual void updateStreams()= 0; virtual bool wasInitOk() const { return initOk; } }; diff --git a/source/shared_lib/sources/sound/ds8/sound_player_ds8.cpp b/source/shared_lib/sources/sound/ds8/sound_player_ds8.cpp index 9353bfda..bbc996f2 100644 --- a/source/shared_lib/sources/sound/ds8/sound_player_ds8.cpp +++ b/source/shared_lib/sources/sound/ds8/sound_player_ds8.cpp @@ -427,12 +427,12 @@ void SoundPlayerDs8::stop(StrSound *strSound, int64 fadeOff){ } } -void SoundPlayerDs8::stopAllSounds(){ +void SoundPlayerDs8::stopAllSounds(int64 fadeOff){ if(initOk == false) return; for(unsigned int i=0; istop(); + source->stop(fadeOff); } }