Game Development Reference
In-Depth Information
Managing resources
Whether we are making a game or a multimedia application, it is important to manage as-
sets correctly and efficiently. Making sure that assets do not get accidentally destroyed, or
that we do not load the same texture twice is important to maintain a solid and efficient
code base. In this section, we will talk about building the groundwork for a resource man-
ager, which you can use in your applications.
Let's start with a simple fact—objects on the stack are destroyed when they get out of
scope. In some languages, all classes are allocated on the heap (where memory is managed
automatically by garbage collection) and this is not a problem. However, in C++, if we
want to keep objects on the stack, which makes memory much easier to manage, we have
to make sure that they stay alive as long as they are used. This is an example where a tex-
ture will get destroyed as soon as the function exits:
If we draw the sprite, we will see that only a white rectangle is drawn and the texture is
nowhere to be seen.
To manage assets' lifecycle correctly throughout the runtime of the program, it is very use-
ful to have a dedicated manager. For this very reason, let's create a class AssetManager ,
which will load, hold, and destroy all assets in our program. Here is how the header file
looks like:
Search WWH ::




Custom Search