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:


Saturday, 26 April 2025

[SFML & C++] 1 - DVD Logo Screensaver // 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

For those of you who may not have grown up with DVDs, they came with a screensaver that consisted of the DVD logo bouncing around the screen. It became something of a meme as the logo would sometimes get tantalisingly close to hitting a corner but it never would.

This is a simple project that will get you loading in images and dealing with simple collisions. Here's what the finished product will look like:


Step 1 - Displaying the Image

First off. In SFML you need a texture to store your image, and then your objects use the texture to display the image. So let's create a texture.


Now we've got a texture. We need an object that can display it. I'll use a sprite.

We'll display the logo in the Render section of the game loop.

The default colour of an SFML background is black. My Png image is black so we won't be able to see it. We could try and change the colour of the logo itself but it's black so even if we multiply by another colour, it will still be black. The setColour() methods in SFML work by changing the hue. So we'll just simply change the background colour for the moment.

Step 2 - Making It Move

In making it move, there are 2 variables we need to think about; the direction and the speed.

We can represent the direction using a Vector and the speed with a float:

To make the logo move, we need to update the position of it each frame. We do that update in the update section before rendering.

If you play, the dvd logo starts moving across the screen diagonally and then goes off the screen. That's because we haven't told it to stop when it hits the "edge".

Also, the dvd logo doesn't move smoothly, that's because the direction and speed should be multiplied along with the delta time. This makes sure that the logo's movement is adjusted according to how much time has passed since the last frame was rendered, ensuring smooth movement at varying frame rates.

With that done, let's add some simple collision.

Step 3 - Simple Collision

I'm going to create a move function above main to store this logic.

I also made our window sizes global constants. We also need a clock for timing so we can make the movement smooth.

Now we just need to call the function:

Without using Delta Time, I had to set the speed very small (0.05) initially, but now you can increase the speed to whatever you like (otherwise it will look like it's not moving).

Optional quest - I've left you a quirk. Find it and fix it.

Step 4 - Extras

Lets do something a little extra though, when the logo hits an edge, lets change the colour of the background. We could try and change the colour of the logo itself but it's black so even if we multiply by another colour, it will still be black.

First lets create a variable to store this in

Our function now needs another argument, lets pass in the colour. We're making it a reference so we can change it in the function and it will stay the new colour between frames.

I made it a small function that we can call. We now just need to modify a few places in the main loop:

And done! That's a small project done in a hundred lines or so. You could also do other things when it hits the side of the screen like play a sound, change the image itself, increase the speed. You've then basically got a very basic version of Pong going on then....

How To SFML Dvd Logo Screensaver Tutorial using C++ - Basic Beginner

(note, the gif is at a lower fps so the movement isn't smooth)