SFML - Sound Help

anewhobby 101 Apr 19, 2012 at 08:59

HI, I am a new programmer.. and would like to add some sound to an apop I am making.. nothing fancy just for some pollish to make it more fun to show people.

Now I cna play sounds fine using the SFML if it is all called in main…. but what I would like is some kind of global calss that I can call and load everything up and then play..

like in main…

MySoundClass sfx;

and then anywhere in the app go


etc etc

Dose anyone have any idea how to do this. As I said I am a new programmer and would greatly apreciate some help

Thanks in advance

2 Replies

Please log in or register to post a reply.

fireside 141 Apr 19, 2012 at 19:21

I’m a little rusty on c++, maybe someone else will write something for you, but I would suggest first studying c++ classes. What you are asking is quite do-able. You would probably pass the name of the sound file to the constructor and have it load it and hold it in buffer until you want to play it. The first thing to do would be to go through the tutorial for loading sounds with SFML. Once you have played around with a small program that loads and plays sounds, you will be familiar with the classes and functions needed to write your own class. SFML looks really quite simple already, however. If you name the buffer the name of your sound, it would be quite easy to call a play function. A simple load function so you didn’t have to print the same error message for each sound loaded into a buffer would probably be all that’s needed.

Stainless 151 Apr 20, 2012 at 09:42

There are two philosophies when it comes to class variables and things like this.

1) Static globals

In your main.cpp, NOT IN THE HEADER FILE, create an instance of your class. Then anywhere it is needed tell the compiler it exists as a static global.

so in main.cpp

#include <SoundManager.h>
SoundManager * soundManager;

void Init()
    soundManager = new SoundManager();

And anywhere else it is needed …

#include <SoundManager.h>

extern SoundManager * soundManager;

void Child::PlaySoundEffect(int id)

This is clean and simple, but it offends the purists. In some OS’s it is also illegal. Symbian only allows a single global variable for apps which has to be accessed through a special mechanism.

2) Pointer passing

The sound manager will be created in a class, not as a global. Whenever you know that you will need to use the sound manager in a child class, you
have to pass the class variable down to the child class at some point, usually when you create the child class.


#include "SoundManager.h"
class Application
      Child * child;

       explicit Application();

       void CreateChildren();

       SoundManager soundManager;


in main.cpp

void Application::CreateChildren()
     child = new Child(soundManager);

This can be a pain in the <beep> when you get your class design wrong and realise you need to see a class instance from somewhere that cannot see the class.

A half way house solution is to wrap all your class instances up in a single class and pass that to everything by default.