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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - UndergroundCoding

Pages: [1]
1
Window / Multi-threading tutorial example does not work
« on: July 08, 2018, 08:23:40 pm »
Hi guys,

I am trying to create a rendering thread where the program would be able to call all the draw/clear/display functions. The main idea is to separate the window drawing mechanisms from the window event handler, exactly as explained in the tutorial:
https://www.sfml-dev.org/tutorials/2.5/graphics-draw.php#drawing-from-threads

However, the sample code provided in the above link does not work. I have substituted the `sf::Thread` class for the recommended `std::thread`, however I get the exact same problem when trying to run the code.

Example code:
Quote
#include <SFML/Graphics.hpp>
#include <iostream>
#include <thread>

void render(sf::RenderWindow* window)
{
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

   
    while(window->isOpen()) {
        window->clear();
        window->draw(shape);
        window->display();
    }   

    return;
}

int main() {
    sf::RenderWindow window(sf::VideoMode(800,600), "OpenGL");
    window.setActive(false);

    std::thread eventThread(render, &window);

    while(window.isOpen()) {
        sf::Event event;

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

    eventThread.join();

    return 1;
}

Where I get the following error:

Quote
Setting vertical sync not supported
XIO:  fatal IO error 11 (Resource temporarily unavailable) on X server ":1"
      after 126 requests (126 known processed) with 0 events remaining.

The first time I run the executable after compiling the program runs, draws the green circle, but will not respond to my close event. After that I get the above error. The number of requests changes each time I run the executable.

I have attempted to use `std::mutex` to avoid race conditions, and despite getting rid of the runtime error, the program is still unresponsive after rendering the drawing.

Can someone please shed some light into what I am doing wrong?

Pages: [1]
anything