SFML在多线程中失败 [英] SFML fails in multithreading
问题描述
#include< SFML \ Graphics.hpp>#include< SFML \ window.hpp>#include< SFML \ system.hpp>#include< iostream>#include< thread>#include< chrono>使用命名空间std;int h(sf :: RenderWindow * win){//做一点事win-> close();this_thread :: sleep_for(chrono ::毫秒(10));返回0;}int main(){sf :: RenderWindow窗口(sf :: VideoMode(800,600),我的窗口");线程t1(h,& window);_sleep(10000000);t1.join();返回0;}
http://www.sfml-dev.org/tutorials/2.0/graphics-draw.php#drawing-from-threads
SFML支持多线程绘图,您甚至不必做任何使它起作用的东西.唯一要记住的是停用在另一个线程中使用它之前的窗口;那是因为有窗户(更确切地说是其OpenGL上下文)不能在多个环境中处于活动状态同时线程.
调用window.setActive(false);在您将main传递给线程之前.
请记住,必须最大程度地移植GUI线程(主线程)中的事件.
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;
}
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.
这篇关于SFML在多线程中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!