Modifs in TransformComponents

This commit is contained in:
Daniel Chappuis 2019-01-03 14:01:11 +01:00
parent 3d892a6689
commit 96b02cfcca
2 changed files with 18 additions and 45 deletions

View File

@ -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, uint32>(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, uint32>(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, uint32>(entity, index));
mNbComponents++;
assert(mSleepingStartIndex <= mNbComponents);

View File

@ -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<Entity, uint32> 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);