SFML + OpenGL:如何同时使用它们?

时间:2017-05-07 13:25:51

标签: c++ c++11 opengl c++14 sfml

我尝试一起使用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”

如果可以,请说明如何正确使用此代码。

0 个答案:

没有答案