Skip to content

Commit

Permalink
Freeing memory for unusued graphics components (fixes #5).
Browse files Browse the repository at this point in the history
  • Loading branch information
jpike committed Jul 26, 2014
1 parent 8d03147 commit 0196c14
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
34 changes: 30 additions & 4 deletions noah_ark/library/noah_ark_library/src/Graphics/GraphicsSystem.cpp
@@ -1,3 +1,4 @@
#include <algorithm>
#include "Graphics/GraphicsSystem.h"

using namespace GRAPHICS;
Expand Down Expand Up @@ -49,9 +50,15 @@ void GraphicsSystem::Render()
NORMAL_VERTICAL_SCALE);

// RENDER ALL OF THE VISIBLE GRAPHICS COMPONENTS.
for (std::shared_ptr<IGraphicsComponent>& graphicsComponent : m_graphicsComponents)
for (std::weak_ptr<IGraphicsComponent>& graphicsComponentWeakReference : m_graphicsComponents)
{
RenderIfVisible(graphicsComponent);
// Check if the graphics component is still in use.
if (!graphicsComponentWeakReference.expired())
{
// Render the current graphics component.
std::shared_ptr<IGraphicsComponent> graphicsComponent = graphicsComponentWeakReference.lock();
RenderIfVisible(graphicsComponent);
}
}

// RESET THE CAMERA'S VIEW.
Expand All @@ -60,10 +67,22 @@ void GraphicsSystem::Render()

void GraphicsSystem::Update(const float elapsedTimeInSeconds)
{
for (std::shared_ptr<IGraphicsComponent>& graphicsComponent : m_graphicsComponents)
// UPDATE ALL GRAPHICS COMPONENTS.
for (std::weak_ptr<IGraphicsComponent>& graphicsComponentWeakReference : m_graphicsComponents)
{
graphicsComponent->Update(elapsedTimeInSeconds);
// Check if the graphics component is still in use.
if (!graphicsComponentWeakReference.expired())
{
// Update the current graphics component.
std::shared_ptr<IGraphicsComponent> graphicsComponent = graphicsComponentWeakReference.lock();
graphicsComponent->Update(elapsedTimeInSeconds);
}
}

// REMOVE ANY GRAPHICS COMPONENTS THAT ARE NO LONGER NEEDED.
// This is done here, rather than in the render call, because more overall time
// is spent in the render call than the update call, so this helps balance things out.
RemoveUnusedGraphicsComponents();
}

void GraphicsSystem::SetCamera(const Camera& camera)
Expand Down Expand Up @@ -149,4 +168,11 @@ void GraphicsSystem::RenderIfVisible(std::shared_ptr<IGraphicsComponent>& graphi
{
graphicsComponent->Render();
}
}

void GraphicsSystem::RemoveUnusedGraphicsComponents()
{
// Remove any graphics components that are no longer being used.
m_graphicsComponents.remove_if(
[](std::weak_ptr<IGraphicsComponent>& graphicsComponent) { return graphicsComponent.expired(); });
}
12 changes: 11 additions & 1 deletion noah_ark/library/noah_ark_library/src/Graphics/GraphicsSystem.h
Expand Up @@ -95,9 +95,19 @@ namespace GRAPHICS
/// @param[in] graphicsComponent - The graphics component to render.
void RenderIfVisible(std::shared_ptr<IGraphicsComponent>& graphicsComponent);

/// @brief Removes unusued graphics components from the system. Allows freeing
/// memory for graphics components that are no longer being used elsewhere
/// in the game.
void RemoveUnusedGraphicsComponents();

HGE* m_pGameEngine; ///< The underlying HGE game engine.
Camera m_camera; ///< The camera indicating what portion of the world is currently in view.
std::shared_ptr<hgeResourceManager> m_resourceManager; ///< The resource manager used to access graphics resources.
std::list< std::shared_ptr<IGraphicsComponent> > m_graphicsComponents; ///< The graphics components to be rendered.

/// @brief The graphics components to be rendered. The methods for creating
/// these components return shared_ptrs, but they are stored as weak_ptrs
/// to allow their memory to be properly released once the objects holding
/// the shared_ptrs are deleted.
std::list< std::weak_ptr<IGraphicsComponent> > m_graphicsComponents;
};
}

0 comments on commit 0196c14

Please sign in to comment.