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

Author Topic: [SOLVED] sf::Music and dynamic allocation  (Read 3707 times)

0 Members and 1 Guest are viewing this topic.

MicroJoe

  • Newbie
  • *
  • Posts: 3
    • View Profile
[SOLVED] sf::Music and dynamic allocation
« 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 :



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.
« Last Edit: June 04, 2013, 03:58:12 am by MicroJoe »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [SOLVED] sf::Music and dynamic allocation
« Reply #1 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

MicroJoe

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: [SOLVED] sf::Music and dynamic allocation
« Reply #2 on: June 04, 2013, 09:45:16 am »
Ok, so these pointers 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)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [SOLVED] sf::Music and dynamic allocation
« Reply #3 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 on my homepage. And don't hesitate to write me an e-mail in case of problems.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

MicroJoe

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: [SOLVED] sf::Music and dynamic allocation
« Reply #4 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.

 

anything