I just played around a bit with it and it seems to be the
std::vector that is causing the problem, which makes sense after a bit thinking, because you're constantly resizing the vector and thus the sound objects get copied around while they're actually playing, which is obviously a problem.
Using for instance
std::deque solves the problem.
Here's a slightly modified, but at least complete example.
The
events boolean can be used to switch between event and real-time mouse input.
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <deque>
class Sound
{
private:
sf::SoundBuffer soundBuffer;
std::deque<sf::Sound> soundInstances;
public:
void Load(std::string filename)
{
soundBuffer.loadFromFile(filename);
}
void Update(void)
{
for (int i = 0 ; i < soundInstances.size() ; ++i)
{
if (soundInstances[i].getStatus() == sf::Sound::Stopped)
{
soundInstances.erase(soundInstances.begin() + i);
--i;
}
}
}
void Play(void)
{
soundInstances.push_back(sf::Sound(soundBuffer));
soundInstances.back().play();
}
unsigned int Count()
{
soundInstances.size();
}
};
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Rotate Test");
window.setFramerateLimit(60);
Sound snd;
snd.Load("laser.wav");
sf::Event event;
bool events = true;
while(window.isOpen())
{
while(window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if(events && event.type == sf::Event::MouseButtonReleased &&
event.mouseButton.button == sf::Mouse::Left)
snd.Play();
}
if(!events && sf::Mouse::isButtonPressed(sf::Mouse::Left))
snd.Play();
if(snd.Count() > 0)
std::cout << snd.Count() << std::endl;
snd.Update();
window.clear();
window.display();
}
}