How to display FPS? Topic is solved

Problems building or running the engine, queries about how to use features etc.
slapin
Bronze Sponsor
Bronze Sponsor
Posts: 77
Joined: Fri May 23, 2025 5:04 pm
x 2

How to display FPS?

Post by slapin »

Here I have a set of basic questions I found no answers by lots of googling
and source code grepping...

  1. How to create timer which will run once per second (in Ogre framework of course)?
    I currently use deltas from FrameListener which sounds as not most effective.
  2. How to get FPS data and draw calls in last frame?
  3. How to make minimal code text overlay window just to display tiny text and what are requirements?
    (this one have several solutions for me but none of them is minimal and they are quite old). The requirement here is
    that it must scale with window sizes (i.e. always live in bottom right window regardless of resolution).

For now I will be satisfied by answers for first 2 questions, 3. looks overwhelmingly complex
and will take months to solve I guess.

rpgplayerrobin
Orc Shaman
Posts: 741
Joined: Wed Mar 18, 2009 3:03 am
x 415

Re: How to display FPS?

Post by rpgplayerrobin »

1.
What is the problem with just storing your own variable that counts up with using the DT from the FrameListener as you are saying?
The update code should just be like this then:

Code: Select all

m_timer += dt;
if(m_timer >= 1.0f)
{
	m_timer -= 1.0f; // Important to not set to 0, otherwise it will not be exactly once per second over a long period of time

// Do your once per second stuff here
}

2.
Draw calls (batches) are stored in the camera, as well as the triangle count. Note however that you would really need to loop through all cameras and not just your main one, since multiple cameras can be used in one render (from compositors and such as well):

Code: Select all

unsigned int totalAmountOfBatches = 0;
unsigned int totalAmountOfFaces = 0;

const Ogre::SceneManager::CameraList& tmpCameras = sceneManager->getCameras();
for (Ogre::SceneManager::CameraList::const_iterator it = tmpCameras.begin(); it != tmpCameras.end(); it++)
{
	Camera* camera = (*it).second;

unsigned int count = camera->_getNumRenderedBatches();
totalAmountOfBatches += count;
camera->_notifyRenderedBatches(0); // This resets the count to the next frame

count = camera->_getNumRenderedFaces();
totalAmountOfFaces += count;
camera->_notifyRenderedFaces(0); // This resets the count to the next frame
}

But this is how it is done in the Ogre source code example, which also gets you the FPS (however, I calculate my FPS myself instead, since DT is not really a good indicator of that since you may want to not render a frame if you use a max-FPS option in your game):

Code: Select all

    unsigned long currentTime = mTimer->getMilliseconds();
    if (areFrameStatsVisible() && currentTime - mLastStatUpdateTime >= FRAME_UPDATE_DELAY)
    {
        Ogre::RenderTarget::FrameStats stats = mWindow->getStatistics();

    mLastStatUpdateTime = currentTime;

    Ogre::String s("FPS: ");
    s += Ogre::StringConverter::toString((int)stats.lastFPS);

    mFpsLabel->setCaption(s);

    if (mStatsPanel->getOverlayElement()->isVisible())
    {
        Ogre::StringVector values;
        Ogre::StringStream oss;

        oss.str("");
        oss << std::fixed << std::setprecision(1) << stats.avgFPS;
        Ogre::String str = oss.str();
        values.push_back(str);

        oss.str("");
        oss << std::fixed << std::setprecision(1) << stats.bestFPS;
        str = oss.str();
        values.push_back(str);

        oss.str("");
        oss << std::fixed << std::setprecision(1) << stats.worstFPS;
        str = oss.str();
        values.push_back(str);

        str = Ogre::StringConverter::toString(stats.triangleCount);
        values.push_back(str);

        str = Ogre::StringConverter::toString(stats.batchCount);
        values.push_back(str);

        mStatsPanel->setAllParamValues(values);
    }
}

3.
I do not know the way to make a very fast and simple code to display UI.
I use a more advanced UI to have full control over it with as few batches as possible (using Gorilla at the core level though: https://github.com/betajaen/gorilla).

slapin
Bronze Sponsor
Bronze Sponsor
Posts: 77
Joined: Fri May 23, 2025 5:04 pm
x 2

Re: How to display FPS?

Post by slapin »

Ah, everybody use advanced UIs but all these UIs except for Dear ImGUI come from hell... where are my good old Xaw days... when complete UI app was 20 lines in C...
Anyway as for 1. and 2. I do the same but I hoped there were some dedicated timer APIs instead of accumulating deltas...

paroj
OGRE Team Member
OGRE Team Member
Posts: 2180
Joined: Sun Mar 30, 2014 2:51 pm
x 1168

Re: How to display FPS?

Post by paroj »

slapin wrote: Mon Jun 09, 2025 2:13 am

Here I have a set of basic questions I found no answers by lots of googling
and source code grepping...

whats wrong with FrameStats that are used all over the SampleBrowser?

meshviewer places them in the lower right corner with imgui like this:
https://github.com/OGRECave/ogre-meshvi ... er.py#L226

slapin
Bronze Sponsor
Bronze Sponsor
Posts: 77
Joined: Fri May 23, 2025 5:04 pm
x 2

Re: How to display FPS?

Post by slapin »

Thanks a lot.

Nothing is wrong about FrameStats, I am already using it.
The only thing which remains a problem here is some timer or absolute ms/us ticks source
which would be cross-platform.

rpgplayerrobin
Orc Shaman
Posts: 741
Joined: Wed Mar 18, 2009 3:03 am
x 415

Re: How to display FPS?

Post by rpgplayerrobin »

The only thing which remains a problem here is some timer or absolute ms/us ticks source
which would be cross-platform.

The DT is of course cross platform already, so why would it be an issue?

You can also make your own timer class if you want, but the code would be pretty much exactly the same as what I already posted.
You could also use the timer class in Ogre to check the current total seconds, and then check instead if a second has passed, but it would have the same results (and would most likely just be slower, even though both approaches are almost completely free).

You can also make a thread and make that call once every 1 second, but that is just much worse and slower than the other approaches.

What kind of function were you looking for exactly that cannot be done in code by yourself?

slapin
Bronze Sponsor
Bronze Sponsor
Posts: 77
Joined: Fri May 23, 2025 5:04 pm
x 2

Re: How to display FPS?

Post by slapin »

The answer is simple, if engine provides the service I want to use it and not reinvent the wheel.

slapin
Bronze Sponsor
Bronze Sponsor
Posts: 77
Joined: Fri May 23, 2025 5:04 pm
x 2

Re: How to display FPS?

Post by slapin »

Ogre::Timer is perfect for my case.
Ended up with the following fps display (in FrameListener):

Code: Select all

		...
		#include <OgreTimer.h>
		...
		Ogre::Timer fps_timer;
		bool frameStarted(const Ogre::FrameEvent &evt) override
		{
			if (fps_timer.getMilliseconds() > 1000.0f) {
				std::cout << "FPS: "
					  << mApp->getRenderWindow()
						     ->getStatistics()
						     .lastFPS
					  << "\n";
				fps_timer.reset();
			}
			return true;
		}