Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Integer to sf::Text  (Read 11815 times)

0 Members and 1 Guest are viewing this topic.

Raidenkk

  • Newbie
  • *
  • Posts: 5
    • View Profile
Integer to sf::Text
« 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?
« Last Edit: May 09, 2012, 01:07:57 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Intenger to sf::Text
« Reply #1 on: May 09, 2012, 10:06:03 am »
-> Integer to string (Google)
-> string to sf::Text (text.setString)
Laurent Gomila - SFML developer

Zefz

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Intenger to sf::Text
« Reply #2 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) );
 
« Last Edit: May 09, 2012, 12:57:58 pm by Zefz »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Intenger to sf::Text
« Reply #3 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 ;)
Laurent Gomila - SFML developer

Zefz

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Intenger to sf::Text
« Reply #4 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));
 

Zefz

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Integer to sf::Text
« Reply #5 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)

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.