我是 sfml 和 c++ 的新手.我有一个使用 sfml 库来绘制图形的项目,但是当我向我的程序添加一个额外的线程时,它无法执行线程内的代码.这是我的代码:(请帮助我!)
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 <SFMLGraphics.hpp>
#include <SFMLwindow.hpp>
#include <SFMLsystem.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 支持多线程绘图,你甚至不必做任何让它工作的东西.唯一要记住的是停用在另一个线程中使用它之前的一个窗口;那是因为一扇窗户(更准确地说是它的 OpenGL 上下文)不能在多个同时线程.
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.
调用 window.setActive(false);在您的 main() 中,在您将其传递给线程之前.
call window.setActive(false); in your main(), before you pass it off to the thread.
请记住,您必须在 GUI 线程(主线程)中处理事件以获得最大的可移植性.
And remember that you must handle events in the GUI thread (the main thread) for maximum portability.
这篇关于SFML 在多线程中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!