SFML community forums

Help => Graphics => Topic started by: blueeyedlion on June 23, 2011, 08:18:10 pm

Title: How do I display a variable?
Post by: blueeyedlion 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?
Title: How do I display a variable?
Post by: Hiura 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).
Title: How do I display a variable?
Post by: Nexus on June 23, 2011, 09:46:02 pm
In C++0x, you can also use the function std::to_string().
Title: How do I display a variable?
Post by: blueeyedlion on June 24, 2011, 06:46:16 am
Please tell me more of this "C++0x".
Title: How do I display a variable?
Post by: Nexus on June 24, 2011, 10:44:42 am
It is the new C++ standard, see the Wikipedia article (http://en.wikipedia.org/wiki/C%2B%2B0x).
Title: How do I display a variable?
Post by: Cuban-Pete 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);
...
Title: How do I display a variable?
Post by: Nexus 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());
Title: How do I display a variable?
Post by: Cuban-Pete 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.
Title: How do I display a variable?
Post by: Nexus 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 (http://www.sfml-dev.org/forum/viewtopic.php?p=22019#22019). But sticking to a stringstream object isn't a bad idea, either.
Title: How do I display a variable?
Post by: Cuban-Pete 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().  :)
Title: How do I display a variable?
Post by: Nexus 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?
Title: How do I display a variable?
Post by: Xander314 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/
Title: How do I display a variable?
Post by: Nexus 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.
Title: How do I display a variable?
Post by: Xander314 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:
Title: How do I display a variable?
Post by: Cuban-Pete 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().
Title: How do I display a variable?
Post by: Nexus on June 28, 2011, 09:15:21 pm
You are almost certainly using it wrong, I have worked with std::stringstream on MSVC 2008 and I have always been calling the member function str(). By the way, you should not overuse using namespace, and completely avoid it in header files.

Can you show a minimal, complete code example with the error?
Title: How do I display a variable?
Post by: Xander314 on June 28, 2011, 09:21:32 pm
You should only have to include <sstream>.

If you are writing oss.string(), then you can't possibly be using std::string.
(Where oss is your std::ostringstream).

But it is definitely str(), anyway.

On the subject of
Code: [Select]
using namespace, IMO it's best to put it in limited scope, for example inside your main function (yes, you can do that ;) ). Then it will only bring stuff in std into scope within main. Of course, sometimes it's easier to put it globally, but that is risky.

@Nexus Beat me to it ^^
Title: How do I display a variable?
Post by: Cuban-Pete on June 28, 2011, 09:23:08 pm
The smallest and worst code where it still works... :P

Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <string>
#include <iostream>
#include <sstream>

using namespace std;

class MakeString  
{
   public:
      template <typename T>
      MakeString& operator<< (const T& value)
      {
         mStream << value;
         return *this;
      }
       
      operator std::string() const
      {
         return mStream.str();
      }
       
   private:
      std::ostringstream mStream;
};


int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "test", sf::Style::Close);

window.SetFramerateLimit(60);

   while (window.IsOpened())
{
        // Process events
        sf::Event event;
        while (window.PollEvent(event))
        {
            // Close window : exit
            if (event.Type == sf::Event::Closed)
                window.Close();

            // Escape key : exit
            if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape))
                window.Close();
        }

sf::Text text( string(MakeString() << window.GetInput().GetMouseX() << " " << window.GetInput().GetMouseY() ) );
text.SetPosition(250.f, 450.f);
text.SetColor(sf::Color(255, 255, 255, 170));
window.Draw(text);

window.Display();
    }

    return 0; //EXIT_SUCCESS
}
Title: How do I display a variable?
Post by: MorleyDev on June 28, 2011, 09:26:23 pm
The boost library provides a boost::lexical_cast function that implements this functionality (http://www.boost.org/doc/libs/1_46_1/libs/conversion/lexical_cast.htm).

Code: [Select]

#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>
     
int main()
{
     int ntest = 10;
     std::string test = boost::lexical_cast<std::string>(test);
     std::cout << test << std::endl;
     return 0;
}
Title: How do I display a variable?
Post by: Xander314 on June 28, 2011, 09:26:30 pm
So it works there? Then you must have changed something in your actual code. Compare the two and see what you can find.
Title: How do I display a variable?
Post by: Cuban-Pete on June 28, 2011, 09:29:08 pm
Yes works for me, but you guys were wondering why it only worked with string() and not str().
Title: How do I display a variable?
Post by: Xander314 on June 28, 2011, 09:36:01 pm
But you have used str():
Code: [Select]
return mStream.str();

This is what we were talking about. The point were you have used 'string' below, it is std::string, as it should be, because you are telling the compiler to construct an std::string from a 'MakeString', which it can do by virtue of this function:
Code: [Select]

      operator std::string() const
      {
         return mStream.str();
      }
Title: How do I display a variable?
Post by: Cuban-Pete on June 28, 2011, 09:44:32 pm
hehe, misunderstanding. I was aiming at this part:

Quote from: "Nexus"

Usage:
Code: [Select]
int main()
{
    int health = 100;
    sf::String str(MakeString() << "Health: " << health);
}


there I use string() instead of str()

Sorry for wasting time...
Title: How do I display a variable?
Post by: Nexus on June 28, 2011, 10:34:47 pm
Quote from: "Cuban-Pete"
there I use string() instead of str()
I don't see why str is a problem, unless you have a macro called like this.

So, instead of guessing all the time... Either show us a complete code where the error occurs, or let's forget about this ;)
Title: How do I display a variable?
Post by: Haikarainen on July 12, 2011, 09:21:09 am
I've noticed that on some systems/compilers/environments/whatever, you need to include "string.h" and not "string" to get all of its functions. Dont ask me why. Personally I've only came across this using GCC
Title: How do I display a variable?
Post by: mercurio7891 on July 12, 2011, 11:25:31 am
Maybe the OP tried to do this:

Code: [Select]

...
...
sf::Text text( str(MakeString() << window.GetInput().GetMouseX() << " " << window.GetInput().GetMouseY() ) );
...
...


which wouldn't work, and would probably get the "error C3861: 'str': identifier not found"

:)
Title: How do I display a variable?
Post by: Disch on July 14, 2011, 07:53:12 pm
Quote
I've noticed that on some systems/compilers/environments/whatever, you need to include "string.h" and not "string" to get all of its functions. Dont ask me why. Personally I've only came across this using GCC


<string.h>  (aka <cstring>) and <string> are two completely different headers.

<cstring> has things like strcpy, strcmp, etc
<string> has the std::string class