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

Author Topic: Simple help needed regarding sf::String.  (Read 1426 times)

0 Members and 1 Guest are viewing this topic.

rojan_neo

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • http://makeagame.tumblr.com
Simple help needed regarding sf::String.
« on: September 27, 2011, 09:47:23 am »
How would I use an integer variable as a parameter for sf::String.
For eg:
I have a integer variable named m_score that keeps track of player's score. How would I display this information on screen using sf::String (OR IS THERE A BETTER WAY TO DISPLAY THIS ON SCREEN??)

I am using SFML 1.6

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Simple help needed regarding sf::String.
« Reply #1 on: September 27, 2011, 01:02:37 pm »
Use std::stringstream to convert int to std::string, which can be passed as constructor argument to sf::String.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Simple help needed regarding sf::String.
« Reply #2 on: September 27, 2011, 01:47:36 pm »
I got a neat little function for doing those conversions for me:
Code: [Select]

#include <sstream>
#include <string>

template< class Type >
std::string ConvertToString( const Type &aValue )
{
std::stringstream stream;
stream << aValue;
return stream.str();
}

int main()
{
int foo = 5;
float bar = 1.27f;
std::string text = ConvertToString< int >( foo );
text += ConvertToString< float >( bar );
return 0;
}
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Simple help needed regarding sf::String.
« Reply #3 on: September 27, 2011, 08:04:59 pm »
A standard library that is compliant to C++11 has a function std::to_string() which does the same as Groogy's ConvertToString(), but probably more optimized. The alternative is boost::lexical_cast().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: