Modified music handling, fixing the music heard during GP lost/won

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@5513 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria 2010-06-13 18:25:49 +00:00
parent c496a82654
commit 4afb71659f
15 changed files with 63 additions and 17 deletions

View File

@ -38,7 +38,8 @@ public:
virtual void updateFading(float percent) = 0;
virtual void updateFaster(float percent, float pitch) = 0;
virtual void update () = 0;
virtual bool isPlaying () = 0;
virtual ~Music () {};
};

View File

@ -339,3 +339,10 @@ void MusicInformation::switchToFastMusic()
} // switchToFastMusic
//-----------------------------------------------------------------------------
bool MusicInformation::isPlaying() const
{
return (m_normal_music != NULL && m_normal_music->isPlaying()) || (m_fast_music != NULL && m_fast_music->isPlaying());
}
//-----------------------------------------------------------------------------

View File

@ -82,5 +82,6 @@ public:
void resumeMusic ();
void volumeMusic (float gain);
void switchToFastMusic();
bool isPlaying() const;
}; // MusicInformation
#endif

View File

@ -134,8 +134,11 @@ void MusicManager::addMusicToTracks()
//-----------------------------------------------------------------------------
void MusicManager::startMusic(MusicInformation* mi)
{
// If this music is already playing, ignore this call.
if (m_current_music != NULL && m_current_music == mi && m_current_music->isPlaying()) return;
// It is possible here that startMusic() will be called without first calling stopMusic().
// This would cause a memory leak by overwriting m_current_music without first releasing it's resources.
// This would cause a memory leak by overwriting m_current_music without first releasing its resources.
// Guard against this here by making sure that stopMusic() is called before starting new music.
stopMusic();
m_current_music = mi;

View File

@ -60,7 +60,8 @@ public:
virtual bool pauseMusic();
virtual bool resumeMusic();
virtual void volumeMusic (float gain);
virtual bool isPlaying();
protected:
bool empty();
bool check(const char* what);
@ -68,7 +69,6 @@ protected:
private:
bool release();
bool isPlaying();
bool streamIntoBuffer(ALuint buffer);
std::string m_fileName;

View File

@ -98,8 +98,11 @@ void AbstractStateManager::pushMenu(std::string name)
setGameState(MENU);
}
switchToScreen(name.c_str());
onTopMostScreenChanged();
}
// -----------------------------------------------------------------------------
void AbstractStateManager::pushScreen(Screen* screen)
@ -107,6 +110,8 @@ void AbstractStateManager::pushScreen(Screen* screen)
if (!screen->isLoaded()) screen->loadFromFile();
pushMenu(screen->getName());
screen->init();
onTopMostScreenChanged();
}
// -----------------------------------------------------------------------------
@ -128,6 +133,8 @@ void AbstractStateManager::replaceTopMostScreen(Screen* screen)
// Send init event to new menu
getCurrentScreen()->init();
onTopMostScreenChanged();
}
// -----------------------------------------------------------------------------
@ -149,6 +156,8 @@ void AbstractStateManager::reshowTopMostMenu()
Screen* screen = getCurrentScreen();
if (!screen->isLoaded()) screen->loadFromFile();
screen->init();
onTopMostScreenChanged();
}
// -----------------------------------------------------------------------------
@ -183,6 +192,8 @@ void AbstractStateManager::popMenu()
if (!screen->isLoaded()) screen->loadFromFile();
screen->init();
}
onTopMostScreenChanged();
}
// -----------------------------------------------------------------------------
@ -202,6 +213,8 @@ void AbstractStateManager::resetAndGoToScreen(Screen* screen)
switchToScreen(name.c_str());
getCurrentScreen()->init();
onTopMostScreenChanged();
}
// -----------------------------------------------------------------------------
@ -223,6 +236,8 @@ void AbstractStateManager::resetAndSetStack(Screen* screens[])
switchToScreen(m_menu_stack[m_menu_stack.size()-1].c_str());
getCurrentScreen()->init();
onTopMostScreenChanged();
}

View File

@ -116,6 +116,8 @@ namespace GUIEngine
*/
virtual void onStackEmptied() = 0;
virtual void onTopMostScreenChanged() = 0;
};
}

View File

@ -25,6 +25,7 @@
#include "irrlicht.h"
#include "config/stk_config.hpp"
#include "guiengine/engine.hpp"
#include "guiengine/widget.hpp"
#include "input/input.hpp"
@ -267,7 +268,7 @@ namespace GUIEngine
*/
virtual void onUpdate(float dt, irr::video::IVideoDriver*) { };
virtual MusicInformation* getMusic() const { return stk_config->m_title_music; }
};
}

View File

@ -118,20 +118,12 @@ void MainLoop::run()
{
IrrlichtDevice* device = irr_driver->getDevice();
bool music_on = false;
m_curr_time = device->getTimer()->getRealTime();
while(!m_abort)
{
m_prev_time = m_curr_time;
float dt = getLimitedDt();
if (!music_on && !World::getWorld())
{
//FIXME: that code can't really work, I don't think "music_on" is updated everytime it should
music_manager->stopMusic(); // stop potential 'left over' music from race
music_manager->startMusic(stk_config->m_title_music);
music_on = true;
}
network_manager->update(dt);
if (World::getWorld()) // race is active if world exists
@ -140,7 +132,6 @@ void MainLoop::run()
// till all clients have reached this state.
if (network_manager->getState()==NetworkManager::NS_READY_SET_GO_BARRIER) continue;
updateRace(dt);
music_on = false;
} // if race is active
music_manager->update(dt);

View File

@ -47,6 +47,8 @@ GrandPrixLose::GrandPrixLose() : Screen("grand_prix_lose.stkgui")
setNeeds3D(true);
m_throttle_FPS = false;
m_music = music_manager->getMusicInformation(file_manager->getMusicFile("lose_theme.music"));
}
// -------------------------------------------------------------------------------------
@ -60,7 +62,7 @@ void GrandPrixLose::loadedFromFile()
void GrandPrixLose::init()
{
music_manager->startMusic(music_manager->getMusicInformation(file_manager->getMusicFile("lose_theme.music")));
//music_manager->startMusic(music_manager->getMusicInformation(file_manager->getMusicFile("lose_theme.music")));
m_phase = 1;
m_sky_angle = 0.0f;

View File

@ -39,6 +39,8 @@ class GrandPrixLose : public GUIEngine::Screen, public GUIEngine::ScreenSingleto
float m_camera_x, m_camera_y, m_camera_z;
float m_camera_target_x, m_camera_target_z;
MusicInformation* m_music;
public:
@ -59,6 +61,8 @@ public:
void setKart(const std::string ident);
virtual MusicInformation* getMusic() const { return m_music; }
};
#endif

View File

@ -34,6 +34,8 @@ GrandPrixWin::GrandPrixWin() : Screen("grand_prix_win.stkgui")
setNeeds3D(true);
m_throttle_FPS = false;
m_music = music_manager->getMusicInformation(file_manager->getMusicFile("win_theme.music"));
}
// -------------------------------------------------------------------------------------
@ -127,7 +129,7 @@ void GrandPrixWin::init()
unlocked_label->add();
}
music_manager->startMusic(music_manager->getMusicInformation(file_manager->getMusicFile("win_theme.music")));
//music_manager->startMusic(music_manager->getMusicInformation(file_manager->getMusicFile("win_theme.music")));
m_phase = 1;
m_sky_angle = 0.0f;

View File

@ -41,6 +41,8 @@ class GrandPrixWin : public GUIEngine::Screen, public GUIEngine::ScreenSingleton
float m_camera_x, m_camera_y, m_camera_z;
float m_camera_target_x, m_camera_target_z;
MusicInformation* m_music;
public:
/** \brief implement callback from parent class GUIEngine::Screen */
@ -61,6 +63,7 @@ public:
/** \pre must be called after pushing the screen, but before onUpdate had the chance to be invoked */
void setKarts(const std::string idents[3]);
virtual MusicInformation* getMusic() const { return m_music; }
};
#endif

View File

@ -198,7 +198,11 @@ void StateManager::onGameStateChange(GameState previousState, GameState newState
if (newState == MENU)
{
music_manager->startMusic(stk_config->m_title_music);
Screen* screen = GUIEngine::getCurrentScreen();
if (screen != NULL)
{
music_manager->startMusic(GUIEngine::getCurrentScreen()->getMusic());
}
}
else if (newState == INGAME_MENU)
{
@ -211,6 +215,13 @@ void StateManager::onGameStateChange(GameState previousState, GameState newState
// ----------------------------------------------------------------------------
void StateManager::onTopMostScreenChanged()
{
music_manager->startMusic(GUIEngine::getCurrentScreen()->getMusic());
}
// ----------------------------------------------------------------------------
void StateManager::onStackEmptied()
{
main_loop->abort();

View File

@ -141,6 +141,9 @@ public:
/** \brief implementing callback from base class AbstractStateManager */
virtual void onStackEmptied();
/** \brief implementing callback from base class AbstractStateManager */
virtual void onTopMostScreenChanged();
// singleton
static StateManager* get();