SFML community forums

Help => General => Topic started by: Contadotempo on October 31, 2010, 06:07:02 pm

Title: [Solved]Draw FPS each second
Post by: Contadotempo on October 31, 2010, 06:07:02 pm
Hi,
I'm trying to make a function that would draw how many FPS were displayed each second.
The original function is:

Code: [Select]
double app_properties::draw_fps()
{
stringstream ss;
getfps=1/(App.GetFrameTime()); //getfps is a float declared in the app_properties class
ss << getfps;
dfps.SetText("FPS: " + (ss.str()).substr(0,2)); //dfps is a font string declared in the app_properties class
App.Draw(dfps);
return getfps;
}


How could I change this and make it draw the FPS only each second?
This is probably very easy to do, but I'm being dumb...

Thanks in advance!
Title: [Solved]Draw FPS each second
Post by: Orwel on October 31, 2010, 06:25:32 pm
You have to add two parameters. A counter and clock.
When the clock equal 1s, else draw counter and reset clock and counter.

 :wink:
Title: [Solved]Draw FPS each second
Post by: Canadadry on October 31, 2010, 06:29:01 pm
And you also could use static variable. like that

Code: [Select]
int app_properties::draw_fps()
{
static bool init = false;
static sf::Clock clock;

if(init == false)
{
init = true;
clock.reste();
}

getfps=1/(App.GetFrameTime()); //getfps is a float declared in the app_properties class

if(clock.GetEllapsedTime() >= 1.0)
{
clock.reste();
stringstream ss;
ss << getfps;
dfps.SetText("FPS: " + (ss.str()).substr(0,2)); //dfps is a font string declared in the app_properties class
App.Draw(dfps);
}

   return getfps;
}
Title: [Solved]Draw FPS each second
Post by: Contadotempo on November 02, 2010, 10:59:44 am
Aah, that was quite simple after all.
Thank you both   :wink: