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
Asked
Active
Viewed 1.8k times
1 Answers
5
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
window.pushGLStates();
static float frameCount=0;
frameCount++;
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.setFont(font);
atext.setCharacterSize(20);
atext.setStyle(sf::Text::Bold);
atext.setColor(sf::Color::White);
atext.setPosition(0,0);
atext.setString(ss.str()); //ss.str() converts the string buffer into a regular string
//draw the string
window.draw(atext);
//restore OpenGL setting that were saved earlier
window.popGLStates();
window.display();

Ken
- 6,116
- 3
- 35
- 51
-
4It's also worth noting that unlike in 1.6, there isn't a default font in SFML 2.0. – Alayric Dec 01 '12 at 15:22