So lets get into the meat of it. My resource manager consists of two classes, Resource and ResourceManager. The names pretty much give away their purposes. Resource represents a single asset that has been loaded into memory. It holds the asstet's raw data along with a filename and the size of the asset.
ResourceManager does most of the heavy lifting. It takes in a zip file or a single file and creates Resources from them. The pointers to the Resources are stored in two std lib containers, a map and a list. Each container has its dedicated purpose. The map is used for random access of the Resources using the Resource's filename as the key. The list is used for looping through the resources. Right now the list is only being used in the destructor of the ResourceManager when it releases all of the resources. If I ever wanted to convert this to a ResourceCache, the list would play a much greater role by becoming a sorted list of assets ordered by last use.
The major flaw I have in my design right now is I store and handout regular pointers to a resource. This becomes a problem when I hand out the same resource more than once and one of the recipients releases the resource. All other pointers to that resource become garbage because the memory that the resource pointer pointed to has been released. This leads me to my next task, adding boost to my solution.In Game Coding Complete they use boost's shared pointers extensively to solve this exact issue. The shared pointers will allow me to hand out multiple copies of the same resource without worrying about resources being released prematurely because the shared pointer keeps track of references to itself and won't release memory until all resources have been released. I've read a little on using boost and I've heard that it isn't the easiest thing to add to a solution, so wish me luck.
No comments:
Post a Comment