SFML community forums
Help => Audio => Topic started by: WSPSNIPER on March 08, 2010, 02:51:50 am
-
when i play a sound about 1000 times it gives me the error and the sound goes away
my code
sf::Sound m_sound;
//this returns the sound buffer for the hit sound
m_sound.SetBuffer(soundMgr->GetSoundBuffer(SoundManager::kHit));
/*
* the function above
* sf::SoundBuffer &SoundManager::GetSoundBuffer(int id)
* {
* return m_buffers[id]; // an array of buffers for the sounds
* }
*/
//when added to weapon manager
WeaponManager::Add(Entity *weapon)
{
weapon->PlaySound();
m_weaponList.push_back(weapon);
}
this is a sound that is located in a knife class that gets added to a vector in my weapon manager when throwen and will play the kife's sound when it is added
so if there is anything i can do
-
should i have a sound buffer for every sound or 1 sound buffer for 1 type of sound
example :
SoundBuffer hit("hit.wav");
Sound hitSound(hit);
Sound hit2Sound(hit);
Sound hit3Sound(hit); exc
or
class MySound{
public:
SoundBuffer;
Sound;
}
just examples code may be off but you get the point
-
when i play a sound about 1000 times it gives me the error and the sound goes away
Is it 1024 times? Do you properly destroy unused sounds, or reuse them (ie. you have no leak)?
should i have a sound buffer for every sound or 1 sound buffer for 1 type of sound
One sound buffer for one type of sound. This is why sf::Sound and sf::SoundBuffer are two separate classes: load the audio data once, play it multiple times.
-
thanks for pointing that out, i dont know exactly how a vector takes care of memory allocation/deallocation, but here is how i did it
std::vector<Weapon*> m_weapons;
void Add(Weapon *weapon)
{
weapon->PlaySound();
m_weapons.pushback(weapon);
}
then i to use the Add function for a knife for example i wound do
weaponMgr->Add(new Knife());
then i did no cleanup, if you could give me any tips on cleaning up the vector it would help
ps. i have a GarbageCollect function that will erase weapons that dont exist anymore
for(int i = 0; i < m_weapons.size(); i++)
{
if(m_weapons[i]->IsDead())
{
m_weapons.erase(m_weapons.begin() + i);
}
}
should this work for getting rid of the sounds ( that are in the weapon class )
or is there a different thing i should be doing to clean it up
-
the vector dosent delete the memory so you have to do this your self
new garbage collector
for(int i = 0; i < m_weapons.size(); i++)
{
if(m_weapons[i]->IsDead())
{
delete m_weapons[i];
m_weapons.erase(m_weapons.begin() + i);
}
}
btw thanks for pointing out the memory thing i wouldent have thought of it ill test it real quick and get back to you on the results, cross my fingers
-
Thanks so much, i love the support that SFML has for the users great job.
-
There's a mistake in your GC loop, it will skip one element every time one is erased.
Here is a corrected code
for (std::vector<Weapon*>::iterator it = m_weapons.begin(); it != m_weapons.end(); )
{
if ((*it)->IsDead())
{
delete *it;
it = m_weapons.erase(it);
}
else
{
++it;
}
}