I'm trying to make a Soundbank class for my project, but it does not play any sounds at all.
Here is the code:
soundbank.h:
#include <SFML/Audio.hpp>
class sound
{
public:
sf::SoundBuffer buffer;
sf::Sound temp;
sound();
bool sound_load( char * file );
void sound_play();
};
sound::sound() {}
bool sound::sound_load( char * file )
{
if( !buffer.loadFromFile( file ) ) return false;
temp.setBuffer( buffer );
return true;
}
void sound::sound_play() { temp.play(); }
class soundbank
{
public:
static soundbank sound_control;
std::vector<sound> sound_list;
soundbank();
int sound_load( char * file );
void play( int ID );
void clean_up();
};
soundbank soundbank::sound_control;
soundbank::soundbank() {}
int soundbank::sound_load( char * file )
{
sound temp;
if( !temp.sound_load( file ) ) return -1;
sound_list.push_back( temp );
return ( sound_list.size() - 1 );
}
void soundbank::play( int ID )
{
if( ID < 0 || ID >= sound_list.size() ) return;
sound_list[ID].sound_play();
}
void soundbank::clean_up() { sound_list.clear(); }
...And in
main.cpp:
#include "soundbank.h"
int main( int argc , char * argv[] )
{
//stuff
int pop = soundbank::sound_control.sound_load( "pop.ogg" );
//more stuff
while( window.isOpen() )
{
sf::Event event;
while( window.pollEvent( event ) )
{
switch( event.type )
{
case sf::Event::Closed:
window_close();
break;
case sf::Event::KeyPressed:
if( event.key.code == sf::Keyboard::E )
soundbank::sound_control.play( pop );
break;
}
}
}
//even more stuff
soundbank::sound_control.clean_up();
return 0;
I have tried loading and playing sounds directly in the main function without using the Soundbank, and it worked fine.
I have also tried this:
soundbank::soundbank() { sound_list.reserve(100); }
...But it made no difference.
I don't know what am I doing wrong, so if someone more experienced could give me some pointers, I would really appreciate it.
Thank you in advance.