From 98e20ecbb78621af3be19e0ddab01b16f692e480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Thu, 17 Sep 2020 18:54:33 +0200 Subject: [PATCH] Last modules fixes --- examples/DopplerEffect/main.cpp | 7 +- examples/MeshInfos/main.cpp | 7 +- examples/RenderTest/build.lua | 1 + examples/RenderTest/main.cpp | 7 +- include/Nazara/Audio/Audio.hpp | 4 +- include/Nazara/Renderer/Renderer.hpp | 3 +- include/NazaraSDK/Application.inl | 8 -- include/NazaraSDK/Sdk.hpp | 35 ++++-- include/NazaraSDK/Sdk.inl | 9 -- src/NazaraSDK/Sdk.cpp | 137 ++++++----------------- src/NazaraSDK/Systems/ListenerSystem.cpp | 12 +- tests/Engine/Audio/Music.cpp | 4 +- tests/Engine/Audio/Sound.cpp | 4 +- tests/SDK/NDK/Systems/ListenerSystem.cpp | 8 +- tests/main.cpp | 12 +- 15 files changed, 95 insertions(+), 163 deletions(-) diff --git a/examples/DopplerEffect/main.cpp b/examples/DopplerEffect/main.cpp index 2cfb8c23b..dcb760bb6 100644 --- a/examples/DopplerEffect/main.cpp +++ b/examples/DopplerEffect/main.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -20,13 +21,13 @@ int main() { // NzKeyboard nécessite l'initialisation du module Utilitaire - Nz::Initializer audio; - if (!audio) + Nz::Modules audio; + /*if (!audio) { std::cout << "Failed to initialize audio module" << std::endl; std::getchar(); return 1; - } + }*/ Nz::Sound sound; if (!sound.LoadFromFile("resources/siren.wav")) diff --git a/examples/MeshInfos/main.cpp b/examples/MeshInfos/main.cpp index 8d7d25adb..86723b77c 100644 --- a/examples/MeshInfos/main.cpp +++ b/examples/MeshInfos/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -14,14 +15,14 @@ int main() { // Pour charger des ressources, il est impératif d'initialiser le module utilitaire - Nz::Initializer utility; - if (!utility) + Nz::Modules nazara; + /*if (!utility) { // Ça n'a pas fonctionné, le pourquoi se trouve dans le fichier NazaraLog.log std::cout << "Failed to initialize Nazara, see NazaraLog.log for further informations" << std::endl; std::getchar(); // On laise le temps de voir l'erreur return EXIT_FAILURE; - } + }*/ for (;;) { diff --git a/examples/RenderTest/build.lua b/examples/RenderTest/build.lua index b50c2131b..dc1eedcb4 100644 --- a/examples/RenderTest/build.lua +++ b/examples/RenderTest/build.lua @@ -10,5 +10,6 @@ EXAMPLE.Libraries = { "NazaraCore", "NazaraPlatform", "NazaraRenderer", + "NazaraShader", "NazaraUtility" } diff --git a/examples/RenderTest/main.cpp b/examples/RenderTest/main.cpp index 541fa2b2f..cef3ecf51 100644 --- a/examples/RenderTest/main.cpp +++ b/examples/RenderTest/main.cpp @@ -8,12 +8,7 @@ int main() { - Nz::Initializer loader; - if (!loader) - { - std::cout << "Failed to initialize Vulkan" << std::endl; - return __LINE__; - } + Nz::Modules nazara; Nz::RenderWindow window; diff --git a/include/Nazara/Audio/Audio.hpp b/include/Nazara/Audio/Audio.hpp index 5ed861d74..87539a8fd 100644 --- a/include/Nazara/Audio/Audio.hpp +++ b/include/Nazara/Audio/Audio.hpp @@ -20,9 +20,9 @@ namespace Nz { friend ModuleBase; - using Dependencies = TypeList; - public: + using Dependencies = TypeList; + Audio(); ~Audio(); diff --git a/include/Nazara/Renderer/Renderer.hpp b/include/Nazara/Renderer/Renderer.hpp index fe6a2c202..efcc089ad 100644 --- a/include/Nazara/Renderer/Renderer.hpp +++ b/include/Nazara/Renderer/Renderer.hpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace Nz { @@ -23,7 +24,7 @@ namespace Nz friend ModuleBase; public: - using Dependencies = TypeList; + using Dependencies = TypeList; Renderer(); ~Renderer(); diff --git a/include/NazaraSDK/Application.inl b/include/NazaraSDK/Application.inl index d74378cfd..68dd600af 100644 --- a/include/NazaraSDK/Application.inl +++ b/include/NazaraSDK/Application.inl @@ -23,11 +23,6 @@ namespace Ndk { NazaraAssert(s_application == nullptr, "You can create only one application instance per program"); s_application = this; - - Nz::ErrorFlags errFlags(Nz::ErrorFlag_ThrowException, true); - - // Initialisation du SDK - Sdk::Initialize(); } /*! @@ -42,9 +37,6 @@ namespace Ndk m_windows.clear(); #endif - // Free of SDK - Sdk::Uninitialize(); - // Automatic free of modules s_application = nullptr; } diff --git a/include/NazaraSDK/Sdk.hpp b/include/NazaraSDK/Sdk.hpp index 787eb29fc..d7668f3b0 100644 --- a/include/NazaraSDK/Sdk.hpp +++ b/include/NazaraSDK/Sdk.hpp @@ -7,22 +7,41 @@ #ifndef NDK_SDK_HPP #define NDK_SDK_HPP +#include +#include #include +#include +#include +#include +#include +#include + +#ifndef NDK_SERVER +#include +#include +#endif + namespace Ndk { - class NDK_API Sdk + class NDK_API Sdk : public Nz::ModuleBase { - public: - Sdk() = delete; - ~Sdk() = delete; + friend ModuleBase; - static bool Initialize(); - static bool IsInitialized(); - static void Uninitialize(); + public: + using CommonDependencies = Nz::TypeList; +#ifdef NDK_SERVER + using Dependencies = CommonDependencies; +#else + using ClientDependencies = Nz::TypeList; + using Dependencies = Nz::TypeListConcat; +#endif + + Sdk(); + ~Sdk(); private: - static unsigned int s_referenceCounter; + static Sdk* s_instance; }; } diff --git a/include/NazaraSDK/Sdk.inl b/include/NazaraSDK/Sdk.inl index 42d92d06e..de8e25de8 100644 --- a/include/NazaraSDK/Sdk.inl +++ b/include/NazaraSDK/Sdk.inl @@ -4,13 +4,4 @@ namespace Ndk { - /*! - * \brief Checks whether the module is initialized - * \return true if module is initialized - */ - - inline bool Sdk::IsInitialized() - { - return s_referenceCounter != 0; - } } diff --git a/src/NazaraSDK/Sdk.cpp b/src/NazaraSDK/Sdk.cpp index 8a14e30a9..0fc94559c 100644 --- a/src/NazaraSDK/Sdk.cpp +++ b/src/NazaraSDK/Sdk.cpp @@ -3,13 +3,7 @@ // For conditions of distribution and use, see copyright notice in Prerequisites.hpp #include -#include #include -#include -#include -#include -#include -#include #include #include #include @@ -38,122 +32,55 @@ namespace Ndk * \brief NDK class that represents the software development kit, a set of tools made to ease the conception of application */ - /*! - * \brief Initializes the Sdk module - * \return true if initialization is successful - * - * \remark Produces a NazaraNotice - */ - - bool Sdk::Initialize() + Sdk::Sdk() : + ModuleBase("SDK", this) { - if (s_referenceCounter++ > 0) - return true; // Already initialized + Nz::ErrorFlags errFlags(Nz::ErrorFlag_ThrowException, true); - try - { - Nz::ErrorFlags errFlags(Nz::ErrorFlag_ThrowException, true); + // SDK Initialization - // Initialize the engine first + // Components + BaseComponent::Initialize(); - // Shared modules - Nz::Physics2D::Initialize(); - Nz::Physics3D::Initialize(); - Nz::Utility::Initialize(); + // Shared components + InitializeComponent("NdkColl2"); + InitializeComponent("NdkColl3"); + InitializeComponent("NdkLiftm"); + InitializeComponent("NdkNode"); + InitializeComponent("NdkPhys2"); + InitializeComponent("NdkPhys3"); + InitializeComponent("NdkVeloc"); + InitializeComponent("NdkCons2"); - #ifndef NDK_SERVER - // Client modules - Nz::Audio::Initialize(); - #endif + #ifndef NDK_SERVER + // Client components + InitializeComponent("NdkList"); + #endif - // SDK Initialization + // Systems - // Components - BaseComponent::Initialize(); + BaseSystem::Initialize(); - // Shared components - InitializeComponent("NdkColl2"); - InitializeComponent("NdkColl3"); - InitializeComponent("NdkLiftm"); - InitializeComponent("NdkNode"); - InitializeComponent("NdkPhys2"); - InitializeComponent("NdkPhys3"); - InitializeComponent("NdkVeloc"); - InitializeComponent("NdkCons2"); + // Shared systems + InitializeSystem(); + InitializeSystem(); + InitializeSystem(); + InitializeSystem(); - #ifndef NDK_SERVER - // Client components - InitializeComponent("NdkList"); - #endif - - // Systems - - BaseSystem::Initialize(); - - // Shared systems - InitializeSystem(); - InitializeSystem(); - InitializeSystem(); - InitializeSystem(); - - #ifndef NDK_SERVER - // Client systems - InitializeSystem(); - #endif - - NazaraNotice("Initialized: SDK"); - return true; - } - catch (const std::exception& e) - { - NazaraError("Failed to initialize NDK: " + Nz::String(e.what())); - return false; - } + #ifndef NDK_SERVER + // Client systems + InitializeSystem(); + #endif } - /*! - * \brief Uninitializes the Sdk module - * - * \remark Produces a NazaraNotice - */ - - void Sdk::Uninitialize() + Sdk::~Sdk() { - if (s_referenceCounter != 1) - { - // Either the module is not initialized, either it was initialized multiple times - if (s_referenceCounter > 1) - s_referenceCounter--; - - return; - } - - // Uninitialize the SDK - s_referenceCounter = 0; - // Components BaseComponent::Uninitialize(); // Systems BaseSystem::Uninitialize(); - - // Uninitialize the engine - - #ifndef NDK_SERVER - // Client modules - Nz::Audio::Uninitialize(); - #endif - - // Shared modules - Nz::Physics2D::Uninitialize(); - Nz::Physics3D::Uninitialize(); - Nz::Utility::Uninitialize(); - - #ifndef NDK_SERVER - #endif - - NazaraNotice("Uninitialized: SDK"); } - unsigned int Sdk::s_referenceCounter = 0; + Sdk* Sdk::s_instance; } diff --git a/src/NazaraSDK/Systems/ListenerSystem.cpp b/src/NazaraSDK/Systems/ListenerSystem.cpp index 314d2b4e1..a197d7c78 100644 --- a/src/NazaraSDK/Systems/ListenerSystem.cpp +++ b/src/NazaraSDK/Systems/ListenerSystem.cpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace Ndk { @@ -37,6 +38,9 @@ namespace Ndk { std::size_t activeListenerCount = 0; + Nz::Audio* audio = Nz::Audio::Instance(); + assert(audio); + for (const Ndk::EntityHandle& entity : GetEntities()) { // Is the listener actif ? @@ -44,18 +48,18 @@ namespace Ndk if (!listener.IsActive()) continue; - Nz::Vector3f oldPos = Nz::Audio::GetListenerPosition(); + Nz::Vector3f oldPos = audio->GetListenerPosition(); // We get the position and the rotation to affect these to the listener const NodeComponent& node = entity->GetComponent(); Nz::Vector3f newPos = node.GetPosition(Nz::CoordSys_Global); - Nz::Audio::SetListenerPosition(newPos); - Nz::Audio::SetListenerRotation(node.GetRotation(Nz::CoordSys_Global)); + audio->SetListenerPosition(newPos); + audio->SetListenerRotation(node.GetRotation(Nz::CoordSys_Global)); // Compute listener velocity based on their old/new position Nz::Vector3f velocity = (newPos - oldPos) / elapsedTime; - Nz::Audio::SetListenerVelocity(velocity); + audio->SetListenerVelocity(velocity); activeListenerCount++; } diff --git a/tests/Engine/Audio/Music.cpp b/tests/Engine/Audio/Music.cpp index 262e87bc6..14cc2ede5 100644 --- a/tests/Engine/Audio/Music.cpp +++ b/tests/Engine/Audio/Music.cpp @@ -29,7 +29,7 @@ SCENARIO("Music", "[AUDIO][MUSIC]") THEN("We can play it and get the time offset") { - Nz::Audio::SetGlobalVolume(0.f); + Nz::Audio::Instance()->SetGlobalVolume(0.f); music.Play(); std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -42,7 +42,7 @@ SCENARIO("Music", "[AUDIO][MUSIC]") music.SetPlayingOffset(3500); REQUIRE(music.GetPlayingOffset() >= 3500); - Nz::Audio::SetGlobalVolume(100.f); + Nz::Audio::Instance()->SetGlobalVolume(100.f); } } } diff --git a/tests/Engine/Audio/Sound.cpp b/tests/Engine/Audio/Sound.cpp index aa58bd537..ec0f655b4 100644 --- a/tests/Engine/Audio/Sound.cpp +++ b/tests/Engine/Audio/Sound.cpp @@ -24,7 +24,7 @@ SCENARIO("Sound", "[AUDIO][SOUND]") THEN("We can play it and get the time offset") { - Nz::Audio::SetGlobalVolume(0.f); + Nz::Audio::Instance()->SetGlobalVolume(0.f); sound.Play(); std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -37,7 +37,7 @@ SCENARIO("Sound", "[AUDIO][SOUND]") sound.SetPlayingOffset(3500); REQUIRE(sound.GetPlayingOffset() >= 3500); - Nz::Audio::SetGlobalVolume(100.f); + Nz::Audio::Instance()->SetGlobalVolume(100.f); } } } diff --git a/tests/SDK/NDK/Systems/ListenerSystem.cpp b/tests/SDK/NDK/Systems/ListenerSystem.cpp index 85a8f8fb1..2415bc62d 100644 --- a/tests/SDK/NDK/Systems/ListenerSystem.cpp +++ b/tests/SDK/NDK/Systems/ListenerSystem.cpp @@ -25,8 +25,8 @@ SCENARIO("ListenerSystem", "[NDK][LISTENERSYSTEM]") THEN("Our listener should have moved") { - REQUIRE(Nz::Audio::GetListenerPosition() == position); - REQUIRE(Nz::Audio::GetListenerRotation() == rotation); + REQUIRE(Nz::Audio::Instance()->GetListenerPosition() == position); + REQUIRE(Nz::Audio::Instance()->GetListenerRotation() == rotation); } THEN("With a component of velocity") @@ -36,8 +36,8 @@ SCENARIO("ListenerSystem", "[NDK][LISTENERSYSTEM]") velocityComponent.linearVelocity = velocity; world.Update(1.f); - REQUIRE(Nz::Audio::GetListenerVelocity() == velocity); + REQUIRE(Nz::Audio::Instance()->GetListenerVelocity() == velocity); } } } -} \ No newline at end of file +} diff --git a/tests/main.cpp b/tests/main.cpp index 0e4581f17..9ca2bc9b1 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,16 +1,16 @@ #define CATCH_CONFIG_RUNNER #include -#include -#include -#include #include -#include +#include +#include +#include +#include int main(int argc, char* argv[]) { - Ndk::Application application(argc, argv); - Nz::Initializer modules; + Nz::Modules nazaza; + Ndk::Application app(argc, argv); Nz::Log::GetLogger()->EnableStdReplication(false);