Handle windows and EnTT with application components
This commit is contained in:
committed by
Jérôme Leclercq
parent
18851c9185
commit
da9eb14ebe
46
include/Nazara/Core/AppEntitySystemComponent.hpp
Normal file
46
include/Nazara/Core/AppEntitySystemComponent.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Platform module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_CORE_APPENTITYSYSTEMCOMPONENT_HPP
|
||||
#define NAZARA_CORE_APPENTITYSYSTEMCOMPONENT_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/ApplicationComponent.hpp>
|
||||
#include <Nazara/Core/Config.hpp>
|
||||
#include <Nazara/Core/Systems/SystemGraph.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_CORE_API AppEntitySystemComponent : public ApplicationComponent
|
||||
{
|
||||
public:
|
||||
inline AppEntitySystemComponent(ApplicationBase& app);
|
||||
AppEntitySystemComponent(const AppEntitySystemComponent&) = delete;
|
||||
AppEntitySystemComponent(AppEntitySystemComponent&&) = delete;
|
||||
~AppEntitySystemComponent() = default;
|
||||
|
||||
template<typename T, typename... Args> T& AddSystem(Args&&... args);
|
||||
|
||||
entt::handle CreateEntity();
|
||||
|
||||
entt::registry& GetRegistry();
|
||||
const entt::registry& GetRegistry() const;
|
||||
template<typename T> T& GetSystem() const;
|
||||
|
||||
void Update(Time elapsedTime) override;
|
||||
|
||||
AppEntitySystemComponent& operator=(const AppEntitySystemComponent&) = delete;
|
||||
AppEntitySystemComponent& operator=(AppEntitySystemComponent&&) = delete;
|
||||
|
||||
private:
|
||||
entt::registry m_registry;
|
||||
SystemGraph m_systemGraph;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Core/AppEntitySystemComponent.inl>
|
||||
|
||||
#endif // NAZARA_CORE_APPENTITYSYSTEMCOMPONENT_HPP
|
||||
44
include/Nazara/Core/AppEntitySystemComponent.inl
Normal file
44
include/Nazara/Core/AppEntitySystemComponent.inl
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Platform module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/AppEntitySystemComponent.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline AppEntitySystemComponent::AppEntitySystemComponent(ApplicationBase& app) :
|
||||
ApplicationComponent(app),
|
||||
m_systemGraph(m_registry)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
T& AppEntitySystemComponent::AddSystem(Args&&... args)
|
||||
{
|
||||
return m_systemGraph.AddSystem<T>(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
inline entt::handle AppEntitySystemComponent::CreateEntity()
|
||||
{
|
||||
return entt::handle(m_registry, m_registry.create());
|
||||
}
|
||||
|
||||
inline entt::registry& AppEntitySystemComponent::GetRegistry()
|
||||
{
|
||||
return m_registry;
|
||||
}
|
||||
|
||||
inline const entt::registry& AppEntitySystemComponent::GetRegistry() const
|
||||
{
|
||||
return m_registry;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& AppEntitySystemComponent::GetSystem() const
|
||||
{
|
||||
return m_systemGraph.GetSystem<T>();
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
@@ -20,7 +20,7 @@ namespace Nz
|
||||
using ApplicationBase::ApplicationBase;
|
||||
Application(const Application&) = delete;
|
||||
Application(Application&&) = delete;
|
||||
~Application() = default;
|
||||
~Application();
|
||||
|
||||
Application& operator=(const Application&) = delete;
|
||||
Application& operator=(Application&&) = delete;
|
||||
|
||||
@@ -7,6 +7,12 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
template<typename... ModuleList>
|
||||
Application<ModuleList...>::~Application()
|
||||
{
|
||||
// Clear components before releasing modules
|
||||
ClearComponents();
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
|
||||
@@ -26,7 +26,11 @@ namespace Nz
|
||||
~ApplicationBase() = default;
|
||||
|
||||
template<typename T, typename... Args> T& AddComponent(Args&&... args);
|
||||
inline void AddComponent(std::unique_ptr<ApplicationComponent>&& component);
|
||||
|
||||
inline void ClearComponents();
|
||||
|
||||
template<typename T> T* GetComponent();
|
||||
template<typename T> const T* GetComponent() const;
|
||||
|
||||
int Run();
|
||||
|
||||
|
||||
@@ -3,20 +3,12 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/ApplicationBase.hpp>
|
||||
#include <Nazara/Core/ApplicationComponentRegistry.hpp>
|
||||
#include <stdexcept>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
template<typename T, typename ...Args>
|
||||
T& Nz::ApplicationBase::AddComponent(Args&& ...args)
|
||||
{
|
||||
std::unique_ptr<T> component = std::make_unique<T>(std::forward<Args>(args)...);
|
||||
T& componentRef = *component;
|
||||
AddComponent(std::move(component));
|
||||
|
||||
return componentRef;
|
||||
}
|
||||
|
||||
inline ApplicationBase::ApplicationBase() :
|
||||
ApplicationBase(0, static_cast<const char**>(nullptr))
|
||||
{
|
||||
@@ -27,9 +19,47 @@ namespace Nz
|
||||
{
|
||||
}
|
||||
|
||||
void ApplicationBase::AddComponent(std::unique_ptr<ApplicationComponent>&& component)
|
||||
template<typename T, typename... Args>
|
||||
T& ApplicationBase::AddComponent(Args&&... args)
|
||||
{
|
||||
m_components.emplace_back(std::move(component));
|
||||
std::size_t componentIndex = ApplicationComponentRegistry<T>::GetComponentId();
|
||||
|
||||
std::unique_ptr<T> component = std::make_unique<T>(*this, std::forward<Args>(args)...);
|
||||
T& componentRef = *component;
|
||||
|
||||
if (componentIndex >= m_components.size())
|
||||
m_components.resize(componentIndex + 1);
|
||||
else if (m_components[componentIndex] != nullptr)
|
||||
throw std::runtime_error("component was added multiple times");
|
||||
|
||||
m_components[componentIndex] = std::move(component);
|
||||
|
||||
return componentRef;
|
||||
}
|
||||
|
||||
inline void ApplicationBase::ClearComponents()
|
||||
{
|
||||
m_components.clear();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T* ApplicationBase::GetComponent()
|
||||
{
|
||||
std::size_t componentIndex = ApplicationComponentRegistry<T>::GetComponentId();
|
||||
if (componentIndex >= m_components.size())
|
||||
return nullptr;
|
||||
|
||||
return m_components[componentIndex].get();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
const T* ApplicationBase::GetComponent() const
|
||||
{
|
||||
std::size_t componentIndex = ApplicationComponentRegistry<T>::GetComponentId();
|
||||
if (componentIndex >= m_components.size())
|
||||
return nullptr;
|
||||
|
||||
return m_components[componentIndex].get();
|
||||
}
|
||||
|
||||
inline void ApplicationBase::Quit()
|
||||
|
||||
@@ -13,18 +13,26 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class ApplicationBase;
|
||||
|
||||
class NAZARA_CORE_API ApplicationComponent
|
||||
{
|
||||
public:
|
||||
ApplicationComponent() = default;
|
||||
inline ApplicationComponent(ApplicationBase& app);
|
||||
ApplicationComponent(const ApplicationComponent&) = delete;
|
||||
ApplicationComponent(ApplicationComponent&&) = delete;
|
||||
virtual ~ApplicationComponent();
|
||||
|
||||
inline ApplicationBase& GetApp();
|
||||
inline const ApplicationBase& GetApp() const;
|
||||
|
||||
virtual void Update(Time elapsedTime);
|
||||
|
||||
ApplicationComponent& operator=(const ApplicationComponent&) = delete;
|
||||
ApplicationComponent& operator=(ApplicationComponent&&) = delete;
|
||||
|
||||
private:
|
||||
ApplicationBase& m_app;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,20 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline ApplicationComponent::ApplicationComponent(ApplicationBase& app) :
|
||||
m_app(app)
|
||||
{
|
||||
}
|
||||
|
||||
inline ApplicationBase& ApplicationComponent::GetApp()
|
||||
{
|
||||
return m_app;
|
||||
}
|
||||
|
||||
inline const ApplicationBase& ApplicationComponent::GetApp() const
|
||||
{
|
||||
return m_app;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
|
||||
24
include/Nazara/Core/ApplicationComponentRegistry.hpp
Normal file
24
include/Nazara/Core/ApplicationComponentRegistry.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// 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_CORE_APPLICATIONCOMPONENTREGISTRY_HPP
|
||||
#define NAZARA_CORE_APPLICATIONCOMPONENTREGISTRY_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/Config.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
template<typename T>
|
||||
struct ApplicationComponentRegistry
|
||||
{
|
||||
static std::size_t GetComponentId();
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Core/ApplicationComponentRegistry.inl>
|
||||
|
||||
#endif // NAZARA_CORE_APPLICATIONCOMPONENTREGISTRY_HPP
|
||||
27
include/Nazara/Core/ApplicationComponentRegistry.inl
Normal file
27
include/Nazara/Core/ApplicationComponentRegistry.inl
Normal file
@@ -0,0 +1,27 @@
|
||||
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// 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/ApplicationComponentRegistry.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace Detail
|
||||
{
|
||||
std::size_t ComponentCounter()
|
||||
{
|
||||
static std::size_t counter = 0;
|
||||
return counter++;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::size_t ApplicationComponentRegistry<T>::GetComponentId()
|
||||
{
|
||||
static std::size_t typeId = Detail::ComponentCounter();
|
||||
return typeId;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
Reference in New Issue
Block a user