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

Author Topic: [solved]sf::SoundBuffer/ Sound not hearing sound  (Read 3481 times)

0 Members and 1 Guest are viewing this topic.

DZeeSea

  • Guest
[solved]sf::SoundBuffer/ Sound not hearing sound
« on: March 03, 2013, 06:16:32 pm »
I'm already using sf::Music to play a streamed background song in my game and it works well. For some reason I can't play any sound effects trough sf::Sound.

Here's a sample of different classes :

Resource Manager(RM) is a singleton holding a SoundBuffer member :


sf::SoundBuffer& RM::hitSoundBuffer()
{
        return _hitSound;
}

RM::RM(void) :
                        SPLASH_SCREEN_PATH("res/texture.png"),
                        START_BUTTON_PATH("res/play.png"),
                        BOARD_PATH("res/board.png"),
                        BLOCKS_PATH("res/blockColors.png"),
                        PLAYSTATE_BG("res/playstate_wallpaper.png"),
                        MUSIC_BG("res/bgMusic.ogg"),
                        SOUND_HIT("res/hit.ogg")
{      
        _splashScreenTexture.loadFromFile(SPLASH_SCREEN_PATH);
        _startButtonTexture.loadFromFile(START_BUTTON_PATH);
        _boardTexture.loadFromFile(BOARD_PATH);
        _blocksTexture.loadFromFile(BLOCKS_PATH);
        _playStateBG.loadFromFile(PLAYSTATE_BG);
        _bgMusic.setLoop(true);
        _bgMusic.setVolume(50.0f);
        _bgMusic.openFromFile(MUSIC_BG);
        _hitSound.loadFromFile(SOUND_HIT);
}
 


Here's where I call it :

                        bool canMoveDown = _gameGrid.attemptPeriodicShapeMoveDown(_droppingShape);
                       
                        if(!canMoveDown)
                        {
                                //Once we put the shape down we,
                                //-Delete the falling shape since it's imprinted in the grid.
                                //-We possibly clear filled rows by the player.
                                //-We check for a loss.
                                //-Sound on hit.
                                delete _droppingShape;
                                _droppingShape = NULL;

                                sf::Sound hitSound(RM::getInstance()->hitSoundBuffer());
                                hitSound.play();

                                _gameGrid.possiblyClearRows();
                                _gameOver = _gameGrid.checkForLoss();
                        }

No idea why I'm not hearing a sound. I tried swapping audio files to no avail. This is 2.0 in VS.
« Last Edit: March 03, 2013, 07:47:48 pm by DZeeSea »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: sf::SoundBuffer/ Sound not hearing sound
« Reply #1 on: March 03, 2013, 06:37:20 pm »
Your hitSound variable is deleted at the end of its scope. Even if it's playing.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Re: sf::SoundBuffer/ Sound not hearing sound
« Reply #2 on: March 03, 2013, 10:25:10 pm »
I'm going to create a global sound object and reset it's buffer for every sound in my game then.
Don't use globals, you'll only make your life harder.
Just think of a better resource management design and keep things at least local to a class.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything