The Sprite Manager Remade.
A Sprite Manager is usually a good idea to make for your game. An even better idea is to make it do as much as possible when being called on so you can focus on other things. What do I mean by that? Not that a Sprite Manager should do everything, but it’s always good to keep down the number of code lines needed to use it.
I write this, because I want to describe how the new Sprite Manager I made works as well as explain why I considered it well worth spending the time required to remake it. That is generally something you should avoid.
So what was so bad about the old Sprite Manager? Well, if you wanted to make a new instance of an object (or a new pointer) you needed about five lines of code to load a texture from a file and set the texture to a sprite. You also needed 8 arguments.
The picture above illustrates how much code was needed to load the background image with the old Sprite Manager. The code line in the yellow box was not really the Sprite Managers fault though. However, the code in the red boxes (as well as the code line in the yellow box) was replaced with one line of code taking 3 arguments with the new Sprite Manager. See below.
The new Sprite Managers also checks if the file with the texture has already been used, and if so, reuses the data from it rather than loading it a second time. Below is a picture of the Sprite Manager class’ header file.
So how does the new Sprite Manager work? Well first it is initialized, meaning you load the directory to where your sprites are store into the manager object.
Here’s an example of the initialize method being used:
This means that the manager will try to load sprites from a folder called “Sprites” that is placed in the same directory as the bin folder that contains the executable file.
Looking back at the second picture we see that a method called “GetSprite” is called. Here is how it works:
The method takes 3 arguments: the name of the file the texture is to be loaded from as well as the values of the files dimensions, or in other words the pictures width and height.
[Yellow Box] The method starts by checking through a <map> container that stores pointers to previously loaded sprites to see if there is an element in it with the same name, which means it indirectly is checking if the specified file has already been used.
[Orange Box] If no element with the specified name is found in the <map> container, “GetSprite” will call on another method called “LoadSprite” that’ll take in the same arguments in order to load a sprite from the specified file and add it to the <map> container.
[Red Box] Lastly “GetSprite” will return the sprite pointer from the <map> container that has the right name.
Now we’ll take a look at the “LoadSprite” method:
[White Box] It starts by making a new sprite pointer.
[Yellow Box] Then it goes through a <map> container with stored pointers to previously loaded textures.
[Orange Box] If it can’t find the texture in question then it will use the “LoadTexture” method to load a new texture from the file and store a pointer to it in the <map> container with texture pointers.
[Red Box] The texture will then be applied to the new sprite pointer, which in turn is stored in the <map> container for sprite pointers.
We’ve almost covered everything; lastly we’ll take a look at the “LoadTexture” function:
[Yellow Box] First off, the method creates a new texture pointer.
[Orange Box] Then it uses SFML’s method for loading textures from files. Note that the filepath variable defined in the initialize method is added to the filename to create the full path to the file specified. For example, if the file was named “block.png” the full path in this case would be “../Sprites/block.png”. It also makes a sub-rectangle defining which part of the picture we want to load as a texture. Since we always want the whole picture the first two values will be 0, 0 and the last two the width and height of the picture. This means we start at point 0, 0 (the upper left corner) and stretch it the whole width horizontally and the whole height vertically.
[Red Box] The texture pointer is then saved in the <map> container for texture pointers, ready to be used.
That is all; I hope you enjoyed reading this and that you learned something from it.
Cheers!