SFML community forums

Help => Graphics => Topic started by: Morganolla on September 18, 2010, 09:46:31 am

Title: How format and draw variables on screen
Post 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.
Title: How format and draw variables on screen
Post by: Kingdom of Fish on September 18, 2010, 11:10:53 am
If you are using C++ look at stringstreams.
Title: How format and draw variables on screen
Post by: Morganolla on September 18, 2010, 03:20:08 pm
Wow, so complex?  :shock:  Any examples?
Title: How format and draw variables on screen
Post by: Hiura on September 18, 2010, 03:25:48 pm
Google is your friend.  :wink:

for example : http://www.cppreference.com/wiki/io/sstream/constructors
Title: How format and draw variables on screen
Post by: Disch on September 18, 2010, 07:10:05 pm
Here's a simple utility function:

Code: [Select]

template <typename T>
inline std::string tostring(const T& v)
{
    std::stringstream s;
    s << v;
    return s.str();
}


Use like this:

Code: [Select]

int myvariable = 150;

sf::String foo( "My variable is:  " + tostring(myvariable) );

mywindow.Draw(foo);
Title: How format and draw variables on screen
Post by: Nexus on September 18, 2010, 07:19:36 pm
If you concatenate a lot of different types in an expression, calling ToString() for each can become tedius. There is an alternative:
Code: [Select]
#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 << ".";
}
Title: How format and draw variables on screen
Post by: Disch on September 18, 2010, 07:23:00 pm
Yeah that's much better than my approach.

Plus added benefit of being able to use iomanip stuff like setw(), etc.
Title: How format and draw variables on screen
Post by: Nexus on September 18, 2010, 07:39:38 pm
Quote from: "Disch"
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. ;)
Title: How format and draw variables on screen
Post by: Disch on September 18, 2010, 09:05:54 pm
Quote from: "Nexus"
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.

Quote
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.
Title: How format and draw variables on screen
Post by: Morganolla on September 18, 2010, 10:57:21 pm
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:
Title: How format and draw variables on screen
Post by: WitchD0ctor on September 21, 2010, 06:50:48 am
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
Title: How format and draw variables on screen
Post by: Morganolla on September 26, 2010, 08:20:36 pm
in SFML - 1000 hairs
Title: How format and draw variables on screen
Post by: panithadrum on September 26, 2010, 10:26:49 pm
Hmmm... I have exactly 1000 FPS in HGE (with 1000 hares) and 350 FPS in SFML.

I wonder why. I have i7-920 & GTX295.
Title: How format and draw variables on screen
Post by: PeterWelzien on September 27, 2010, 07:54:53 pm
Quote from: "panithadrum"
(with 1000 hares)

That's a whole lot of bunnies!  :D