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

Author Topic: Play a sound when moving a ship  (Read 3882 times)

0 Members and 1 Guest are viewing this topic.

valenciano

  • Newbie
  • *
  • Posts: 16
    • View Profile
Play a sound when moving a ship
« on: May 14, 2018, 11:28:49 pm »
Hello,

I'd like to play a sound when my ship is moving. Where can I call "ship_sound.play()" ? I tried inside the sf::Keyboard::IsKeyPressed() but it plays the sound and restart it at the same time (which is logic)

void Game::run()
{
        window.create(sf::VideoMode(800,600), "Test", sf::Style::Default);
                window.setVerticalSyncEnabled(true);
               
        sf::SoundBuffer buffer;
       
        if(!buffer.loadFromFile("ship.ogg"))
        {
                std::cerr << "Failed to load ship sound" << std::endl;
        }
        else
        {
                std::unique_ptr<Ship> player = std::make_unique<Ship>(10,10,40);
               
                sf::Sound ship_sound;
                        ship_sound.setBuffer(buffer);
       
                while(window.isOpen() == true)
                {
                        sf::Event ev;
                       
                        while(window.pollEvent(ev) == true)
                        {
                                if(ev.type == sf::Event::Closed)
                                {
                                        window.close();
                                }
                                if(ev.type == sf::Event::KeyPressed)
                                {
                                        if(ev.key.code == sf::Keyboard::Escape)
                                        {
                                                window.close();
                                        }
                                }
                        }
                       
                        //Left : If only Left Key is pressed
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                //Left rotation: If Left + Either Down or Up are pressed
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up)))
                                {
                                        player->rotate(10);
                                }
                                else
                                {
                                        player->move_x(-10);
                                }
                        }
                       
                        //Right : If only Right Key is pressed
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                //Right rotation : If Right + Either Down or Up are pressed
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up)))
                                {
                                        player->rotate(-10);
                                }
                                else
                                {
                                        player->move_x(10);
                                }
                        }

                        //Up : If only Up Key is pressed
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) == false && sf::Keyboard::isKeyPressed(sf::Keyboard::Right) == false))
                        {
                                player->move_y(-10);
                        }
                       
                        //Down : If only Down Key is pressed
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) == false && sf::Keyboard::isKeyPressed(sf::Keyboard::Right) == false))
                        {
                                player->move_y(10);
                        }
                       
                        window.clear(sf::Color::Black);
                        player->drawOn(&window);
                        window.display();
                }
        }
}

Regards

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Play a sound when moving a ship
« Reply #1 on: May 14, 2018, 11:40:41 pm »
Depends what you really want.

If you just want to play it once, you can first check if it's still playing and only if it isn't call play() again.
If you want to play multiple sounds at the same time, I'd recommend picking a std::deque<sf::Sound> that way you can push_back() new sounds and and pop_front() when the front element has stopped playing.
Keep in mind that there's a limit of around 256 concurrent sounds (depending on your system), so maybe add a check to not over do it, after all 200 sounds at the same time, doesn't sound very good anyways.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

valenciano

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Play a sound when moving a ship
« Reply #2 on: May 15, 2018, 12:31:15 am »
Thanks for your answer eXpl0it3r,

I'd just like one sound to be played when the ship moves (every positions).

Here is a method I wrote for the Ship class :

void Ship::move_sound(sf::Sound *sound)
{
        if(isSoundPlaying == false)
        {
                sound->play();
                isSoundPlaying = true;
        }
        else
        {
                sound->stop();
                isSoundPlaying = false;
        }
}

isSoundPlaying is a boolean that is initialized to false when the Ship object is created. Then I put :

player->move_sound();

inside each if statements but it doesn't give me the loop effect I'd like to have.

I'd like that the ship sound (1 second) would repeat each time it reaches the end of its loop and that either Up, Down, Left or Right is pressed (at least).

valenciano

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Play a sound when moving a ship
« Reply #3 on: May 17, 2018, 03:32:58 pm »
Up !

 

anything