320 lines
12 KiB
C++
320 lines
12 KiB
C++
/********************************************************************************
|
|
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
|
* Copyright (c) 2010-2018 Daniel Chappuis *
|
|
*********************************************************************************
|
|
* *
|
|
* This software is provided 'as-is', without any express or implied warranty. *
|
|
* In no event will the authors be held liable for any damages arising from the *
|
|
* use of this software. *
|
|
* *
|
|
* Permission is granted to anyone to use this software for any purpose, *
|
|
* including commercial applications, and to alter it and redistribute it *
|
|
* freely, subject to the following restrictions: *
|
|
* *
|
|
* 1. The origin of this software must not be misrepresented; you must not claim *
|
|
* that you wrote the original software. If you use this software in a *
|
|
* product, an acknowledgment in the product documentation would be *
|
|
* appreciated but is not required. *
|
|
* *
|
|
* 2. Altered source versions must be plainly marked as such, and must not be *
|
|
* misrepresented as being the original software. *
|
|
* *
|
|
* 3. This notice may not be removed or altered from any source distribution. *
|
|
* *
|
|
********************************************************************************/
|
|
|
|
// Libraries
|
|
#include "TransformComponents.h"
|
|
#include "engine/EntityManager.h"
|
|
#include <cassert>
|
|
#include <random>
|
|
|
|
// We want to use the ReactPhysics3D namespace
|
|
using namespace reactphysics3d;
|
|
|
|
|
|
// Constructor
|
|
TransformComponents::TransformComponents(MemoryAllocator& allocator)
|
|
:mMemoryAllocator(allocator), mNbComponents(0), mNbAllocatedComponents(0),
|
|
mSleepingStartIndex(0), mBuffer(nullptr), mMapEntityToComponentIndex(allocator) {
|
|
|
|
// Allocate memory for the components data
|
|
allocate(INIT_ALLOCATED_COMPONENTS);
|
|
}
|
|
|
|
// Destructor
|
|
TransformComponents::~TransformComponents() {
|
|
|
|
if (mNbAllocatedComponents > 0) {
|
|
|
|
// Destroy all the remaining components
|
|
for (uint32 i = 0; i < mNbComponents; i++) {
|
|
destroyComponent(i);
|
|
}
|
|
|
|
// Size for the data of a single component (in bytes)
|
|
const size_t totalSizeBytes = mNbAllocatedComponents * COMPONENT_DATA_SIZE;
|
|
|
|
// Release the allocated memory
|
|
mMemoryAllocator.release(mBuffer, totalSizeBytes);
|
|
}
|
|
}
|
|
|
|
// Allocate memory for a given number of components
|
|
void TransformComponents::allocate(uint32 nbComponentsToAllocate) {
|
|
|
|
assert(nbComponentsToAllocate > mNbAllocatedComponents);
|
|
|
|
// Size for the data of a single component (in bytes)
|
|
const size_t totalSizeBytes = nbComponentsToAllocate * COMPONENT_DATA_SIZE;
|
|
|
|
// Allocate memory
|
|
void* newBuffer = mMemoryAllocator.allocate(totalSizeBytes);
|
|
assert(newBuffer != nullptr);
|
|
|
|
// New pointers to components data
|
|
Entity* newEntities = static_cast<Entity*>(newBuffer);
|
|
Transform* newTransforms = reinterpret_cast<Transform*>(newEntities + nbComponentsToAllocate);
|
|
|
|
// If there was already components before
|
|
if (mNbComponents > 0) {
|
|
|
|
// Copy component data from the previous buffer to the new one
|
|
memcpy(newTransforms, mTransforms, mNbComponents * sizeof(Transform));
|
|
memcpy(newEntities, mEntities, mNbComponents * sizeof(Entity));
|
|
|
|
// Deallocate previous memory
|
|
mMemoryAllocator.release(mBuffer, mNbAllocatedComponents * COMPONENT_DATA_SIZE);
|
|
}
|
|
|
|
mBuffer = newBuffer;
|
|
mEntities = newEntities;
|
|
mTransforms = newTransforms;
|
|
mNbAllocatedComponents = nbComponentsToAllocate;
|
|
}
|
|
|
|
// Add a component
|
|
void TransformComponents::addComponent(Entity entity, bool isSleeping, const TransformComponent& component) {
|
|
|
|
// If we need to allocate more components
|
|
if (mNbComponents == mNbAllocatedComponents) {
|
|
allocate(mNbAllocatedComponents * 2);
|
|
}
|
|
|
|
// 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));
|
|
|
|
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]);
|
|
|
|
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));
|
|
|
|
mSleepingStartIndex++;
|
|
}
|
|
|
|
mNbComponents++;
|
|
|
|
assert(mSleepingStartIndex <= mNbComponents);
|
|
assert(mNbComponents == static_cast<uint32>(mMapEntityToComponentIndex.size()));
|
|
}
|
|
|
|
// Perform garbage collection to remove unused components
|
|
void TransformComponents::garbageCollection(const EntityManager& entityManager) {
|
|
|
|
// TODO : Make sure we call this method each frame
|
|
|
|
// We use lazy garbage collection. The idea is to pick random components and destroy
|
|
// them if their corresponding entities have been destroyed. We do this until we hit
|
|
// GARBAGE_COLLECTION_MAX_VALID_ENTITIES in a row. Therefore, it cost almost nothing
|
|
// if there are no destroyed entities and it very quickly destroys components where there
|
|
// are a lot of destroyed entities.
|
|
|
|
uint32 nbHitValidEntitiesInARow = 0;
|
|
|
|
// For random number generation
|
|
std::random_device rd;
|
|
std::mt19937 eng(rd());
|
|
|
|
while (mNbComponents > 0 && nbHitValidEntitiesInARow < GARBAGE_COLLECTION_MAX_VALID_ENTITIES) {
|
|
|
|
// Select a random index in the components array
|
|
std::uniform_int_distribution<uint32> distr(0, mNbComponents - 1);
|
|
uint32 i = distr(eng);
|
|
|
|
// If the corresponding entity is valid
|
|
if (entityManager.isValid(mEntities[i])) {
|
|
nbHitValidEntitiesInARow++;
|
|
|
|
continue;
|
|
}
|
|
|
|
nbHitValidEntitiesInARow = 0;
|
|
|
|
// Destroy the component
|
|
removeComponent(i);
|
|
}
|
|
|
|
assert(mNbComponents == static_cast<uint32>(mMapEntityToComponentIndex.size()));
|
|
}
|
|
|
|
// Remove a component at a given index
|
|
void TransformComponents::removeComponent(uint32 index) {
|
|
|
|
assert(index < mNbComponents);
|
|
|
|
// We want to keep the arrays tightly packed. Therefore, when a component is removed,
|
|
// we replace it with the last element of the array. But we need to make sure that sleeping
|
|
// and non-sleeping components stay grouped together.
|
|
|
|
// Destroy the component
|
|
destroyComponent(index);
|
|
|
|
// If the component to remove is sleeping
|
|
if (index >= mSleepingStartIndex) {
|
|
|
|
// If the component is not the last one
|
|
if (index != mNbComponents - 1) {
|
|
|
|
// We replace it by the last sleeping component
|
|
moveComponentToIndex(mNbComponents - 1, index);
|
|
}
|
|
}
|
|
else { // If the component to remove is not sleeping
|
|
|
|
// If it not the last awake component
|
|
if (index != mSleepingStartIndex - 1) {
|
|
|
|
// We replace it by the last awake component
|
|
moveComponentToIndex(mSleepingStartIndex - 1, index);
|
|
}
|
|
|
|
// If there are sleeping components at the end
|
|
if (mSleepingStartIndex != mNbComponents) {
|
|
|
|
// We replace the last awake component by the last sleeping component
|
|
moveComponentToIndex(mNbComponents - 1, index);
|
|
|
|
mSleepingStartIndex--;
|
|
}
|
|
}
|
|
|
|
mNbComponents--;
|
|
|
|
assert(mNbComponents == static_cast<uint32>(mMapEntityToComponentIndex.size()));
|
|
}
|
|
|
|
// Notify if a given entity is sleeping or not
|
|
void TransformComponents::setIsEntitySleeping(Entity entity, bool isSleeping) {
|
|
|
|
const uint32 index = mMapEntityToComponentIndex[entity];
|
|
|
|
// If the component was sleeping and is not sleeping anymore
|
|
if (!isSleeping && index >= mSleepingStartIndex) {
|
|
|
|
assert(mSleepingStartIndex < mNbComponents);
|
|
|
|
// If the sleeping component is not the first sleeping component
|
|
if (mSleepingStartIndex != index) {
|
|
|
|
// Swap the first sleeping component with the one we need to wake up
|
|
swapComponents(index, mSleepingStartIndex);
|
|
}
|
|
|
|
mSleepingStartIndex++;
|
|
}
|
|
// If the component was awake and must now go to sleep
|
|
else if (isSleeping && index < mSleepingStartIndex) {
|
|
|
|
assert(mSleepingStartIndex > 0);
|
|
|
|
// If the awake component is not the only awake component
|
|
if (index != mSleepingStartIndex - 1) {
|
|
|
|
// Swap the last awake component with the one we need to put to sleep
|
|
swapComponents(index, mSleepingStartIndex - 1);
|
|
}
|
|
|
|
mSleepingStartIndex--;
|
|
}
|
|
|
|
assert(mSleepingStartIndex <= mNbComponents);
|
|
assert(mNbComponents == static_cast<uint32>(mMapEntityToComponentIndex.size()));
|
|
}
|
|
|
|
// Move a component from a source to a destination index in the components array
|
|
// The destination location must contain a constructed object
|
|
void TransformComponents::moveComponentToIndex(uint32 srcIndex, uint32 destIndex) {
|
|
|
|
// Copy the data of the source component to the destination location
|
|
new (mEntities + destIndex) Entity(mEntities[srcIndex]);
|
|
new (mTransforms + destIndex) Transform(mTransforms[srcIndex]);
|
|
|
|
const Entity entity = mEntities[srcIndex];
|
|
|
|
// Destroy the source component
|
|
destroyComponent(srcIndex);
|
|
|
|
// Update the entity to component index mapping
|
|
mMapEntityToComponentIndex.add(Pair<Entity, uint32>(entity, destIndex));
|
|
|
|
assert(mMapEntityToComponentIndex[mEntities[destIndex]] == destIndex);
|
|
}
|
|
|
|
// Swap two components in the array
|
|
void TransformComponents::swapComponents(uint32 index1, uint32 index2) {
|
|
|
|
// Copy component 1 data
|
|
Entity entity1(mEntities[index1]);
|
|
Transform transform1(mTransforms[index1]);
|
|
|
|
// Destroy component 1
|
|
destroyComponent(index1);
|
|
|
|
moveComponentToIndex(index2, index1);
|
|
|
|
// Reconstruct component 1 at component 2 location
|
|
new (mEntities + index2) Entity(entity1);
|
|
new (mTransforms + index2) Transform(transform1);
|
|
|
|
// Update the entity to component index mapping
|
|
mMapEntityToComponentIndex.add(Pair<Entity, uint32>(entity1, index2));
|
|
|
|
assert(mMapEntityToComponentIndex[mEntities[index1]] == index1);
|
|
assert(mMapEntityToComponentIndex[mEntities[index2]] == index2);
|
|
assert(mNbComponents == static_cast<uint32>(mMapEntityToComponentIndex.size()));
|
|
}
|
|
|
|
// Destroy a component at a given index
|
|
void TransformComponents::destroyComponent(uint32 index) {
|
|
|
|
mMapEntityToComponentIndex.remove(mEntities[index]);
|
|
|
|
mEntities[index].~Entity();
|
|
mTransforms[index].~Transform();
|
|
}
|