Hello, i have a frame rate, but i don't know how to get it to display on screen using sf::String. How do i get a variable on screen? I am using sfml 1.6.
using sf::String to get a variable on screen
I too haven't used SFML, but you can always use std::stringstream. Usage is similar to std::cout and std::cin (you can use std::istringstream and std::ostringstream if you want):
#include <sstream>
int main()
{
// ...
int fps = 60;
std::stringstream stream;
stream << "FPS: " <<< fps;
sf::String text(stream.str(), MyFont, 50);
// Render text to screen
}
If you are using a NUL terminated character array based API (i.e. "C strings"), you can use the c_str() member of the returned std::string.
sf::String takes a std::string or similar (using sf::String::SetText(text)).
sf::String rightly doesn't provide a half-dozen different functions like SetInt() or SetBool() or SetFloat(), that'd be unnecessary. If every display-type class has to provide functions for setting every type of variable, it'd be alot of redundant work.
Instead, classes like sf::String provide a SetText() which takes a std::string, and you can use other functions to convert an int to a std::string.
If you're using C++11, you could use std::to_string(myInt) to convert your int to a string, like this:
int myInt = 357;
std::string myString = std::to_string(myInt);
sf::String myDisplayableText;
myDisplayableText.SetText(myString);
renderTarget.Draw(myDisplayableText);
Note: In reality, you don't want to create the sf::String every frame. Instead, sf::String should be owned by the class that contains it as a member-variable, and SetText() should only be called when the text changes. Ask questions if you don't understand what I mean.
If you aren't using C++11, and are just using 'C++' (which is probably C++03) then you can use a function like this:
#include <sstream>
std::string IntToString(int value)
{
//Uses a stringstream to format text, similar to std::cout,
//but without printing anything on the screen.
std::stringstream sstream;
//Pass the value into the stringstream.
sstream << value;
//Get the contents as a std::string.
return sstream.str();
}