From 70ee053f3f7229104aff6066b50fb6d3e38a509e Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 30 Jul 2016 15:51:46 +0200 Subject: [PATCH] Sdk: Integrate particles Former-commit-id: fa4474b434340d15363ff2ffc91b58d2df3a62fc [formerly 8a52795fc49a847e7b558425fc03915b21139521] Former-commit-id: ac159c0cf652699400f720ef6fdad0fbf649308f --- SDK/include/NDK/Components.hpp | 4 +- .../Components/ParticleEmitterComponent.hpp | 45 ++++++++++++++ .../Components/ParticleEmitterComponent.inl | 18 ++++++ .../NDK/Components/ParticleGroupComponent.hpp | 35 +++++++++++ .../NDK/Components/ParticleGroupComponent.inl | 17 ++++++ SDK/include/NDK/Systems.hpp | 3 +- SDK/include/NDK/Systems/ParticleSystem.hpp | 29 +++++++++ SDK/include/NDK/Systems/ParticleSystem.inl | 3 + SDK/include/NDK/Systems/RenderSystem.hpp | 1 + .../Components/ParticleEmitterComponent.cpp | 59 +++++++++++++++++++ .../NDK/Components/ParticleGroupComponent.cpp | 10 ++++ SDK/src/NDK/Sdk.cpp | 6 ++ SDK/src/NDK/Systems/ParticleSystem.cpp | 26 ++++++++ SDK/src/NDK/Systems/RenderSystem.cpp | 16 +++++ SDK/src/NDK/World.cpp | 2 + 15 files changed, 272 insertions(+), 2 deletions(-) create mode 100644 SDK/include/NDK/Components/ParticleEmitterComponent.hpp create mode 100644 SDK/include/NDK/Components/ParticleEmitterComponent.inl create mode 100644 SDK/include/NDK/Components/ParticleGroupComponent.hpp create mode 100644 SDK/include/NDK/Components/ParticleGroupComponent.inl create mode 100644 SDK/include/NDK/Systems/ParticleSystem.hpp create mode 100644 SDK/include/NDK/Systems/ParticleSystem.inl create mode 100644 SDK/src/NDK/Components/ParticleEmitterComponent.cpp create mode 100644 SDK/src/NDK/Components/ParticleGroupComponent.cpp create mode 100644 SDK/src/NDK/Systems/ParticleSystem.cpp diff --git a/SDK/include/NDK/Components.hpp b/SDK/include/NDK/Components.hpp index ca993b5b6..79bc196f7 100644 --- a/SDK/include/NDK/Components.hpp +++ b/SDK/include/NDK/Components.hpp @@ -1,4 +1,4 @@ -// This file was automatically generated on 01 Jun 2016 at 13:11:09 +// This file was automatically generated on 30 Jul 2016 at 15:29:16 #pragma once @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include diff --git a/SDK/include/NDK/Components/ParticleEmitterComponent.hpp b/SDK/include/NDK/Components/ParticleEmitterComponent.hpp new file mode 100644 index 000000000..add138ee9 --- /dev/null +++ b/SDK/include/NDK/Components/ParticleEmitterComponent.hpp @@ -0,0 +1,45 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#pragma once + +#ifndef NDK_SERVER +#ifndef NDK_COMPONENTS_PARTICLEEMITTERCOMPONENT_HPP +#define NDK_COMPONENTS_PARTICLEEMITTERCOMPONENT_HPP + +#include +#include + +namespace Ndk +{ + class NDK_API ParticleEmitterComponent : public Component, public Nz::ParticleEmitter + { + public: + using SetupFunc = std::function; + + ParticleEmitterComponent(Nz::ParticleGroup* group); + ParticleEmitterComponent(const ParticleEmitterComponent& emitter); + ~ParticleEmitterComponent(); + + inline bool IsActive() const; + + void SetActive(bool active = true); + void SetGroup(Nz::ParticleGroup* group); + inline void SetSetupFunc(SetupFunc func); + + static ComponentIndex componentIndex; + + private: + void SetupParticles(Nz::ParticleMapper& mapper, unsigned int count) const override; + + SetupFunc m_setupFunc; + Nz::ParticleGroup* m_particleGroup; + bool m_isActive; + }; +} + +#include + +#endif // NDK_COMPONENTS_PARTICLEEMITTERCOMPONENT_HPP +#endif // NDK_SERVER \ No newline at end of file diff --git a/SDK/include/NDK/Components/ParticleEmitterComponent.inl b/SDK/include/NDK/Components/ParticleEmitterComponent.inl new file mode 100644 index 000000000..6cc9e8c29 --- /dev/null +++ b/SDK/include/NDK/Components/ParticleEmitterComponent.inl @@ -0,0 +1,18 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#include + +namespace Ndk +{ + inline bool ParticleEmitterComponent::IsActive() const + { + return m_isActive; + } + + inline void Ndk::ParticleEmitterComponent::SetSetupFunc(SetupFunc func) + { + m_setupFunc = std::move(func); + } +} diff --git a/SDK/include/NDK/Components/ParticleGroupComponent.hpp b/SDK/include/NDK/Components/ParticleGroupComponent.hpp new file mode 100644 index 000000000..638f877d3 --- /dev/null +++ b/SDK/include/NDK/Components/ParticleGroupComponent.hpp @@ -0,0 +1,35 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#pragma once + +#ifndef NDK_SERVER +#ifndef NDK_COMPONENTS_PARTICLEGROUPCOMPONENT_HPP +#define NDK_COMPONENTS_PARTICLEGROUPCOMPONENT_HPP + +#include +#include + +namespace Ndk +{ + class ParticleGroupComponent; + + using ParticleGroupComponentHandle = Nz::ObjectHandle; + + class NDK_API ParticleGroupComponent : public Component, public Nz::ParticleGroup + { + public: + inline ParticleGroupComponent(unsigned int maxParticleCount, Nz::ParticleLayout layout); + inline ParticleGroupComponent(unsigned int maxParticleCount, Nz::ParticleDeclarationConstRef declaration); + ParticleGroupComponent(const ParticleGroupComponent&) = default; + ~ParticleGroupComponent() = default; + + static ComponentIndex componentIndex; + }; +} + +#include + +#endif // NDK_COMPONENTS_PARTICLEGROUPCOMPONENT_HPP +#endif // NDK_SERVER \ No newline at end of file diff --git a/SDK/include/NDK/Components/ParticleGroupComponent.inl b/SDK/include/NDK/Components/ParticleGroupComponent.inl new file mode 100644 index 000000000..7aefdad56 --- /dev/null +++ b/SDK/include/NDK/Components/ParticleGroupComponent.inl @@ -0,0 +1,17 @@ +#include "ParticleGroupComponent.hpp" +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +namespace Ndk +{ + inline ParticleGroupComponent::ParticleGroupComponent(unsigned int maxParticleCount, Nz::ParticleLayout layout) : + ParticleGroup(maxParticleCount, layout) + { + } + + inline ParticleGroupComponent::ParticleGroupComponent(unsigned int maxParticleCount, Nz::ParticleDeclarationConstRef declaration) : + ParticleGroup(maxParticleCount, std::move(declaration)) + { + } +} diff --git a/SDK/include/NDK/Systems.hpp b/SDK/include/NDK/Systems.hpp index 176a956ee..269bead6f 100644 --- a/SDK/include/NDK/Systems.hpp +++ b/SDK/include/NDK/Systems.hpp @@ -1,4 +1,4 @@ -// This file was automatically generated on 01 Jun 2016 at 13:11:09 +// This file was automatically generated on 30 Jul 2016 at 15:29:16 #pragma once @@ -6,6 +6,7 @@ #define NDK_SYSTEMS_GLOBAL_HPP #include +#include #include #include #include diff --git a/SDK/include/NDK/Systems/ParticleSystem.hpp b/SDK/include/NDK/Systems/ParticleSystem.hpp new file mode 100644 index 000000000..3ae00a995 --- /dev/null +++ b/SDK/include/NDK/Systems/ParticleSystem.hpp @@ -0,0 +1,29 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#pragma once + +#ifndef NDK_SYSTEMS_PARTICLESYSTEM_HPP +#define NDK_SYSTEMS_PARTICLESYSTEM_HPP + +#include + +namespace Ndk +{ + class NDK_API ParticleSystem : public System + { + public: + ParticleSystem(); + ~ParticleSystem() = default; + + static SystemIndex systemIndex; + + private: + void OnUpdate(float elapsedTime) override; + }; +} + +#include + +#endif // NDK_SYSTEMS_PARTICLESYSTEM_HPP diff --git a/SDK/include/NDK/Systems/ParticleSystem.inl b/SDK/include/NDK/Systems/ParticleSystem.inl new file mode 100644 index 000000000..e5f296d27 --- /dev/null +++ b/SDK/include/NDK/Systems/ParticleSystem.inl @@ -0,0 +1,3 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp diff --git a/SDK/include/NDK/Systems/RenderSystem.hpp b/SDK/include/NDK/Systems/RenderSystem.hpp index f2fbc7a65..82e558773 100644 --- a/SDK/include/NDK/Systems/RenderSystem.hpp +++ b/SDK/include/NDK/Systems/RenderSystem.hpp @@ -60,6 +60,7 @@ namespace Ndk EntityList m_directionalLights; EntityList m_lights; EntityList m_pointSpotLights; + EntityList m_particleGroups; Nz::BackgroundRef m_background; Nz::DepthRenderTechnique m_shadowTechnique; Nz::Matrix4f m_coordinateSystemMatrix; diff --git a/SDK/src/NDK/Components/ParticleEmitterComponent.cpp b/SDK/src/NDK/Components/ParticleEmitterComponent.cpp new file mode 100644 index 000000000..6a3cfeb7e --- /dev/null +++ b/SDK/src/NDK/Components/ParticleEmitterComponent.cpp @@ -0,0 +1,59 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#include +#include + +namespace Ndk +{ + ParticleEmitterComponent::ParticleEmitterComponent(Nz::ParticleGroup* group) : + m_particleGroup(group), + m_isActive(true) + { + if (m_particleGroup) + m_particleGroup->AddEmitter(this); + } + + ParticleEmitterComponent::ParticleEmitterComponent(const ParticleEmitterComponent& emitter) : + m_particleGroup(emitter.m_particleGroup), + m_isActive(emitter.m_isActive) + { + if (m_isActive) + m_particleGroup->AddEmitter(this); + } + + ParticleEmitterComponent::~ParticleEmitterComponent() + { + m_particleGroup->RemoveEmitter(this); + } + + inline void Ndk::ParticleEmitterComponent::SetActive(bool active) + { + if (m_isActive != active) + { + if (active) + m_particleGroup->AddEmitter(this); + else + m_particleGroup->RemoveEmitter(this); + } + } + + void ParticleEmitterComponent::SetGroup(Nz::ParticleGroup* group) + { + if (m_particleGroup) + m_particleGroup->RemoveEmitter(this); + + m_particleGroup = group; + if (m_particleGroup && m_isActive) + m_particleGroup = group; + } + + inline void ParticleEmitterComponent::SetupParticles(Nz::ParticleMapper& mapper, unsigned int count) const + { + if (m_setupFunc) + m_setupFunc(m_entity, mapper, count); + } + + ComponentIndex ParticleEmitterComponent::componentIndex; +} diff --git a/SDK/src/NDK/Components/ParticleGroupComponent.cpp b/SDK/src/NDK/Components/ParticleGroupComponent.cpp new file mode 100644 index 000000000..4e0845871 --- /dev/null +++ b/SDK/src/NDK/Components/ParticleGroupComponent.cpp @@ -0,0 +1,10 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#include + +namespace Ndk +{ + ComponentIndex ParticleGroupComponent::componentIndex; +} diff --git a/SDK/src/NDK/Sdk.cpp b/SDK/src/NDK/Sdk.cpp index 7f29bb18e..964913bf8 100644 --- a/SDK/src/NDK/Sdk.cpp +++ b/SDK/src/NDK/Sdk.cpp @@ -25,6 +25,9 @@ #include #include #include +#include +#include +#include #include #include #endif @@ -71,6 +74,8 @@ namespace Ndk InitializeComponent("NdkLight"); InitializeComponent("NdkList"); InitializeComponent("NdkGfx"); + InitializeComponent("NdkPaEmi"); + InitializeComponent("NdkPaGrp"); #endif // Systems @@ -84,6 +89,7 @@ namespace Ndk #ifndef NDK_SERVER // Client systems InitializeSystem(); + InitializeSystem(); InitializeSystem(); #endif diff --git a/SDK/src/NDK/Systems/ParticleSystem.cpp b/SDK/src/NDK/Systems/ParticleSystem.cpp new file mode 100644 index 000000000..a9f63cb6c --- /dev/null +++ b/SDK/src/NDK/Systems/ParticleSystem.cpp @@ -0,0 +1,26 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#include +#include + +namespace Ndk +{ + ParticleSystem::ParticleSystem() + { + Requires(); + } + + void ParticleSystem::OnUpdate(float elapsedTime) + { + for (const Ndk::EntityHandle& entity : GetEntities()) + { + ParticleGroupComponent& group = entity->GetComponent(); + + group.Update(elapsedTime); + } + } + + SystemIndex ParticleSystem::systemIndex; +} diff --git a/SDK/src/NDK/Systems/RenderSystem.cpp b/SDK/src/NDK/Systems/RenderSystem.cpp index 7b90761b6..fcfd5c6a5 100644 --- a/SDK/src/NDK/Systems/RenderSystem.cpp +++ b/SDK/src/NDK/Systems/RenderSystem.cpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace Ndk { @@ -25,8 +26,11 @@ namespace Ndk void RenderSystem::OnEntityRemoved(Entity* entity) { m_cameras.Remove(entity); + m_directionalLights.Remove(entity); m_drawables.Remove(entity); m_lights.Remove(entity); + m_particleGroups.Remove(entity); + m_pointSpotLights.Remove(entity); } void RenderSystem::OnEntityValidation(Entity* entity, bool justAdded) @@ -71,6 +75,11 @@ namespace Ndk m_lights.Remove(entity); m_pointSpotLights.Remove(entity); } + + if (entity->HasComponent()) + m_particleGroups.Insert(entity); + else + m_particleGroups.Remove(entity); } void RenderSystem::OnUpdate(float elapsedTime) @@ -118,6 +127,13 @@ namespace Ndk lightComponent.AddToRenderQueue(renderQueue, Nz::Matrix4f::ConcatenateAffine(m_coordinateSystemMatrix, lightNode.GetTransformMatrix())); } + for (const Ndk::EntityHandle& particleGroup : m_particleGroups) + { + ParticleGroupComponent& groupComponent = particleGroup->GetComponent(); + + groupComponent.AddToRenderQueue(renderQueue, Nz::Matrix4f::Identity()); //< ParticleGroup doesn't use Matrix4f + } + camComponent.ApplyView(); Nz::SceneData sceneData; diff --git a/SDK/src/NDK/World.cpp b/SDK/src/NDK/World.cpp index e95785896..5d0e4900b 100644 --- a/SDK/src/NDK/World.cpp +++ b/SDK/src/NDK/World.cpp @@ -9,6 +9,7 @@ #ifndef NDK_SERVER #include +#include #include #endif @@ -27,6 +28,7 @@ namespace Ndk #ifndef NDK_SERVER AddSystem(); + AddSystem(); AddSystem(); #endif }