SFML community forums

Help => Graphics => Topic started by: grim on January 29, 2014, 11:42:05 pm

Title: text object prints out name of font when drawn
Post by: grim on January 29, 2014, 11:42:05 pm
This is the code that creates my text and font object:

   
font.loadFromFile("sansation.ttf");
text.setColor(sf::Color::Black);
text.setFont(font);
text.setCharacterSize(20);
text.setString(""+count);

It kind of works but instead of printing out the value of count, it prints out the name of the font i'm using. Now here is the funny part.  I have an event that is supposed to increase the value of count, but instead it updates the string so that it does this:
sansation.ttf
ansation.ttf
nsation.ttf
..and so on until the entire word is gone.

Why won't it print out what I set the string to?
Title: Re: text object prints out name of font when drawn
Post by: Laurent on January 29, 2014, 11:51:31 pm
"" is a pointer to string literal. "" + count is a pointer to something which is located "count" bytes after the string literal in memory; most likely another string literal, like your font name.

Integers don't automatically convert to string in C++, you need to use something explicitly (depends on whether you use C++11 or not).
Title: Re: text object prints out name of font when drawn
Post by: Jesper Juhl on January 30, 2014, 09:19:42 pm
With C++11 this does what you seem to want:

  text.setString(std::to_string(count));

With C++98/03 you can use a stringstream to obtain a textual representation of your count integer (this also works with C++11 of course).

There is also the option of using something like boost::lexical_cast or even functions from the C library (like sprintf) although I would not recommend that.