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

Author Topic: Static class my map of sounds gets de-allocated  (Read 1503 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Static class my map of sounds gets de-allocated
« on: March 14, 2015, 03:10:28 pm »
Hello there,

Let me first off show you my static sound manager class

header file

#pragma once

#include "SFML/Audio.hpp"
#include "Enums.h"
#include <map>
#include <vector>

static class SoundLoader
{
public:
        SoundLoader();
        ~SoundLoader();

        void LoadSounds(GameStates state);
        void PlaySound(SoundNames soundName);

        std::map<SoundNames, sf::SoundBuffer> Sounds;
        std::vector<sf::Sound> playingSounds;

        float volume;
};

Class file

#include "SoundLoader.h"

SoundLoader::SoundLoader()
{
        volume = 100;
}

SoundLoader::~SoundLoader()
{

}

void SoundLoader::LoadSounds(GameStates gameState)
{
        Sounds[SoundNames::Explosion1].loadFromFile("Assets/Sounds/Explosion1.wav");
}

void SoundLoader::PlaySound(SoundNames soundName)
{
        std::cout << std::to_string(playingSounds.size()) << std::endl;

        if (playingSounds.size() == 0)
        {
                playingSounds.push_back(sf::Sound());
                playingSounds.at(0).setVolume(volume);
                playingSounds.at(0).setBuffer(Sounds[soundName]);
                playingSounds.at(0).play();
        }
        else
        {
                int location = -1;
                for (int i = 0; i < playingSounds.size(); i++)
                {
                        if (playingSounds.at(i).getStatus() != sf::Sound::Playing && location == -1)
                        {
                                location = i;
                        }
                }

                if (location != -1)
                {
                        playingSounds.at(0).setVolume(volume);
                        playingSounds.at(location).setBuffer(Sounds[soundName]);
                        playingSounds.at(location).play();
                }
                else
                {
                        playingSounds.push_back(sf::Sound());
                        playingSounds.at(playingSounds.size() - 1).setVolume(volume);
                        playingSounds.at(playingSounds.size() - 1).setBuffer(Sounds[soundName]);
                        playingSounds.at(playingSounds.size() - 1).play();
                }

        }
}

I understand that files will of course be destroyed if they go out of scope e.g. the function is finished. but as I have a public map the sound that is loaded shouldn't be de-allocated

When I call LoadSounds() my Sounds have a soundbuffer populated. Then when I go ahead and call PlaySound with the correct enum name, I viewed my Sounds map and the size is 0, can anyone see what I am doing wrong here? Also I am declaring my soundloader like so in my header files

game.h file
SoundLoader soundLoader;

Basically I want to have only one soundLoader which my other classes can just define a static soundLoader which will access the soundLoader which is initialized at the start of the game.
« Last Edit: March 14, 2015, 03:15:55 pm by Canvas »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Static class my map of sounds gets de-allocated
« Reply #1 on: March 16, 2015, 07:49:54 pm »
static class SoundLoader
 
You probably don't need the "static" keyword here. I don't think it is doing anything.

Quote
When I call LoadSounds() my Sounds have a soundbuffer populated. Then when I go ahead and call PlaySound with the correct enum name, I viewed my Sounds map and the size is 0, can anyone see what I am doing wrong here?
Just looking at your code I didn't spot anything directly related to your problem. You might need to show us the code using soundLoader. For example, you may be inadvertently creating a copy of the soundLoader and doing the load on one copy and play on the other copy.

Quote
game.h file
SoundLoader soundLoader;
 
Won't this cause a linker error if you try to include the header in more than one cpp file?

Quote
Basically I want to have only one soundLoader which my other classes can just define a static soundLoader which will access the soundLoader which is initialized at the start of the game.
You can consider using the "singleton pattern" for this. You can google that term for more information if you aren't familiar with it. Some people really like using singletons for resource management when creating games, but some people will say it is a bad design practice. You will just need to understand the pros and cons and decide if it works for you. An alternative to singletons is to just create your SoundLoader object in your main function before anything else and then just pass it to the objects that need it in their constructors or something. No need for global variables.