6
« on: February 12, 2012, 09:46:56 am »
Hers what I have so far:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>
#include <string>
sf::Time thetime;
sf::Clock elapsed;
std::string IntToString(int d)
{
std::stringstream ss;
ss << d;
return ss.str();
}
std::string GetFrameRate()
{ static int frameCounter = 0;
static int fps = 0;
frameCounter++;
thetime = elapsed.GetElapsedTime();
if(thetime.AsMilliseconds() > 999)
{
fps = frameCounter;
frameCounter = 0;
elapsed.Restart();
}
return IntToString(fps);
}
int main()
{
sf::RenderWindow Window(sf::VideoMode(1024, 768, 32), "Test");
Window.EnableVerticalSync(true);
sf::Text FPS("0", sf::Font::GetDefaultFont(), 25);
while (Window.IsOpen())
{
Window.Clear();
FPS.SetString(GetFrameRate());
Window.Draw(FPS);
Window.Display();
}
return 0;
}
The problem with my code is that its not exactly frames per second.
Its always slightly higher than 1000 milliseconds and would probably be much higher if I were to draw more things in the loop.
So what do I change here?