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

Author Topic: Event lag when music is playing  (Read 2698 times)

0 Members and 1 Guest are viewing this topic.

DrenikowB

  • Newbie
  • *
  • Posts: 1
    • View Profile
Event lag when music is playing
« on: October 12, 2012, 02:10:43 am »
Hello,

I am having an issue with my SFML. I am new to programming with SFML and the issue I have is that when I run my loop for playing music I want to be able to pause the music. It works but it takes multiple attempts of pressing the key before it takes in the event and actually pauses the music. Does anyone know how to fix it? Thanks :D

Here's some code for what I'm trying to do.

        sf::Sound Sound(Buffer);
        while (App.IsOpened()){

                if(first ==true){
                        Sound.Play();
                        first =false;
                }
               
                // Loop while the sound is playing
                if (Sound.GetStatus() == sf::Sound::Playing)
                {
                        // Display the playing position
                        std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << Sound.GetPlayingOffset() << " sec";

                        // Leave some CPU time for other threads
                        sf::Sleep(0.1f);

                        while (App.GetEvent(Event))
               {
                                // Close window : exit
                                App.GetEvent(Event);
                                if (Event.Type == sf::Event::Closed){
                                        App.Close();
                                        Sound.Stop();
                                }

                                // Escape key : exit
                                if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)){
                                        App.Close();
                                        Sound.Stop();
                                }
                                if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key:: P)){
                                        Sound.Pause();
                                }

                                const sf::Input &input = App.GetInput();

                        }

                        App.Display();
                }
« Last Edit: October 12, 2012, 07:52:33 am by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11053
    • View Profile
    • development blog
    • Email
Re: Event lag when music is playing
« Reply #1 on: October 12, 2012, 07:18:38 am »
Please make use of the code=cpp tag in this forum.

I strongly advise you to use SFML 2. SFML 1.6 hasn't changed in over 2 years and contains many bugs and lacks quite a few features.

The problem with your code might be that you call GetEvent twice in a row, thus when only one event is triggered when the key is pressed it won't get proccessed. Only call GetEvent with the while statement.

Btw if(x==true) is useless, just write if(x) for the same effect. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything