Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Passing sound buffers as references  (Read 3117 times)

0 Members and 1 Guest are viewing this topic.

tokiwotomare

  • Newbie
  • *
  • Posts: 21
    • View Profile
Passing sound buffers as references
« on: April 09, 2011, 06:25:05 pm »
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:
Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
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?

Svenstaro

  • Full Member
  • ***
  • Posts: 222
    • View Profile
Passing sound buffers as references
« Reply #1 on: April 09, 2011, 10:57:14 pm »
Shouldn't it be
Code: [Select]
sound.SetBuffer(file_cabinet.get_sound_buffer("ground_collide"));

tokiwotomare

  • Newbie
  • *
  • Posts: 21
    • View Profile
Passing sound buffers as references
« Reply #2 on: April 09, 2011, 11:01:53 pm »
LOL. /facepalm

Yes. Yes it should be. Thanks. :oops: