SFML community forums
Help => Graphics => Topic started by: Morganolla on September 18, 2010, 09:46:31 am
-
Hi. I'm beginer. How can I format and output my variables (int, float, ets.) on screen. ( Like printf(" MyInt= %i", MyInt) )
I could not find this item in Tutorials.
-
If you are using C++ look at stringstreams.
-
Wow, so complex? :shock: Any examples?
-
Google is your friend. :wink:
for example : http://www.cppreference.com/wiki/io/sstream/constructors
-
Here's a simple utility function:
template <typename T>
inline std::string tostring(const T& v)
{
std::stringstream s;
s << v;
return s.str();
}
Use like this:
int myvariable = 150;
sf::String foo( "My variable is: " + tostring(myvariable) );
mywindow.Draw(foo);
-
If you concatenate a lot of different types in an expression, calling ToString() for each can become tedius. There is an alternative:
#include <sstream>
class MakeString
{
public:
template <typename T>
MakeString& operator<< (const T& value)
{
myStream << value;
return *this;
}
operator std::string() const
{
return myStream.str();
}
private:
std::stringstream myStream;
};
int main()
{
std::string str = MakeString() << "The image has width " << 407 << " and height " << 588 << ".";
}
-
Yeah that's much better than my approach.
Plus added benefit of being able to use iomanip stuff like setw(), etc.
-
Yeah that's much better than my approach.
I wouldn't say it's always better. To convert single values, your code fits quite well.
By the way, boost::lexical_cast() is similar to your tostring(), but optimized for builtin types like int, double, etc. Only for the seldom case where speed matters that much. ;)
-
I wouldn't say it's always better. To convert single values, your code fits quite well.
Yours would work just the same. All it needs is a ctor that takes a templated argument.
By the way, boost::lexical_cast() is similar to your tostring(), but optimized for builtin types like int, double, etc. Only for the seldom case where speed matters that much. ;)
I have so many mixed feelings about boost.
-
Thanks guys!
Here I try to compare HGE & SFML - my test ( http://www.forum.boolean.name/attachment.php?attachmentid=11305&d=1284841879)
On my video card ATI X600 I had HGE ~145fps and SFML ~150fps
How about yours? :lol:
-
how many hairs are drawn in the sfml demo? i got about 300FPS on schools computer with the SFML demo, and about 150FPS when drawing 2000 hairs with HGE and 285, when drawing 1000
-
in SFML - 1000 hairs
-
Hmmm... I have exactly 1000 FPS in HGE (with 1000 hares) and 350 FPS in SFML.
I wonder why. I have i7-920 & GTX295.
-
(with 1000 hares)
That's a whole lot of bunnies! :D