SFML community forums

Help => Graphics => Topic started by: nullGrind on February 02, 2010, 03:34:55 pm

Title: Drawing int or double with sf::String
Post 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.
Title: Drawing int or double with sf::String
Post by: Luinechor on February 02, 2010, 03:36:56 pm
You could cast it into a std::string and set it as text of the sf::String.
Code: [Select]
// Converts an int into a string
static inline std::string int2Str(int x)
{
std::stringstream type;
type << x;
return type.str();
}
Title: Drawing int or double with sf::String
Post by: nullGrind on February 02, 2010, 03:42:10 pm
Thanks. Didnt thought about just casting it.
Title: Drawing int or double with sf::String
Post by: solgar on February 02, 2010, 04:51:13 pm
itoa (http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/) or sprintf (http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/)
Title: Drawing int or double with sf::String
Post by: Nexus on February 02, 2010, 05:04:42 pm
Quote from: "solgar"
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. ;)
Title: Drawing int or double with sf::String
Post by: nullGrind on February 02, 2010, 05:32:44 pm
My easy to use solution now is:
Code: [Select]
std::ostringstream StrP2;
StrP2 << scoreP2;
std::string scoreP2(StrP2.str());
Title: Drawing int or double with sf::String
Post by: The_Mezentine on July 29, 2011, 08:16:43 pm
Quote from: "Luinechor"
You could cast it into a std::string and set it as text of the sf::String.
Code: [Select]
// 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.
Title: Drawing int or double with sf::String
Post by: Contadotempo on July 29, 2011, 08:54:33 pm
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
Title: Re: Drawing int or double with sf::String
Post by: Jesper Juhl on August 26, 2014, 08:19:43 pm
Just for future people finding this via a search engine: std::to_string (http://en.cppreference.com/w/cpp/string/basic_string/to_string).