2015-05-19 64 views
0

我想我的窗口事件循環在我的主線程和其他一切運行在一個單獨的線程。如何使用SFML 2.2切換OpenGL上下文的線程?

循環是這樣的:

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(); 
} 

如何切換上下文的線索?

回答

0
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

相關問題