Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to display framerate and make some inputs  (Read 2822 times)

0 Members and 1 Guest are viewing this topic.

zarcel

  • Newbie
  • *
  • Posts: 6
    • View Profile
How to display framerate and make some inputs
« 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.

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
How to display framerate and make some inputs
« Reply #1 on: May 30, 2010, 01:40:21 pm »
1 - There are many ways to do this. I like using stringstreams, it makes me feel cool.

Code: [Select]
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!

Code: [Select]
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!

zarcel

  • Newbie
  • *
  • Posts: 6
    • View Profile
How to display framerate and make some inputs
« Reply #2 on: May 30, 2010, 02:22:34 pm »
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:

Code: [Select]
ss.str("");

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
How to display framerate and make some inputs
« Reply #3 on: May 31, 2010, 04:28:59 pm »
Quote from: "zarcel"
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:

Code: [Select]
ss.str("");


No you don't, you should define the stringstream in it's own block, something like:

Code: [Select]
void setfpstext(sf::String fps_string) {
std::stringstream ss;
ss << fps;
...
}

 

anything