SoundAsset Array

Few fixes to soundasset array to stop it complaining
This commit is contained in:
marauder2k7 2021-09-20 10:49:34 +01:00
parent 3ad6d47ca9
commit 1ea693fea6
4 changed files with 80 additions and 61 deletions

View file

@ -107,6 +107,7 @@ ConsoleSetType(TypeSoundAssetId)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
SoundAsset::SoundAsset() SoundAsset::SoundAsset()
: AssetBase()
{ {
mSoundFile = StringTable->EmptyString(); mSoundFile = StringTable->EmptyString();
mSoundPath = StringTable->EmptyString(); mSoundPath = StringTable->EmptyString();

View file

@ -362,43 +362,51 @@ DefineEngineMethod(className, set##name, bool, (const char* shape), , assetText
#pragma region Arrayed Asset Macros #pragma region Arrayed Asset Macros
#define DECLARE_SOUNDASSET_ARRAY(className, name, max) public: \ #define DECLARE_SOUNDASSET_ARRAY(className,name,max) public: \
static const U32 sm##name##count = max;\ static const U32 sm##name##Count = max;\
Resource<SFXResource> m##name[max];\ Resource<SFXResource> m##name[max];\
StringTableEntry m##name##Name[max]; \ StringTableEntry m##name##Name[max]; \
StringTableEntry m##name##AssetId[max];\ StringTableEntry m##name##AssetId[max];\
AssetPtr<SoundAsset> m##name##Asset[max] = NULL;\ AssetPtr<SoundAsset> m##name##Asset[max];\
SFXProfile* m##name##Profile[max] = NULL;\ SFXProfile* m##name##Profile[max];\
public: \ public: \
const StringTableEntry get##name##File(const U32& id) const { return m##name##Name[id]; }\ const StringTableEntry get##name##File(const U32& index) const { return m##name##Name[index]; }\
void set##name##File(const FileName &_in, const U32& id) { m##name##Name[id] = StringTable->insert(_in.c_str());}\ void set##name##File(const FileName &_in, const U32& index) { m##name##Name[index] = StringTable->insert(_in.c_str());}\
const AssetPtr<SoundAsset> & get##name##Asset(const U32& id) const { return m##name##Asset[id]; }\ const AssetPtr<SoundAsset> & get##name##Asset(const U32& index) const { return m##name##Asset[index]; }\
void set##name##Asset(const AssetPtr<SoundAsset> &_in, const U32& id) { m##name##Asset[id] = _in;}\ void set##name##Asset(const AssetPtr<SoundAsset> &_in, const U32& index) { m##name##Asset[index] = _in;}\
\ \
bool _set##name(StringTableEntry _in, const U32& id)\ bool _set##name(StringTableEntry _in, const U32& index)\
{\ {\
if(m##name##AssetId[id] != _in || m##name##Name[id] != _in)\ if(m##name##AssetId[index] != _in || m##name##Name[index] != _in)\
{\ {\
if(index >= sm##name##Count || index < 0) \
return false;\
if (_in == NULL || _in == StringTable->EmptyString())\ if (_in == NULL || _in == StringTable->EmptyString())\
{\ {\
if(id >= sm##name##Count || id < 0) \ m##name##Name[index] = StringTable->EmptyString();\
return false;\ m##name##AssetId[index] = StringTable->EmptyString();\
m##name##Name[id] = StringTable->EmptyString();\ m##name##Asset[index] = NULL;\
m##name##AssetId[id] = StringTable->EmptyString();\ m##name[index] = NULL;\
m##name##Asset[id] = NULL;\ return true;\
m##name[id] = NULL;\ }\
else if(_in[0] == '$' || _in[0] == '#')\
{\
m##name##Name[index] = _in;\
m##name##AssetId[index] = StringTable->EmptyString();\
m##name##Asset[index] = NULL;\
m##name[index] = NULL;\
return true;\ return true;\
}\ }\
\ \
if (AssetDatabase.isDeclaredAsset(_in))\ if (AssetDatabase.isDeclaredAsset(_in))\
{\ {\
m##name##AssetId[id] = _in;\ m##name##AssetId[index] = _in;\
\ \
U32 assetState = SoundAsset::getAssetById(m##name##AssetId[id], &m##name##Asset[id]);\ U32 assetState = SoundAsset::getAssetById(m##name##AssetId[index], &m##name##Asset[index]);\
\ \
if (SoundAsset::Ok == assetState)\ if (SoundAsset::Ok == assetState)\
{\ {\
m##name##Name[id] = StringTable->EmptyString();\ m##name##Name[index] = StringTable->EmptyString();\
}\ }\
}\ }\
else\ else\
@ -406,56 +414,58 @@ public: \
StringTableEntry assetId = SoundAsset::getAssetIdByFileName(_in);\ StringTableEntry assetId = SoundAsset::getAssetIdByFileName(_in);\
if (assetId != StringTable->EmptyString())\ if (assetId != StringTable->EmptyString())\
{\ {\
m##name##AssetId[id] = assetId;\ m##name##AssetId[index] = assetId;\
if(SoundAsset::getAssetById(m##name##AssetId, &m##name##Asset) == SoundAsset::Ok)\ if(SoundAsset::getAssetById(m##name##AssetId[index], &m##name##Asset[index]) == SoundAsset::Ok)\
{\ {\
m##name##Name[id] = StringTable->EmptyString();\ m##name##Name[index] = StringTable->EmptyString();\
}\ }\
}\ }\
else\ else\
{\ {\
m##name##Name[id] = _in;\ m##name##Name[index] = _in;\
m##name##AssetId[id] = StringTable->EmptyString();\ m##name##AssetId[index] = StringTable->EmptyString();\
m##name##Asset[id] = NULL;\ m##name##Asset[index] = NULL;\
}\ }\
}\ }\
}\ }\
if (get##name(id) != StringTable->EmptyString() && m##name##Asset[id].notNull())\ if (get##name(index) != StringTable->EmptyString() && m##name##Asset[index].notNull())\
{\ {\
m##name[id] = m##name##Asset[id]->getSoundResource();\ m##name[index] = m##name##Asset[index]->getSoundResource();\
}\ }\
else\ else\
{\ {\
m##name[id] = NULL;\ m##name[index] = NULL;\
}\ }\
\ \
if (m##name##Asset[id].notNull() && m##name##Asset[id]->getStatus() != SoundAsset::Ok)\ if (m##name##Asset[index].notNull() && m##name##Asset[index]->getStatus() != SoundAsset::Ok)\
{\ {\
Con::errorf("%s(%s)::_set%s(%i) - sound asset failure\"%s\" due to [%s]", macroText(className), getName(), macroText(name),id, _in, SoundAsset::getAssetErrstrn(m##name##Asset->getStatus()).c_str());\ Con::errorf("%s(%s)::_set%s(%i) - sound asset failure\"%s\" due to [%s]", macroText(className), getName(), macroText(name),index, _in, SoundAsset::getAssetErrstrn(m##name##Asset[index]->getStatus()).c_str());\
return false; \ return false; \
}\ }\
else if (!m##name)\ else if (!m##name[index])\
{\ {\
Con::errorf("%s(%s)::_set%s(%i) - Couldn't load sound \"%s\"", macroText(className), getName(), macroText(name),id, _in);\ Con::errorf("%s(%s)::_set%s(%i) - Couldn't load sound \"%s\"", macroText(className), getName(), macroText(name),index, _in);\
return false;\ return false;\
}\ }\
return true;\ return true;\
}\ }\
\ \
const StringTableEntry get##name(const U32& id) const\ const StringTableEntry get##name(const U32& index) const\
{\ {\
if (m##name##Asset[id] && (m##name##Asset[id]->getSoundPath() != StringTable->EmptyString()))\ if (m##name##Asset[index] && (m##name##Asset[index]->getSoundPath() != StringTable->EmptyString()))\
return m##name##Asset[id]->getSoundPath();\ return m##name##Asset[index]->getSoundPath();\
else if (m##name##AssetId[id] != StringTable->EmptyString())\ else if (m##name##AssetId[index] != StringTable->EmptyString())\
return m##name##AssetId[id];\ return m##name##AssetId[index];\
else if (m##name##Name[id] != StringTable->EmptyString())\ else if (m##name##Name[index] != StringTable->EmptyString())\
return StringTable->insert(m##name##Name[id]);\ return StringTable->insert(m##name##Name[index]);\
else\ else\
return StringTable->EmptyString();\ return StringTable->EmptyString();\
}\ }\
Resource<SFXResource> get##name##Resource() \ Resource<SFXResource> get##name##Resource(const U32& id) \
{\ {\
return m##name;\ if(id >= sm##name##Count || id < 0)\
return ResourceManager::get().load( "" );\
return m##name[id];\
} }
#define DECLARE_SOUNDASSET_ARRAY_SETGET(className, name)\ #define DECLARE_SOUNDASSET_ARRAY_SETGET(className, name)\
@ -512,14 +522,14 @@ DefineEngineMethod(className, set##name, bool, (const char* map, S32 index), , a
#ifdef TORQUE_SHOW_LEGACY_FILE_FIELDS #ifdef TORQUE_SHOW_LEGACY_FILE_FIELDS
#define INITPERSISTFIELD_IMAGEASSET_ARRAY(name, arraySize, consoleClass, docs) \ #define INITPERSISTFIELD_IMAGEASSET_ARRAY(name, arraySize, consoleClass, docs) \
addProtectedField(#name, TypeImageFilename, Offset(m##name##Name, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, docs)); \ addProtectedField(#name, TypeSoundFilename, Offset(m##name##Name, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, docs)); \
addProtectedField(assetText(name, Asset), TypeImageAssetId, Offset(m##name##AssetId, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, asset docs.)); addProtectedField(assetText(name, Asset), TypeImageAssetId, Offset(m##name##AssetId, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, asset docs.));
#else #else
#define INITPERSISTFIELD_SOUNDASSET_ARRAY(name, arraySize, consoleClass, docs) \ #define INITPERSISTFIELD_SOUNDASSET_ARRAY(name, arraySize, consoleClass, docs) \
addProtectedField(#name, TypeImageFilename, Offset(m##name##Name, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, docs), AbstractClassRep::FIELD_HideInInspectors); \ addProtectedField(#name, TypeSoundFilename, Offset(m##name##Name, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, docs), AbstractClassRep::FIELD_HideInInspectors); \
addProtectedField(assetText(name, Asset), TypeImageAssetId, Offset(m##name##AssetId, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, asset docs.)); addProtectedField(assetText(name, Asset), TypeSoundAssetId, Offset(m##name##AssetId, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, asset docs.));
#endif #endif

View file

@ -241,7 +241,7 @@ LightningData::LightningData()
INIT_SOUNDASSET(StrikeSound); INIT_SOUNDASSET(StrikeSound);
for (S32 i = 0; i < MaxThunders; i++) for (S32 i = 0; i < MaxThunders; i++)
thunderSounds[i] = NULL; INIT_SOUNDASSET_ARRAY(ThunderSound, MaxThunders);
for (S32 i = 0; i < MaxTextures; i++) for (S32 i = 0; i < MaxTextures; i++)
{ {
@ -263,10 +263,8 @@ void LightningData::initPersistFields()
INITPERSISTFIELD_SOUNDASSET(StrikeSound, LightningData, "Sound to play when lightning STRIKES!"); INITPERSISTFIELD_SOUNDASSET(StrikeSound, LightningData, "Sound to play when lightning STRIKES!");
addField( "thunderSounds", TYPEID< SFXTrack >(), Offset(thunderSounds, LightningData), MaxThunders, INITPERSISTFIELD_SOUNDASSET_ARRAY(ThunderSound, MaxThunders, LightningData, "Sounds for thunder.");
"@brief List of thunder sound effects to play.\n\n"
"A random one of these sounds will be played shortly after each strike "
"occurs." );
addField( "strikeTextures", TypeString, Offset(strikeTextureNames, LightningData), MaxTextures, addField( "strikeTextures", TypeString, Offset(strikeTextureNames, LightningData), MaxTextures,
"List of textures to use to render lightning strikes." ); "List of textures to use to render lightning strikes." );
@ -292,15 +290,18 @@ bool LightningData::preload(bool server, String &errorStr)
//dQsort(thunderSounds, MaxThunders, sizeof(SFXTrack*), cmpSounds); //dQsort(thunderSounds, MaxThunders, sizeof(SFXTrack*), cmpSounds);
for (S32 i = 0; i < MaxThunders; i++) { for (S32 i = 0; i < MaxThunders; i++) {
if (thunderSounds[i]!= NULL) numThunders++; if (mThunderSound[i] == NULL)
{
_setThunderSound(getThunderSound(i), i);
}
} }
if (server == false) if (server == false)
{ {
String sfxErrorStr; String sfxErrorStr;
for (U32 i = 0; i < MaxThunders; i++) { for (U32 i = 0; i < MaxThunders; i++) {
if( !sfxResolve( &thunderSounds[ i ], sfxErrorStr ) ) if (!getThunderProfile(i))
Con::errorf(ConsoleLogEntry::General, "LightningData::preload: Invalid packet: %s", sfxErrorStr.c_str()); Con::errorf(ConsoleLogEntry::General, "LightningData::preload: Cant get an sfxProfile for thunder.");
} }
if(!getSFXProfile()) if(!getSFXProfile())
@ -330,8 +331,7 @@ void LightningData::packData(BitStream* stream)
U32 i; U32 i;
for (i = 0; i < MaxThunders; i++) for (i = 0; i < MaxThunders; i++)
{ {
if (stream->writeFlag(thunderSounds[i])) PACKDATA_SOUNDASSET_ARRAY(ThunderSound, i);
sfxWrite(stream, thunderSounds[i]);
} }
stream->writeInt(mNumStrikeTextures, 4); stream->writeInt(mNumStrikeTextures, 4);
@ -349,10 +349,7 @@ void LightningData::unpackData(BitStream* stream)
U32 i; U32 i;
for (i = 0; i < MaxThunders; i++) for (i = 0; i < MaxThunders; i++)
{ {
if (stream->readFlag()) UNPACKDATA_SOUNDASSET_ARRAY(ThunderSound, i);
sfxRead(stream, &thunderSounds[i]);
else
thunderSounds[i] = NULL;
} }
mNumStrikeTextures = stream->readInt(4); mNumStrikeTextures = stream->readInt(4);
@ -585,7 +582,7 @@ void Lightning::scheduleThunder(Strike* newStrike)
if (t <= 0.03f) { if (t <= 0.03f) {
// If it's really close, just play it... // If it's really close, just play it...
U32 thunder = sgLightningRand.randI(0, mDataBlock->numThunders - 1); U32 thunder = sgLightningRand.randI(0, mDataBlock->numThunders - 1);
SFX->playOnce(mDataBlock->thunderSounds[thunder]); SFX->playOnce(mDataBlock->getThunderProfile(thunder));
} else { } else {
Thunder* pThunder = new Thunder; Thunder* pThunder = new Thunder;
pThunder->tRemaining = t; pThunder->tRemaining = t;
@ -652,7 +649,7 @@ void Lightning::advanceTime(F32 dt)
// Play the sound... // Play the sound...
U32 thunder = sgLightningRand.randI(0, mDataBlock->numThunders - 1); U32 thunder = sgLightningRand.randI(0, mDataBlock->numThunders - 1);
SFX->playOnce(mDataBlock->thunderSounds[thunder]); SFX->playOnce(mDataBlock->getThunderProfile(thunder));
} else { } else {
pThunderWalker = &((*pThunderWalker)->next); pThunderWalker = &((*pThunderWalker)->next);
} }

View file

@ -63,7 +63,10 @@ class LightningData : public GameBaseData
//-------------------------------------- Console set variables //-------------------------------------- Console set variables
public: public:
SFXTrack* thunderSounds[MaxThunders]; //SFXTrack* thunderSounds[MaxThunders];
DECLARE_SOUNDASSET_ARRAY(LightningData, ThunderSound, MaxThunders);
DECLARE_SOUNDASSET_ARRAY_SETGET(LightningData, ThunderSound);
DECLARE_SOUNDASSET(LightningData, StrikeSound); DECLARE_SOUNDASSET(LightningData, StrikeSound);
DECLARE_SOUNDASSET_SETGET(LightningData, StrikeSound); DECLARE_SOUNDASSET_SETGET(LightningData, StrikeSound);
@ -91,6 +94,14 @@ class LightningData : public GameBaseData
DECLARE_CONOBJECT(LightningData); DECLARE_CONOBJECT(LightningData);
static void initPersistFields(); static void initPersistFields();
SFXProfile* getThunderProfile(U32 id)
{
if (mThunderSoundAsset[id] != NULL)
return mThunderSoundAsset[id]->getSfxProfile();
else
return NULL;
}
SFXProfile* getSFXProfile() { SFXProfile* getSFXProfile() {
if (mStrikeSoundAsset.notNull()) if (mStrikeSoundAsset.notNull())
return mStrikeSoundAsset->getSfxProfile(); return mStrikeSoundAsset->getSfxProfile();