Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to use an Object Manager with sf::Music? (NonCopyable)  (Read 7386 times)

0 Members and 1 Guest are viewing this topic.

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
How to use an Object Manager with sf::Music? (NonCopyable)
« 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):

Code: [Select]

//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).

bullno1

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
How to use an Object Manager with sf::Music? (NonCopyable)
« Reply #1 on: February 06, 2009, 02:01:17 pm »
Quote
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.

Quote
I still can't get into the habit of using the -> operator...i know i'm weird

SFML does that too.

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
How to use an Object Manager with sf::Music? (NonCopyable)
« Reply #2 on: February 06, 2009, 05:08:48 pm »
If I try that, I get:
Code: [Select]

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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to use an Object Manager with sf::Music? (NonCopyable)
« Reply #3 on: February 06, 2009, 05:19:28 pm »
Why is everything const?

(BTW, Play/Pause/Stop shouldn't be const; I'll fix that)
Laurent Gomila - SFML developer

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
How to use an Object Manager with sf::Music? (NonCopyable)
« Reply #4 on: February 06, 2009, 05:25:16 pm »
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to use an Object Manager with sf::Music? (NonCopyable)
« Reply #5 on: February 06, 2009, 05:29:11 pm »
Quote
Code: [Select]
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.
Laurent Gomila - SFML developer

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
How to use an Object Manager with sf::Music? (NonCopyable)
« Reply #6 on: February 07, 2009, 07:11:52 am »
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?)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to use an Object Manager with sf::Music? (NonCopyable)
« Reply #7 on: February 07, 2009, 09:07:00 am »
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.
Laurent Gomila - SFML developer

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
How to use an Object Manager with sf::Music? (NonCopyable)
« Reply #8 on: February 07, 2009, 11:55:09 pm »
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...)

 

anything