O'Reilly logo

SFML Game Development By Example by Raimondas Pupius

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Audio manager

Similar to what we did for textures and fonts, we're going to need a way to manage sf::SoundBuffer instances easily. Luckily, our ResourceManager class is there to make it extremely convenient, so let's create the AudioManager.h file and define the way sound buffers are set up:

class AudioManager : public ResourceManager<
  AudioManager, sf::SoundBuffer>
{
public:
  AudioManager() : ResourceManager("audio.cfg"){}

  sf::SoundBuffer* Load(const std::string& l_path){
    sf::SoundBuffer* sound = new sf::SoundBuffer();
    if (!sound->loadFromFile(
      Utils::GetWorkingDirectory() + l_path))
    {
      delete sound;
      sound = nullptr;
      std::cerr << "! Failed to load sound: "
        << l_path << std::endl;
    }
    return sound;
  }
};

As you can tell already, the sound interface is ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required