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

Author Topic: Soundbank class won't work  (Read 2552 times)

0 Members and 1 Guest are viewing this topic.

Kain5056

  • Newbie
  • *
  • Posts: 26
    • View Profile
Soundbank class won't work
« on: August 18, 2013, 01:43:19 pm »
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:

Code: [Select]
#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:

Code: [Select]
#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:
Code: [Select]
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.  :)

Kain5056

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Soundbank class won't work
« Reply #1 on: August 18, 2013, 01:53:48 pm »
A few minutes after posting the topic, I figure out the solution. Typical me.  :-[

I canged this...:

Code: [Select]
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 );
}

...To this:

Code: [Select]
int soundbank::sound_load( char * file )
{
    sound temp;
    sound_list.push_back( temp );
    if( !sound_list[( sound_list.size() - 1 )].sound_load( file ) ) return -1;
    return ( sound_list.size() - 1 );
}

...And now it works perfectly!  ;D

I apologize for wasting your time creating a useless topic.  :-[

 

anything