Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/NDK' into NDK-ShadowMapping
Browse files Browse the repository at this point in the history
Conflicts:
	SDK/include/NDK/Systems/RenderSystem.hpp
	SDK/src/NDK/Systems/RenderSystem.cpp
  • Loading branch information
SirLynix committed Jun 22, 2015
2 parents e03aa67 + 6bfda27 commit 3bd0c6a
Show file tree
Hide file tree
Showing 18 changed files with 328 additions and 288 deletions.
9 changes: 9 additions & 0 deletions SDK/include/NDK/BaseSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ namespace Ndk

inline const std::vector<EntityHandle>& GetEntities() const;
inline SystemIndex GetIndex() const;
inline float GetUpdateRate() const;
inline World& GetWorld() const;

inline bool HasEntity(const Entity* entity) const;

inline void SetUpdateRate(float updatePerSecond);

inline void Update(float elapsedTime);

BaseSystem& operator=(const BaseSystem&) = delete;
BaseSystem& operator=(BaseSystem&&) noexcept = default;

Expand All @@ -55,6 +60,8 @@ namespace Ndk
template<typename ComponentType1, typename ComponentType2, typename... Rest> void RequiresAny();
inline void RequiresAnyComponent(ComponentIndex index);

virtual void OnUpdate(float elapsedTime) = 0;

private:
inline void AddEntity(Entity* entity);

Expand All @@ -79,6 +86,8 @@ namespace Ndk
NzBitset<> m_requiredComponents;
SystemIndex m_systemIndex;
World* m_world;
float m_updateCounter;
float m_updateRate;

static SystemIndex s_nextIndex;
};
Expand Down
26 changes: 25 additions & 1 deletion SDK/include/NDK/BaseSystem.inl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ namespace Ndk
inline BaseSystem::BaseSystem(SystemIndex systemId) :
m_systemIndex(systemId)
{
SetUpdateRate(30);
}

inline BaseSystem::BaseSystem(const BaseSystem& system) :
m_excludedComponents(system.m_excludedComponents),
m_requiredComponents(system.m_requiredComponents),
m_systemIndex(system.m_systemIndex)
m_systemIndex(system.m_systemIndex),
m_updateCounter(0.f),
m_updateRate(system.m_updateRate)
{
}

Expand All @@ -29,6 +32,11 @@ namespace Ndk
return m_systemIndex;
}

inline float BaseSystem::GetUpdateRate() const
{
return 1.f / m_updateRate;
}

inline World& BaseSystem::GetWorld() const
{
return *m_world;
Expand All @@ -42,6 +50,22 @@ namespace Ndk
return m_entityBits.UnboundedTest(entity->GetId());
}

inline void BaseSystem::SetUpdateRate(float updatePerSecond)
{
m_updateCounter = 0.f;
m_updateRate = (updatePerSecond > 0.f) ? 1.f / updatePerSecond : 0.f; // 0.f means no limit
}

inline void BaseSystem::Update(float elapsedTime)
{
m_updateCounter -= elapsedTime;
if (m_updateCounter < 0.f)
{
m_updateCounter += m_updateRate;
OnUpdate(elapsedTime);
}
}

template<typename ComponentType>
void BaseSystem::Excludes()
{
Expand Down
5 changes: 3 additions & 2 deletions SDK/include/NDK/Systems/ListenerSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ namespace Ndk
ListenerSystem();
~ListenerSystem() = default;

void Update(float elapsedTime);

static SystemIndex systemIndex;

private:
void OnUpdate(float elapsedTime) override;
};
}

Expand Down
3 changes: 1 addition & 2 deletions SDK/include/NDK/Systems/PhysicsSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ namespace Ndk
NzPhysWorld& GetWorld();
const NzPhysWorld& GetWorld() const;

void Update(float elapsedTime);

static SystemIndex systemIndex;

private:
void OnEntityValidation(Entity* entity, bool justAdded) override;
void OnUpdate(float elapsedTime) override;

EntityList m_dynamicObjects;
EntityList m_staticObjects;
Expand Down
3 changes: 1 addition & 2 deletions SDK/include/NDK/Systems/RenderSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ namespace Ndk
inline RenderSystem(const RenderSystem& renderSystem);
~RenderSystem() = default;

void Update(float elapsedTime);

static SystemIndex systemIndex;

private:
void OnEntityRemoved(Entity* entity) override;
void OnEntityValidation(Entity* entity, bool justAdded) override;
void OnUpdate(float elapsedTime) override;
void UpdateShadowMaps();

EntityList m_cameras;
Expand Down
5 changes: 3 additions & 2 deletions SDK/include/NDK/Systems/VelocitySystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ namespace Ndk
VelocitySystem();
~VelocitySystem() = default;

void Update(float elapsedTime);

static SystemIndex systemIndex;

private:
void OnUpdate(float elapsedTime) override;
};
}

Expand Down
1 change: 1 addition & 0 deletions SDK/include/NDK/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ namespace Ndk
template<typename SystemType> void RemoveSystem();

void Update();
inline void Update(float elapsedTime);

private:
inline void Invalidate();
Expand Down
9 changes: 9 additions & 0 deletions SDK/include/NDK/World.inl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ namespace Ndk
RemoveSystem(index);
}

inline void World::Update(float elapsedTime)
{
Update(); //< Update entities

// And then update systems
for (auto& systemPtr : m_systems)
systemPtr->Update(elapsedTime);
}

inline void World::Invalidate()
{
m_dirtyEntities.Resize(m_entities.size(), false);
Expand Down
2 changes: 1 addition & 1 deletion SDK/src/NDK/Systems/ListenerSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Ndk
Requires<ListenerComponent, NodeComponent>();
}

void ListenerSystem::Update(float elapsedTime)
void ListenerSystem::OnUpdate(float elapsedTime)
{
NazaraUnused(elapsedTime);

Expand Down
30 changes: 15 additions & 15 deletions SDK/src/NDK/Systems/PhysicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,21 @@ namespace Ndk
{
}

void PhysicsSystem::Update(float elapsedTime)
void PhysicsSystem::OnEntityValidation(Entity* entity, bool justAdded)
{
// Si l'entité ne vient pas d'être ajoutée au système, il est possible qu'elle fasse partie du mauvais tableau
if (!justAdded)
{
// On prend le tableau inverse de celui dont l'entité devrait faire partie
auto& entities = (entity->HasComponent<PhysicsComponent>()) ? m_staticObjects : m_dynamicObjects;
entities.Remove(entity);
}

auto& entities = (entity->HasComponent<PhysicsComponent>()) ? m_dynamicObjects : m_staticObjects;
entities.Insert(entity);
}

void PhysicsSystem::OnUpdate(float elapsedTime)
{
m_world.Step(elapsedTime);

Expand Down Expand Up @@ -75,19 +89,5 @@ namespace Ndk
}
}

void PhysicsSystem::OnEntityValidation(Entity* entity, bool justAdded)
{
// Si l'entité ne vient pas d'être ajoutée au système, il est possible qu'elle fasse partie du mauvais tableau
if (!justAdded)
{
// On prend le tableau inverse de celui dont l'entité devrait faire partie
auto& entities = (entity->HasComponent<PhysicsComponent>()) ? m_staticObjects : m_dynamicObjects;
entities.Remove(entity);
}

auto& entities = (entity->HasComponent<PhysicsComponent>()) ? m_dynamicObjects : m_staticObjects;
entities.Insert(entity);
}

SystemIndex PhysicsSystem::systemIndex;
}
93 changes: 62 additions & 31 deletions SDK/src/NDK/Systems/RenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,35 @@ namespace Ndk
{
}

void RenderSystem::Update(float elapsedTime)
void RenderSystem::OnEntityRemoved(Entity* entity)
{
UpdateShadowMaps();
m_cameras.Remove(entity);
m_drawables.Remove(entity);
m_lights.Remove(entity);
}

for (const Ndk::EntityHandle& camera : m_cameras)
void RenderSystem::OnEntityValidation(Entity* entity, bool justAdded)
{
if (entity->HasComponent<CameraComponent>() && entity->HasComponent<NodeComponent>())
{
CameraComponent& camComponent = camera->GetComponent<CameraComponent>();
camComponent.ApplyView();

NzAbstractRenderQueue* renderQueue = m_renderTechnique.GetRenderQueue();
renderQueue->Clear();

for (const Ndk::EntityHandle& drawable : m_drawables)
{
GraphicsComponent& graphicsComponent = drawable->GetComponent<GraphicsComponent>();
NodeComponent& drawableNode = drawable->GetComponent<NodeComponent>();

graphicsComponent.AddToRenderQueue(renderQueue);
}

for (const Ndk::EntityHandle& light : m_lights)
m_cameras.Insert(entity);
std::sort(m_cameras.begin(), m_cameras.end(), [](const EntityHandle& handle1, const EntityHandle& handle2)
{
LightComponent& lightComponent = light->GetComponent<LightComponent>();
NodeComponent& lightNode = light->GetComponent<NodeComponent>();

lightComponent.AddToRenderQueue(renderQueue, lightNode.GetTransformMatrix());
}

NzColorBackground background;
return handle1->GetComponent<CameraComponent>().GetLayer() < handle2->GetComponent<CameraComponent>().GetLayer();
});
}
else
m_cameras.Remove(entity);

NzSceneData sceneData;
sceneData.ambientColor = NzColor(25, 25, 25);
sceneData.background = &background;
sceneData.viewer = &camComponent;
if (entity->HasComponent<GraphicsComponent>() && entity->HasComponent<NodeComponent>())
m_drawables.Insert(entity);
else
m_drawables.Remove(entity);

m_renderTechnique.Draw(sceneData);
}
if (entity->HasComponent<LightComponent>() && entity->HasComponent<NodeComponent>())
m_lights.Insert(entity);
else
m_lights.Remove(entity);
}

void RenderSystem::OnEntityRemoved(Entity* entity)
Expand Down Expand Up @@ -87,6 +79,45 @@ namespace Ndk
m_lights.Remove(entity);
}

void RenderSystem::OnUpdate(float elapsedTime)
{
UpdateShadowMaps();

for (const Ndk::EntityHandle& camera : m_cameras)
{
CameraComponent& camComponent = camera->GetComponent<CameraComponent>();
camComponent.ApplyView();

NzAbstractRenderQueue* renderQueue = m_renderTechnique.GetRenderQueue();
renderQueue->Clear();

for (const Ndk::EntityHandle& drawable : m_drawables)
{
GraphicsComponent& graphicsComponent = drawable->GetComponent<GraphicsComponent>();
NodeComponent& drawableNode = drawable->GetComponent<NodeComponent>();

graphicsComponent.AddToRenderQueue(renderQueue);
}

for (const Ndk::EntityHandle& light : m_lights)
{
LightComponent& lightComponent = light->GetComponent<LightComponent>();
NodeComponent& lightNode = light->GetComponent<NodeComponent>();

lightComponent.AddToRenderQueue(renderQueue, lightNode.GetTransformMatrix());
}

NzColorBackground background;

NzSceneData sceneData;
sceneData.ambientColor = NzColor(25, 25, 25);
sceneData.background = &background;
sceneData.viewer = &camComponent;

m_renderTechnique.Draw(sceneData);
}
}

void RenderSystem::UpdateShadowMaps()
{
if (!m_shadowRT.IsValid())
Expand Down
2 changes: 1 addition & 1 deletion SDK/src/NDK/Systems/VelocitySystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Ndk
Excludes<PhysicsComponent>();
}

void VelocitySystem::Update(float elapsedTime)
void VelocitySystem::OnUpdate(float elapsedTime)
{
for (const Ndk::EntityHandle& entity : GetEntities())
{
Expand Down
Loading

0 comments on commit 3bd0c6a

Please sign in to comment.