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

Author Topic: Unable to add a sf::Sound object to a map  (Read 2266 times)

0 Members and 1 Guest are viewing this topic.

Skorpion

  • Newbie
  • *
  • Posts: 28
    • View Profile
Unable to add a sf::Sound object to a map
« on: February 10, 2016, 12:57:03 am »
Hi all,

I'm using this code for handling sf::Texture objects and it works almous fine so far.
        Texture & Textures::add(const string & name, const string & directory)
        {
                map<string, Texture>::const_iterator it = tekstures.find(name);
                if (it != textures.end())
                        return textures.at(name);

                Texture tex;
                tex.loadFromFile(directory);
                textures[name] = tex;

                return textures.at(name);
        }

And I tried doing this:
        void Sounds::add(const string & name, const string & directory)
        {
                map<string, Sound>::const_iterator it = sounds.find(name);
                if (it != sounds.end())
                        return;

                SoundBuffer buffer;
                bufor.loadFromFile(directory);
               
                Sound sound;
                sound.setBuffer(buffer);
                sounds[name] = sound;   // here I get Runtime error
        }

The Runtime Error I get says
Expression: map/set iterator is not incrementable
How can I solve it?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Unable to add a sf::Sound object to a map
« Reply #1 on: February 10, 2016, 02:10:17 am »
I'm not sure why it happens there, I don't really see any iterators in use, but there are a ton of posts with that issue on StackOverflow and other places. Maybe one of them will help you further.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Unable to add a sf::Sound object to a map
« Reply #2 on: February 10, 2016, 03:12:46 am »
Although this is not directly answering your iterator question, there is a major difference between the two and the latter (for sf::Sound) just won't work.

Textures are resources so are held in your map. When you later use these textures, you use drawable objects (sf::Sprite etc.) that point to these texture resources.
Using the same concept for sounds would be like this:
Sound buffers are resources so are held in the map. When you later use these sound buffers, you use playable objects (sf::Sound) that point to these sound buffer resources.

In your code, you create a temporary sound buffer, load the sound sample from a file (without testing to see if it fails), then assign it to the new sound object. As soon as the add() method ends, the sound buffer is destroyed while the sound is still pointing to it  :(
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Skorpion

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Unable to add a sf::Sound object to a map
« Reply #3 on: February 10, 2016, 10:43:17 am »
Thank you Hapax. I didn't know it's working that way. I'll immediately check if it solves my problem and post if it doesn't.

In your code, you create a temporary sound buffer, load the sound sample from a file (without testing to see if it fails), then assign it to the new sound object.

I chcek if it fails, I only didn't want to show too much code to keep it clear :)

 

anything