1
General / 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
Class file
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
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.
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;
};
#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();
}
}
}
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.