Reputation: 1854
void CalculateFrameRate()
{
static float framesPerSecond = 0.0f; // This will store our fps
static float lastTime = 0.0f; // This will hold the time from the last frame
float currentTime = GetTickCount() * 0.001f;
++framesPerSecond;
if( currentTime - lastTime > 1.0f )
{
lastTime = currentTime;
if(SHOW_FPS == 1) fprintf(stderr, "\nCurrent Frames Per Second: %d\n\n", (int)framesPerSecond);
framesPerSecond = 0;
}
}
Should I call this function in void play(void)
or void display(void)
?
Or it does not make any difference?
Upvotes: 8
Views: 26047
Reputation: 56
I think you should just be able to divide the delta time by 1. Since the delta time is the time elapsed since the last frame or the time this frame took to render, dividing this by 1 should get how many times you can multiply that frame time to get one second. How many frames you get in one second is the framerate.
float lastTime = 0.0f;
void Application::Update()
{
// ...
// Frame info
float currentTime = glfwGetTime();
float deltaTime = currentTime - lastTime;
lastTime = currentTime;
std::cout << "FPS: " << (1 / deltaTime) << "/" << deltaTime << std::endl;
{
Upvotes: 0
Reputation: 642
Call this in every frame:
int _fpsCount = 0;
int fps = 0; // this will store the final fps for the last second
time_point<steady_clock> lastTime = steady_clock::now();
void CalculateFrameRate() {
auto currentTime = steady_clock::now();
const auto elapsedTime = duration_cast<nanoseconds>(currentTime - lastTime).count();
++_fpsCount;
if (elapsedTime > 1000000000) {
lastTime = currentTime;
fps = _fpsCount;
_fpsCount = 0;
fmt::print("{} fps\n", fps); // print out fps in every second (or you can use it elsewhere)
}
}
You can access the fps value from the fps
variable, or you can print it only once a second as it's in the code currently.
Upvotes: 0
Reputation: 15664
You should put it in the display loop. Here's an article that explains some intricacies of game loops that you should read.
Upvotes: 8
Reputation: 1
void CalculateFrameRate()
{
static float framesPerSecond = 0.0f;
static int fps;
static float lastTime = 0.0f;
float currentTime = GetTickCount() * 0.001f;
++framesPerSecond;
glPrint("Current Frames Per Second: %d\n\n", fps);
if (currentTime - lastTime > 1.0f)
{
lastTime = currentTime;
fps = (int)framesPerSecond;
framesPerSecond = 0;
}
}
Upvotes: 0
Reputation: 46051
If you have any kind of synchronization routine I suggest you place the call just after that, ie prior the big calculations. Otherwise the timing calculations can be shaky and give different values each loop...and a note, it's better to have a steady FPS than a fluctuating FPS just to maximize it. The fluctuation even so subtle makes the viewer/player aware that it's all a game and the immersion is lost.
Upvotes: 1