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

Author Topic: Playing sound once on every cursor entering of specific area.  (Read 4407 times)

0 Members and 1 Guest are viewing this topic.

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
I would like to play a sound file once, every time when mouse cursor is entering a given area.
How to do that? Many thanks in advance. Here is my code (SFML 2.1):

#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

int main()
{
   sf::RenderWindow mMainWindow(sf::VideoMode(200,200,32), "soundlooptest", sf::Style::Titlebar);

   sf::Event mMainEvent;   

    sf::SoundBuffer soundBuffer;
    if (!soundBuffer.loadFromFile("click.wav"))
        return -1;
    sf::Sound sound;
    sound.setBuffer(soundBuffer);



   while(mMainWindow.isOpen())
   {
      
      while(mMainWindow.pollEvent(mMainEvent))
      {
       if(sf::Mouse::getPosition(mMainWindow).x >= 0
         && sf::Mouse::getPosition(mMainWindow).y >= 0
         && sf::Mouse::getPosition(mMainWindow).x <= 100
         && sf::Mouse::getPosition(mMainWindow).y <= 100)
       sound.play();
      }
      {
       if(sf::Mouse::getPosition(mMainWindow).x >= 0
         && sf::Mouse::getPosition(mMainWindow).y >= 0
         && sf::Mouse::getPosition(mMainWindow).x <= 100
         && sf::Mouse::getPosition(mMainWindow).y <= 100)
       sound.setLoop(false);
      }


      mMainWindow.clear();
      mMainWindow.setFramerateLimit(30);
      mMainWindow.display();
   }
   return 0;
}

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Playing sound once on every cursor entering of specific area.
« Reply #1 on: May 20, 2014, 09:21:00 pm »
Just use a boolean to keep track of whether the cursor is currently in that area or not. When you see the cursor move within the region, and the boolean tells you it wasn't in that region during the last frame, play the sound.

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Playing sound once on every cursor entering of specific area.
« Reply #2 on: May 21, 2014, 01:45:27 pm »
I've found a solution. It's not clean and perfect, but ok. It works best with the click.wav which is attached to this post.

#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

int main()
{
   sf::RenderWindow mMainWindow(sf::VideoMode(200,200,32), "soundlooptest", sf::Style::Titlebar);

   sf::Event mMainEvent;   

    sf::SoundBuffer soundBuffer;
    if (!soundBuffer.loadFromFile("click.wav"))
        return -1;
    sf::Sound sound;
    sound.setBuffer(soundBuffer);

   const int button1_WIDTH = 100;
   const int button1_HEIGHT = 100;
   sf::Color button1_COLOR = sf::Color::Red;


   sf::RectangleShape button1;
   button1.setSize(sf::Vector2f(button1_WIDTH, button1_HEIGHT));
   button1.setFillColor(button1_COLOR);
   button1.setPosition(sf::Vector2f(0, 0));

   sf::Clock clock;
   sf::Time time;

   time = clock.getElapsedTime();

   while(mMainWindow.isOpen())
   {
      
      while(mMainWindow.pollEvent(mMainEvent))
      {
      if (button1.getGlobalBounds().contains(mMainWindow.mapPixelToCoords(sf::Mouse::getPosition(mMainWindow))))
         {
        if (clock.getElapsedTime().asMilliseconds() > 300.0f)
            {
      sound.stop();
      sound.setLoop(false);
            }
      else
         {
      sound.play();
      }
   }
   else
      {
      sound.setLoop(false);
      clock.restart();
      }
   }


      mMainWindow.clear();
      mMainWindow.setFramerateLimit(30);
      mMainWindow.draw(button1);
      mMainWindow.display();
   }
   return 0;
}


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Playing sound once on every cursor entering of specific area.
« Reply #3 on: May 21, 2014, 01:57:06 pm »
Please make use of the [code=cpp]code[/code] tag when posting code. ;)

Also the code is bad. You shouldn't have that check inside the event loop. You don't check for events, so it has nothing to do in there.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Playing sound once on every cursor entering of specific area.
« Reply #4 on: May 21, 2014, 05:32:35 pm »
I've found out, that my old click.wav was somehow faulty, because it was too short and sfml seemed to have problems with it. Now, with a new click.wav (which is too big (about 860KB) for an attachment) everything is perfect.

#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

int main()
{
        sf::RenderWindow mMainWindow(sf::VideoMode(200,200,32), "soundlooptest", sf::Style::Titlebar);

        sf::Event mMainEvent;  

    sf::SoundBuffer soundBuffer;
    if (!soundBuffer.loadFromFile("click.wav"))
        return -1;
    sf::Sound sound;
    sound.setBuffer(soundBuffer);

        const int button1_WIDTH = 100;
        const int button1_HEIGHT = 100;
        sf::Color button1_COLOR = sf::Color::Red;


        sf::RectangleShape button1;
        button1.setSize(sf::Vector2f(button1_WIDTH, button1_HEIGHT));
        button1.setFillColor(button1_COLOR);
        button1.setPosition(sf::Vector2f(0, 0));

        sf::Clock clock;
        sf::Time time;
        time = clock.getElapsedTime();
       

        while(mMainWindow.isOpen())
        {
               
                while(mMainWindow.pollEvent(mMainEvent))
                {

                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape))
                        mMainWindow.close();
                }




                if (button1.getGlobalBounds().contains(mMainWindow.mapPixelToCoords(sf::Mouse::getPosition(mMainWindow))))
                        {
        if (clock.getElapsedTime().asMilliseconds() > 300.0f)
                                {
                sound.setLoop(false);
                                }
                else
                        {
                sound.play();
                }
        }
        else
                {
                sound.setLoop(false);
                clock.restart();
                }



                mMainWindow.clear();
                mMainWindow.setFramerateLimit(30);
                mMainWindow.draw(button1);
                mMainWindow.display();
        }
        return 0;
}






 
« Last Edit: May 21, 2014, 05:39:07 pm by Bogdan »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Playing sound once on every cursor entering of specific area.
« Reply #5 on: May 21, 2014, 05:42:04 pm »
You're still using the sf::Keybaord input class inside the event loop.
Read the tutorial on events and input classes again. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
SFML / OS X developer