SFML community forums
Help => Graphics => Topic started by: zarcel on May 30, 2010, 01:18:28 pm
-
Hey guy's!
I have 2 questions for now.
1. How can I display current framerate ? I know how to calc it and I want to display it by passing framerate variable to String::setText() func. Obviously it doesn't work because types don't match. Is there any other way to display it or maybe I have just to cast it ? There should be some convert func as for me :P
2. How can I make some input field's such as text forms ? I guess by using some gui, but I have to be sure.
-
1 - There are many ways to do this. I like using stringstreams, it makes me feel cool.
std::stringstream ss;
ss << "FPS: " << FPS;//FPS being your framerate int/float/whatever
std::string str = ss.str();
You can then use SetText(str) or could probably go straight from the stringstream to sf::String. Note that stringstream needs you to include <sstream>
2 - SFML doesn't have built in support for text boxes exactly. As you said you could use one of the GUI projects, but I find that over kill for most things. I usually just use the TextEntered event.
Here's an example, complete with working backspace!
std::string inputString;
if (Event.Type == sf::Event::TextEntered)
{
char c = Event.Text.Unicode;
if (c == 8)//8 is backspace
{
if (inputString.size() > 0)//check that there is something to delete
{
inputString.erase(inputString.end() - 1);//delete the last character
}
}
else if (c < 128 && c > 31)//pretty much anything you want should be between 31 and 128
{
inputString.push_back(c);
}
}
sf::String whatever;
whatever.SetText(inputString);
Hope that was of help!
-
Heh shame on my i didn't figure it out by myself. Thanks dude.
Just in case somebody will have the same problem.
You have to set string stream to empty after every frame.
Like this:
ss.str("");
-
Heh shame on my i didn't figure it out by myself. Thanks dude.
Just in case somebody will have the same problem.
You have to set string stream to empty after every frame.
Like this:
ss.str("");
No you don't, you should define the stringstream in it's own block, something like:
void setfpstext(sf::String fps_string) {
std::stringstream ss;
ss << fps;
...
}