2017-05-07 110 views
0

我嘗試一起使用SFML + OpenGL,但我不明白它的好處。在教程中寫到我應該使用window.setActive(false)SFML + OpenGL:如何在同一時間使用它們?

我的代碼:

#include <SFML/Graphics.hpp> 
#include <SFML/OpenGL.hpp> 
#include <iostream> 
#include <sstream> 
#include <math.h> 

using namespace std; 

int main() 
{ 
    sf::RenderWindow window(sf::VideoMode(640, 480), "SFML works!"); 

    sf::FloatRect frect; 
    sf::Font font; 
    font.loadFromFile("PaPYRUS.ttf"); 
    int z=45; 
    int x=0, y=0; 
    stringstream l; 
    l<<z; 
    cout<<l.str()<<endl; 
    string s="Lololololooooooooooooooooooooooooo0000000000oooooooooo\nlololololol "+l.str()+" lololo"; 
    sf::Text text(s, font, 20); 
    text.setPosition(20, 40); 
    frect=text.getGlobalBounds(); 
    cout<<frect.height<<" "<<frect.left<<" "<<frect.top<<" "<<frect.width; 

    bool running=true; 
    int x2=0, y2=0; 
    sf::CircleShape shape(30); 
    shape.setFillColor(sf::Color::Green); 


    while (running) 
    { 
     sf::Event event; 
     while (window.pollEvent(event)) 
     { 
      x2+=2; 
      y2++; 
      shape.setPosition(x2, y2); 
      if (event.type == sf::Event::Closed) 
       running=false; 
     } 

     // From that place problems begin 
     window.setActive(true); 
      glClearColor(0.0f, 0.0f, 0.1f, 0.0f); 
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     window.setActive(false); 
     window.draw(shape); 
     window.draw(text); 

     window.setActive(true); 
      glColor3f(1.0f, 0.0f, 0.1f); 
      glBegin(GL_POLYGON); 
      for (float i = 0; i<2 * 3.14; i += float(3.14/4)) 
      { 
       glVertex2f(20 + 5*sinf(i), 40 + 6*cosf(i)); 
      } 
     window.setActive(false); 

     window.display(); 
    } 
    return 0; 
} 

當我評論的OpenGL部分與圓的圖紙,一切正常。我把window.setActive(false)放在代碼的其他地方,但它不起作用。

錯誤: 「無法激活窗口的上下文」 「無法停用OpenGL的背景情況:」

請,如果可以的話,展示如何使這個代碼的權利。

+0

使用window.setActive(false)是錯誤。後來嘗試了window.pushGLStates();和window.popGLStates();,但沒什麼好 –

回答

0

pushGLStatespopGLStates是你應該使用的。由於每tutorial你需要與SFML圖形模塊繪製前推的狀態,然後彈出當您完成狀態,並希望重新使用原始的OpenGL:

glDraw... 

window.pushGLStates(); 

window.draw(...); 

window.popGLStates(); 

glDraw... 

如果您仍然有問題,有一個example包含在源代碼中,您可以使用它來驗證是否存在更嚴重的問題