Original Post
So I'm revamping my game engine (again) and I've added support for runtime modules. These are essentially plugins that implement different subsystems such as video, audio, input, etc. With this new design I've run into new problems and wanted to ask for advice on how to solve them. For one thing, I've run into issues with properly shutting down libraries that are shared amongst modules. For example, I'm implementing modules using OpenGL and SDL libraries. No one module can say, call SDL_Quit(), because it would invalidate all of the other SDL based modules. I've come up with two solutions for this. The first, is to simply not allow active modules to be shut down. When the kernel is destroyed, it will free all modules at once. However, this is a dirty solution, would result in multiple calls to shutdown code, and means that once a module is loaded and running, it simply can't be shutdown, replaced, removed, etc. What I've implemented at this point is a special kind of module: a reference counter module. Each reference counter module is highly atomic, and does one job. It shuts down a single library. In the case of my OpenGL and SDL based modules, all I needed was an SDL reference counter module. Other modules query the kernel for the proper shutdown module, and grab or drop that module to influence a reference count. Naturally, when the count hits zero, that library is completely shut down. I'm not sure this is the best solution. A variant I've thought of is to allow certain modules to "post" a reference counter with a particular ID or name, and modules that see it posted will grab and drop that. This avoids packing the reference counting into flow blown modules (not that coding a module is difficult). Is there a better way to solve this problem? Another question I have doesn't have much to do with modules. I'm unsure if a central resource manager is necessary or not. At the moment, modules are in charge of the resources they allocate (images, textures, joysticks, etc.). These are accessible through a proxy object that talks to the module. Modules provide very basic resource management. Would it be worth my while to try to create a central resource manager that at least knows about the existence of all allocated resources? In the last revision of my engine, I didn't have this, and it didn't seem to be a problem, but many articles I've seen (even here) about basic engine design allude to or describe some kind of central resource manager. Should my kernel be doing this? Any advice is appreciated. Thanks! :-)