Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm using some SFML built in functions to draw sprites and text as an overlay on top of some OpenGL rending in an SFML RenderWindow. The opengl rendering appears fine until I add the code to draw the sprites or text. The sprite or text drawing causes the OpenGL stuff to disappear.

The follow code show what I'm trying to do

sf::RenderWindow window(sf::VideoMode(viewport.width,viewport.height,32), "SFML Window");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0,viewport.width,0,viewport.height,0,1);

while (window.pollEvent(Event))
    {
    //event handling...

    //begin drawing
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    glColor3f(col.x,col.y,col.z);
    for(int i=0;i<3;i++)
        glVertex2f(pos.x+verts[i].x,pos.y+verts[i].y);
    glEnd();


    // adding this line causes all the  previous opengl triangles not to appear
    window.draw("Sometext");


    window.display();
}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

Whats happening is that SFML also uses OpenGL to draw the text (or sprites) in 2D. So drawing of the text by SFML usually messes up whatever OpenGL state that you are using for you own OpenGL drawing.

SFML RenderWindow provides a pair of methods which allow you to 'save' the current OpenGL state onto a stack and restore the saved state from the stack. Just wrap the SFML drawing code with window.pushGLStates(); and window.popGLStates(); as in the code below.

sf::RenderWindow window(sf::VideoMode(viewport.width,viewport.height,32), "SFML Window");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0,viewport.width,0,viewport.height,0,1);

while (window.pollEvent(Event))
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    glColor3f(col.x,col.y,col.z);
    for(int i=0;i<3;i++)
        glVertex2f(pos.x+verts[i].x,pos.y+verts[i].y);
    glEnd();

    window.pushGLStates(); //save the current OpenGL state
    window.draw("Sometext");

    window.popGLStates(); //restore the saved OpenGL state to the way it was before we drew the text

    window.display();
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.