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

Author Topic: How do I display a variable?  (Read 25235 times)

0 Members and 1 Guest are viewing this topic.

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
How do I display a variable?
« on: June 23, 2011, 08:18:10 pm »
Let's say that I have the integer "x", how do I display its value on the screen?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
How do I display a variable?
« Reply #1 on: June 23, 2011, 08:26:08 pm »
Use std::stringstream to convert the variable to std::string then use sf::String (SFML 1.6) / sf::Text (2.0).
SFML / OS X developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
How do I display a variable?
« Reply #2 on: June 23, 2011, 09:46:02 pm »
In C++0x, you can also use the function std::to_string().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
How do I display a variable?
« Reply #3 on: June 24, 2011, 06:46:16 am »
Please tell me more of this "C++0x".

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
How do I display a variable?
« Reply #4 on: June 24, 2011, 10:44:42 am »
It is the new C++ standard, see the Wikipedia article.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Cuban-Pete

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
How do I display a variable?
« Reply #5 on: June 28, 2011, 06:51:26 pm »
Something like this?

Code: [Select]

#include <sstream>
#include <string>

...
string hello;

stringstream ss (stringstream::in | stringstream::out);
ss << window.GetInput().GetMouseX();
ss << " ";
ss << window.GetInput().GetMouseY();

hello = ss.str();

sf::Text text(hello);
text.SetPosition(250.f, 450.f);
text.SetColor(sf::Color(255, 255, 255, 170));
window.Draw(text);
...

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
How do I display a variable?
« Reply #6 on: June 28, 2011, 06:59:32 pm »
Yes, but you are doing it more complicated than necessary.
Code: [Select]
std::ostringstream ss; // only output stream!
ss << window.GetInput().GetMouseX() << " " << window.GetInput().GetMouseY();

sf::Text text(ss.str());
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Cuban-Pete

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
How do I display a variable?
« Reply #7 on: June 28, 2011, 07:28:36 pm »
Thanks. That is a lot cleaner.

Is it wise to make a global ostringstream and use it multiple times? I guess I also need to clear it after each use.

Or would a function be better. How would I make such a function, the identifier can be int/char or whatever floats.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
How do I display a variable?
« Reply #8 on: June 28, 2011, 07:35:42 pm »
Quote from: "Cuban-Pete"
Is it wise to make a global ostringstream and use it multiple times? I guess I also need to clear it after each use.
No. Generally, keep variables as local as possible.

Quote from: "Cuban-Pete"
Or would a function be better. How would I make such a function, the identifier can be int/char or whatever floats.
I wouldn't write a function, rather a small class with conversion operators, as shown here. But sticking to a stringstream object isn't a bad idea, either.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Cuban-Pete

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
How do I display a variable?
« Reply #9 on: June 28, 2011, 08:01:22 pm »
Ahh, that works gorgeous. Small crit on your code in the other post. For me it only worked when changing str() to string().  :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
How do I display a variable?
« Reply #10 on: June 28, 2011, 08:46:16 pm »
Quote from: "Cuban-Pete"
For me it only worked when changing str() to string().
This shouldn't happen. std::stringstream clearly has a member function called str() and not string(). Do you have a macro with this name?

Or could you show a minimal code and the error message?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Xander314

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://sfmlcoder.wordpress.com/
    • Email
How do I display a variable?
« Reply #11 on: June 28, 2011, 08:46:59 pm »
Just a note: to 'clear' the stringstream buffer, just do this:
Code: [Select]

std::ostringstream oss;
// etc
oss.str("");


Quote
changing str() to string()

Hmmm. That seems odd. What compiler are you using?
http://cplusplus.com/reference/iostream/ostringstream/str/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
How do I display a variable?
« Reply #12 on: June 28, 2011, 08:51:30 pm »
Quote from: "Xander314"
This works as str() returns a reference to the buffer, rather than just its value.
No, str() returns a std::string with the same content as the buffer, i.e. a copy.

str() is just an overloaded function. The overload with a parameter acts as a setter, the one without as a getter.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Xander314

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://sfmlcoder.wordpress.com/
    • Email
How do I display a variable?
« Reply #13 on: June 28, 2011, 09:01:59 pm »
Sorry, my mistake. How embarassing - I linked to the documentation and I hadn't even read it properly  :oops:

Cuban-Pete

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
How do I display a variable?
« Reply #14 on: June 28, 2011, 09:03:24 pm »
I get this error:

Quote from: "VS2008"
error C3861: 'str': identifier not found


I have to use this also to make it work:
Quote

#include <string>
#include <iostream>
#include <sstream>
using namespace std;


Which suggests that I'm using std::string() instead of str().