I have a game engine design written in C++ where a platform-independent game object is contained within a platform-specific Application object.
The problem I’m trying to solve is the case where I need to pass OS-specific data from the Application to the game. In this case, I’d need to pass the main HWND from Windows for DirectX or an OpenGL context for the other platforms to the renderer I’m using. Unfortunately I have little control over the renderer, which can expect platform-specific data.
I realize I could initialize the renderer on the Application side, but I’d rather have the game decide when and where to do it. Generally, I have control over the Application side but not the game side. The game writer might choose to use a different renderer.
I’ve also entertained the idea of having some kind of “Property Manager” where I can pass data around through strings, but I don’t like that idea very much.
Any ideas?
Remember that you only need to know the target platform at compile time. With this information, you can ‘swap in and out’ components for the correct platform.
In a good design, the Game should not require any information about it’s platform; it should only hold the logic and related components.
Your ‘Engine’ classes should worry about the platform.
The Game classes should only interface with the Engine objects via public functions that aren’t specific to the platform; you can have multiple versions of the Engine objects for each platform, and choose which one to use at compile time.
For example, you could have a Texture ‘engine’ class that represents a texture in the game. If you support OS X and Windows, you could have a “Texture.h” which includes “Windows/Texture.h” or “OSX/Texture.h” depending on the platform you’re compiling on. Both headers will define a Texture class with the same interface (i.e. they’ll both have the same public functions with the same arguments), but their implementation will be platform-specific.
To clarify, the Game should tell the Application to initialize the Renderer; there should be a strict line between the game logic and the implementation details. The renderer is an implementation detail, not part of the game logic. The game classes should know nothing about the system and only about the game world.