SFML community forums
Help => Audio => Topic started by: Astrof on February 06, 2009, 05:43:35 am
-
Ok, so I'm trying to make a simple Resource Manager that holds all my resources (Images, Music, Fonts, etc).
The problem I am getting is when I ask for a music, I get an error with the NonCopyable thing (which threw me off at first until I realized that it was the Music, not really a Thread that I had created).
here is my code for the resource manager (Kind of cannibalized from tutorials and other posts on this forum):
//function for loading the music
void ResourceManager::loadMusic(const std::string& name, const std::string& filepath)
{
// Only load image if key does not already exist (no duplicate images)
if(Music.find(name) == Music.end())
{
Music[name] = new sf::Music();
Music[name]->OpenFromFile(filepath);//load image from the file
Music[name]->SetLoop(true);
}
}
//function for returning the music requested
const sf::Music& ResourceManager::getMusic(const std::string& name)
{
return *Music[name];
}
//call from main that asks for the music, Resources being a ResourceManager
const sf::Music MenuMusic=Resources.getMusic("FlatOut.ogg");
I'm kind of new to C++ (have had only about a semester-ish of it) but have extensive experience with Java.
Should I have the getMusic() function return a pointer instead? I'm a little bit weary with pointers (I still can't get into the habit of using the -> operator...i know i'm weird).
-
const sf::Music MenuMusic=Resources.getMusic("FlatOut.ogg");
Change it to const sf::Music& MenuMusic=Resources.getMusic("FlatOut.ogg");
PS: Since you are returning a reference, you need to throw exception or return a dummy sound if the loading fails.
I still can't get into the habit of using the -> operator...i know i'm weird
SFML does that too.
-
If I try that, I get:
Error 2 error C2662: 'sf::SoundStream::Play' : cannot convert 'this' pointer from 'const sf::Music' to 'sf::SoundStream &'
I tried it with using pointers and it compiled properly (idk if it worked; I'm having another problem with setting up the boost libs, but thats another problem).
If I comment out the .Play thing then it goes back to NonCopyable Thread thing.
This seems to be when I try to call the Play method, if I comment it out it builds properly.
-
Why is everything const?
(BTW, Play/Pause/Stop shouldn't be const; I'll fix that)
-
What shouldn't be const? I'm sorry, I don't really understand the const all that well (I do know it differs from the final keyword from java, but still I don't know exactly how until I look it up). I guess I have the habit of const-ing everything.
-
const sf::Music& ResourceManager::getMusic(const std::string& name)
You always return const references to your Music instances, so you'll never be able to modify them (i.e. call non-const functions). You should rather return a non-const reference.
-
ok, so I do the same for Music and fonts, what do I have to do to change those? (are Images still const? what about Fonts?)
-
Just ask yourself "what am I going to do with the returned objects?". If it's only to access attributes, or call functions that don't affect the object state, you can return a const reference. If it's for modifying the object, return a non-const reference.
-
hmmm ok, I'm using the ResourceManager to hold the Objects and going to access them through the class.
I guess I don't want to modify the Images (I'll be using Sprites for that...right?)
Music I guess can change. Fonts, I think they can be const
Anyway thanks, it did compile, now I just have to fix the linking of the boost libs (I hate setting up the linker...)