SFML community forums
Help => Graphics => Topic started by: nullGrind on February 02, 2010, 03:34:55 pm
-
Hello
I would like to draw changing numbers (type int or double) with sf::String.
Is there a way to do so?
I would like to display a changing highscore.
-
You could cast it into a std::string and set it as text of the sf::String.
// Converts an int into a string
static inline std::string int2Str(int x)
{
std::stringstream type;
type << x;
return type.str();
}
-
Thanks. Didnt thought about just casting it.
-
itoa (http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/) or sprintf (http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/)
-
itoa (http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/) or sprintf (http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/)
These are both functions from the C standard library and should be used carefully (regarding problems like buffer overflows, memory management and these). In a lot of cases, std::stringstream is easier and safer to use.
Nevertheless it's good that you posted an alternative. I just wanted to hint at some related issues. ;)
-
My easy to use solution now is:
std::ostringstream StrP2;
StrP2 << scoreP2;
std::string scoreP2(StrP2.str());
-
You could cast it into a std::string and set it as text of the sf::String.
// Converts an int into a string
static inline std::string int2Str(int x)
{
std::stringstream type;
type << x;
return type.str();
}
Sorry to bump an old thread, but this was the exact problem I was working on. Unfortunately I ran into a problem using the quoted code: I get "incomplete type is not allowed" with regards to the "type" in that code.
EDIT: *sigh* my bad, I assumed that StringStream would be included in iostream.
-
For future reference I think this topic is one of the best for this subject:
http://www.sfml-dev.org/forum/viewtopic.php?t=5154
-
Just for future people finding this via a search engine: std::to_string (http://en.cppreference.com/w/cpp/string/basic_string/to_string).