【问题标题】:SFML+OpenGL: How to work with them at the same time?SFML+OpenGL:如何同时使用它们?
【发布时间】: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


【解决方案1】:

pushGLStatespopGLStates 是您应该使用的。根据tutorial,您需要在使用 SFML 图形模块绘制之前推送状态,然后在完成后弹出状态并希望返回使用原始 opengl:

glDraw...

window.pushGLStates();

window.draw(...);

window.popGLStates();

glDraw...

如果您仍然遇到问题,源代码中包含一个example,您可以使用它来验证没有更严重的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多