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

Author Topic: Update CircleShape color in other thread  (Read 1435 times)

0 Members and 1 Guest are viewing this topic.

Martin379

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Update CircleShape color in other thread
« on: May 25, 2017, 12:58:07 am »
Hello guys i'm trying to do what the title says, i'm not doing any multi-threading rendering, its just a thread that updates the color of a CircleShape, it works fine excepts that i only see the new color when i move the mouse or press any key, it means on any event.

i cant find something similar on the forum or google.

i appreciate any clue about what is happening.

thanks!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Update CircleShape color in other thread
« Reply #1 on: May 25, 2017, 01:08:28 am »
And why do you think you need an additional thread for that?
The answer is basically, you don't need another thread for it and even if you do, you have to synchronize the circle shape.

Generally when things only change when you move the mouse, then it means you have the processing happening in the event loop instead of the main loop.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Martin379

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Update CircleShape color in other thread
« Reply #2 on: May 25, 2017, 01:15:06 am »
This is the Main Loop:

void Manager::Run()
{
while (m_window->m_renderWindow.isOpen())
    {
        sf::Event evnt;
        while (m_window->m_renderWindow.pollEvent(evnt));
        {
            if (evnt.type == sf::Event::Closed)
                m_window->CloseWindowes();
        }

        m_window->MessagePump();
        ProcessMessages();
        Update();
        Draw();
    }
}

This is the Draw funtion:

void Manager::Draw()
{
    m_window->m_renderWindow.clear(sf::Color::Black);
    //Drive container.second only has a pointer to class EgClass*
    DriveContainer::iterator itr = m_DriveContainer.begin();
    for (; itr != m_DriveContainer.end(); )
    {
        itr->second->Draw(m_window->m_renderWindow);
        ++itr;
    }

    m_window->m_renderWindow.display();
}

this is the draw function of the instance:

void EgClass::Draw(sf::RenderWindow& renderWindow)
{
    renderWindow.draw(m_spriteSD);
    renderWindow.draw(m_circleShape);
    renderWindow.draw(m_text);
}

and this is a thread launched when i create the instance

void EgClass::Run()
{
    m_circleShape.setFillColor(sf::Color::Blue);

    while (m_state != L"Done")
    {
        m_state = L"Done";

        sf::sleep(sf::seconds(15));
    }
    m_circleShape.setFillColor(sf::Color::Green);
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Update CircleShape color in other thread
« Reply #3 on: May 25, 2017, 03:13:27 am »
Sp why don't you just implement a function that you can call right after you change the m_state variable?

I still don't see a reason to use a separate thread.
Why do you think you need it?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Martin379

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Update CircleShape color in other thread
« Reply #4 on: May 25, 2017, 03:48:49 am »
I reduced the code to just basics. The app basically draw a sprite and a circle shape showing the state of a usb card-reader connected to the pc. When the card-reader is plugged in changes the circleshape color, then it do some stuff, like giving format to the card-reader, copy some files to,..., and then finally do a safe-remove.

every state is coded with a color and it handles as max usb ports as u have to plug in card-readers, so the second thread is intended to Format drive, copy files,..., safe-remove, without stopping the main thread who is listening windows events, drawing, and creating threads for every card-reader.

if i just call a function i will be calling a function from the second thread, so i suppose is the same.

what i'm doing is creating a win32 app, starting a windows, subscribing it to the WM_DEVICECHANGE windows event and hiding it. What i discover from unhide that window is when the win32 window is onfocus or active i don't have the problem to move the mouse or press a key to update the rendering.

why it happens idk. That is why i asked here https://en.sfml-dev.org/forums/index.php?topic=22016.0 if there is a way to subscribe the render window to the WM_DEVICECHANGE windows event directly with out creating the Win32 window.

or can i create a sf::view or a sf::renderwindow inside the win32 window?

Thanks anyway.