From c9cfceec39c1a8c424330e822cbc4ac007528f40 Mon Sep 17 00:00:00 2001 From: gered Date: Mon, 1 Apr 2013 19:43:33 -0400 Subject: [PATCH] shift GraphicsDevice's complex initialization to a new Initialize() method --- src/framework/basegameapp.cpp | 3 +- src/framework/graphics/graphicsdevice.cpp | 73 ++++++++++++++++------- src/framework/graphics/graphicsdevice.h | 15 +++-- 3 files changed, 65 insertions(+), 26 deletions(-) diff --git a/src/framework/basegameapp.cpp b/src/framework/basegameapp.cpp index 33699b9..c63af79 100644 --- a/src/framework/basegameapp.cpp +++ b/src/framework/basegameapp.cpp @@ -92,8 +92,9 @@ BOOL BaseGameApp::Initialize(GameWindowParams *windowParams) return FALSE; } - m_graphics = new GraphicsDevice(m_window); + m_graphics = new GraphicsDevice(); ASSERT(m_graphics != NULL); + m_graphics->Initialize(m_window); m_content = new ContentManager(this); ASSERT(m_content != NULL); diff --git a/src/framework/graphics/graphicsdevice.cpp b/src/framework/graphics/graphicsdevice.cpp index a34801c..a9f72cb 100644 --- a/src/framework/graphics/graphicsdevice.cpp +++ b/src/framework/graphics/graphicsdevice.cpp @@ -42,22 +42,49 @@ const unsigned int MAX_BOUND_TEXTURES = 8; const unsigned int MAX_GPU_ATTRIB_SLOTS = 8; -GraphicsDevice::GraphicsDevice(GameWindow *window) +GraphicsDevice::GraphicsDevice() { STACK_TRACE; m_hasNewContextRunYet = FALSE; - m_boundVertexBuffer = NULL; m_boundIndexBuffer = NULL; m_boundShader = NULL; m_shaderVertexAttribsSet = FALSE; - - m_boundTextures = new const Texture*[MAX_BOUND_TEXTURES]; + m_boundTextures = NULL; m_boundFramebuffer = NULL; m_boundRenderbuffer = NULL; + m_activeViewContext = NULL; + m_defaultViewContext = NULL; + m_debugRenderer = NULL; + m_solidColorTextures = NULL; + m_simpleColorShader = NULL; + m_simpleColorTextureShader = NULL; + m_simpleTextureShader = NULL; + m_simpleTextureVertexLerpShader = NULL; + m_simpleTextureVertexSkinningShader = NULL; + m_sprite2dShader = NULL; + m_sprite3dShader = NULL; + m_debugShader = NULL; + m_isDepthTextureSupported = FALSE; + m_isNonPowerOfTwoTextureSupported = FALSE; + m_window = NULL; +} - m_enabledVertexAttribIndices.reserve(MAX_GPU_ATTRIB_SLOTS); +BOOL GraphicsDevice::Initialize(GameWindow *window) +{ + STACK_TRACE; + ASSERT(m_window == NULL); + if (m_window != NULL) + return FALSE; + ASSERT(window != NULL); + if (window == NULL) + return FALSE; + + m_hasNewContextRunYet = FALSE; + m_boundTextures = new const Texture*[MAX_BOUND_TEXTURES]; + m_enabledVertexAttribIndices.reserve(MAX_GPU_ATTRIB_SLOTS); + #ifdef MOBILE m_isDepthTextureSupported = IsGLExtensionPresent("OES_depth_texture"); m_isNonPowerOfTwoTextureSupported = IsGLExtensionPresent("OES_texture_npot"); @@ -70,31 +97,21 @@ GraphicsDevice::GraphicsDevice(GameWindow *window) LOG_INFO(LOGCAT_GRAPHICS, "Support for depth textures was %s.\n", m_isDepthTextureSupported ? "found" : "not found"); LOG_INFO(LOGCAT_GRAPHICS, "Support for NPOT textures was %s.\n", m_isNonPowerOfTwoTextureSupported ? "found" : "not found"); - + m_window = window; - m_activeViewContext = NULL; m_defaultViewContext = new ViewContext(); m_defaultViewContext->Create(this); m_activeViewContext = m_defaultViewContext; - + m_currentTextureParams = TEXPARAM_DEFAULT; - - m_debugRenderer = NULL; - + m_solidColorTextures = new SolidColorTextureCache(this); - - m_simpleColorShader = NULL; - m_simpleColorTextureShader = NULL; - m_simpleTextureShader = NULL; - m_simpleTextureVertexLerpShader = NULL; - m_simpleTextureVertexSkinningShader = NULL; - m_sprite2dShader = NULL; - m_sprite3dShader = NULL; - m_debugShader = NULL; + + return TRUE; } -GraphicsDevice::~GraphicsDevice() +void GraphicsDevice::Release() { STACK_TRACE; SAFE_DELETE(m_defaultViewContext); @@ -109,6 +126,20 @@ GraphicsDevice::~GraphicsDevice() SAFE_DELETE(m_sprite3dShader); SAFE_DELETE(m_debugShader); SAFE_DELETE_ARRAY(m_boundTextures); + m_enabledVertexAttribIndices.clear(); + m_managedResources.clear(); + + m_hasNewContextRunYet = FALSE; + m_boundVertexBuffer = NULL; + m_boundIndexBuffer = NULL; + m_boundShader = NULL; + m_shaderVertexAttribsSet = FALSE; + m_boundFramebuffer = NULL; + m_boundRenderbuffer = NULL; + m_activeViewContext = NULL; + m_isDepthTextureSupported = FALSE; + m_isNonPowerOfTwoTextureSupported = FALSE; + m_window = NULL; } SimpleColorShader* GraphicsDevice::GetSimpleColorShader() diff --git a/src/framework/graphics/graphicsdevice.h b/src/framework/graphics/graphicsdevice.h index f56430c..89e5d4e 100644 --- a/src/framework/graphics/graphicsdevice.h +++ b/src/framework/graphics/graphicsdevice.h @@ -43,14 +43,21 @@ typedef stl::vector EnabledVertexAttribList; class GraphicsDevice { public: + GraphicsDevice(); + virtual ~GraphicsDevice() { Release(); } + /** - * Creates a graphics device object based on a parent window that is + * Releases all resources associated with this object. + */ + void Release(); + + /** + * Initializes the graphics device object based on a parent window that is * hosting the OpenGL context. * @param window a window with an active OpenGL context associated with it + * @return TRUE if successful, FALSE if not */ - GraphicsDevice(GameWindow *window); - - virtual ~GraphicsDevice(); + BOOL Initialize(GameWindow *window); /** * New OpenGL graphics context creation callback.