【发布时间】:2017-10-05 12:50:58
【问题描述】:
我尝试将SFML+OpenGL一起使用,但我不太了解它。在教程中写到我应该使用 window.setActive(false)。
我的代码:
#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 上下文:A”
如果可以,请展示如何正确编写此代码。
【问题讨论】:
-
使用 window.setActive(false) 是错误的。后来尝试使用 window.pushGLStates();和 window.popGLStates();,但没什么好
标签: c++ c++11 opengl c++14 sfml