I'm writing a game framework and I'm trying to generalize and encapsulate platform dependent code like the renderer so that it makes it a bit easier to port. I'm trying to accomplish this while still having a clean way of using the framework. I'm currently having a problem with static variables and namespaces...
// Renderer.h
namespace Renderer
{
static IRenderer* g_pRenderer = NULL;
static IRenderer* Get(void) { return g_pRenderer; }
static IRenderer* CreateD3DRenderer()
{
g_pRenderer = new RendererD3D(); // Derived from IRenderer
return g_pRenderer;
}
}
So in my main(), I can call CreateD3DRenderer() and it returns an instance just fine; g_pRenderer is holding its value since it's being created and returned within the scope of its function... However, Renderer::Get() returns NULL. Removing 'static' from the init of g_pRenderer causes clashes when used in other files.
What's going on?