anewhobby at April 19th, 2012 04:59 — #1
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...
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
fireside at April 19th, 2012 15:21 — #2
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 at April 20th, 2012 05:42 — #3
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
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, you
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 \ 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.