diff --git a/SDK/include/NDK/Algorithm.hpp b/SDK/include/NDK/Algorithm.hpp index 1a7dc52ab..c98e63c6c 100644 --- a/SDK/include/NDK/Algorithm.hpp +++ b/SDK/include/NDK/Algorithm.hpp @@ -14,6 +14,8 @@ namespace Ndk template ComponentId BuildComponentId(const char (&name)[N]); template constexpr ComponentIndex GetComponentIndex(); template constexpr SystemIndex GetSystemIndex(); + template ComponentIndex InitializeComponent(const char (&name)[N]); + template SystemIndex InitializeSystem(); } #include diff --git a/SDK/include/NDK/Algorithm.inl b/SDK/include/NDK/Algorithm.inl index f609af717..2f2586868 100644 --- a/SDK/include/NDK/Algorithm.inl +++ b/SDK/include/NDK/Algorithm.inl @@ -30,4 +30,18 @@ namespace Ndk { return SystemType::systemIndex; } + + template + ComponentIndex InitializeComponent(const char (&name)[N]) + { + ComponentType::componentIndex = ComponentType::RegisterComponent(name); + return ComponentType::componentIndex; + } + + template + SystemIndex InitializeSystem() + { + SystemType::systemIndex = SystemType::RegisterSystem(); + return SystemType::systemIndex; + } } diff --git a/SDK/include/NDK/BaseComponent.hpp b/SDK/include/NDK/BaseComponent.hpp index f4b3a80bc..92b3b198b 100644 --- a/SDK/include/NDK/BaseComponent.hpp +++ b/SDK/include/NDK/BaseComponent.hpp @@ -16,6 +16,8 @@ namespace Ndk { class NDK_API BaseComponent { + friend class Sdk; + public: using Factory = std::function; @@ -32,6 +34,9 @@ namespace Ndk static ComponentIndex RegisterComponent(ComponentId id, Factory factoryFunc); private: + static bool Initialize(); + static void Uninitialize(); + struct ComponentEntry { ComponentId id; diff --git a/SDK/include/NDK/BaseComponent.inl b/SDK/include/NDK/BaseComponent.inl index de0ecd643..0ac1678e0 100644 --- a/SDK/include/NDK/BaseComponent.inl +++ b/SDK/include/NDK/BaseComponent.inl @@ -34,4 +34,16 @@ namespace Ndk return index; } + + inline bool BaseComponent::Initialize() + { + // Rien à faire + return true; + } + + inline void BaseComponent::Uninitialize() + { + s_entries.clear(); + s_idToIndex.clear(); + } } diff --git a/SDK/include/NDK/BaseSystem.hpp b/SDK/include/NDK/BaseSystem.hpp index b68b13472..ee03bf1f2 100644 --- a/SDK/include/NDK/BaseSystem.hpp +++ b/SDK/include/NDK/BaseSystem.hpp @@ -17,7 +17,8 @@ namespace Ndk class NDK_API BaseSystem { - friend class Entity; + friend class Sdk; + friend Entity; friend World; public: @@ -55,6 +56,9 @@ namespace Ndk void SetWorld(World& world); + static bool Initialize(); + static void Uninitialize(); + std::vector m_entities; NzBitset m_entityBits; NzBitset<> m_excludedComponents; diff --git a/SDK/include/NDK/BaseSystem.inl b/SDK/include/NDK/BaseSystem.inl index 5b44ec006..95a091b83 100644 --- a/SDK/include/NDK/BaseSystem.inl +++ b/SDK/include/NDK/BaseSystem.inl @@ -113,4 +113,17 @@ namespace Ndk { m_world = &world; } + + inline bool BaseSystem::Initialize() + { + s_nextIndex = 0; + + return true; + } + + inline void BaseSystem::Uninitialize() + { + // Rien à faire + } + } diff --git a/SDK/include/NDK/Sdk.hpp b/SDK/include/NDK/Sdk.hpp new file mode 100644 index 000000000..7883162ea --- /dev/null +++ b/SDK/include/NDK/Sdk.hpp @@ -0,0 +1,31 @@ +// 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_SDK_HPP +#define NDK_SDK_HPP + +#include + +namespace Ndk +{ + class NDK_API Sdk + { + public: + Sdk() = delete; + ~Sdk() = delete; + + static bool Initialize(); + static bool IsInitialized(); + static void Uninitialize(); + + private: + static unsigned int s_referenceCounter; + }; +} + +#include + +#endif // NDK_SDK_HPP diff --git a/SDK/include/NDK/Sdk.inl b/SDK/include/NDK/Sdk.inl new file mode 100644 index 000000000..0bdc1e2cd --- /dev/null +++ b/SDK/include/NDK/Sdk.inl @@ -0,0 +1,11 @@ +// 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 bool Sdk::IsInitialized() + { + return s_referenceCounter != 0; + } +} diff --git a/SDK/src/NDK/BaseSystem.cpp b/SDK/src/NDK/BaseSystem.cpp index aadb63e4c..6c60f435c 100644 --- a/SDK/src/NDK/BaseSystem.cpp +++ b/SDK/src/NDK/BaseSystem.cpp @@ -40,5 +40,5 @@ namespace Ndk NazaraUnused(entity); } - SystemIndex BaseSystem::s_nextIndex = 0; + SystemIndex BaseSystem::s_nextIndex; } diff --git a/SDK/src/NDK/Sdk.cpp b/SDK/src/NDK/Sdk.cpp new file mode 100644 index 000000000..f7cb1c5ca --- /dev/null +++ b/SDK/src/NDK/Sdk.cpp @@ -0,0 +1,89 @@ +// 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 +#include +#include +#include +#include +#include +#include +#include + +namespace Ndk +{ + bool Sdk::Initialize() + { + if (s_referenceCounter++ > 0) + return true; // Déjà initialisé + + try + { + NzErrorFlags errFlags(nzErrorFlag_ThrowException, true); + + // Initialisation du moteur + + // Modules clients + NzAudio::Initialize(); + NzGraphics::Initialize(); + + // Modules serveurs + NzLua::Initialize(); + NzNoise::Initialize(); + NzPhysics::Initialize(); + NzUtility::Initialize(); + + // Initialisation du SDK + + // Initialisation des composants et systèmes + BaseComponent::Initialize(); + BaseSystem::Initialize(); + + // Composants + + NazaraNotice("Initialized: SDK"); + return true; + } + catch (const std::exception& e) + { + NazaraError("Failed to initialize NDK: " + NzString(e.what())); + + return false; + } + } + + void Sdk::Uninitialize() + { + if (s_referenceCounter != 1) + { + // Le module est soit encore utilisé, soit pas initialisé + if (s_referenceCounter > 1) + s_referenceCounter--; + + return; + } + + // Libération du SDK + s_referenceCounter = 0; + + // Libération du moteur + + // Modules clients + NzAudio::Uninitialize(); + NzGraphics::Uninitialize(); + + // Modules serveurs + NzLua::Uninitialize(); + NzNoise::Uninitialize(); + NzPhysics::Uninitialize(); + NzUtility::Uninitialize(); + + NazaraNotice("Uninitialized: SDK"); + } + + unsigned int Sdk::s_referenceCounter = 0; +}