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

Author Topic: sf::string Text using as sprintf?  (Read 11966 times)

0 Members and 1 Guest are viewing this topic.

Demian

  • Newbie
  • *
  • Posts: 16
    • View Profile
sf::string Text using as sprintf?
« on: April 09, 2010, 10:22:04 pm »
Hello

I want to use the sf::string class to print a variable, just as printf and sprintf in DOS.
I tried something like this:

Code: [Select]

//...
sf::Image Image;
    sf::Font MyFont;
    if (!Image.LoadFromFile("Bubbles.png") ||
        !MyFont.LoadFromFile("arial.ttf", 50))
           return EXIT_FAILURE;

    sf::String Hello;
    Hello.SetText("Pos y : %f Pos x : %f " ,Posy, Posx);
    Hello.SetFont(MyFont);
    Hello.SetColor(sf::Color(0, 128, 128));
    Hello.SetPosition(100.f, 100.f);
    Hello.SetRotation(15.f);
    Hello.SetSize(50.f);


The coordinates ought to show the point of the picture, but it didnt work.
I figured out "SetText" can only contain a naked text without variables.
Code: [Select]
C:\...\main.cpp|34|error: no matching function for call to `sf::String::SetText(float&)'|

Now I wonder if there is any way to pass out a variable to the window with SFML.
In case I ignored something in the tuts don't be mean to me, please!
(I really was searching for the answer!)

Thank you : )

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
sf::string Text using as sprintf?
« Reply #1 on: April 09, 2010, 10:58:21 pm »
You should use std::stringstream classes in order to convert ints, floats, etc. to std::string.
An example here:
Code: [Select]
#include <sstream> // String stream library
#include <iostream>
#include <string>

int main()
{
    // Create string stream
    std::stringstream stream;
    // Insert an int, with value 10
    stream << 10;
    // Create our final string
    std::string string;
    // Insert the value of the stream to the string
    stream >> string;
    // Check string's value
    std::cout << string << std::endl;
    // Now it prints 10, because string contains the value "10" in characters
}


Hope this helps!

Demian

  • Newbie
  • *
  • Posts: 16
    • View Profile
sf::string Text using as sprintf?
« Reply #2 on: April 09, 2010, 11:29:57 pm »
ok, I changed my code but it doesn't show up on the window :

Code: [Select]


int main()
{

//...
std::stringstream stream;


   while (App.IsOpened())
   {

        //...

        App.Display();

        stream << 2;


        std::string string;


        stream >> string;


        std::cout << string << std::endl;



   }

    return EXIT_SUCCESS;
}




I thought I have to convert it like this...somehow it doesn't show up either ; (
Code: [Select]


int main()
{
//...

std::stringstream stream;

sf::String Text;
Text.SetText("Nothing yet");
Text.SetFont(MyFont);
Text.SetSize(50);

//...

while (App.IsOpened())
   {

        //...


        stream << 2;


        std::string string;


        stream >> string;

        Text.SetText(string);

        App.Draw(Text);
        App.Display();


   }

    return EXIT_SUCCESS;
}






EDIT :

Hey, i have to apologize.
it worked!

But if I write it like this :
Code: [Select]

//...
        stream << "Something else";

        std::string string;

        stream >> string;
        Hello.SetText(string);


        // Clear screen
        App.Clear();

        // Display sprite in our window
        App.Draw(Sprite);



        App.Draw(Hello);

        // Display window contents on screen
        App.Display();
//...


The text appears like this "else Something"

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
sf::string Text using as sprintf?
« Reply #3 on: April 09, 2010, 11:56:16 pm »
Quote from: "Demian"
But if I write it like this :
Code: [Select]

//...
        stream << "Something else";

        std::string string;

        stream >> string;
        Hello.SetText(string);


        // Clear screen
        App.Clear();

        // Display sprite in our window
        App.Draw(Sprite);



        App.Draw(Hello);

        // Display window contents on screen
        App.Display();
//...


The text appears like this "else Something"


Haha it really does? It occurs because stringstream separates strings at every space character.
Anyway, there is no reason to use a std::stringstream to convert a string to a string. You know, it is already a string. You should convert only numbers. If you want to append a number to a string you can do this:
Code: [Select]
// Our string
std::string myString = "Hello, I am ";
// A stringstream to convert 21 to string format
std::stringstream myStream;
myStream << 21;
// Auxiliary string
std::string aux;
myStream >> aux;
// Append aux and another string to myString
myString += aux + " years old";
std::cout << myString << std::endl;
// The output is: "Hello, I am 15 years old"


;-)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::string Text using as sprintf?
« Reply #4 on: April 10, 2010, 10:38:11 am »
Using an ostringstream and its str() function is better (so that you get the whole content of the stream).

Defining a function is also much more convenient.
Code: [Select]
template <typename T>
std::string str(const T& x)
{
    std::ostringstream oss;
    oss << x;

    return oss.str();
}

sf::String text;
text.SetText("Pos y : " + str(Posx) + " Pos x : " + str(Posy));
Laurent Gomila - SFML developer

christoph

  • Full Member
  • ***
  • Posts: 102
    • View Profile
    • http://www.christoph-egger.org
sf::string Text using as sprintf?
« Reply #5 on: April 10, 2010, 01:25:50 pm »
Quote from: "Laurent"
Using an ostringstream and its str() function is better (so that you get the whole content of the stream).

Defining a function is also much more convenient.
Code: [Select]
template <typename T>
std::string str(const T& x)
{
    std::ostringstream oss;
    oss << x;

    return oss.str();
}

sf::String text;
text.SetText("Pos y : " + str(Posx) + " Pos x : " + str(Posy));


He that looks really much like boost's lexical_cast (and works the same way).

an interesting alternative might be boost::format working basically like that:

Code: [Select]
Hello.SetText(boost::format("Pos y : %f1% Pos x : %2% ") % Posy % Posx);

Demian

  • Newbie
  • *
  • Posts: 16
    • View Profile
sf::string Text using as sprintf?
« Reply #6 on: April 10, 2010, 04:01:42 pm »
Thank you all.

Now it works as it should be.
Another thing, I don't get the boost::format to work.

Are there any special Header to be included?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::string Text using as sprintf?
« Reply #7 on: April 10, 2010, 04:09:54 pm »
You have to download and install boost, it is not standard.
Laurent Gomila - SFML developer

nulloid

  • Full Member
  • ***
  • Posts: 134
    • View Profile
sf::string Text using as sprintf?
« Reply #8 on: April 11, 2010, 03:05:58 am »
Well, if you don't want to use boost, consider trying sprintf(). Something like that:

Code: [Select]
char* charPtr;
int i = 10;
float x = 3.03f;
char c = 'r';
sprintf(charPtr, "Whatever you %d want to %f write here %c", i, x, c);
std::string string(charPtr);


and then use sf::String::Settext(string);

I know it's rather old-school C, but if you don't want to bother with boost, you should try that. And you wanted something printf-like ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::string Text using as sprintf?
« Reply #9 on: April 11, 2010, 09:57:57 am »
If he doesn't want to bother with boost then he should use std::ostringstream, not sprintf... ;)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
sf::string Text using as sprintf?
« Reply #10 on: April 11, 2010, 02:43:40 pm »
Quote from: "nulloid"
Well, if you don't want to use boost, consider trying sprintf(). Something like that:

Code: [Select]
char* charPtr;
int i = 10;
float x = 3.03f;
char c = 'r';
sprintf(charPtr, "Whatever you %d want to %f write here %c", i, x, c);
std::string string(charPtr);
sprintf() is quite dangerous. You have already made a mistake in this short example (you didn't initialize charPtr). There are a lot of other problems coming with the I/O functions of C.

As Laurent said, std::stringstream (respectively std::ostringstream, std::istringstream) are the C++ stream classes for this purpose.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Demian

  • Newbie
  • *
  • Posts: 16
    • View Profile
sf::string Text using as sprintf?
« Reply #11 on: April 15, 2010, 06:02:19 pm »
Thank you all.
All tips are working : D

nulloid

  • Full Member
  • ***
  • Posts: 134
    • View Profile
sf::string Text using as sprintf?
« Reply #12 on: April 15, 2010, 09:15:13 pm »
Quote
You have already made a mistake in this short example


Oops... that's why I don't use them :)

 

anything