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
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…
and then anywhere in the app go
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
Please log in or register to post a reply.
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.
There are two philosophies when it comes to class variables and things
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
so in main.cpp
SoundManager * soundManager;
soundManager = new SoundManager();
And anywhere else it is needed …
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,
have to pass the class variable down to the child class at some point,
usually when you create the child class.
Child * child;
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.