Coding a Simple SFML Game Framework in C++/ SFML


A while back, I made this video. And in
this video I stated the video a type I enjoy creating the most are the ones where I
show me creating a project and the type of videos I mean can be seen as an
example right here. And by far the most requested video type is where people are
telling me to create a game. So, yeah I’m gonna start doing that I guess, but each
game I’m gonna make this can have something in common and this thing is
called the game engine or game framework or whatever you wanna call it. And so rather
than creating this and having to re-explain it for every creating game figure i make.
I thought it would be better to do it just once in this video. It’s not exactly
gonna be the most featureful or the most fancy thing in the world, but it
will save a lot of time in the videos to come. So, anyways, let’s begin. To begin I
created probably the most important parts of any game engine or framework.
The main game loop and the game state system. The main game loop is what
actually controls the game it’s basically an infinite loop that only
ends when the game is closed. And this loop basically has three main functions:
handling player input, updating the game and rendering. But depending on what you
are doing these functions can mean different things and this is where the
game state system comes in. With a game state system I can nearly organize the
code of different stages of the game into their own classes. The different
game state classes inherit from the base game state class and implement the
virtual functions and then the main game loop simply causes functions in the
currently active game state over and over again polymorphically until the
game is closed. For example imagine a main menu it can be considered a game
state and in the update functions it will check the input on the buttons for
the update function it would change the background and for the render function
it will draw the buttons and draw the background. Another important part of
the main loop is the ability to have a fixed update which means a fixed time
between updating. The number of times the fixed update is called per second is
called the tick rate and for example in Minecraft this is 20 ticks per second. So,
anyways, that is now the game state system completed. The next important part
of games is a pseudo-random number generator. And it’s called pseudo-random
as it’s not actually possible for computers to create a truly random
number. Luckily the C++ standard library already
has random number functionality. But I just prefer to create abstractions
around these functions simply for readability seek. My abstractions for the
C++ 11 random number generator can be seen in action right here when I’m
generating 25 random numbers between 0 and 100. So, I guess, that’s part of the
utilities out of the way. The next thing that I needed to do but some kind of
resource management system. The idea behind this sort of thing is so that
resources were only ever be loaded just once. Where resources are things such as
textures, sounds and fonts. For example take this picture of some random mario
game. You can probably see there’s quite a lot of Goombas there. See the idea
behind the faceless merger is all these numbers can share the same texture
rather than having to load their own texture for each instance of a Goomba.
Not only does this save memory but also saves time, because basically loading
from files is actually a rather slow thing to do. Furthermore it also allows
me to have a fail resource just in case it twice the load of resource which
doesn’t actually exist. For example if a texture fails to load it will instead
load a pink texture. Here is an example of loading up the texture successfully
now let’s see what happens if I changed the name to something which is a file
which doesn’t actually exist. And when i load it now instead loads up the pink texture. So, anyways,
that’s the resource manager done. Enough a very important part of any game engine
or framework is an animation system. And well this is needed to make games look a
lot more alive and interesting. In SFML that is really easy to implement because
most durable objects have this function called set texture rect. And this function
basically allows you to specify a version of a texture to be used when a
sprite is drawn. For example take this megaman sprite sheet. If we want to make
you look like the player is running then you simply tell SFML the coordinates and
the size of the frame of the animation as the player runs. Now I’m not exactly a
great artist as you probably know already, so, I just sort of made a
bouncing ball animation do tested my animation system actually works. And
terror is my animation system me in action. As you can see I can also change
how long it is between each frame of the animation which allows me to either
speed up or slow it down. And so that’s the animation system of
the way and the last thing I need to do is some kind of GUI system. Now it is clear what is need
GUI system is probably my least favorite thing to do because I’m not really that
good. But you know… it sort of has to be done.
Anyways, to start off with I created the class called stack menu to represent
your classic game menu with the menu rigid in a sort of like column. For
example the Minecraft main menu. Now one of the reasons I really do not like
doing GUI systems, is because it’s awkward to sort of positionable
like for example the stack menu the uncrating I want all the buttons and
widgets or whatever to be center-aligned so they appeared to be in the mid of the
window. It’s not exactly a difficult thing to do, but I just find it kind of
awkward, but whatever. Now you probably heard me use the word widget. So,
basically a widget is a functional part of the menu for example a button or a
text box. The first widget type like created was a button which is simply a
sprite which cause a function when it’s clicked. So. I snap her a couple of test
buttons here button 1 and button 2. And as you can see when I click them is
printing something to the conole window. I then added a text box which of course is
a little box which allows you to enter text. And uh yeah that’s pretty much the
extent of my knowledge when it comes to graphical user interface implementations.
And world to be fair I did say I wasn’t very good at doing that sort of thing. So anyways,
it’s a very simple game framework thing that I’ll be using in my later creating
game videos. Obviously it’s nothing too fancy or to featureful, but it’s gonna
be extremely useful. I like to quickly say thank you to my super patreon
supporters. So Thank You, Stanley Morris Synthetic, Timothy Gibbons and I’ll
connect. So, anyways, once again thank you for watching and good bye.

Leave a Reply

Your email address will not be published. Required fields are marked *