Showing posts with label framerate. Show all posts
Showing posts with label framerate. Show all posts

Monday, 28 April 2025

[SFML & C++] 3 - Real Time Clock // Tutorial

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++20

In this project we'll be creating a simple app that displays the current time on your system. Here's what the finished product will look like:

How To SFML Display Real Time Clock Tutorial using C++ - Basic Beginner


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:




Step 2 - Getting The System Time

Go to your project settings. Make sure your C++ standard is set to 20 as we'll be using some functions from the new chrono library.


First we'll need some new header files:

Then, inside our game loop, we need to get the current time:

Now, I'm normally not a fan of using auto ( I generally just don't like type obfuscation), however, std::chrono can return some absolutely disgusting looking type names so in that case, I allow it.

So we have the current system time but it's not in any format we can display that makes sense to humans. Let's do that:


Here, I'm using C++20's std::chrono::system_clock::now() to get the current time, which is much nicer than the old C-style time functions. The localtime_s function converts our raw time value into a structured tm format that breaks down the time into hours, minutes, seconds, and other components. This is a safer version of the older localtime function. I then use a stringstream with std::put_time to format this structured time into the desired HH:MM:SS string format, which I feel is much cleaner that manually building strings.

I'm a big fan of stringstreams and you'll see me use them a lot in my code examples. Pretty much anytime I need to do string manipulation, I use stringstreams.

Compile and done!

How To SFML Display Real Time Clock Tutorial using C++ - Basic Beginner

Exercise 
As a game engine programmer, I'm all about optimisation. 

Since std::chrono::system_clock::now() always returns the current time, we always have the correct clock display regardless of the framerate. But, we're getting the time and updating the text object/doing text manipulation every frame when really we don't need to (for the purposes of this exercise anyway).

Change this program to only do the time calculations and string formatting once per second.

Hint - You can use code from my previous tutorial (Displaying the Framerate) or use SFML built in types like sf::Time.

If you get stuck: Exercise Solutions. There are many ways to do this; this is just one way of doing it.

Sunday, 27 April 2025

[SFML & C++] 2 - Framerate Display // Tutorial

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

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:

How To SFML Display Framerate FPS Tutorial using C++ - Basic Beginner


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:



How To SFML Display Framerate FPS Tutorial using C++ - Basic Beginner

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: