Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

using sf::String to get a variable on screen

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
5 replies to this topic

#1 Crusable   Members   

594
Like
0Likes
Like

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.


#2 mdias   Members   

823
Like
0Likes
Like

Posted 19 January 2013 - 06:30 AM

I never worked with sfml, but this seems to be what you're looking for.



#3 rip-off   Moderators   

10892
Like
2Likes
Like

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.



#4 Servant of the Lord   Members   

33692
Like
3Likes
Like

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

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
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 | [Fly with me on Twitter]

#5 riverreal   Members   

791
Like
0Likes
Like

Posted 19 January 2013 - 12:19 PM

Edit:

Servant of the Lord put a similar code.


Edited by riverreal, 19 January 2013 - 12:21 PM.


#6 Crusable   Members   

594
Like
0Likes
Like

Posted 19 January 2013 - 03:10 PM

Thanks :)






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.