diff --git a/GlitchyEngine/src/World/ComponentPool.bf b/GlitchyEngine/src/World/ComponentPool.bf index e8c4678..6d8bc1f 100644 --- a/GlitchyEngine/src/World/ComponentPool.bf +++ b/GlitchyEngine/src/World/ComponentPool.bf @@ -2,29 +2,27 @@ using System; namespace GlitchyEngine.World { - public class ComponentPool + public interface IComponentPool { - int _objectSize; - int _capacity; + public void* Get(int index); + } - int PoolSize => _objectSize * _capacity; + public class ComponentPool : IComponentPool + { + int PoolSize => sizeof(T) * _data.Count; + T[] _data ~ delete _; - uint8* _rawData ~ delete _; - - public this(int objectSize, int capacity) + public this(int capacity) { - _objectSize = objectSize; - _capacity = capacity; - - _rawData = new uint8[_capacity * _objectSize]*; + _data = new T[capacity]; } [Inline] public void* Get(int index) { - Log.EngineLogger.AssertDebug(index >= 0 && index < _capacity); + Log.EngineLogger.AssertDebug(index >= 0 && index < _data.Count); - return _rawData + index * _objectSize; + return &_data[index]; } } } diff --git a/GlitchyEngine/src/World/EcsWorld.bf b/GlitchyEngine/src/World/EcsWorld.bf index 270acc1..e2b42bb 100644 --- a/GlitchyEngine/src/World/EcsWorld.bf +++ b/GlitchyEngine/src/World/EcsWorld.bf @@ -17,7 +17,7 @@ namespace GlitchyEngine.World typealias DisposeFunction = function void(void* component); - internal typealias ComponentPoolEntry = (uint32 Id, ComponentPool Pool, DisposeFunction DisposeFunction); + internal typealias ComponentPoolEntry = (uint32 Id, IComponentPool Pool, DisposeFunction DisposeFunction); internal Dictionary _componentPools = new .(); /// A list containing all pools whose components need to be disposed before removal. @@ -47,7 +47,7 @@ namespace GlitchyEngine.World public void Register() where T: struct { uint32 id = (uint32)_componentPools.Count; - ComponentPool componentPool = new ComponentPool(sizeof(T), MaxEntities); + ComponentPool componentPool = new ComponentPool(MaxEntities); _componentPools.Add(typeof(T), (id, componentPool, null)); } @@ -58,7 +58,8 @@ namespace GlitchyEngine.World public void Register() where T: struct, IDisposableComponent { uint32 id = (uint32)_componentPools.Count; - ComponentPool componentPool = new ComponentPool(sizeof(T), MaxEntities); + ComponentPool componentPool = new ComponentPool(MaxEntities); + DisposeFunction disposeFunction = => T.DisposeComponent; _componentPools.Add(typeof(T), (id, componentPool, disposeFunction));