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.
Edited by Mathew Bergen, 19 January 2013 - 03:25 AM.
Posted 19 January 2013 - 03:24 AM
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.
Edited by Mathew Bergen, 19 January 2013 - 03:25 AM.
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
Posted 19 January 2013 - 11:09 AM
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.
Posted 19 January 2013 - 11:17 AM
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();
}
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal