1
Audio / sf::Sound derivative not playing
« on: May 19, 2014, 07:21:16 pm »
I am trying to make an sound manager, from where I can play sounds through a system of IDs, for this I created a child class(Called ironically Sound_Struct). The problem I'm running into is that the sound I have loaded into it doesn't play. I have checked the file itself(through use of sf::Sound), aswell as the sf::SoundBuffer vector where I load/store the buffers. Here is the code, if someone could help that would be wonderful.
SoundManager.hpp
SoundManager.cpp (only including the functions that are relevant)
The actual loading/playing is done in the main file
SoundManager.hpp
#pragma once
#include <SFML\Audio.hpp>
#include <list>
#include <string>
#include <iomanip>
class Sound_Struct : public sf::Sound
{
public:
Sound_Struct(sf::SoundBuffer S_BUFF_SRC, std::string n_designation, int n_id);
Sound_Struct(std::string n_designation, int n_id);
void set_ID(int n_id){this->m_id = n_id;}
int get_ID(){return this->m_id;}
void set_Name(std::string n_designation){this->m_designation = n_designation;}
std::string get_Name(){return this->m_designation;}
private:
std::string m_designation;
int m_id;
};
struct Music_Struct : public sf::Music
{
public:
Music_Struct(std::string m_filename, std::string n_designation, int n_id);
void set_ID(int n_id){this->m_id = n_id;}
int get_ID(){return this->m_id;}
void set_Name(std::string n_designation){this->m_designation = n_designation;}
std::string get_Name(){return this->m_designation;}
private:
std::string m_designation;
int m_id;
};
class Sound_Manager
{
public:
Sound_Manager();
bool Add_Sound_Buffer(std::string m_filename); //Adds an sound_buffer into the Sound_Buffer list. Used for initial setup.
bool Add_Sound(std::string m_filename, std::string m_designation, int m_id);
void Add_Sound(sf::SoundBuffer m_S_BUFF, std::string m_designation, int m_id);
void Add_Sound(sf::Sound* m_Sound, std::string m_designation, int m_id);
void Add_Sound(int S_BUFF_ID, std::string m_designation, int m_id);
void Add_Music(std::string m_filename, std::string m_designation, int m_id);
void Delete_Sound(int m_id);
void Delete_Sound(std::string m_designation);
sf::SoundBuffer getBuffer(int ID);
void Play_Sound(std::string m_designation);
void Play_Sound(int m_id);
void Play_Music(std::string m_designation);
void Play_Music(int m_id);
void Pause_All();
void Resume_Previous();
int no_Of_Sounds()
{
return this->Sounds.size();
};
int no_Of_Buffers()
{
return this->Sound_Buffers.size();
};
public:
std::list<Sound_Struct> Sounds; //Since sounds are given unique IDs, and likely change during runtime these are placed in a list
std::vector<sf::SoundBuffer> Sound_Buffers; //Unchanging after startup, so placed in a vector
std::vector<Music_Struct*> Songs; //Unchanging after startup, so placed in a vector
};
#include <SFML\Audio.hpp>
#include <list>
#include <string>
#include <iomanip>
class Sound_Struct : public sf::Sound
{
public:
Sound_Struct(sf::SoundBuffer S_BUFF_SRC, std::string n_designation, int n_id);
Sound_Struct(std::string n_designation, int n_id);
void set_ID(int n_id){this->m_id = n_id;}
int get_ID(){return this->m_id;}
void set_Name(std::string n_designation){this->m_designation = n_designation;}
std::string get_Name(){return this->m_designation;}
private:
std::string m_designation;
int m_id;
};
struct Music_Struct : public sf::Music
{
public:
Music_Struct(std::string m_filename, std::string n_designation, int n_id);
void set_ID(int n_id){this->m_id = n_id;}
int get_ID(){return this->m_id;}
void set_Name(std::string n_designation){this->m_designation = n_designation;}
std::string get_Name(){return this->m_designation;}
private:
std::string m_designation;
int m_id;
};
class Sound_Manager
{
public:
Sound_Manager();
bool Add_Sound_Buffer(std::string m_filename); //Adds an sound_buffer into the Sound_Buffer list. Used for initial setup.
bool Add_Sound(std::string m_filename, std::string m_designation, int m_id);
void Add_Sound(sf::SoundBuffer m_S_BUFF, std::string m_designation, int m_id);
void Add_Sound(sf::Sound* m_Sound, std::string m_designation, int m_id);
void Add_Sound(int S_BUFF_ID, std::string m_designation, int m_id);
void Add_Music(std::string m_filename, std::string m_designation, int m_id);
void Delete_Sound(int m_id);
void Delete_Sound(std::string m_designation);
sf::SoundBuffer getBuffer(int ID);
void Play_Sound(std::string m_designation);
void Play_Sound(int m_id);
void Play_Music(std::string m_designation);
void Play_Music(int m_id);
void Pause_All();
void Resume_Previous();
int no_Of_Sounds()
{
return this->Sounds.size();
};
int no_Of_Buffers()
{
return this->Sound_Buffers.size();
};
public:
std::list<Sound_Struct> Sounds; //Since sounds are given unique IDs, and likely change during runtime these are placed in a list
std::vector<sf::SoundBuffer> Sound_Buffers; //Unchanging after startup, so placed in a vector
std::vector<Music_Struct*> Songs; //Unchanging after startup, so placed in a vector
};
SoundManager.cpp (only including the functions that are relevant)
Sound_Struct::Sound_Struct(sf::SoundBuffer S_BUFF_SRC, std::string n_designation, int n_id) : sf::Sound(S_BUFF_SRC)
{
this->m_designation = n_designation;
this->m_id = n_id;
}
Sound_Manager::Sound_Manager()
{
this->Songs.clear();
this->Sounds.clear();
this->Sound_Buffers.clear();
}
bool Sound_Manager::Add_Sound_Buffer(std::string m_filename)
{
sf::SoundBuffer S_BUFF;
if(S_BUFF.loadFromFile(m_filename))
{
this->Sound_Buffers.push_back(S_BUFF);
return true;
}
else
return false;
}
bool Sound_Manager::Add_Sound(std::string m_filename, std::string m_designation, int m_id)
{
sf::SoundBuffer S_BUFF;
if(S_BUFF.loadFromFile(m_filename))
{
this->Sound_Buffers.push_back(S_BUFF);
Sound_Struct ne_Sound(S_BUFF,m_designation,m_id);
this->Sounds.push_back(ne_Sound);
return true;
}
else
return false;
}
void Sound_Manager::Add_Sound(sf::SoundBuffer m_S_BUFF, std::string m_designation, int m_id)
{
this->Sound_Buffers.push_back(m_S_BUFF);
Sound_Struct ne_Sound(m_S_BUFF, m_designation, m_id);
this->Sounds.push_back(ne_Sound);
}
void Sound_Manager::Play_Sound(std::string m_designation)
{
std::list<Sound_Struct>::iterator iter;
for(iter = this->Sounds.begin(); iter != this->Sounds.end(); iter++)
{
if(iter->get_Name() == m_designation)
iter->play();
}
}
void Sound_Manager::Play_Sound(int m_id)
{
std::list<Sound_Struct>::iterator iter;
for(iter = this->Sounds.begin(); iter != this->Sounds.end(); iter++)
{
if(iter->get_ID() == m_id)
iter->play();
}
}
{
this->m_designation = n_designation;
this->m_id = n_id;
}
Sound_Manager::Sound_Manager()
{
this->Songs.clear();
this->Sounds.clear();
this->Sound_Buffers.clear();
}
bool Sound_Manager::Add_Sound_Buffer(std::string m_filename)
{
sf::SoundBuffer S_BUFF;
if(S_BUFF.loadFromFile(m_filename))
{
this->Sound_Buffers.push_back(S_BUFF);
return true;
}
else
return false;
}
bool Sound_Manager::Add_Sound(std::string m_filename, std::string m_designation, int m_id)
{
sf::SoundBuffer S_BUFF;
if(S_BUFF.loadFromFile(m_filename))
{
this->Sound_Buffers.push_back(S_BUFF);
Sound_Struct ne_Sound(S_BUFF,m_designation,m_id);
this->Sounds.push_back(ne_Sound);
return true;
}
else
return false;
}
void Sound_Manager::Add_Sound(sf::SoundBuffer m_S_BUFF, std::string m_designation, int m_id)
{
this->Sound_Buffers.push_back(m_S_BUFF);
Sound_Struct ne_Sound(m_S_BUFF, m_designation, m_id);
this->Sounds.push_back(ne_Sound);
}
void Sound_Manager::Play_Sound(std::string m_designation)
{
std::list<Sound_Struct>::iterator iter;
for(iter = this->Sounds.begin(); iter != this->Sounds.end(); iter++)
{
if(iter->get_Name() == m_designation)
iter->play();
}
}
void Sound_Manager::Play_Sound(int m_id)
{
std::list<Sound_Struct>::iterator iter;
for(iter = this->Sounds.begin(); iter != this->Sounds.end(); iter++)
{
if(iter->get_ID() == m_id)
iter->play();
}
}
The actual loading/playing is done in the main file
if(!Miss_Lisa.Add_Sound_Buffer("Sounds/Laser_Shot.wav"))
{
return;
}
Miss_Lisa.Add_Sound(0,"Laser_Shot",1);
//The sound can now be played by calling
Miss_Lisa.play_Sound(1)
{
return;
}
Miss_Lisa.Add_Sound(0,"Laser_Shot",1);
//The sound can now be played by calling
Miss_Lisa.play_Sound(1)