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

Author Topic: Updating shape when property is modified  (Read 1778 times)

0 Members and 1 Guest are viewing this topic.

MarkJohnson94

  • Newbie
  • *
  • Posts: 20
    • View Profile
Updating shape when property is modified
« on: June 30, 2014, 02:08:55 pm »
Hi,

I have just started using SFML and it's a great resource. I have been searching for a solution to my problem but as of yet I haven't found one so I thought I would come here to see if anyone could help.

I'm developing an audio plugin that overrides the Soundstream and feeds the samples to my library before returning the average peak information, which my application can then display as a level meter (rising and falling based on the loudness of the current part of the audio). I have a global variable named levelValue which is set to the current peak after each return value from the library, and I set the rectangle outline thickness to this value. My thinking was that everytime I change the levelValue, then the thickness of the rectangle would change, allowing me to display a level meter.

However the rectangle doesn't update with the new value of levelValue unless I move my mouse/press buttons. This is my main GUI loop:
while (window.isOpen())
    {
                clock.restart();
        sf::Event event;
        while (window.pollEvent(event))
        {
                        sf::Keyboard::P;
            if (event.type == sf::Event::Closed)
                        {
                                window.close();
                        }if (window.waitEvent(event))
                        {
                                rectangle.setOutlineThickness(levelValue);
                        }                      
        }

        window.clear();
        window.draw(rectangle);
        window.display();
    }

If anyone would be able to help me so that I can easily update my GUI every time the levelValue changes then that would be great. levelValue changes approx 43 times a second as I am calculating the average peak for 512 samples at a time. It may also be worth noting that I cannot update the rectangle from the area in which levelValue is set, as this is in a separate method, also a separate thread.

Thanks,
Mark.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10987
    • View Profile
    • development blog
    • Email
Re: Updating shape when property is modified
« Reply #1 on: June 30, 2014, 02:17:58 pm »
SFML is designed for realtime applications. There's no noticeable impact on your system if you just update it every frame.

    window.setVerticalSyncEnabled(true);
    while (window.isOpen())
    {
        clock.restart();
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
        }

        rectangle.setOutlineThickness(levelValue);

        window.clear();
        window.draw(rectangle);
        window.display();
    }

Keep in mind though that the audio playing is multi-threaded and as such there might be a race condition on levelValue.
« Last Edit: June 30, 2014, 02:20:14 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MarkJohnson94

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Updating shape when property is modified
« Reply #2 on: June 30, 2014, 02:45:34 pm »
Hi,

Thanks very much for the help. Just as a side note to anyone else who comes across this problem, I found that if I didn't include the line window.setVerticalSyncEnabled(true);, then my code had the same problem as mentioned above. Another fix was to remove this line, and use window.setActive(true);. It appears as though the active thread was the one doing the calculations, not updating the gui, but using either of these lines of code solves the problem.

Thanks for your reply, although what exactly do you mean by a race condition, I'm not sure how it applies here? Do you just mean that my display could update just before a new value is given, and then by the time it updates again, there is another new value and so it skips displaying one value as it isn't fast enough?

Thanks,
Mark.

MarkJohnson94

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Updating shape when property is modified
« Reply #3 on: June 30, 2014, 02:49:03 pm »
I've just done some more reading and found this wiki entry. Is this what you are describing, and is there any way to avoid this? Would this occur as surely I am only modifying the data with my calculation thread, and the gui read is simply reading it?

Thanks,
Mark

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Updating shape when property is modified
« Reply #4 on: June 30, 2014, 02:56:16 pm »
Even reading a variable can go wrong if it's being modified in another thread. On today's processors this is very unlikely to happen for a variable of type int, but it's still a good habit to protect shared variables.

To make your code correct, you would have to use a std::atomic<int> instead of a plain int, or protect its access with a mutex.
Laurent Gomila - SFML developer