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

Author Topic: Attempting to reference a deleted function passing sf::RenderWindow to a thread  (Read 4625 times)

0 Members and 1 Guest are viewing this topic.

Powereleven

  • Newbie
  • *
  • Posts: 36
    • View Profile
What's the terrible mistake I'm making here?

Minimal code:
#include <SFML/Graphics.hpp>
#include <thread>

void run(sf::RenderWindow &window, sf::CircleShape &shape)
{
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

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

int main()
{
        sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        sf::CircleShape shape(100.f);
        shape.setFillColor(sf::Color::Green);

        std::thread t1(run, window, shape);
        t1.join();

        return 0;
}
 

Error log:
'sf::RenderWindow::RenderWindow(const sf::RenderWindow &)': attempting to reference a deleted function
 

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
You want to use std::ref() for things you want to pass by reference to std::thread().

This is because std::thread() movies/copies by value.
« Last Edit: May 29, 2018, 11:44:16 pm by dabbertorres »

Powereleven

  • Newbie
  • *
  • Posts: 36
    • View Profile
I did what you said, but I get spammed with errors on the console:
failed to activate opengl context the requested resource is in use
 
PS.: I added
sf::Context context;
 
In the function used by the thread but the error persists

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
That's a different issue - as I recall, the correct way is to call setActive() on the OpenGL resource, (the sf::RenderWindow& in this case).

Also, as an fyi, I know some OS' don't allow event polling on anything other than the main thread. So you may encounter issues there in the future.

Powereleven

  • Newbie
  • *
  • Posts: 36
    • View Profile
Where exactly do I need to call setActive(); ? I tried many permutations but still no success

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
window.setActive(false); needs to go in the thread you do not want to do OpenGL stuff, and window.setActive(); needs to go in the thread you want to do OpenGL stuff.

Here's a contrived modification of your original code that solves the event handling issues you'll see (ie: frozen window, can't move/resize, etc) if you don't handle events on the main thread.
#include <atomic>
#include <thread>

#include <SFML/Graphics.hpp>

std::atomic_bool run = true;

void render(sf::RenderWindow &window, sf::CircleShape &shape)
{
    window.setActive();

    while (run)
    {
        window.clear();
        window.draw(shape);
        window.display();
    }
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    window.setActive(false);
    std::thread t1(render, std::ref(window), std::ref(shape));

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                run = false;
                window.close();
            }
        }
    }

    if (t1.joinable())
        t1.join();

    return 0;
}

Powereleven

  • Newbie
  • *
  • Posts: 36
    • View Profile
Thanks, but I still get the error on the console:
An internal OpenGL call failed in RenderTextureImplFBO.cpp(192).
Expression:
   GLEXT_glBindFramebuffer(GLEXT_GL_FRAMEBUFFER, 0)
Error description:
   GL_INVALID_OPERATION
   The specified operation is not allowed in the current state.
 
Is it just me?
« Last Edit: May 31, 2018, 11:39:59 pm by Powereleven »

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Hmm, I had no errors with either version.

What're your system specs? Drivers up to date? SFML version?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
We had some issues with RenderTexture's implementation in 2.5.0, so if you can try building from master. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything