(NDK) Added explicit initialisation

Components and systems just can't be initialized at startup, so we need
some kind of explicit initialisation. I followed the same layout as the
others modules by adding a core class (Ndk::Sdk) which will initialize
components and systems, and Nazara's modules.

This is starting to get serious, I like it.


Former-commit-id: 263500e8d16db70ef7f92047b8a7e3235c08bcd0
This commit is contained in:
Lynix 2015-04-07 17:39:20 +02:00
parent aedf416949
commit 46d125d205
10 changed files with 183 additions and 2 deletions

View File

@ -14,6 +14,8 @@ namespace Ndk
template<unsigned int N> ComponentId BuildComponentId(const char (&name)[N]);
template<typename ComponentType> constexpr ComponentIndex GetComponentIndex();
template<typename SystemType> constexpr SystemIndex GetSystemIndex();
template<typename ComponentType, unsigned int N> ComponentIndex InitializeComponent(const char (&name)[N]);
template<typename SystemType> SystemIndex InitializeSystem();
}
#include <Ndk/Algorithm.inl>

View File

@ -30,4 +30,18 @@ namespace Ndk
{
return SystemType::systemIndex;
}
template<typename ComponentType, unsigned int N>
ComponentIndex InitializeComponent(const char (&name)[N])
{
ComponentType::componentIndex = ComponentType::RegisterComponent(name);
return ComponentType::componentIndex;
}
template<typename SystemType>
SystemIndex InitializeSystem()
{
SystemType::systemIndex = SystemType::RegisterSystem();
return SystemType::systemIndex;
}
}

View File

@ -16,6 +16,8 @@ namespace Ndk
{
class NDK_API BaseComponent
{
friend class Sdk;
public:
using Factory = std::function<BaseComponent*()>;
@ -32,6 +34,9 @@ namespace Ndk
static ComponentIndex RegisterComponent(ComponentId id, Factory factoryFunc);
private:
static bool Initialize();
static void Uninitialize();
struct ComponentEntry
{
ComponentId id;

View File

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

View File

@ -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<EntityHandle> m_entities;
NzBitset<nzUInt64> m_entityBits;
NzBitset<> m_excludedComponents;

View File

@ -113,4 +113,17 @@ namespace Ndk
{
m_world = &world;
}
inline bool BaseSystem::Initialize()
{
s_nextIndex = 0;
return true;
}
inline void BaseSystem::Uninitialize()
{
// Rien à faire
}
}

31
SDK/include/NDK/Sdk.hpp Normal file
View File

@ -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 <NDK/Prerequesites.hpp>
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 <NDK/Sdk.inl>
#endif // NDK_SDK_HPP

11
SDK/include/NDK/Sdk.inl Normal file
View File

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

View File

@ -40,5 +40,5 @@ namespace Ndk
NazaraUnused(entity);
}
SystemIndex BaseSystem::s_nextIndex = 0;
SystemIndex BaseSystem::s_nextIndex;
}

89
SDK/src/NDK/Sdk.cpp Normal file
View File

@ -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 <NDK/Sdk.hpp>
#include <Nazara/Audio/Audio.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Graphics/Graphics.hpp>
#include <Nazara/Lua/Lua.hpp>
#include <Nazara/Noise/Noise.hpp>
#include <Nazara/Physics/Physics.hpp>
#include <Nazara/Utility/Utility.hpp>
#include <NDK/Algorithm.hpp>
#include <NDK/BaseSystem.hpp>
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;
}