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

Author Topic: Drawing int or double with sf::String  (Read 17467 times)

0 Members and 1 Guest are viewing this topic.

nullGrind

  • Newbie
  • *
  • Posts: 23
    • View Profile
Drawing int or double with sf::String
« 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.

Luinechor

  • Guest
Drawing int or double with sf::String
« Reply #1 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();
}

nullGrind

  • Newbie
  • *
  • Posts: 23
    • View Profile
Drawing int or double with sf::String
« Reply #2 on: February 02, 2010, 03:42:10 pm »
Thanks. Didnt thought about just casting it.

solgar

  • Newbie
  • *
  • Posts: 36
    • View Profile
Drawing int or double with sf::String
« Reply #3 on: February 02, 2010, 04:51:13 pm »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Drawing int or double with sf::String
« Reply #4 on: February 02, 2010, 05:04:42 pm »
Quote from: "solgar"
itoa or 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. ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

nullGrind

  • Newbie
  • *
  • Posts: 23
    • View Profile
Drawing int or double with sf::String
« Reply #5 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());

The_Mezentine

  • Newbie
  • *
  • Posts: 16
    • View Profile
Drawing int or double with sf::String
« Reply #6 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.

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Drawing int or double with sf::String
« Reply #7 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

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Drawing int or double with sf::String
« Reply #8 on: August 26, 2014, 08:19:43 pm »
Just for future people finding this via a search engine: std::to_string.

 

anything