SFML community forums

Help => Audio => Topic started by: MicroJoe on June 04, 2013, 03:53:23 am

Title: [SOLVED] sf::Music and dynamic allocation
Post by: MicroJoe on June 04, 2013, 03:53:23 am
Good evening.

I was trying to make a little playlist manager for one of my games using the m3u format. The m3u-thing is working good but I decided to use dynamic allocation because sf::Music wasn't copyable.

Before that, I was playing music well before but since dynamic allocation the music is marked as playing but nothing goes out of my headphones.

Here is the part were I allocate the sf::Music instance :

m_currentMusics.clear();
for(auto &musicName : it->second)
{
        sf::Music* music = new sf::Music;
        if (!music->openFromFile(musicName))
        {
                delete music;
                continue;
        }
        music->setVolume(m_volume);
        m_currentMusics.push_back(music);
}

m_currentMusicIndex = 0;

if (m_currentMusics.size() > 0)
{
        m_currentMusics[m_currentMusicIndex]->play();
}

std::cout << (m_currentMusics[m_currentMusicIndex]->getStatus() == sf::Music::Status::Playing) << std::endl;
 

I do got a 1 answer on stdout meaning that the song's status is set to playing. Moreover, I toke a screenshot from VS debugger in order to show you that the song is loaded in the MusicManager after a call to the concerned method :

(http://i.imgur.com/JWEZVeb.png)

Any ideas ? :)

EDIT : Sorry for this useless topic, I set the m_volume to 0.6f instead of 60.f. Time to go bed I think.
Title: Re: [SOLVED] sf::Music and dynamic allocation
Post by: Nexus on June 04, 2013, 09:36:11 am
By the way, if you already use C++11, you should definitely work with std::unique_ptr instead of manual new and delete ;)
for(auto &musicName : it->second)
{
    std::unique_ptr<sf::Music> music(new sf::Music);
    if (!music->openFromFile(musicName))
        continue; // no delete necessary!

    music->setVolume(m_volume);
    m_currentMusics.push_back(std::move(music)); // transfer ownership to container
}

// No delete for container elements anywhere else necessary (e.g. destructor)

For a detailed example that shows the RAII advantages, you might want to check out this thread (http://en.sfml-dev.org/forums/index.php?topic=9359.msg63566#msg63566).
Title: Re: [SOLVED] sf::Music and dynamic allocation
Post by: MicroJoe on June 04, 2013, 09:45:16 am
Ok, so these pointers (http://en.cppreference.com/w/cpp/memory/unique_ptr) will be automatically destroyed on ~vector() called by my ~PlaylistManager() class ? I'm quite new to these C++11 things but I try to discover it step by step ; theses std::unique_ptr things seems very interesting because I love C++'s RAII (like you from your quote :D).

(Btw, I'm probably going to use Thor in this game too, looks… powerful! Hope it will compile with MSVC++11)
Title: Re: [SOLVED] sf::Music and dynamic allocation
Post by: Nexus on June 04, 2013, 09:55:57 am
Yes, unique pointers work quite simply: If the pointer goes out of scope, the held object will be deleted. By using std::move(), you can transfer an object from one pointer to another. At no time two pointers can refer to the same object (hence the name "unique"). These smart pointers save you from handling memory manually, in fact the problem of memory leaks belongs to the past. And the best thing is, they incur zero performance overhead :)

Thor should work with VS 2012. Just make sure you follow the tutorial  (http://www.bromeon.ch/libraries/thor/v2.0/tutorial-installation.html) on my homepage. And don't hesitate to write me an e-mail in case of problems.
Title: Re: [SOLVED] sf::Music and dynamic allocation
Post by: MicroJoe on June 04, 2013, 09:58:48 am
Ok, thank you very much for your answers ; I will test these std::unique_ptr in a sandbox C++ project before though.