SFML community forums

Help => Graphics => Topic started by: Raidenkk on May 09, 2012, 10:03:16 am

Title: Integer to sf::Text
Post by: Raidenkk on May 09, 2012, 10:03:16 am
I want to put an integer on my screen. Can somebody explain me, how i do it?
Title: Re: Intenger to sf::Text
Post by: Laurent on May 09, 2012, 10:06:03 am
-> Integer to string (Google)
-> string to sf::Text (text.setString)
Title: Re: Intenger to sf::Text
Post by: Zefz on May 09, 2012, 12:56:12 pm
Both of these functions can be used to convert Integer to a String (using SFML data types). Both solutions should be cross-platform and compiler friendly.
#include <sstream>
using namespace std;

String toString(Int64 integer)
{
    ostringstream os;
    os << integer;
    return os.str();
}
 

#include <string>
using namespace std;

String toString(Int32 integer)
{
    char numstr[10]; // enough to hold all numbers up to 32-bits
    sprintf(numstr, "%i", integer);
    return numstr;
}
 

As Laurent mentioned above, you could then do something like this:
Text myText;
Int32 myInteger = 1000;
myText.setString( toString(myInteger) );
 
Title: Re: Intenger to sf::Text
Post by: Laurent on May 09, 2012, 12:58:16 pm
Instead of the old unsafe C code, you should mention the new C++11 to_string function ;)
Title: Re: Intenger to sf::Text
Post by: Zefz on May 09, 2012, 01:03:41 pm
Ah true. Thanks for reminding me :) I guess I'm still not used to all the fancy new stuff c++11 supports. Assuming you are compiling with the latest C++11 standard then you should definitively use to_string (defined in <string>):

Text myText;
Int32 myInteger = 1000;
myText.setString(to_string(myInteger));
 
Title: Re: Integer to sf::Text
Post by: Zefz on May 09, 2012, 01:16:41 pm
Doing some more research into std::to_string() and I found out that it is not fully supported on Windows by MinGW because of a bug (Bug 52015 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015))

This bug was last confirmed 2012-02-27 in GCC 4.6.1 (I can confirm this myself my MinGW compiler does not find any deceleration of std::to_string()). So I guess you have to use one of my previous solutions until this bug is fixed.