I'm trying to develop Multi-threaded game using SFML. I compiled the code below from SFML official website.
#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);
    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();
    }
    return 0;
}
It worked well. The problem happened when I modified the code to use thread like below.
#include <SFML/Graphics.hpp>
#include <thread>
#include <iostream>
void Render(sf::RenderWindow& _window, sf::CircleShape& _shape)
{
    while (_window.isOpen())
    {
        sf::Event event;
        while (_window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                std::cout << "Closing window...\n";
                _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 renderThread(Render, std::ref(window), std::ref(shape));
    renderThread.join();
    return 0;
}
This code gives me
Segmentation fault
message on terminal when I close the window.
I want to know why this code gives me segmentation fault error and fix the error.
 
Aucun commentaire:
Enregistrer un commentaire