From 1ac3f3f1701c238b3caf37c1de2a33a7e66c8fb3 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Sat, 2 Jun 2012 16:06:10 +0200 Subject: [PATCH] Fixed: Sound CSheetID implementation (part 3) (partially tested) --- code/nel/samples/sound/sound_sources/main.cpp | 1 + .../samples/sound/stream_file/stream_file.cpp | 1 + .../stream_ogg_vorbis/stream_ogg_vorbis.cpp | 1 + code/nel/src/misc/sheet_id.cpp | 17 ++++++++--- code/nel/src/sound/sound_bank.cpp | 29 ++++++++++++++----- .../object_viewer/particle_sound_page.cpp | 10 +++---- .../plugins/object_viewer/sound_system.cpp | 13 +++++++-- 7 files changed, 53 insertions(+), 19 deletions(-) diff --git a/code/nel/samples/sound/sound_sources/main.cpp b/code/nel/samples/sound/sound_sources/main.cpp index 2c0ccedab..75f732b5a 100644 --- a/code/nel/samples/sound/sound_sources/main.cpp +++ b/code/nel/samples/sound/sound_sources/main.cpp @@ -49,6 +49,7 @@ void Init() { try { + CSheetId::initWithoutSheet(); CPath::addSearchPath(NL_SOUND_DATA"/data", true, false); diff --git a/code/nel/samples/sound/stream_file/stream_file.cpp b/code/nel/samples/sound/stream_file/stream_file.cpp index b07da8ba4..6362a0414 100644 --- a/code/nel/samples/sound/stream_file/stream_file.cpp +++ b/code/nel/samples/sound/stream_file/stream_file.cpp @@ -62,6 +62,7 @@ static void initSample() { if (!INelContext::isContextInitialised()) new CApplicationContext(); + CSheetId::initWithoutSheet(); CPath::addSearchPath(NL_SOUND_DATA"/data", true, false); printf("Sample demonstrating OGG playback using stream file .sound sheets."); diff --git a/code/nel/samples/sound/stream_ogg_vorbis/stream_ogg_vorbis.cpp b/code/nel/samples/sound/stream_ogg_vorbis/stream_ogg_vorbis.cpp index e4a2e507c..1f491b21c 100644 --- a/code/nel/samples/sound/stream_ogg_vorbis/stream_ogg_vorbis.cpp +++ b/code/nel/samples/sound/stream_ogg_vorbis/stream_ogg_vorbis.cpp @@ -59,6 +59,7 @@ static void initSample() { if (!INelContext::isContextInitialised()) new CApplicationContext(); + CSheetId::initWithoutSheet(); CPath::addSearchPath(NL_SOUND_DATA"/database/build/", true, false); printf("Sample demonstrating OGG playback using UStreamSource."); diff --git a/code/nel/src/misc/sheet_id.cpp b/code/nel/src/misc/sheet_id.cpp index e76f66656..9bb5894b8 100644 --- a/code/nel/src/misc/sheet_id.cpp +++ b/code/nel/src/misc/sheet_id.cpp @@ -109,7 +109,11 @@ CSheetId::CSheetId( const string& sheetName ) CSheetId::CSheetId( const std::string& sheetName, const std::string &defaultType ) { if (CFile::getExtension(sheetName) == "" && defaultType != "") - *this = CSheetId(sheetName + "." + defaultType); + { + std::string withType = sheetName + "." + defaultType; + *this = CSheetId(withType); + nldebug("SHEETID: Constructing CSheetId from name '%s' without explicit type, defaulting as '%s' to '%s'", sheetName.c_str(), defaultType.c_str(), withType.c_str()); + } else *this = CSheetId(sheetName); } @@ -131,6 +135,7 @@ bool CSheetId::buildSheetId(const std::string& sheetName) if (it == _DevSheetNameToId.end()) { // Create a new dynamic sheet ID. + nldebug("SHEETID: Creating a new dynamic sheet id for '%s'", sheetName.c_str()); std::string sheetType = CFile::getExtension(sheetNameLc); std::string sheetName = CFile::getFilenameWithoutExtension(sheetNameLc); std::map::iterator tit = _DevTypeNameToId.find(sheetType); @@ -149,7 +154,9 @@ bool CSheetId::buildSheetId(const std::string& sheetName) _DevSheetIdToName[typeId].push_back(sheetName); _Id.IdInfos.Type = typeId; _Id.IdInfos.Id = _DevSheetIdToName[typeId].size() - 1; + // nldebug("SHEETID: Type %i, id %i, sheetid %i", _Id.IdInfos.Type, _Id.IdInfos.Id, _Id.Id); _DevSheetNameToId[sheetNameLc] = _Id.Id; + return true; } _Id.Id = it->second; return true; @@ -326,7 +333,8 @@ void CSheetId::init(bool removeUnknownSheet) // allow multiple calls to init in case libraries depending on sheetid call this init from their own if (_Initialised) { - nlassert(!_DontHaveSheetKnowledge); + if (_DontHaveSheetKnowledge) + nlinfo("SHEETID: CSheetId is already initialized without sheet_id.bin"); return; } @@ -357,8 +365,9 @@ void CSheetId::initWithoutSheet() _DevSheetIdToName[0].push_back("unknown"); _DevSheetNameToId["unknown.unknown"] = 0;*/ - CSheetId unknown = CSheetId("unknown.unknown"); - nlassert(unknown == CSheetId::Unknown); + CSheetId unknownunknown = CSheetId("unknown.unknown"); + // nldebug("SHEETID: unknown: %i, Unknown: %i", unknownunknown._Id, Unknown._Id); + nlassert(unknownunknown == CSheetId::Unknown); } diff --git a/code/nel/src/sound/sound_bank.cpp b/code/nel/src/sound/sound_bank.cpp index 1124eeaa5..e48ed6254 100644 --- a/code/nel/src/sound/sound_bank.cpp +++ b/code/nel/src/sound/sound_bank.cpp @@ -123,7 +123,9 @@ CSoundBank::~CSoundBank() void CSoundBank::addSound(CSound *sound) { - nlassert(_Sounds.size() > sound->getName().getShortId()); + // nlassert(_Sounds.size() > sound->getName().getShortId()); + if (_Sounds.size() <= sound->getName().getShortId()) + _Sounds.resize(sound->getName().getShortId() + 1); _Sounds[sound->getName().getShortId()] = sound; } @@ -274,9 +276,16 @@ void CSoundBank::load(const std::string &packedSheetDir, bool packedSheetUpdate) maxShortId = first->first.getShortId(); } ++maxShortId; // inc for size = last idx + 1 - nlassert(maxShortId < (container.size() * 8)); // ensure no ridiculous sheet id values - if (maxShortId > _Sounds.size()) - _Sounds.resize(maxShortId); + if (container.size() == 0) + { + nlwarning("NLSOUND: No sound sheets have been loaded, missing sound sheet directory or packed sound sheets file"); + } + else + { + nlassert(maxShortId < (container.size() * 8)); // ensure no ridiculous sheet id values + if (maxShortId > _Sounds.size()) + _Sounds.resize(maxShortId); + } } // add all the loaded sound in the sound banks @@ -348,9 +357,15 @@ CSound* CSoundBank::getSound(const NLMISC::CSheetId &sheetId) { if (sheetId == NLMISC::CSheetId::Unknown) return NULL; - - nlassert(sheetId.getShortId() < _Sounds.size()); - + + // nlassert(sheetId.getShortId() < _Sounds.size()); + if (sheetId.getShortId() >= _Sounds.size()) + { + std::string sheetName = sheetId.toString(); + nlwarning("NLSOUND: Sound sheet id '%s' exceeds loaded sound sheets", sheetName.c_str()); + return NULL; + } + return _Sounds[sheetId.getShortId()]; } diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/object_viewer/particle_sound_page.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/object_viewer/particle_sound_page.cpp index 17e0c09d7..ce7034e56 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/object_viewer/particle_sound_page.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/object_viewer/particle_sound_page.cpp @@ -86,7 +86,7 @@ void CSoundPage::setEditedItem(CWorkspaceNode *ownerNode, NL3D::CPSLocatedBindab _ui.pitchWidget->setWorkspaceNode(_Node); _ui.pitchWidget->updateUi(); - _ui.soundNameLineEdit->setText(QString(NLMISC::CStringMapper::unmap(_Sound->getSoundName()).c_str())); + _ui.soundNameLineEdit->setText(QString(_Sound->getSoundName().toString().c_str())); _ui.spawnCheckBox->setChecked(_Sound->getSpawn()); _ui.muteCheckBox->setChecked(_Sound->getMute()); @@ -95,7 +95,7 @@ void CSoundPage::setEditedItem(CWorkspaceNode *ownerNode, NL3D::CPSLocatedBindab void CSoundPage::browse() { - std::vector names; + std::vector names; NLSOUND::UAudioMixer *audioMixer = Modules::sound().getAudioMixer(); @@ -108,7 +108,7 @@ void CSoundPage::browse() QStringList items; items << tr(""); for(size_t i = 0; i < names.size(); ++i) - items << QString(names[i]->c_str()); + items << QString(names[i].toString().c_str()); bool ok; QString item = QInputDialog::getItem(this, tr("Select your sound"), @@ -162,7 +162,7 @@ void CSoundPage::setKeepPitch(bool state) void CSoundPage::setSoundName(const QString &text) { - _Sound->setSoundName(NLMISC::CStringMapper::map(text.toStdString())); + _Sound->setSoundName(NLMISC::CSheetId(text.toStdString())); } void CSoundPage::setEmissionPercent(float value) @@ -171,4 +171,4 @@ void CSoundPage::setEmissionPercent(float value) updateModifiedFlag(); } -} /* namespace NLQT */ \ No newline at end of file +} /* namespace NLQT */ diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/object_viewer/sound_system.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/object_viewer/sound_system.cpp index a8a204c96..5ca313b0f 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/object_viewer/sound_system.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/object_viewer/sound_system.cpp @@ -30,6 +30,8 @@ #include #include #include +#include + // Qt includes #include @@ -67,6 +69,9 @@ void CSoundSystem::init() { //H_AUTO2 nldebug("CSoundSystem::init"); + + // require sheet id without sheet id bin + NLMISC::CSheetId::initWithoutSheet(); // create audiomixer _AudioMixer = NULL; @@ -153,9 +158,10 @@ void CSoundSystem::play(const std::string &soundName) { if (_AudioMixer) { - NLSOUND::USource *src = _AudioMixer->createSource(NLMISC::CStringMapper::map(soundName), true); + NLSOUND::USource *src = _AudioMixer->createSource(NLMISC::CSheetId(soundName, "sound"), true); if (src) { + // FIXME: Use relative positioning, and set pos to 0,0,0 src->setLooping(false); const NLMISC::CVector &pos = _AudioMixer->getListener()->getPos(); src->setPos(pos); @@ -172,9 +178,10 @@ NLSOUND::USource *CSoundSystem::create(const std::string &soundName) { if (_AudioMixer) { - NLSOUND::USource *src = _AudioMixer->createSource(NLMISC::CStringMapper::map(soundName), false); + NLSOUND::USource *src = _AudioMixer->createSource(NLMISC::CSheetId(soundName, "sound"), false); if (src) { + // FIXME: Use relative positioning, and set pos to 0,0,0 src->setLooping(false); const NLMISC::CVector &pos = _AudioMixer->getListener()->getPos(); src->setPos(pos); @@ -236,4 +243,4 @@ void CSoundSystem::releaseGraphics() NL3D::UParticleSystemSound::setPSSound(NULL); } -} /* namespace NLQT */ \ No newline at end of file +} /* namespace NLQT */