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/Core/Clock.hpp>
#include <Nazara/Core/Modules.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Platform/Keyboard.hpp>
#include <Nazara/Platform/Platform.hpp>
@ -20,13 +21,13 @@
int main()
{
// NzKeyboard nécessite l'initialisation du module Utilitaire
Nz::Initializer<Nz::Audio, Nz::Platform> audio;
if (!audio)
Nz::Modules<Nz::Audio, Nz::Platform> 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"))

View File

@ -1,4 +1,5 @@
#include <Nazara/Core/File.hpp>
#include <Nazara/Core/Modules.hpp>
#include <Nazara/Math/Box.hpp>
#include <Nazara/Utility/Animation.hpp>
#include <Nazara/Utility/Joint.hpp>
@ -14,14 +15,14 @@
int main()
{
// Pour charger des ressources, il est impératif d'initialiser le module utilitaire
Nz::Initializer<Nz::Utility> utility;
if (!utility)
Nz::Modules<Nz::Utility> 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 (;;)
{

View File

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

View File

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

View File

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

View File

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

View File

@ -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;
}

View File

@ -7,22 +7,41 @@
#ifndef NDK_SDK_HPP
#define NDK_SDK_HPP
#include <Nazara/Core/ModuleBase.hpp>
#include <Nazara/Core/TypeList.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
{
class NDK_API Sdk
class NDK_API Sdk : public Nz::ModuleBase<Sdk>
{
public:
Sdk() = delete;
~Sdk() = delete;
friend ModuleBase;
static bool Initialize();
static bool IsInitialized();
static void Uninitialize();
public:
using CommonDependencies = Nz::TypeList<Nz::Network, Nz::Physics2D, Nz::Physics3D, Nz::Utility>;
#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:
static unsigned int s_referenceCounter;
static Sdk* s_instance;
};
}

View File

@ -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;
}
}

View File

@ -3,13 +3,7 @@
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
#include <NazaraSDK/Sdk.hpp>
#include <Nazara/Audio/Audio.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/BaseSystem.hpp>
#include <NazaraSDK/Components/CollisionComponent2D.hpp>
@ -38,34 +32,11 @@ 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()
{
if (s_referenceCounter++ > 0)
return true; // Already initialized
try
Sdk::Sdk() :
ModuleBase("SDK", this)
{
Nz::ErrorFlags errFlags(Nz::ErrorFlag_ThrowException, true);
// Initialize the engine first
// Shared modules
Nz::Physics2D::Initialize();
Nz::Physics3D::Initialize();
Nz::Utility::Initialize();
#ifndef NDK_SERVER
// Client modules
Nz::Audio::Initialize();
#endif
// SDK Initialization
// Components
@ -100,60 +71,16 @@ namespace Ndk
// Client systems
InitializeSystem<ListenerSystem>();
#endif
NazaraNotice("Initialized: SDK");
return true;
}
catch (const std::exception& e)
Sdk::~Sdk()
{
NazaraError("Failed to initialize NDK: " + Nz::String(e.what()));
return false;
}
}
/*!
* \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
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;
}

View File

@ -6,6 +6,7 @@
#include <Nazara/Audio/Audio.hpp>
#include <NazaraSDK/Components/ListenerComponent.hpp>
#include <NazaraSDK/Components/NodeComponent.hpp>
#include <cassert>
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<NodeComponent>();
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++;
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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,7 +36,7 @@ SCENARIO("ListenerSystem", "[NDK][LISTENERSYSTEM]")
velocityComponent.linearVelocity = velocity;
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
#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/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[])
{
Ndk::Application application(argc, argv);
Nz::Initializer<Nz::Network> modules;
Nz::Modules<Ndk::Sdk> nazaza;
Ndk::Application app(argc, argv);
Nz::Log::GetLogger()->EnableStdReplication(false);