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

Author Topic: How format and draw variables on screen  (Read 6295 times)

0 Members and 1 Guest are viewing this topic.

Morganolla

  • Newbie
  • *
  • Posts: 4
    • View Profile
How format and draw variables on screen
« 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.

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
How format and draw variables on screen
« Reply #1 on: September 18, 2010, 11:10:53 am »
If you are using C++ look at stringstreams.

Morganolla

  • Newbie
  • *
  • Posts: 4
    • View Profile
How format and draw variables on screen
« Reply #2 on: September 18, 2010, 03:20:08 pm »
Wow, so complex?  :shock:  Any examples?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
How format and draw variables on screen
« Reply #3 on: September 18, 2010, 03:25:48 pm »
Google is your friend.  :wink:

for example : http://www.cppreference.com/wiki/io/sstream/constructors
SFML / OS X developer

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
How format and draw variables on screen
« Reply #4 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);

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
How format and draw variables on screen
« Reply #5 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 << ".";
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
How format and draw variables on screen
« Reply #6 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
How format and draw variables on screen
« Reply #7 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. ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
How format and draw variables on screen
« Reply #8 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.

Morganolla

  • Newbie
  • *
  • Posts: 4
    • View Profile
How format and draw variables on screen
« Reply #9 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:

WitchD0ctor

  • Full Member
  • ***
  • Posts: 100
    • View Profile
    • http://www.teleforce-blogspot.com
How format and draw variables on screen
« Reply #10 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
John Carmack can Divide by zer0.

Morganolla

  • Newbie
  • *
  • Posts: 4
    • View Profile
How format and draw variables on screen
« Reply #11 on: September 26, 2010, 08:20:36 pm »
in SFML - 1000 hairs

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
How format and draw variables on screen
« Reply #12 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.

PeterWelzien

  • Newbie
  • *
  • Posts: 38
    • View Profile
How format and draw variables on screen
« Reply #13 on: September 27, 2010, 07:54:53 pm »
Quote from: "panithadrum"
(with 1000 hares)

That's a whole lot of bunnies!  :D
/Peter Welzien