Simplified dispose function of DisposableComponents

This commit is contained in:
Simon Lübeß
2021-12-30 16:52:13 +01:00
parent b3ab30e72a
commit b555cc37d1
7 changed files with 26 additions and 29 deletions
+2 -4
View File
@@ -19,11 +19,9 @@ namespace GlitchyEngine.Renderer
} }
} }
public static void DisposeComponent(void* component) public void Dispose() mut
{ {
Self* self = (Self*)component; ReleaseRefAndNullify!(_mesh);
ReleaseRefAndNullify!(self._mesh);
} }
} }
} }
@@ -28,12 +28,10 @@ namespace GlitchyEngine.World
public SkeletonPose Pose => _pose; public SkeletonPose Pose => _pose;
public static void DisposeComponent(void* component) public void Dispose()
{ {
Self* animationComponent = (Self*)component; _animationClip?.ReleaseRef();
delete _pose;
animationComponent._animationClip?.ReleaseRef();
delete animationComponent._pose;
} }
} }
} }
@@ -18,11 +18,9 @@ namespace GlitchyEngine.World
_debugName = new String(name); _debugName = new String(name);
} }
public static void DisposeComponent(void* component) public void Dispose() mut
{ {
Self* self = (Self*)component; DeleteAndNullify!(_debugName);
DeleteAndNullify!(self._debugName);
} }
} }
} }
+13 -7
View File
@@ -17,7 +17,7 @@ namespace GlitchyEngine.World
typealias DisposeFunction = function void(void* component); typealias DisposeFunction = function void(void* component);
internal typealias ComponentPoolEntry = (uint32 Id, IComponentPool Pool, DisposeFunction DisposeFunction); internal typealias ComponentPoolEntry = (uint32 Id, IComponentPool Pool, DisposeFunction DisposeComponent);
internal Dictionary<Type, ComponentPoolEntry> _componentPools = new .(); internal Dictionary<Type, ComponentPoolEntry> _componentPools = new .();
/// A list containing all pools whose components need to be disposed before removal. /// A list containing all pools whose components need to be disposed before removal.
@@ -60,7 +60,7 @@ namespace GlitchyEngine.World
uint32 id = (uint32)_componentPools.Count; uint32 id = (uint32)_componentPools.Count;
ComponentPool<T> componentPool = new ComponentPool<T>(MaxEntities); ComponentPool<T> componentPool = new ComponentPool<T>(MaxEntities);
DisposeFunction disposeFunction = => T.DisposeComponent; DisposeFunction disposeFunction = => DisposeComponent<T>;
_componentPools.Add(typeof(T), (id, componentPool, disposeFunction)); _componentPools.Add(typeof(T), (id, componentPool, disposeFunction));
@@ -68,6 +68,13 @@ namespace GlitchyEngine.World
_disposingPools.Add(typeof(T)); _disposingPools.Add(typeof(T));
} }
/// Enables us to bind a function pointer to the components dispose function
private static void DisposeComponent<T>(void* component) where T : IDisposableComponent
{
T* myComponent = (T*)component;
(*myComponent).Dispose();
}
/** @brief Returns the component pool for the given component type. /** @brief Returns the component pool for the given component type.
* @param TComponent The type of the component whose component pool will be returned. * @param TComponent The type of the component whose component pool will be returned.
* @returns A reference to the component pool. * @returns A reference to the component pool.
@@ -137,7 +144,7 @@ namespace GlitchyEngine.World
// Get pointer to component // Get pointer to component
void* component = pool.Pool.Get(listEntity.ID.Index); void* component = pool.Pool.Get(listEntity.ID.Index);
// Dispose component // Dispose component
pool.DisposeFunction(component); pool.DisposeComponent(component);
} }
} }
} }
@@ -218,7 +225,7 @@ namespace GlitchyEngine.World
listEntity.ComponentMask[entry.Id] = false; listEntity.ComponentMask[entry.Id] = false;
// Get component and call dispose // Get component and call dispose
T.DisposeComponent(entry.Pool.Get(entity.Index)); DisposeComponent<T>(entry.Pool.Get(entity.Index));
} }
public T* GetComponent<T>(Entity entity) where T : struct public T* GetComponent<T>(Entity entity) where T : struct
@@ -307,10 +314,9 @@ namespace GlitchyEngine.World
{ {
public bool IsDisposed; public bool IsDisposed;
public static void DisposeComponent(void* component) public void Dispose() mut
{ {
Self* testComponent = (Self*)component; IsDisposed = true;
testComponent.IsDisposed = true;
} }
} }
@@ -7,6 +7,6 @@ namespace GlitchyEngine.World
* @Note This function will be called when the given component was removed from the entity * @Note This function will be called when the given component was removed from the entity
* or when the entity itself was removed. * or when the entity itself was removed.
*/ */
static void DisposeComponent(void* component); void Dispose() mut;
} }
} }
@@ -23,10 +23,9 @@ namespace GlitchyEngine.World
} }
} }
public static void DisposeComponent(void* component) public void Dispose() mut
{ {
Self* meshRenderComponent = (Self*)component; _material?.ReleaseRef();
meshRenderComponent._material?.ReleaseRef();
} }
} }
} }
@@ -40,12 +40,10 @@ namespace GlitchyEngine.World
} }
} }
public static void DisposeComponent(void* component) public void Dispose()
{ {
Self* meshRenderComponent = (Self*)component; _material?.ReleaseRef();
_skeleton?.ReleaseRef();
meshRenderComponent._material?.ReleaseRef();
meshRenderComponent._skeleton?.ReleaseRef();
} }
} }
} }