SFML community forums

Help => Graphics => Topic started by: tpih on December 19, 2011, 09:46:00 am

Title: Printing Variables
Post by: tpih on December 19, 2011, 09:46:00 am
I have searched around google but found nothing. How do I print variables to the screen? I have a score that I would like the user to be able to see.

Thanks
Title: Printing Variables
Post by: Laurent on December 19, 2011, 09:58:47 am
The question is not how to draw numbers on screen, but rather how to convert your numbers to strings that you can draw with SFML.

-> Google "c++ int to string" ;)
Title: Printing Variables
Post by: Turbine on December 19, 2011, 03:39:13 pm
Can be as easy as:
Code: [Select]
char out[16];
int score = 5;
sprintf(out, "Score: %i", score)
sf::Text text(out);
renderWindow.Draw(text);
Title: Printing Variables
Post by: Laurent on December 19, 2011, 03:48:52 pm
Nop, this code is C and unsafe.

There's only one good solution (with the current standard and no third-party libraries):
Code: [Select]
#include <sstream>

std::ostringstream oss;
oss << score;
sf::Text text(oss.str());
Title: Printing Variables
Post by: Stupebrett on December 20, 2011, 11:04:12 pm
If your compiler supports C++11, you can simply use the to_string function.