Resource manager in Adolfish

When I started Adolfish I choose C++ as my project language and the SDL as my 2D library. At first I thought that the SDL library had some sort of resource management, but I was wrong! For our first demo levels this was not a problem, but as long as we started making longer levels this became a nightmare. In a simple level we had several enemies, something like 20 or 30, and for each of them the engine had to waste time loading the data and memory loading its texture, so I came up with a simple solution. The first thing was to get the filename of the resource (the filename is used as is unique to each resource) and check whether it existed in a “resource” list, if not it gets loaded and its name added to the list, otherwise the function returned just a reference of the resource. After a few tries, it worked! But what happens if we release the memory of a dead enemy? The whole level crashed! So the solution was to check if there were other enemies using those resources and to do so I added a reference counter in the resource list. When the counter goes to 0, we can safely remove the resource from the memory. Here it is a pseudo-code of the resource loader:

and a pseudo-code to release the memory:

Leave a Reply