So, I have a global resource manager. It has worked well with sprites so far, using the approach:
- Resource manager has sf::Image instances
- Any sprite that needs an image calls a reference to the appropriate sf::Image to use with SetImage
I tried doing the same with audio:
- Resource manager has sf::SoundBuffer instances, which load from files
- Any sf::Sound that needs to play first calls get_sound_buffer with the appropriate filename.
If I do this in main:
sf::SoundBuffer buf;
buf.LoadFromFile("Sounds\\Land.wav");
sf::Sound sound;
sound.SetBuffer(buf);
sound.Play();
while(sound.GetStatus() == sf::Sound::Status::Playing) { }
It works just fine.
However, when I try and use the resource manager:
file_cabinet.cpp:
(in the constructor) sound_map["ground_collide"].LoadFromFile("Sounds\\Land.wav");
sf::SoundBuffer &file_cabinet::get_sound_buffer(const std::string &Filename) {
return sound_map[Filename];
}
(this is exactly the same as my get_image function, only replacing sf::Image with sf::SoundBuffers)
later, when I need to use a sound buffer:
sf::Sound sound;
sound.SetBuffer(file_cabinet.get_sound_buffer("Sounds\\Land.wav"));
sound.Play();
while(sound.GetStatus() == sf::Sound::Status::Playing) {}
wherever I need to invoke file_cabinet, I pass in a reference to the one file_cabinet object that exists, which is instantiated in main. This doesn't work -- there are no errors, but sounds do not play.
Is there something tricky I need to keep in mind for sound buffers?