Sdk/World: Add profiler

This commit is contained in:
Jérôme Leclercq
2018-02-08 16:25:27 +01:00
parent 6161b1a751
commit a9364ab7e2
5 changed files with 149 additions and 30 deletions

View File

@@ -2,6 +2,7 @@
// This file is part of the "Nazara Development Kit"
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
#include <NDK/World.hpp>
#include <Nazara/Core/Error.hpp>
#include <type_traits>
@@ -14,7 +15,8 @@ namespace Ndk
*/
inline World::World(bool addDefaultSystems) :
m_orderedSystemsUpdated(false)
m_orderedSystemsUpdated(false),
m_isProfilerEnabled(false)
{
if (addDefaultSystems)
AddDefaultSystems();
@@ -47,7 +49,10 @@ namespace Ndk
// We must ensure that the vector is big enough to hold the new system
if (index >= m_systems.size())
{
m_systems.resize(index + 1);
m_profilerData.updateTime.resize(index + 1, 0);
}
// Affectation and return of system
m_systems[index] = std::move(system);
@@ -82,7 +87,6 @@ namespace Ndk
*
* \param count Number of entities to create
*/
inline World::EntityVector World::CreateEntities(unsigned int count)
{
EntityVector list;
@@ -94,16 +98,79 @@ namespace Ndk
return list;
}
/*!
* \brief Disables the profiler, clearing up results
*
* This is just a shortcut to EnableProfiler(false)
*
* \param enable Should the entity be enabled
*
* \see EnableProfiler
*/
inline void World::DisableProfiler()
{
EnableProfiler(false);
}
/*!
* \brief Enables/Disables the internal profiler
*
* Worlds come with a built-in profiler, allowing to measure update count along with time passed in refresh and system updates.
* This is disabled by default as it adds an small overhead to the update process.
*
* \param enable Should the profiler be enabled
*
* \remark Disabling the profiler clears up results, as if ResetProfiler has been called
*/
inline void World::EnableProfiler(bool enable)
{
if (m_isProfilerEnabled != enable)
{
m_isProfilerEnabled = enable;
if (enable)
ResetProfiler();
}
}
/*!
* \brief Gets an entity
* \return A constant reference to a handle of the entity
*
* \param id Identifier of the entity
*
* \remark Handle referenced by this function can move in memory when updating the world, do not keep a handle reference from a world update to another
* \remark If an invalid identifier is provided, an error got triggered and an invalid handle is returned
*/
inline const EntityHandle& World::GetEntity(EntityId id)
{
if (IsEntityIdValid(id))
return m_entityBlocks[id]->handle;
else
{
NazaraError("Invalid ID");
return EntityHandle::InvalidHandle;
}
}
/*!
* \brief Gets every entities in the world
* \return A constant reference to the entities
*/
inline const EntityList& World::GetEntities() const
{
return m_aliveEntities;
}
/*!
* \brief Gets the latest profiler data
* \return A constant reference to the profiler data
*/
inline const World::ProfilerData& World::GetProfilerData() const
{
return m_profilerData;
}
/*!
* \brief Gets a system in the world by index
* \return A reference to the system
@@ -192,26 +259,6 @@ namespace Ndk
KillEntity(entity);
}
/*!
* \brief Gets an entity
* \return A constant reference to a handle of the entity
*
* \param id Identifier of the entity
*
* \remark Handle referenced by this function can move in memory when updating the world, do not keep a handle reference from a world update to another
* \remark If an invalid identifier is provided, an error got triggered and an invalid handle is returned
*/
inline const EntityHandle& World::GetEntity(EntityId id)
{
if (IsEntityIdValid(id))
return m_entityBlocks[id]->handle;
else
{
NazaraError("Invalid ID");
return EntityHandle::InvalidHandle;
}
}
/*!
* \brief Checks whether or not an entity is valid
* \return true If it is the case
@@ -230,12 +277,22 @@ namespace Ndk
*
* \param id Identifier of the entity
*/
inline bool World::IsEntityIdValid(EntityId id) const
{
return id < m_entityBlocks.size() && m_entityBlocks[id]->entity.IsValid();
}
/*!
* \brief Checks whether or not the profiler is enabled
* \return true If it is the case
*
* \see EnableProfiler
*/
inline bool World::IsProfilerEnabled() const
{
return m_isProfilerEnabled;
}
/*!
* \brief Removes each system from the world
*/
@@ -265,10 +322,21 @@ namespace Ndk
}
}
/*!
* \brief Clear profiler results
*
* This reset the profiler results, filling all counters with zero
*/
inline void World::ResetProfiler()
{
m_profilerData.refreshTime = 0;
m_profilerData.updateCount = 0;
std::fill(m_profilerData.updateTime.begin(), m_profilerData.updateTime.end(), 0);
}
/*!
* \brief Removes a system from the world by type
*/
template<typename SystemType>
void World::RemoveSystem()
{
@@ -292,6 +360,8 @@ namespace Ndk
m_killedEntities = std::move(world.m_killedEntities);
m_orderedSystems = std::move(world.m_orderedSystems);
m_orderedSystemsUpdated = world.m_orderedSystemsUpdated;
m_profilerData = std::move(world.m_profilerData);
m_isProfilerEnabled = m_isProfilerEnabled;
m_entities = std::move(world.m_entities);
for (EntityBlock& block : m_entities)