I want to display some text and numerical data over my OpenGL scene in SFML 2.0. I've looked at the SFML 1.6 text tutorials here. But I can't get the code to work. There is (as of yet) no text tutorial for 2.0

  • \$\begingroup\$ SFML 1.6 and 2.0 are very different. I suggest you follow the 2.0 documentation rather than the 1.6 one. \$\endgroup\$ – Bugster Nov 9 '12 at 19:52
  • \$\begingroup\$ @ThePlan, SFML 2.0 documentation is still fairly sparse, that's why I put this question and answer here \$\endgroup\$ – Ken Nov 9 '12 at 21:20

A lot of the method names have changed in the transition from SFML 1.6 to 2.0. So the 1.6 tutorials will not work without modification.

First you will need to creat a font, you can do this using a .ttf file

//create a font
sf::Font font;

// Load it from a file
if (!font.loadFromFile("../sansation.ttf"))
    //find this file in the "pong" example in the SFML examples folder
    std::cout << "Error loading font\n" ;

Inside your render loop, after drawing your scene, you can add this code to render some text and numerical data:

//Draw scene
//.........Draw Scene stuff......

//save the openGLstate if using OpenGL
//becase text drawing may well change some OpenGL settings

static float frameCount=0;

std::ostringstream ss; //string buffer to convert numbers to string
ss << "Hello World , frame count is: " << frameCount;// put float into string buffer

//set up text properties
sf::Text atext;

atext.setString(ss.str()); //ss.str() converts the string buffer into a regular string 

//draw the string

//restore OpenGL setting that were saved earlier
  • 4
    \$\begingroup\$ It's also worth noting that unlike in 1.6, there isn't a default font in SFML 2.0. \$\endgroup\$ – Alayric Dec 1 '12 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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