From 96b02cfcca24d121a0f4d350793b9710682ed520 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Thu, 3 Jan 2019 14:01:11 +0100 Subject: [PATCH] Modifs in TransformComponents --- src/components/TransformComponents.cpp | 35 +++++++++++--------------- src/components/TransformComponents.h | 28 +++------------------ 2 files changed, 18 insertions(+), 45 deletions(-) diff --git a/src/components/TransformComponents.cpp b/src/components/TransformComponents.cpp index f478f9bf..0d20f4b3 100644 --- a/src/components/TransformComponents.cpp +++ b/src/components/TransformComponents.cpp @@ -35,8 +35,7 @@ using namespace reactphysics3d; // Constructor TransformComponents::TransformComponents(MemoryAllocator& allocator) - :mMemoryAllocator(allocator), mNbComponents(0), mNbAllocatedComponents(0), - mSleepingStartIndex(0), mBuffer(nullptr), mMapEntityToComponentIndex(allocator) { + :Components(allocator), mSleepingStartIndex(0){ // Allocate memory for the components data allocate(INIT_ALLOCATED_COMPONENTS); @@ -101,42 +100,36 @@ void TransformComponents::addComponent(Entity entity, bool isSleeping, const Tra allocate(mNbAllocatedComponents * 2); } + uint32 index; + // If the component to add is part of a sleeping entity or there are no sleeping entity if (isSleeping || mSleepingStartIndex == mNbComponents) { // Add the component at the end of the array - uint32 index = mNbComponents; - - // Map the entity with the new component lookup index - mMapEntityToComponentIndex.add(Pair(entity, index)); + index = mNbComponents; if (isSleeping) { mSleepingStartIndex = index; } - - // Insert the new component data - new (mEntities + index) Entity(entity); - new (mTransforms + index) Transform(component.transform); } // If the component to add is not part of a sleeping entity and there are others sleeping components else { - // Copy the first sleeping component to the end of the array - new (mEntities + mNbComponents) Entity(mEntities[mSleepingStartIndex]); - new (mTransforms + mNbComponents) Transform(mTransforms[mSleepingStartIndex]); + // Move the first sleeping component to the end of the array + moveComponentToIndex(mSleepingStartIndex, mNbComponents); - mMapEntityToComponentIndex[mEntities[mSleepingStartIndex]] = mNbComponents; - - // Copy the new component to the previous location of the fist sleeping component - mEntities[mSleepingStartIndex] = entity; - mTransforms[mSleepingStartIndex] = component.transform; - - // Map the entity with the new component lookup index - mMapEntityToComponentIndex.add(Pair(entity, mSleepingStartIndex)); + index = mSleepingStartIndex; mSleepingStartIndex++; } + // Insert the new component data + new (mEntities + index) Entity(entity); + new (mTransforms + index) Transform(component.transform); + + // Map the entity with the new component lookup index + mMapEntityToComponentIndex.add(Pair(entity, index)); + mNbComponents++; assert(mSleepingStartIndex <= mNbComponents); diff --git a/src/components/TransformComponents.h b/src/components/TransformComponents.h index 3f7ec1a2..5df79c65 100644 --- a/src/components/TransformComponents.h +++ b/src/components/TransformComponents.h @@ -29,6 +29,7 @@ // Libraries #include "mathematics/Transform.h" #include "engine/Entity.h" +#include "components/Components.h" #include "containers/Map.h" // ReactPhysics3D namespace @@ -44,40 +45,19 @@ class EntityManager; * different entities. The position and orientation of the bodies are stored there. * The components of the sleeping entities (bodies) are always stored at the end of the array. */ -class TransformComponents { +class TransformComponents : public Components { private: // -------------------- Constants -------------------- // - /// Number of components to allocated at the beginning - const uint32 INIT_ALLOCATED_COMPONENTS = 10; - - /// Number of valid entities to hit before stopping garbage collection - const uint32 GARBAGE_COLLECTION_MAX_VALID_ENTITIES = 5; - const size_t COMPONENT_DATA_SIZE = sizeof(Entity) + sizeof(Transform); // -------------------- Attributes -------------------- // - /// Memory allocator - MemoryAllocator& mMemoryAllocator; - - /// Current number of components - uint32 mNbComponents; - - /// Number of allocated components - uint32 mNbAllocatedComponents; - /// Index of the first component of a sleeping entity (sleeping components are stored at the end) uint32 mSleepingStartIndex; - /// Allocated memory for all the data of the components - void* mBuffer; - - /// Map an entity to the index of its component in the array - Map mMapEntityToComponentIndex; - /// Array of entities of each component Entity* mEntities; @@ -92,7 +72,7 @@ class TransformComponents { /// Destroy a component at a given index void destroyComponent(uint32 index); - // Move a component from a source to a destination index in the components array + /// Move a component from a source to a destination index in the components array void moveComponentToIndex(uint32 srcIndex, uint32 destIndex); /// Swap two components in the array @@ -117,7 +97,7 @@ class TransformComponents { TransformComponents(MemoryAllocator& allocator); /// Destructor - ~TransformComponents(); + virtual ~TransformComponents(); /// Allocate memory for a given number of components void allocate(uint32 nbComponentsToAllocate);