Modifs in TransformComponents
This commit is contained in:
parent
3d892a6689
commit
96b02cfcca
|
@ -35,8 +35,7 @@ using namespace reactphysics3d;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
TransformComponents::TransformComponents(MemoryAllocator& allocator)
|
TransformComponents::TransformComponents(MemoryAllocator& allocator)
|
||||||
:mMemoryAllocator(allocator), mNbComponents(0), mNbAllocatedComponents(0),
|
:Components(allocator), mSleepingStartIndex(0){
|
||||||
mSleepingStartIndex(0), mBuffer(nullptr), mMapEntityToComponentIndex(allocator) {
|
|
||||||
|
|
||||||
// Allocate memory for the components data
|
// Allocate memory for the components data
|
||||||
allocate(INIT_ALLOCATED_COMPONENTS);
|
allocate(INIT_ALLOCATED_COMPONENTS);
|
||||||
|
@ -101,42 +100,36 @@ void TransformComponents::addComponent(Entity entity, bool isSleeping, const Tra
|
||||||
allocate(mNbAllocatedComponents * 2);
|
allocate(mNbAllocatedComponents * 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32 index;
|
||||||
|
|
||||||
// If the component to add is part of a sleeping entity or there are no sleeping entity
|
// If the component to add is part of a sleeping entity or there are no sleeping entity
|
||||||
if (isSleeping || mSleepingStartIndex == mNbComponents) {
|
if (isSleeping || mSleepingStartIndex == mNbComponents) {
|
||||||
|
|
||||||
// Add the component at the end of the array
|
// Add the component at the end of the array
|
||||||
uint32 index = mNbComponents;
|
index = mNbComponents;
|
||||||
|
|
||||||
// Map the entity with the new component lookup index
|
|
||||||
mMapEntityToComponentIndex.add(Pair<Entity, uint32>(entity, index));
|
|
||||||
|
|
||||||
if (isSleeping) {
|
if (isSleeping) {
|
||||||
mSleepingStartIndex = index;
|
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
|
// If the component to add is not part of a sleeping entity and there are others sleeping components
|
||||||
else {
|
else {
|
||||||
|
|
||||||
// Copy the first sleeping component to the end of the array
|
// Move the first sleeping component to the end of the array
|
||||||
new (mEntities + mNbComponents) Entity(mEntities[mSleepingStartIndex]);
|
moveComponentToIndex(mSleepingStartIndex, mNbComponents);
|
||||||
new (mTransforms + mNbComponents) Transform(mTransforms[mSleepingStartIndex]);
|
|
||||||
|
|
||||||
mMapEntityToComponentIndex[mEntities[mSleepingStartIndex]] = mNbComponents;
|
index = mSleepingStartIndex;
|
||||||
|
|
||||||
// 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));
|
|
||||||
|
|
||||||
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++;
|
mNbComponents++;
|
||||||
|
|
||||||
assert(mSleepingStartIndex <= mNbComponents);
|
assert(mSleepingStartIndex <= mNbComponents);
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "mathematics/Transform.h"
|
#include "mathematics/Transform.h"
|
||||||
#include "engine/Entity.h"
|
#include "engine/Entity.h"
|
||||||
|
#include "components/Components.h"
|
||||||
#include "containers/Map.h"
|
#include "containers/Map.h"
|
||||||
|
|
||||||
// ReactPhysics3D namespace
|
// ReactPhysics3D namespace
|
||||||
|
@ -44,40 +45,19 @@ class EntityManager;
|
||||||
* different entities. The position and orientation of the bodies are stored there.
|
* 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.
|
* The components of the sleeping entities (bodies) are always stored at the end of the array.
|
||||||
*/
|
*/
|
||||||
class TransformComponents {
|
class TransformComponents : public Components {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
// -------------------- Constants -------------------- //
|
// -------------------- 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);
|
const size_t COMPONENT_DATA_SIZE = sizeof(Entity) + sizeof(Transform);
|
||||||
|
|
||||||
// -------------------- Attributes -------------------- //
|
// -------------------- 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)
|
/// Index of the first component of a sleeping entity (sleeping components are stored at the end)
|
||||||
uint32 mSleepingStartIndex;
|
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
|
/// Array of entities of each component
|
||||||
Entity* mEntities;
|
Entity* mEntities;
|
||||||
|
|
||||||
|
@ -92,7 +72,7 @@ class TransformComponents {
|
||||||
/// Destroy a component at a given index
|
/// Destroy a component at a given index
|
||||||
void destroyComponent(uint32 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);
|
void moveComponentToIndex(uint32 srcIndex, uint32 destIndex);
|
||||||
|
|
||||||
/// Swap two components in the array
|
/// Swap two components in the array
|
||||||
|
@ -117,7 +97,7 @@ class TransformComponents {
|
||||||
TransformComponents(MemoryAllocator& allocator);
|
TransformComponents(MemoryAllocator& allocator);
|
||||||
|
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~TransformComponents();
|
virtual ~TransformComponents();
|
||||||
|
|
||||||
/// Allocate memory for a given number of components
|
/// Allocate memory for a given number of components
|
||||||
void allocate(uint32 nbComponentsToAllocate);
|
void allocate(uint32 nbComponentsToAllocate);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user