ne555 hinted to me that I don't load the sprites for alot of elements. So do I really Need to load the sprite for every bullet that is created? I thought I'll just load it once and then render that Image multiple times at the same time...
|
You want to only load textures into memory a single time since grabbing a texture from your hard drive and loading it is a very slow operation. This is where a resource manager comes in, but before we go into that I feel I should explain the difference between a Sprite and a Texture and other assets.
A texture is a heavyweight object (Meaning it is a large object which will take time to load into memory). This is basically the image you will be using for your bullet (To keep things simple I won't go into spritesheets). You only want a single instance of this image loaded into memory when the image is needed.
Now a Sprite is a lightweight object that is meant to represent every single 2D entity in your game. The sprite will hold pointers to all the assets that make up that entity. It does not actually contain the texture itself and has no part in the actual loading of the texture or other assets because that would make it a heavyweight object with bad performance.
So lets say we want to create 10 bullets on the screen. Are we going to want to load up and store the bullet texture in memory 10 times? No we definitely do not want to do that, instead we only want to load the bullet texture into memory 1 time and then have all of those 10 bullet sprites on screen contain a pointer to that 1 bullet texture to use when it is drawn.
Hopefully that makes sense.
Now for the resource manager part. The sole purpose of a resource manager is to make sure a certain type of asset (Textures, Audio Clips, Shaders, ect) are only loaded into memory a single time and then to provide access (Usually by returning a pointer to use) to those assets whenever a Sprite or other entity requires it.
So for example here is a simple resource manager in SFML
https://github.com/bjumbeck/AsteroidsClone/blob/master/Source/ResourceManagement/ResourceManager.hpp . Each instance of that class acts as a container to store a single type of asset (Though the class itself is generic and can handle all asset types supported by SFML). The load method basically assures that the asset hasn't been loaded into memory already and then loads and stores that asset away.
The get method then is able to retrieve any asset that has been loaded already. For example lets say your sprites constructor takes a single parameter which is a pointer to the texture which you want that sprite to use (Which is quite common). With this resource manager you would just do something simple like
Sprite bullet(textureManager.get("bullet.png"));
and that is it.
I didn't do anything fancy with this resource manager but you could make it so that the get() method first checks to see if the asset is loaded already and if not loads it and returns a pointer but that is up to you.
Here is some other things regards the resource manager that might help also.
I used enums as IDs for the different assets and also at the bottom you can see common typedefs for the resource managers -
https://github.com/bjumbeck/AsteroidsClone/blob/master/Source/ResourceManagement/ResourceIDs.hpp
Here you can see how you can use the resource manager to preload assets into memory -
https://github.com/bjumbeck/Space-Shooter/blob/master/Application.cpp#L26
Hopefully this helps a little bit explaining about why you don't want a bunch of the same texture loaded into memory and how resource manager look. Even though the source code is in SFML it shouldn't be to hard to transfer it over to SDL.