SFML community forums

Help => General => Topic started by: sfml_fan on May 17, 2012, 06:41:41 am

Title: Text overwrites anything I draw to screen (SFML 2) [SOLVED]
Post by: sfml_fan on May 17, 2012, 06:41:41 am
I cannot print text and opengl primitives to screen at the same time.

In the code below simply comment out the call for drawing text to screen in order to see the triangle.

  #include <stdio.h>
  #include <SFML/Audio.hpp>
  #include <SFML/Graphics.hpp>
  #include <GL/gl.h>
  #include <GL/glu.h>
 
  int main() {
    sf::RenderWindow window(sf::VideoMode(800, 800), "SFML window");
 
    sf::Font font;
    if (!font.loadFromFile("tahoma.ttf"))
      return EXIT_FAILURE;
    sf::Text text("Hello SFML", font, 50);
 
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  
    glClearDepth(1.0);        
    glDepthFunc(GL_LESS);      
    glEnable(GL_DEPTH_TEST);      
    glShadeModel(GL_SMOOTH);      
 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();      
    gluPerspective(45.0f, 2.f, 0.1f, 100.0f);
    glMatrixMode(GL_MODELVIEW);
 
    while (window.isOpen()) {
      window.setActive();
      if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
        window.close();
      }
      window.clear();
 
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
      glLoadIdentity();      
.     glTranslatef(0.f,0.0f,-6.0f);
 
     //COMMENT OUT THIS LINE
      window.draw(text);

      glBegin(GL_TRIANGLES);                      // Drawing Using Triangles
      glVertex3f( 0.0f, 1.0f, 0.0f);              // Top
      glVertex3f(-1.0f,-1.0f, 0.0f);              // Bottom Left
      glVertex3f( 1.0f,-1.0f, 0.0f);              // Bottom Right
      glEnd();
 
      window.display();
    }
    return EXIT_SUCCESS;
  }


 

So if I comment out the "window.draw(text)" line, then I can see a triangle on screen.  If I don't comment it out, I see the text "Hello SMFL" without a triangle on screen.

What am I doing wrong?

Fedora 15
SFML 2.0
Title: Re: Text overwrites anything I draw to screen (SFML 2)
Post by: sfml_fan on May 17, 2012, 08:23:27 am
Ok I figured it out after reading this:

http://www.sfml-dev.org/documentation/2.0/classsf_1_1RenderWindow.php#details

A working example of main is below for future reference :)


int main() {
    sf::RenderWindow window(sf::VideoMode(800, 800), "SFML window");
 
    sf::Font font;
    if (!font.loadFromFile("tahoma.ttf"))
      return EXIT_FAILURE;
    sf::Text text("Hello SFML", font, 50);
 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, 2.f, 0.1f, 100.0f);
    glMatrixMode(GL_MODELVIEW);
 
    while (window.isOpen()) {
      window.setActive();
      if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
        window.close();
      }
      window.clear();
 
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
      glLoadIdentity();
      glTranslatef(0.f,0.0f,-6.0f);

      glBegin(GL_TRIANGLES);                      // Drawing Using Triangles
      glVertex3f( 0.0f, 1.0f, 0.0f);              // Top
      glVertex3f(-1.0f,-1.0f, 0.0f);              // Bottom Left
      glVertex3f( 1.0f,-1.0f, 0.0f);              // Bottom Right
      glEnd();
 
      window.pushGLStates();
      window.draw(text);
      window.popGLStates();
.
      window.display();
    }
    return EXIT_SUCCESS;
  }