Last modules fixes

This commit is contained in:
Jérôme Leclercq 2020-09-17 18:54:33 +02:00
parent 631aeb77a2
commit 98e20ecbb7
15 changed files with 95 additions and 163 deletions

View File

@ -10,6 +10,7 @@
#include <Nazara/Audio.hpp> #include <Nazara/Audio.hpp>
#include <Nazara/Core/Clock.hpp> #include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/Modules.hpp>
#include <Nazara/Math/Vector3.hpp> #include <Nazara/Math/Vector3.hpp>
#include <Nazara/Platform/Keyboard.hpp> #include <Nazara/Platform/Keyboard.hpp>
#include <Nazara/Platform/Platform.hpp> #include <Nazara/Platform/Platform.hpp>
@ -20,13 +21,13 @@
int main() int main()
{ {
// NzKeyboard nécessite l'initialisation du module Utilitaire // NzKeyboard nécessite l'initialisation du module Utilitaire
Nz::Initializer<Nz::Audio, Nz::Platform> audio; Nz::Modules<Nz::Audio, Nz::Platform> audio;
if (!audio) /*if (!audio)
{ {
std::cout << "Failed to initialize audio module" << std::endl; std::cout << "Failed to initialize audio module" << std::endl;
std::getchar(); std::getchar();
return 1; return 1;
} }*/
Nz::Sound sound; Nz::Sound sound;
if (!sound.LoadFromFile("resources/siren.wav")) if (!sound.LoadFromFile("resources/siren.wav"))

View File

@ -1,4 +1,5 @@
#include <Nazara/Core/File.hpp> #include <Nazara/Core/File.hpp>
#include <Nazara/Core/Modules.hpp>
#include <Nazara/Math/Box.hpp> #include <Nazara/Math/Box.hpp>
#include <Nazara/Utility/Animation.hpp> #include <Nazara/Utility/Animation.hpp>
#include <Nazara/Utility/Joint.hpp> #include <Nazara/Utility/Joint.hpp>
@ -14,14 +15,14 @@
int main() int main()
{ {
// Pour charger des ressources, il est impératif d'initialiser le module utilitaire // Pour charger des ressources, il est impératif d'initialiser le module utilitaire
Nz::Initializer<Nz::Utility> utility; Nz::Modules<Nz::Utility> nazara;
if (!utility) /*if (!utility)
{ {
// Ça n'a pas fonctionné, le pourquoi se trouve dans le fichier NazaraLog.log // Ç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::cout << "Failed to initialize Nazara, see NazaraLog.log for further informations" << std::endl;
std::getchar(); // On laise le temps de voir l'erreur std::getchar(); // On laise le temps de voir l'erreur
return EXIT_FAILURE; return EXIT_FAILURE;
} }*/
for (;;) for (;;)
{ {

View File

@ -10,5 +10,6 @@ EXAMPLE.Libraries = {
"NazaraCore", "NazaraCore",
"NazaraPlatform", "NazaraPlatform",
"NazaraRenderer", "NazaraRenderer",
"NazaraShader",
"NazaraUtility" "NazaraUtility"
} }

View File

@ -8,12 +8,7 @@
int main() int main()
{ {
Nz::Initializer<Nz::Renderer> loader; Nz::Modules<Nz::Renderer> nazara;
if (!loader)
{
std::cout << "Failed to initialize Vulkan" << std::endl;
return __LINE__;
}
Nz::RenderWindow window; Nz::RenderWindow window;

View File

@ -20,9 +20,9 @@ namespace Nz
{ {
friend ModuleBase; friend ModuleBase;
using Dependencies = TypeList<Core>;
public: public:
using Dependencies = TypeList<Core>;
Audio(); Audio();
~Audio(); ~Audio();

View File

@ -12,6 +12,7 @@
#include <Nazara/Platform/Platform.hpp> #include <Nazara/Platform/Platform.hpp>
#include <Nazara/Renderer/Config.hpp> #include <Nazara/Renderer/Config.hpp>
#include <Nazara/Renderer/RendererImpl.hpp> #include <Nazara/Renderer/RendererImpl.hpp>
#include <Nazara/Shader/Shader.hpp>
namespace Nz namespace Nz
{ {
@ -23,7 +24,7 @@ namespace Nz
friend ModuleBase; friend ModuleBase;
public: public:
using Dependencies = TypeList<Platform>; using Dependencies = TypeList<Platform, Shader>;
Renderer(); Renderer();
~Renderer(); ~Renderer();

View File

@ -23,11 +23,6 @@ namespace Ndk
{ {
NazaraAssert(s_application == nullptr, "You can create only one application instance per program"); NazaraAssert(s_application == nullptr, "You can create only one application instance per program");
s_application = this; s_application = this;
Nz::ErrorFlags errFlags(Nz::ErrorFlag_ThrowException, true);
// Initialisation du SDK
Sdk::Initialize();
} }
/*! /*!
@ -42,9 +37,6 @@ namespace Ndk
m_windows.clear(); m_windows.clear();
#endif #endif
// Free of SDK
Sdk::Uninitialize();
// Automatic free of modules // Automatic free of modules
s_application = nullptr; s_application = nullptr;
} }

View File

@ -7,22 +7,41 @@
#ifndef NDK_SDK_HPP #ifndef NDK_SDK_HPP
#define NDK_SDK_HPP #define NDK_SDK_HPP
#include <Nazara/Core/ModuleBase.hpp>
#include <Nazara/Core/TypeList.hpp>
#include <NazaraSDK/Prerequisites.hpp> #include <NazaraSDK/Prerequisites.hpp>
#include <Nazara/Network/Network.hpp>
#include <Nazara/Physics2D/Physics2D.hpp>
#include <Nazara/Physics3D/Physics3D.hpp>
#include <Nazara/Shader/Shader.hpp>
#include <Nazara/Utility/Utility.hpp>
#ifndef NDK_SERVER
#include <Nazara/Audio/Audio.hpp>
#include <Nazara/Renderer/Renderer.hpp>
#endif
namespace Ndk namespace Ndk
{ {
class NDK_API Sdk class NDK_API Sdk : public Nz::ModuleBase<Sdk>
{ {
public: friend ModuleBase;
Sdk() = delete;
~Sdk() = delete;
static bool Initialize(); public:
static bool IsInitialized(); using CommonDependencies = Nz::TypeList<Nz::Network, Nz::Physics2D, Nz::Physics3D, Nz::Utility>;
static void Uninitialize(); #ifdef NDK_SERVER
using Dependencies = CommonDependencies;
#else
using ClientDependencies = Nz::TypeList<Nz::Audio, Nz::Renderer>;
using Dependencies = Nz::TypeListConcat<CommonDependencies, ClientDependencies>;
#endif
Sdk();
~Sdk();
private: private:
static unsigned int s_referenceCounter; static Sdk* s_instance;
}; };
} }

View File

@ -4,13 +4,4 @@
namespace Ndk namespace Ndk
{ {
/*!
* \brief Checks whether the module is initialized
* \return true if module is initialized
*/
inline bool Sdk::IsInitialized()
{
return s_referenceCounter != 0;
}
} }

View File

@ -3,13 +3,7 @@
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp // For conditions of distribution and use, see copyright notice in Prerequisites.hpp
#include <NazaraSDK/Sdk.hpp> #include <NazaraSDK/Sdk.hpp>
#include <Nazara/Audio/Audio.hpp>
#include <Nazara/Core/ErrorFlags.hpp> #include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Physics2D/Physics2D.hpp>
#include <Nazara/Physics3D/Physics3D.hpp>
#include <Nazara/Platform/Platform.hpp>
#include <Nazara/Utility/Utility.hpp>
#include <NazaraSDK/Algorithm.hpp> #include <NazaraSDK/Algorithm.hpp>
#include <NazaraSDK/BaseSystem.hpp> #include <NazaraSDK/BaseSystem.hpp>
#include <NazaraSDK/Components/CollisionComponent2D.hpp> #include <NazaraSDK/Components/CollisionComponent2D.hpp>
@ -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 NDK class that represents the software development kit, a set of tools made to ease the conception of application
*/ */
/*! Sdk::Sdk() :
* \brief Initializes the Sdk module ModuleBase("SDK", this)
* \return true if initialization is successful
*
* \remark Produces a NazaraNotice
*/
bool Sdk::Initialize()
{ {
if (s_referenceCounter++ > 0) Nz::ErrorFlags errFlags(Nz::ErrorFlag_ThrowException, true);
return true; // Already initialized
try // SDK Initialization
{
Nz::ErrorFlags errFlags(Nz::ErrorFlag_ThrowException, true);
// Initialize the engine first // Components
BaseComponent::Initialize();
// Shared modules // Shared components
Nz::Physics2D::Initialize(); InitializeComponent<CollisionComponent2D>("NdkColl2");
Nz::Physics3D::Initialize(); InitializeComponent<CollisionComponent3D>("NdkColl3");
Nz::Utility::Initialize(); InitializeComponent<LifetimeComponent>("NdkLiftm");
InitializeComponent<NodeComponent>("NdkNode");
InitializeComponent<PhysicsComponent2D>("NdkPhys2");
InitializeComponent<PhysicsComponent3D>("NdkPhys3");
InitializeComponent<VelocityComponent>("NdkVeloc");
InitializeComponent<VelocityComponent>("NdkCons2");
#ifndef NDK_SERVER #ifndef NDK_SERVER
// Client modules // Client components
Nz::Audio::Initialize(); InitializeComponent<ListenerComponent>("NdkList");
#endif #endif
// SDK Initialization // Systems
// Components BaseSystem::Initialize();
BaseComponent::Initialize();
// Shared components // Shared systems
InitializeComponent<CollisionComponent2D>("NdkColl2"); InitializeSystem<LifetimeSystem>();
InitializeComponent<CollisionComponent3D>("NdkColl3"); InitializeSystem<PhysicsSystem2D>();
InitializeComponent<LifetimeComponent>("NdkLiftm"); InitializeSystem<PhysicsSystem3D>();
InitializeComponent<NodeComponent>("NdkNode"); InitializeSystem<VelocitySystem>();
InitializeComponent<PhysicsComponent2D>("NdkPhys2");
InitializeComponent<PhysicsComponent3D>("NdkPhys3");
InitializeComponent<VelocityComponent>("NdkVeloc");
InitializeComponent<VelocityComponent>("NdkCons2");
#ifndef NDK_SERVER #ifndef NDK_SERVER
// Client components // Client systems
InitializeComponent<ListenerComponent>("NdkList"); InitializeSystem<ListenerSystem>();
#endif #endif
// Systems
BaseSystem::Initialize();
// Shared systems
InitializeSystem<LifetimeSystem>();
InitializeSystem<PhysicsSystem2D>();
InitializeSystem<PhysicsSystem3D>();
InitializeSystem<VelocitySystem>();
#ifndef NDK_SERVER
// Client systems
InitializeSystem<ListenerSystem>();
#endif
NazaraNotice("Initialized: SDK");
return true;
}
catch (const std::exception& e)
{
NazaraError("Failed to initialize NDK: " + Nz::String(e.what()));
return false;
}
} }
/*! Sdk::~Sdk()
* \brief Uninitializes the Sdk module
*
* \remark Produces a NazaraNotice
*/
void Sdk::Uninitialize()
{ {
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 // Components
BaseComponent::Uninitialize(); BaseComponent::Uninitialize();
// Systems // Systems
BaseSystem::Uninitialize(); 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;
} }

View File

@ -6,6 +6,7 @@
#include <Nazara/Audio/Audio.hpp> #include <Nazara/Audio/Audio.hpp>
#include <NazaraSDK/Components/ListenerComponent.hpp> #include <NazaraSDK/Components/ListenerComponent.hpp>
#include <NazaraSDK/Components/NodeComponent.hpp> #include <NazaraSDK/Components/NodeComponent.hpp>
#include <cassert>
namespace Ndk namespace Ndk
{ {
@ -37,6 +38,9 @@ namespace Ndk
{ {
std::size_t activeListenerCount = 0; std::size_t activeListenerCount = 0;
Nz::Audio* audio = Nz::Audio::Instance();
assert(audio);
for (const Ndk::EntityHandle& entity : GetEntities()) for (const Ndk::EntityHandle& entity : GetEntities())
{ {
// Is the listener actif ? // Is the listener actif ?
@ -44,18 +48,18 @@ namespace Ndk
if (!listener.IsActive()) if (!listener.IsActive())
continue; 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 // We get the position and the rotation to affect these to the listener
const NodeComponent& node = entity->GetComponent<NodeComponent>(); const NodeComponent& node = entity->GetComponent<NodeComponent>();
Nz::Vector3f newPos = node.GetPosition(Nz::CoordSys_Global); Nz::Vector3f newPos = node.GetPosition(Nz::CoordSys_Global);
Nz::Audio::SetListenerPosition(newPos); audio->SetListenerPosition(newPos);
Nz::Audio::SetListenerRotation(node.GetRotation(Nz::CoordSys_Global)); audio->SetListenerRotation(node.GetRotation(Nz::CoordSys_Global));
// Compute listener velocity based on their old/new position // Compute listener velocity based on their old/new position
Nz::Vector3f velocity = (newPos - oldPos) / elapsedTime; Nz::Vector3f velocity = (newPos - oldPos) / elapsedTime;
Nz::Audio::SetListenerVelocity(velocity); audio->SetListenerVelocity(velocity);
activeListenerCount++; activeListenerCount++;
} }

View File

@ -29,7 +29,7 @@ SCENARIO("Music", "[AUDIO][MUSIC]")
THEN("We can play it and get the time offset") THEN("We can play it and get the time offset")
{ {
Nz::Audio::SetGlobalVolume(0.f); Nz::Audio::Instance()->SetGlobalVolume(0.f);
music.Play(); music.Play();
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
@ -42,7 +42,7 @@ SCENARIO("Music", "[AUDIO][MUSIC]")
music.SetPlayingOffset(3500); music.SetPlayingOffset(3500);
REQUIRE(music.GetPlayingOffset() >= 3500); REQUIRE(music.GetPlayingOffset() >= 3500);
Nz::Audio::SetGlobalVolume(100.f); Nz::Audio::Instance()->SetGlobalVolume(100.f);
} }
} }
} }

View File

@ -24,7 +24,7 @@ SCENARIO("Sound", "[AUDIO][SOUND]")
THEN("We can play it and get the time offset") THEN("We can play it and get the time offset")
{ {
Nz::Audio::SetGlobalVolume(0.f); Nz::Audio::Instance()->SetGlobalVolume(0.f);
sound.Play(); sound.Play();
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
@ -37,7 +37,7 @@ SCENARIO("Sound", "[AUDIO][SOUND]")
sound.SetPlayingOffset(3500); sound.SetPlayingOffset(3500);
REQUIRE(sound.GetPlayingOffset() >= 3500); REQUIRE(sound.GetPlayingOffset() >= 3500);
Nz::Audio::SetGlobalVolume(100.f); Nz::Audio::Instance()->SetGlobalVolume(100.f);
} }
} }
} }

View File

@ -25,8 +25,8 @@ SCENARIO("ListenerSystem", "[NDK][LISTENERSYSTEM]")
THEN("Our listener should have moved") THEN("Our listener should have moved")
{ {
REQUIRE(Nz::Audio::GetListenerPosition() == position); REQUIRE(Nz::Audio::Instance()->GetListenerPosition() == position);
REQUIRE(Nz::Audio::GetListenerRotation() == rotation); REQUIRE(Nz::Audio::Instance()->GetListenerRotation() == rotation);
} }
THEN("With a component of velocity") THEN("With a component of velocity")
@ -36,8 +36,8 @@ SCENARIO("ListenerSystem", "[NDK][LISTENERSYSTEM]")
velocityComponent.linearVelocity = velocity; velocityComponent.linearVelocity = velocity;
world.Update(1.f); world.Update(1.f);
REQUIRE(Nz::Audio::GetListenerVelocity() == velocity); REQUIRE(Nz::Audio::Instance()->GetListenerVelocity() == velocity);
} }
} }
} }
} }

View File

@ -1,16 +1,16 @@
#define CATCH_CONFIG_RUNNER #define CATCH_CONFIG_RUNNER
#include <Catch/catch.hpp> #include <Catch/catch.hpp>
#include <NazaraSDK/Application.hpp>
#include <Nazara/Core/AbstractLogger.hpp>
#include <Nazara/Core/Initializer.hpp>
#include <Nazara/Core/Log.hpp> #include <Nazara/Core/Log.hpp>
#include <Nazara/Network/Network.hpp> #include <Nazara/Core/AbstractLogger.hpp>
#include <Nazara/Core/Modules.hpp>
#include <NazaraSDK/Application.hpp>
#include <NazaraSDK/Sdk.hpp>
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
Ndk::Application application(argc, argv); Nz::Modules<Ndk::Sdk> nazaza;
Nz::Initializer<Nz::Network> modules; Ndk::Application app(argc, argv);
Nz::Log::GetLogger()->EnableStdReplication(false); Nz::Log::GetLogger()->EnableStdReplication(false);