This is part of a series of small projects and tutorials using C++ and SFML
Library version: SFML 3.0.0
C++ Standard: ISO C++17
My Base Project: https://github.com/l-paz91/SFMLProjects/blob/main/main.cpp
In this project we'll be creating a simple app that displays the frame-rate on the screen. This can be a useful tool for developers when debugging an application. Here's what the finished product will look like:
Step 1 - Displaying Text
So first, we need to display some text to the screen. We can do that using SF::Text.
In SFML, you need to create a font as well that you can pass to the text object, otherwise you won't be able to display anything. So let's do that as well:
In this project, I'm just using a default font on my PC, but you can download and use any font you like. If you run the program, you can see the text but currently it's not updated by anything.
Step 2 - Calculating Frames Per Second
SFML doesn't have a built in way to do this but we can use some simple arithmetic to achieve it.
That's a lot of frames. All we're doing here is adding Delta Time together each time we go round the loop. Once Delta Time reaches 1 (or just over it), we reset our variables back to 0 so we can accurately measure up to 1 again.
This is showing that my PC can process and render around 2000 frames in 1 second. That sounds impressive but this app isn't exactly doing much.
Note - Avoiding Framerate Dependency
Have you ever played a game and noticed your movements slow down or speed up depending on the current state of your framerate? This is framerate dependency. I first noticed it when I bought the SNES mini and played Super Mario World on an LCD TV for the first time and my muscle memory timing was all off due to how slow the game was. I thought I had mis-remembered how the game played as a child but then I remembered that CRT monitors have insane refresh rates compared to a 2010 LCD TV. Back then, SNES games were tied to how much you could process during V-Blanks (the time it took the ray gun to get back to the top of the screen), so Super Mario World ran much faster when I played it as a kid on my 14" CRT.
The framerate counter we've implemented shows how quickly your PC can process and render frames. However, if you tie game movement or mechanics directly to frames (like moving an object 5 pixels every frame), your game will run faster on powerful computers and slower on weaker ones.
This creates an inconsistent experience for players.
The solution is something you may have already seen (I implemented it in my first SFML tutorial here: https://lptcp.blogspot.com/2025/04/sfml-c-dvd-logo-screensaver-tutorial.html).
It's to scale your movement and actions by Delta Time. So instead of
object.move(5, 0) // 5 pixels per second (very fast if not connected to deltaTime)
use
object.move(5 * deltaTime, 0) // 5 pixels per second becomes very slow when scaled by deltaTime so you might want to make this much higher
This will move your object at the given speed regardless of framerate. This will make your game run at the same speed for everyone but it doesn't stop players with powerful PCs having a smoother experience than those with weaker PCs.
If you'd like to read more about this, here's a great resource:
No comments:
Post a Comment