Implementing easily extensible kart sfx system. See sfx_manager.hpp for sparse notes.

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3895 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
rforder 2009-08-20 18:29:59 +00:00
parent 5aee938257
commit 97bbad19e7
6 changed files with 95 additions and 32 deletions

View File

@ -252,7 +252,7 @@ int SFXManager::addSingleSfx(std::string sfxFile,
}
// debugging
/*printf("addSingleSfx() id:%d sfxFile:%s\n", sfxID, sfxFile.c_str());*/
printf("addSingleSfx() id:%d sfxFile:%s\n", sfxID, sfxFile.c_str());
return sfxID;
}
@ -302,7 +302,7 @@ SFXBase *SFXManager::newSFX(int id)
if (id < 0 || id >= (int)m_sfx_gain.size())
{
printf("newSFX: Invalid SFX ID.\n");
printf("newSFX: Invalid SFX ID %d.\n", id);
return NULL;
}

View File

@ -50,6 +50,35 @@ public:
NUM_SOUNDS
};
/*
Entries for custom SFX sounds. These are unique for each kart.
eg. kart->playCustomSFX(SFX_MANAGER::CUSTOM_HORN)
*/
enum CustomSFX
{
CUSTOM_HORN,
CUSTOM_CRASH,
CUSTOM_WIN,
CUSTOM_EXPLODE,
NUM_CUSTOMS
};
// LISP (or in the future xml) tag for each custom sound
// TODO: fix this to use some kind of string array ini, I'm just too stupid with c++ to figure it out
static const char *getCustomTagName(int id)
{
switch (id)
{
case 0: return "horn-sound";
case 1: return "crash-sound";
case 2: return "win-sound";
case 3: return "explode-sound";
};
return "";
}
/** Status of a sound effect. */
enum SFXStatus
{

View File

@ -82,6 +82,9 @@ Kart::Kart (const std::string& kart_name, int position,
m_view_blocked_by_plunger = 0;
// Initialize custom sound vector
m_custom_sounds.resize(SFXManager::NUM_CUSTOMS);
// Set position and heading:
m_reset_transform = init_transform;
@ -100,20 +103,17 @@ Kart::Kart (const std::string& kart_name, int position,
m_rescue = false;
m_wheel_rotation = 0;
// Create SFXBase for each custom sound
for (int n = 0; n < SFXManager::NUM_CUSTOMS; n++)
{
int id = m_kart_properties->getCustomSfxId((SFXManager::CustomSFX)n);
m_custom_sounds[n] = sfx_manager->newSFX(id);
}
m_engine_sound = sfx_manager->newSFX(m_kart_properties->getEngineSfxType());
// If horn sfx is defined, load it. Otherwise use default
int sfxId;
if (m_kart_properties->getHornSfxFile() == "")
sfxId = SFXManager::SOUND_BEEP;
else
sfxId = sfx_manager->addSingleSfx(m_kart_properties->getHornSfxFile(), 1, 0.2f,1.0f);
m_beep_sound = sfx_manager->newSFX( sfxId );
m_crash_sound = sfx_manager->newSFX( SFXManager::SOUND_CRASH );
m_skid_sound = sfx_manager->newSFX( SFXManager::SOUND_SKID );
m_goo_sound = sfx_manager->newSFX( SFXManager::SOUND_GOO );
m_skid_sound = sfx_manager->newSFX( SFXManager::SOUND_SKID );
if(!m_engine_sound)
{
@ -256,8 +256,15 @@ Kart::~Kart()
{
m_engine_sound->stop();
}
// Delete all custom sounds
for (int n = 0; n < SFXManager::NUM_CUSTOMS; n++)
{
if (m_custom_sounds[n] != NULL)
sfx_manager->deleteSFX(m_custom_sounds[n]);
}
sfx_manager->deleteSFX(m_engine_sound );
sfx_manager->deleteSFX(m_beep_sound );
sfx_manager->deleteSFX(m_crash_sound );
sfx_manager->deleteSFX(m_skid_sound );
sfx_manager->deleteSFX(m_goo_sound );
@ -593,8 +600,12 @@ void Kart::update(float dt)
Moveable::update(dt);
for (int n = 0; n < SFXManager::NUM_CUSTOMS; n++)
{
if (m_custom_sounds[n] != NULL) m_custom_sounds[n]->position ( getXYZ() );
}
m_engine_sound->position ( getXYZ() );
m_beep_sound->position ( getXYZ() );
m_crash_sound->position ( getXYZ() );
m_skid_sound->position ( getXYZ() );
@ -843,9 +854,25 @@ void Kart::crashed(Kart *k)
// -----------------------------------------------------------------------------
void Kart::beep()
{
m_beep_sound->play();
playCustomSFX(SFXManager::CUSTOM_HORN);
} // beep
// -----------------------------------------------------------------------------
// Plays custom SFX, returns whether or not the sound was played
bool Kart::playCustomSFX(unsigned int type)
{
bool ret = false;
if (type < SFXManager::NUM_CUSTOMS)
if (m_custom_sounds[type] != NULL)
{
ret = true;
// Don't stutter
if (m_custom_sounds[type]->getStatus() != SFXManager::SFX_PLAYING)
m_custom_sounds[type]->play();
}
return ret;
}
// -----------------------------------------------------------------------------
void Kart::updatePhysics (float dt)
{

View File

@ -126,8 +126,8 @@ private:
bool m_rescue;
bool m_eliminated;
std::vector<SFXBase*> m_custom_sounds;
SFXBase *m_engine_sound;
SFXBase *m_beep_sound;
SFXBase *m_crash_sound;
SFXBase *m_skid_sound;
SFXBase *m_goo_sound;
@ -283,6 +283,7 @@ public:
virtual void update (float dt);
virtual void raceFinished (float time);
void beep ();
bool playCustomSFX (unsigned int type);
};

View File

@ -45,6 +45,7 @@ KartProperties::KartProperties() : m_icon_material(0)
m_icon_file = "tuxicon.png";
m_shadow_file = "tuxkartshadow.png";
m_groups.clear();
m_custom_sfx_id.resize(SFXManager::NUM_CUSTOMS);
// Set all other values to undefined, so that it can later be tested
// if everything is defined properly.
@ -201,19 +202,24 @@ void KartProperties::getAllData(const lisp::Lisp* lisp)
lisp->get("brake-factor", m_brake_factor);
lisp->get("mass", m_mass);
// Load SFX filenames
if (lisp->get("horn-sound", m_horn_sfx_file))
{
m_horn_sfx_file = file_manager->getDataDir() + "karts/" + getIdent() + "/" + m_horn_sfx_file;
}
else
{
/* TODO: Think of cleaner way to define when there
is no sfx file (empty filename is hackish)
*/
m_horn_sfx_file = "";
}
// Load custom kart SFX files ===================================================
std::string tempFile;
for (int i = 0; i < SFXManager::NUM_CUSTOMS; i++)
{
// Get lisp string tag for each custom sfx
if (lisp->get(sfx_manager->getCustomTagName(i), tempFile))
{
// retrieve filename, load file and store id in vector
tempFile = file_manager->getDataDir() + "/karts/" + getIdent() + "/" + tempFile;
m_custom_sfx_id[i] = sfx_manager->addSingleSfx(tempFile, 1, 0.2f,1.0f);
printf("%s custom SFX #%d : %s\n", getIdent().c_str(), i, tempFile.c_str());
}
else
{
m_custom_sfx_id[i] = -1;
}
}
std::string sfx_type_string;
lisp->get("engine-sound", sfx_type_string);

View File

@ -55,7 +55,7 @@ private:
// SFX files
// ---------------
std::string m_horn_sfx_file; /**< Absolute path name of horn SFX file */
std::vector<int> m_custom_sfx_id; /**< Vector of custom SFX ids */
// Display and gui
// ---------------
@ -179,8 +179,8 @@ public:
const std::string& getIdent () const {return m_ident; }
const std::string& getShadowFile() const {return m_shadow_file; }
const std::string& getIconFile () const {return m_icon_file; }
const
std::string& getHornSfxFile() const {return m_horn_sfx_file; }
const int getCustomSfxId (SFXManager::CustomSFX type)
const {return m_custom_sfx_id[type]; }
/** Returns the version of the .kart file. */
int getVersion () const {return m_version; }