Jump to content
  • Advertisement
Sign in to follow this  
Solid_Spy

Testing for framerate doesn't work!

This topic is 2118 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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 .

Edited by Solid_Spy

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!