Ldddd
Ldddd

Reputation: 220

SFML fails in multithreading

im new to sfml and c++.and I have a project that uses the sfml library's to draw the graphics but when I add an additional thread to my program it fails to execute the code inside the thread. this is my code:(please help me!)

#include <SFML\Graphics.hpp>
#include <SFML\window.hpp>
#include <SFML\system.hpp>
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;

int h(sf::RenderWindow* win){
    //do something
    win->close();
    this_thread::sleep_for(chrono::milliseconds(10));
    return 0;
}


int main(){
    sf::RenderWindow window(sf::VideoMode(800,600),"My window");
    thread t1(h,&window);
    _sleep(10000000);
    t1.join();
    return 0;
}

Upvotes: 5

Views: 6535

Answers (1)

DCurro
DCurro

Reputation: 1889

http://www.sfml-dev.org/tutorials/2.0/graphics-draw.php#drawing-from-threads

SFML supports multi-threaded drawing, and you don't even have to do anything to make it work. The only thing to remember is to deactivate a window before using it in another thread; that's because a window (more precisely its OpenGL context) cannot be active in multiple threads at the same time.

call window.setActive(false); in your main(), before you pass it off to the thread.

And remember that you must handle events in the GUI thread (the main thread) for maximum portability.

Upvotes: 19

Related Questions