SFML community forums
Help => Graphics => Topic started 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?
-
Use std::stringstream to convert the variable to std::string then use sf::String (SFML 1.6) / sf::Text (2.0).
-
In C++0x, you can also use the function std::to_string().
-
Please tell me more of this "C++0x".
-
It is the new C++ standard, see the Wikipedia article (http://en.wikipedia.org/wiki/C%2B%2B0x).
-
Something like this?
#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);
...
-
Yes, but you are doing it more complicated than necessary.
std::ostringstream ss; // only output stream!
ss << window.GetInput().GetMouseX() << " " << window.GetInput().GetMouseY();
sf::Text text(ss.str());
-
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.
-
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.
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.
-
Ahh, that works gorgeous. Small crit on your code in the other post. For me it only worked when changing str() to string(). :)
-
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?
-
Just a note: to 'clear' the stringstream buffer, just do this:
std::ostringstream oss;
// etc
oss.str("");
changing str() to string()
Hmmm. That seems odd. What compiler are you using?
http://cplusplus.com/reference/iostream/ostringstream/str/
-
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.
-
Sorry, my mistake. How embarassing - I linked to the documentation and I hadn't even read it properly :oops:
-
I get this error:
error C3861: 'str': identifier not found
I have to use this also to make it work:
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
Which suggests that I'm using std::string() instead of str().
-
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?
-
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 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 ^^
-
The smallest and worst code where it still works... :P
#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
}
-
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).
#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;
}
-
So it works there? Then you must have changed something in your actual code. Compare the two and see what you can find.
-
Yes works for me, but you guys were wondering why it only worked with string() and not str().
-
But you have used str():
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:
operator std::string() const
{
return mStream.str();
}
-
hehe, misunderstanding. I was aiming at this part:
Usage:
int main()
{
int health = 100;
sf::String str(MakeString() << "Health: " << health);
}
there I use string() instead of str()
Sorry for wasting time...
-
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 ;)
-
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
-
Maybe the OP tried to do this:
...
...
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"
:)
-
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