SFML community forums
Help => Graphics => Topic started by: Demian 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:
//...
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.
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 : )
-
You should use std::stringstream classes in order to convert ints, floats, etc. to std::string.
An example here:
#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!
-
ok, I changed my code but it doesn't show up on the window :
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 ; (
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 :
//...
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"
-
But if I write it like this :
//...
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:
// 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"
;-)
-
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.
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));
-
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.
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:
Hello.SetText(boost::format("Pos y : %f1% Pos x : %2% ") % Posy % Posx);
-
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?
-
You have to download and install boost, it is not standard.
-
Well, if you don't want to use boost, consider trying sprintf(). Something like that:
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 ;)
-
If he doesn't want to bother with boost then he should use std::ostringstream, not sprintf... ;)
-
Well, if you don't want to use boost, consider trying sprintf(). Something like that:
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.
-
Thank you all.
All tips are working : D
-
You have already made a mistake in this short example
Oops... that's why I don't use them :)