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