Testing for framerate doesn't work!

Started by
1 comment, last by SolDirix 10 years, 6 months ago

Hello, i've been trying to do a simple framerate test that outputs the current framerate:


#include <iostream>
#include <Windows.h>

LARGE_INTEGER currentTime;
LARGE_INTEGER lastTime;
LARGE_INTEGER Frequency;
bool entered;
float finalTime;
float acc;

void calcDeltaTime(float& c);

int main()
{
	SetThreadAffinityMask(GetCurrentThread(), 0x01);
	float deltaTime;
	entered = false;
	deltaTime = 0.0f;
	acc = 0;

	QueryPerformanceFrequency(&Frequency);

	while(true)
	{
		QueryPerformanceCounter(&currentTime);
		finalTime = deltaTime;
		while(deltaTime >= 1000.0f / 60.0f)
		{
			if(entered == false)
			{
				acc += 1;
				std::cout << "Frame:" << acc << "framerate:" << 1.0f / (finalTime / 1000.0f) << std::endl;
				entered = true;
			}
			deltaTime -= 1000.0f / 60.0f;
		}
		entered = false;
		Sleep(1);
		QueryPerformanceCounter(&lastTime);
		calcDeltaTime(deltaTime);
	}

	return 0;
}

void calcDeltaTime(float& c)
{
	c += ((float)lastTime.QuadPart - (float)currentTime.QuadPart) / (float)Frequency.QuadPart * 1000.0f;
}

The loop is locked to 60 fps or lower. However, I get framerates like 22 or 19, instead of exactly 60 fps like I should get. Is it because cout is too slow, or am I doing something wrong here?

View my game dev blog here!

Advertisement
I can’t follow the intentions of your logic at all, as that is about the most convoluted way to determine FPS I have ever seen.

However, you are spamming std::cout, and yes, it is very slow.


Printing FPS is very simple:


main() {
    DWORD dwFrames = 0;
    LARGE_INTEGER liFreq;
    LARGE_INTEGER liLastTime;
    QueryPerformanceFrequency( &liFreq );
    QueryPerformanceCounter( &liLastTime );
 
 
    while ( Running ) {
        // Do Stuff.
 
 
 
        // How much time has passed since we last printed?
        LARGE_INTEGER liTime;
        QueryPerformanceCounter( &liTime );
        UINT64 ui64Time = liTime.QuadPart - liLastTime.QuadPart;
        // Count the frames.
        ++dwFrames;
        // After 1 second, print the FPS.
        if ( ui64Time >= liFreq.QuadPart ) {
            // How many seconds passed exactly?
            FLOAT fSeconds = static_cast<FLOAT>(ui64Time) / static_cast<FLOAT>(liFreq.QuadPart);
            std::cout << dwFrames / fSeconds << std::endln;
            dwFrames = 0;
            liLastTime = liTime;
        }
    }
}

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

I can’t follow the intentions of your logic at all, as that is about the most convoluted way to determine FPS I have ever seen.

However, you are spamming std::cout, and yes, it is very slow.


Printing FPS is very simple:


main() {
    DWORD dwFrames = 0;
    LARGE_INTEGER liFreq;
    LARGE_INTEGER liLastTime;
    QueryPerformanceFrequency( &liFreq );
    QueryPerformanceCounter( &liLastTime );
 
 
    while ( Running ) {
        // Do Stuff.
 
 
 
        // How much time has passed since we last printed?
        LARGE_INTEGER liTime;
        QueryPerformanceCounter( &liTime );
        UINT64 ui64Time = liTime.QuadPart - liLastTime.QuadPart;
        // Count the frames.
        ++dwFrames;
        // After 1 second, print the FPS.
        if ( ui64Time >= liFreq.QuadPart ) {
            // How many seconds passed exactly?
            FLOAT fSeconds = static_cast<FLOAT>(ui64Time) / static_cast<FLOAT>(liFreq.QuadPart);
            std::cout << dwFrames / fSeconds << std::endln;
            dwFrames = 0;
            liLastTime = liTime;
        }
    }
}
L. Spiro

well, thanks for the help smile.png .

View my game dev blog here!

This topic is closed to new replies.

Advertisement