【问题标题】:How do I switch the thread of an OpenGL context with SFML 2.2?如何使用 SFML 2.2 切换 OpenGL 上下文的线程?
【发布时间】:2015-07-30 23:05:50
【问题描述】:

我想让我的窗口事件循环在我的主线程中,而其他一切都在一个单独的线程中运行。

循环如下所示:

void loop(sf::RenderWindow& window)
{
    //I need the context here

    while (window.isOpen())
    {
        //Do stuff here
    }
}

int main()
{
    sf::RenderWindow window(...);

    std::thread lthread(&loop, std::ref(window));

    while (window.isOpen())
    {
        sf::Event event;
        while (window.waitEvent(event))
        {
            //Handle events
        }
    }

    lthread.join();
}

如何切换上下文的线程?

【问题讨论】:

    标签: c++ multithreading sfml openglcontext


    【解决方案1】:
    void renderingThread(sf::Window* window)
    {
        // activate the window's context
        window->setActive(true);
    
        // the rendering loop
        while (window->isOpen())
        {
            // draw...
    
            // end the current frame -- this is a rendering function (it requires the context to be active)
            window->display();
        }
    }
    
    int main()
    {
        // create the window (remember: it's safer to create it in the main thread due to OS limitations)
        sf::Window window(sf::VideoMode(800, 600), "OpenGL");
    
        // deactivate its OpenGL context
        window.setActive(false);
    
        // launch the rendering thread
        sf::Thread thread(&renderingThread, &window);
        thread.Launch();
    
        // the event/logic/whatever loop
        while (window.isOpen())
        {
            ...
        }
    
        return 0;
    }
    

    http://www.sfml-dev.org/tutorials/2.0/window-opengl.php

    【讨论】:

      猜你喜欢
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 2011-07-27
      • 2011-06-01
      • 2012-01-12
      • 2010-09-23
      相关资源
      最近更新 更多