Rework modules

This commit is contained in:
Jérôme Leclercq
2020-09-10 20:12:09 +02:00
parent 385927b05a
commit a7fac3beb8
31 changed files with 568 additions and 787 deletions

View File

@@ -10,46 +10,45 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Audio/Config.hpp>
#include <Nazara/Audio/Enums.hpp>
#include <Nazara/Core/Core.hpp>
#include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Vector3.hpp>
namespace Nz
{
class NAZARA_AUDIO_API Audio
class NAZARA_AUDIO_API Audio : public Module<Audio>
{
friend Module;
using Dependencies = TypeList<Core>;
public:
Audio() = delete;
~Audio() = delete;
Audio();
~Audio();
static AudioFormat GetAudioFormat(unsigned int channelCount);
static float GetDopplerFactor();
static float GetGlobalVolume();
static Vector3f GetListenerDirection();
static Vector3f GetListenerPosition();
static Quaternionf GetListenerRotation();
static Vector3f GetListenerVelocity();
static float GetSpeedOfSound();
AudioFormat GetAudioFormat(unsigned int channelCount);
float GetDopplerFactor();
float GetGlobalVolume();
Vector3f GetListenerDirection();
Vector3f GetListenerPosition();
Quaternionf GetListenerRotation();
Vector3f GetListenerVelocity();
float GetSpeedOfSound();
static bool Initialize();
static bool IsFormatSupported(AudioFormat format);
static bool IsInitialized();
static void SetDopplerFactor(float dopplerFactor);
static void SetGlobalVolume(float volume);
static void SetListenerDirection(const Vector3f& direction);
static void SetListenerDirection(float dirX, float dirY, float dirZ);
static void SetListenerPosition(const Vector3f& position);
static void SetListenerPosition(float x, float y, float z);
static void SetListenerRotation(const Quaternionf& rotation);
static void SetListenerVelocity(const Vector3f& velocity);
static void SetListenerVelocity(float velX, float velY, float velZ);
static void SetSpeedOfSound(float speed);
static void Uninitialize();
bool IsFormatSupported(AudioFormat format);
void SetDopplerFactor(float dopplerFactor);
void SetGlobalVolume(float volume);
void SetListenerDirection(const Vector3f& direction);
void SetListenerDirection(float dirX, float dirY, float dirZ);
void SetListenerPosition(const Vector3f& position);
void SetListenerPosition(float x, float y, float z);
void SetListenerRotation(const Quaternionf& rotation);
void SetListenerVelocity(const Vector3f& velocity);
void SetListenerVelocity(float velX, float velY, float velZ);
void SetSpeedOfSound(float speed);
private:
static unsigned int s_moduleReferenceCounter;
static Audio* s_instance;
};
}

View File

@@ -8,24 +8,26 @@
#define NAZARA_CORE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Module.hpp>
#include <Nazara/Core/TypeList.hpp>
namespace Nz
{
class NAZARA_CORE_API Core
class NAZARA_CORE_API Core : public Module<Core>
{
friend Module;
public:
Core() = delete;
~Core() = delete;
using Dependencies = TypeList<>;
static bool Initialize();
static bool IsInitialized();
static void Uninitialize();
Core();
~Core();
private:
static unsigned int s_moduleReferenceCounter;
static Core* s_instance;
};
}
#include <Nazara/Core/Core.inl>
#endif // NAZARA_CORE_HPP

View File

@@ -0,0 +1,12 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Core.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -0,0 +1,40 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_MODULE_HPP
#define NAZARA_MODULE_HPP
#include <Nazara/Prerequisites.hpp>
#include <string>
namespace Nz
{
template<typename T>
class Module
{
friend class Core;
public:
static T* Instance();
protected:
Module(std::string moduleName, T* pointer);
~Module();
private:
struct NoLog {};
Module(std::string moduleName, T* pointer, NoLog);
void LogInit();
std::string m_moduleName;
};
}
#include <Nazara/Core/Module.inl>
#endif // NAZARA_MODULE_HPP

View File

@@ -0,0 +1,46 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Module.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
template<typename T>
Module<T>::Module(std::string moduleName, T* pointer) :
Module(std::move(moduleName), pointer, NoLog{})
{
LogInit();
}
template<typename T>
Module<T>::Module(std::string moduleName, T* pointer, NoLog) :
m_moduleName(std::move(moduleName))
{
NazaraAssert(T::s_instance == nullptr, "only one instance of " + m_moduleName + " must exist at a given time");
T::s_instance = pointer;
}
template<typename T>
Module<T>::~Module()
{
NazaraNotice("Uninitializing " + m_moduleName + "...");
T::s_instance = nullptr;
}
template<typename T>
T* Module<T>::Instance()
{
return T::s_instance;
}
template<typename T>
void Module<T>::LogInit()
{
NazaraNotice("Initializing " + m_moduleName + "...");
}
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -0,0 +1,48 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_MODULES_HPP
#define NAZARA_MODULES_HPP
#include <Nazara/Core/TypeList.hpp>
namespace Nz
{
namespace Detail
{
template<typename>
struct BuildDepList;
template<typename Module, typename... Modules>
struct ModuleTuple : ModuleTuple<Module>, ModuleTuple<Modules...>
{
};
template<typename Module>
struct ModuleTuple<Module>
{
Module m;
};
}
template<typename... ModuleList>
class Modules
{
public:
Modules() = default;
~Modules() = default;
private:
using OrderedModuleList = TypeListUnique<typename Detail::BuildDepList<TypeList<ModuleList...>>::Result>;
using Tuple = TypeListInstantiate<OrderedModuleList, Detail::ModuleTuple>;
Tuple m_modules;
};
}
#include <Nazara/Core/Modules.inl>
#endif

View File

@@ -0,0 +1,26 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Modules.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz::Detail
{
template<>
struct BuildDepList<TypeList<>>
{
using Result = TypeList<>;
};
template<typename Module, typename... ModuleList>
struct BuildDepList<TypeList<Module, ModuleList...>>
{
using ModuleDependencies = typename BuildDepList<typename Module::Dependencies>::Result;
using ModuleDependenciesIncModule = TypeListAppend<ModuleDependencies, Module>;
using RestDependencies = typename BuildDepList<TypeList<ModuleList...>>::Result;
using Result = TypeListConcat<ModuleDependenciesIncModule, RestDependencies>;
};
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -0,0 +1,64 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_TYPELIST_HPP
#define NAZARA_TYPELIST_HPP
#include <cstddef>
namespace Nz
{
template<typename...> struct TypeList {};
namespace Detail
{
template<typename, typename>
struct ListAppend;
template<typename, std::size_t>
struct ListAt;
template<typename, typename>
struct ListConcat;
template<typename, typename>
struct ListFind;
template<typename, template<typename...> typename>
struct ListInstantiate;
template<typename, typename>
struct ListPrepend;
template<typename, typename>
struct ListUnique;
}
template<typename List, typename NewType>
using TypeListAppend = typename Detail::ListAppend<List, NewType>::Result;
template<typename List, std::size_t Index>
using TypeListAt = typename Detail::ListAt<List, Index>::Type;
template<typename FirstList, typename SecondList>
using TypeListConcat = typename Detail::ListConcat<FirstList, SecondList>::Result;
template<typename List, typename Type>
constexpr bool TypeListFind = Detail::ListFind<List, Type>::Find();
template<typename List, template<typename...> typename Class>
using TypeListInstantiate = typename Detail::ListInstantiate<List, Class>::Result;
template<typename List, typename NewType>
using TypeListPrepend = typename Detail::ListPrepend<List, NewType>::Result;
template<typename List>
using TypeListUnique = typename Detail::ListUnique<TypeList<>, List>::Result;
}
#include <Nazara/Core/TypeList.inl>
#endif // NAZARA_TYPELIST_HPP

View File

@@ -0,0 +1,112 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/TypeList.hpp>
#include <type_traits>
#include <Nazara/Core/Debug.hpp>
namespace Nz::Detail
{
template<typename NewType, typename... ListTypes>
struct ListAppend<TypeList<ListTypes...>, NewType>
{
using Result = TypeList<ListTypes..., NewType>;
};
template<typename T, typename... ListTypes>
struct ListAt<TypeList<T, ListTypes...>, 0>
{
using Type = T;
};
template<std::size_t Index, typename T, typename... ListTypes>
struct ListAt<TypeList<T, ListTypes...>, Index>
{
static_assert(Index <= sizeof...(ListTypes), "Index out of range");
using Type = typename ListAt<TypeList<ListTypes...>, Index - 1>::Type;
};
template<typename... First>
struct ListConcat<TypeList<First...>, TypeList<>>
{
using Result = TypeList<First...>;
};
template<typename... Second>
struct ListConcat<TypeList<>, TypeList<Second...>>
{
using Result = TypeList<Second...>;
};
template<typename... First, typename T>
struct ListConcat<TypeList<First...>, TypeList<T>>
{
using Result = TypeList<First..., T>;
};
template<typename... First, typename T1, typename T2, typename... Second>
struct ListConcat<TypeList<First...>, TypeList<T1, T2, Second...>>
{
using Result = typename ListConcat<TypeList<First..., T1>, TypeList<T2, Second...>>::Result;
};
struct ListFindHelper
{
template<typename ToFind, typename Type, typename... Rest> static constexpr bool Find()
{
if constexpr (std::is_same_v<ToFind, Type>)
return true;
else
return Find<ToFind, Rest...>();
}
template<typename ToFind> static constexpr bool Find()
{
return false;
}
};
template<typename TypeToFind, typename... ListTypes>
struct ListFind<TypeList<ListTypes...>, TypeToFind>
{
static constexpr bool Find()
{
return ListFindHelper::Find<TypeToFind, ListTypes...>();
}
};
template<template<typename...> typename Class, typename... ListTypes>
struct ListInstantiate<TypeList<ListTypes...>, Class>
{
using Result = Class<ListTypes...>;
};
template<typename NewType, typename... ListTypes>
struct ListPrepend<TypeList<ListTypes...>, NewType>
{
using Result = TypeList<NewType, ListTypes...>;
};
template<typename... Types, typename T1>
struct ListUnique<TypeList<Types...>, TypeList<T1>>
{
static constexpr bool IsTypePresent = ListFind<TypeList<Types...>, T1>::Find();
using Result = std::conditional_t<!IsTypePresent, TypeList<Types..., T1>, TypeList<Types...>>;
};
template<typename... Types, typename T1, typename T2, typename... Rest>
struct ListUnique<TypeList<Types...>, TypeList<T1, T2, Rest...>>
{
using Result = typename ListUnique<typename ListUnique<TypeList<Types...>, TypeList<T1>>::Result, TypeList<T2, Rest...>>::Result;
};
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -8,25 +8,24 @@
#define NAZARA_MODULENAME_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Core.hpp>
#include <Nazara/Network/Config.hpp>
namespace Nz
{
class NAZARA_NETWORK_API Network
class NAZARA_NETWORK_API Network : public Module<Network>
{
friend Module;
public:
Network() = delete;
~Network() = delete;
using Dependencies = TypeList<Core>;
static bool Initialize();
static bool IsInitialized();
static void Uninitialize();
Network();
~Network();
private:
static unsigned int s_moduleReferenceCounter;
};
static Network* s_instance;
};
}
#endif // NAZARA_MODULENAME_HPP

View File

@@ -8,24 +8,23 @@
#define NAZARA_PHYSICS2D_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Core.hpp>
#include <Nazara/Physics2D/Config.hpp>
namespace Nz
{
class NAZARA_PHYSICS2D_API Physics2D
class NAZARA_PHYSICS2D_API Physics2D : public Module<Physics2D>
{
friend Module;
public:
Physics2D() = delete;
~Physics2D() = delete;
using Dependencies = TypeList<Core>;
static bool Initialize();
static bool IsInitialized();
static void Uninitialize();
Physics2D();
~Physics2D() = default;
private:
static unsigned int s_moduleReferenceCounter;
static Physics2D* s_instance;
};
}

View File

@@ -8,26 +8,25 @@
#define NAZARA_PHYSICS3D_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Core.hpp>
#include <Nazara/Physics3D/Config.hpp>
namespace Nz
{
class NAZARA_PHYSICS3D_API Physics3D
class NAZARA_PHYSICS3D_API Physics3D : public Module<Physics3D>
{
friend Module;
public:
Physics3D() = delete;
~Physics3D() = delete;
using Dependencies = TypeList<Core>;
static unsigned int GetMemoryUsed();
Physics3D();
~Physics3D();
static bool Initialize();
static bool IsInitialized();
static void Uninitialize();
unsigned int GetMemoryUsed();
private:
static unsigned int s_moduleReferenceCounter;
static Physics3D* s_instance;
};
}

View File

@@ -8,24 +8,23 @@
#define NAZARA_PLATFORM_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Utility/Utility.hpp>
#include <Nazara/Platform/Config.hpp>
namespace Nz
{
class NAZARA_PLATFORM_API Platform
class NAZARA_PLATFORM_API Platform : public Module<Platform>
{
friend Module;
public:
Platform() = delete;
~Platform() = delete;
using Dependencies = TypeList<Utility>;
static bool Initialize();
static bool IsInitialized();
static void Uninitialize();
Platform();
~Platform();
private:
static unsigned int s_moduleReferenceCounter;
static Platform* s_instance;
};
}

View File

@@ -119,8 +119,6 @@
#define NAZARA_PLATFORM_LINUX
#define NAZARA_PLATFORM_POSIX
#define NAZARA_PLATFORM_GLX
#define NAZARA_EXPORT __attribute__((visibility ("default")))
#define NAZARA_IMPORT __attribute__((visibility ("default")))
/*#elif defined(__APPLE__) && defined(__MACH__)

View File

@@ -9,38 +9,32 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/DynLib.hpp>
#include <Nazara/Platform/Platform.hpp>
#include <Nazara/Renderer/Config.hpp>
#include <Nazara/Renderer/RendererImpl.hpp>
#include <Nazara/Utility/Enums.hpp>
namespace Nz
{
class AbstractBuffer;
class Buffer;
class NAZARA_RENDERER_API Renderer
class NAZARA_RENDERER_API Renderer : public Module<Renderer>
{
friend Module;
public:
Renderer() = delete;
~Renderer() = delete;
using Dependencies = TypeList<Platform>;
static inline RendererImpl* GetRendererImpl();
Renderer();
~Renderer();
static bool Initialize();
static inline bool IsInitialized();
static inline void SetParameters(const ParameterList& parameters);
static void Uninitialize();
inline RendererImpl* GetRendererImpl();
private:
static AbstractBuffer* CreateHardwareBufferImpl(Buffer* parent, BufferType type);
std::unique_ptr<RendererImpl> m_rendererImpl;
DynLib m_rendererLib;
static std::unique_ptr<RendererImpl> s_rendererImpl;
static DynLib s_rendererLib;
static ParameterList s_initializationParameters;
static unsigned int s_moduleReferenceCounter;
static Renderer* s_instance;
};
}

View File

@@ -9,17 +9,7 @@ namespace Nz
{
inline RendererImpl* Renderer::GetRendererImpl()
{
return s_rendererImpl.get();
}
inline bool Renderer::IsInitialized()
{
return s_moduleReferenceCounter != 0;
}
void Renderer::SetParameters(const ParameterList& parameters)
{
s_initializationParameters = parameters;
return m_rendererImpl.get();
}
}

View File

@@ -8,25 +8,23 @@
#define NAZARA_SHADER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Initializer.hpp>
#include <Nazara/Core/Core.hpp>
#include <Nazara/Shader/Config.hpp>
namespace Nz
{
class NAZARA_SHADER_API Shader
class NAZARA_SHADER_API Shader : public Module<Shader>
{
friend Module;
public:
Shader() = delete;
~Shader() = delete;
using Dependencies = TypeList<Core>;
static bool Initialize();
static bool IsInitialized();
static void Uninitialize();
Shader();
~Shader() = default;
private:
static unsigned int s_moduleReferenceCounter;
static Shader* s_instance;
};
}

View File

@@ -8,28 +8,23 @@
#define NAZARA_UTILITY_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Core.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Enums.hpp>
namespace Nz
{
class NAZARA_UTILITY_API Utility
class NAZARA_UTILITY_API Utility : public Module<Utility>
{
friend Module;
public:
Utility() = delete;
~Utility() = delete;
using Dependencies = TypeList<Core>;
static bool Initialize();
static bool IsInitialized();
static void Uninitialize();
static unsigned int ComponentCount[ComponentType_Max+1];
static std::size_t ComponentStride[ComponentType_Max+1];
Utility();
~Utility();
private:
static unsigned int s_moduleReferenceCounter;
static Utility* s_instance;
};
}