SFML OpenGL:如何同时与他们一起工作

SFML+OpenGL: How to work with them at the same time?

本文关键字:他们 一起 工作 何同时 OpenGL SFML      更新时间:2023-10-16

我尝试将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="Lololololooooooooooooooooooooooooo0000000000oooooooooonlololololol  "+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

请,如果可以的话,请显示如何正确制定此代码。

pushGLStatespopGLStates是您应该使用的。根据教程,您需要在使用SFML图形模块绘制之前推动状态,然后在完成后弹出状态,并希望返回使用RAW OPENGL:

glDraw...
window.pushGLStates();
window.draw(...);
window.popGLStates();
glDraw...

如果您仍然遇到麻烦,则可以用来验证来源的示例,没有更严重的问题