From f137a75267e52c46044f67d0d4375c724a0352fc Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 12 Apr 2015 19:38:38 +0200 Subject: [PATCH] (NDK) Added Listener component and system Former-commit-id: 0553c4ad9a7e33608e9ab91bd4ca8812272a6293 --- .../NDK/Components/ListenerComponent.hpp | 32 +++++++++++++++ .../NDK/Components/ListenerComponent.inl | 21 ++++++++++ SDK/include/NDK/Systems/ListenerSystem.hpp | 28 +++++++++++++ SDK/include/NDK/Systems/ListenerSystem.inl | 3 ++ SDK/include/NDK/World.hpp | 4 +- SDK/include/NDK/World.inl | 6 +++ SDK/src/NDK/Components/ListenerComponent.cpp | 10 +++++ SDK/src/NDK/Components/NodeComponent.cpp | 2 +- SDK/src/NDK/Sdk.cpp | 6 +++ SDK/src/NDK/Systems/ListenerSystem.cpp | 40 +++++++++++++++++++ SDK/src/NDK/World.cpp | 6 +++ 11 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 SDK/include/NDK/Components/ListenerComponent.hpp create mode 100644 SDK/include/NDK/Components/ListenerComponent.inl create mode 100644 SDK/include/NDK/Systems/ListenerSystem.hpp create mode 100644 SDK/include/NDK/Systems/ListenerSystem.inl create mode 100644 SDK/src/NDK/Components/ListenerComponent.cpp create mode 100644 SDK/src/NDK/Systems/ListenerSystem.cpp diff --git a/SDK/include/NDK/Components/ListenerComponent.hpp b/SDK/include/NDK/Components/ListenerComponent.hpp new file mode 100644 index 000000000..15d6d07f7 --- /dev/null +++ b/SDK/include/NDK/Components/ListenerComponent.hpp @@ -0,0 +1,32 @@ +// 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_COMPONENTS_LISTENERCOMPONENT_HPP +#define NDK_COMPONENTS_LISTENERCOMPONENT_HPP + +#include + +namespace Ndk +{ + class NDK_API ListenerComponent : public Component + { + public: + ListenerComponent(); + ~ListenerComponent() = default; + + bool IsActive() const; + bool SetActive(bool active = true); + + static ComponentIndex componentIndex; + + private: + bool m_isActive; + }; +} + +#include + +#endif // NDK_COMPONENTS_LISTENERCOMPONENT_HPP diff --git a/SDK/include/NDK/Components/ListenerComponent.inl b/SDK/include/NDK/Components/ListenerComponent.inl new file mode 100644 index 000000000..fb18a862f --- /dev/null +++ b/SDK/include/NDK/Components/ListenerComponent.inl @@ -0,0 +1,21 @@ +// 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 ListenerComponent::ListenerComponent() : + m_isActive(true) + { + } + + inline bool ListenerComponent::IsActive() const + { + return m_isActive; + } + + inline bool ListenerComponent::SetActive(bool active) + { + m_isActive = active; + } +} diff --git a/SDK/include/NDK/Systems/ListenerSystem.hpp b/SDK/include/NDK/Systems/ListenerSystem.hpp new file mode 100644 index 000000000..a3de7b149 --- /dev/null +++ b/SDK/include/NDK/Systems/ListenerSystem.hpp @@ -0,0 +1,28 @@ +// 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_LISTENERSYSTEM_HPP +#define NDK_SYSTEMS_LISTENERSYSTEM_HPP + +#include + +namespace Ndk +{ + class NDK_API ListenerSystem : public System + { + public: + ListenerSystem(); + ~ListenerSystem() = default; + + void Update(float elapsedTime); + + static SystemIndex systemIndex; + }; +} + +#include + +#endif // NDK_SYSTEMS_LISTENERSYSTEM_HPP diff --git a/SDK/include/NDK/Systems/ListenerSystem.inl b/SDK/include/NDK/Systems/ListenerSystem.inl new file mode 100644 index 000000000..e5f296d27 --- /dev/null +++ b/SDK/include/NDK/Systems/ListenerSystem.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/World.hpp b/SDK/include/NDK/World.hpp index c82565c8e..ee54afe98 100644 --- a/SDK/include/NDK/World.hpp +++ b/SDK/include/NDK/World.hpp @@ -26,9 +26,11 @@ namespace Ndk public: using EntityList = std::vector; - World() = default; + World(bool addDefaultSystems = true); ~World(); + void AddDefaultSystems(); + BaseSystem& AddSystem(std::unique_ptr&& system); template SystemType& AddSystem(Args&&... args); diff --git a/SDK/include/NDK/World.inl b/SDK/include/NDK/World.inl index 4cb985b28..0b0b30b63 100644 --- a/SDK/include/NDK/World.inl +++ b/SDK/include/NDK/World.inl @@ -7,6 +7,12 @@ namespace Ndk { + inline World::World(bool addDefaultSystems) + { + if (addDefaultSystems) + AddDefaultSystems(); + } + inline BaseSystem& World::AddSystem(std::unique_ptr&& system) { NazaraAssert(system, "System must be valid"); diff --git a/SDK/src/NDK/Components/ListenerComponent.cpp b/SDK/src/NDK/Components/ListenerComponent.cpp new file mode 100644 index 000000000..7d88c5be5 --- /dev/null +++ b/SDK/src/NDK/Components/ListenerComponent.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 ListenerComponent::componentIndex; +} diff --git a/SDK/src/NDK/Components/NodeComponent.cpp b/SDK/src/NDK/Components/NodeComponent.cpp index b3ee8546a..40584236a 100644 --- a/SDK/src/NDK/Components/NodeComponent.cpp +++ b/SDK/src/NDK/Components/NodeComponent.cpp @@ -6,5 +6,5 @@ namespace Ndk { - ComponentIndex NodeComponent::componentIndex = 0; + ComponentIndex NodeComponent::componentIndex; } diff --git a/SDK/src/NDK/Sdk.cpp b/SDK/src/NDK/Sdk.cpp index 8a7324470..070446cc7 100644 --- a/SDK/src/NDK/Sdk.cpp +++ b/SDK/src/NDK/Sdk.cpp @@ -13,7 +13,9 @@ #include #include #include +#include #include +#include namespace Ndk { @@ -45,8 +47,12 @@ namespace Ndk BaseSystem::Initialize(); // Composants + InitializeComponent("NdkList"); InitializeComponent("NdkNode"); + // Systèmes + InitializeSystem(); + NazaraNotice("Initialized: SDK"); return true; } diff --git a/SDK/src/NDK/Systems/ListenerSystem.cpp b/SDK/src/NDK/Systems/ListenerSystem.cpp new file mode 100644 index 000000000..e7a90c5a9 --- /dev/null +++ b/SDK/src/NDK/Systems/ListenerSystem.cpp @@ -0,0 +1,40 @@ +// 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 +#include +#include + +namespace Ndk +{ + ListenerSystem::ListenerSystem() + { + Requires(); + } + + void ListenerSystem::Update(float elapsedTime) + { + unsigned int activeListenerCount = 0; + + for (const Ndk::EntityHandle& entity : GetEntities()) + { + // Le listener est-il actif ? + const ListenerComponent& listener = entity->GetComponent(); + if (!listener.IsActive()) + continue; + + const NodeComponent& node = entity->GetComponent(); + NzAudio::SetListenerPosition(node.GetPosition(nzCoordSys_Global)); + NzAudio::SetListenerRotation(node.GetRotation(nzCoordSys_Global)); + + activeListenerCount++; + } + + if (activeListenerCount > 1) + NazaraWarning(NzString::Number(activeListenerCount) + " listeners were active in the same update loop"); + } + + SystemIndex ListenerSystem::systemIndex; +} diff --git a/SDK/src/NDK/World.cpp b/SDK/src/NDK/World.cpp index d2a0f0568..efa1d3a41 100644 --- a/SDK/src/NDK/World.cpp +++ b/SDK/src/NDK/World.cpp @@ -4,6 +4,7 @@ #include #include +#include namespace Ndk { @@ -13,6 +14,11 @@ namespace Ndk Clear(); } + void World::AddDefaultSystems() + { + AddSystem(); + } + const EntityHandle& World::CreateEntity() { EntityId id;