Jump to content
  • Advertisement
Sign in to follow this  
Crusable77

using sf::String to get a variable on screen

This topic is 2074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!