Handle windows and EnTT with application components
This commit is contained in:
parent
18851c9185
commit
da9eb14ebe
|
|
@ -1,10 +1,12 @@
|
|||
// Sources pour https://github.com/NazaraEngine/NazaraEngine/wiki/(FR)-Tutoriel:-%5B01%5D-Hello-World
|
||||
|
||||
#include <Nazara/Core/Application.hpp>
|
||||
#include <Nazara/Core/AppEntitySystemComponent.hpp>
|
||||
#include <Nazara/Core/Systems.hpp>
|
||||
#include <Nazara/Graphics.hpp>
|
||||
#include <Nazara/Graphics/Components.hpp>
|
||||
#include <Nazara/Graphics/Systems.hpp>
|
||||
#include <Nazara/Platform/AppWindowingComponent.hpp>
|
||||
#include <Nazara/Renderer.hpp>
|
||||
#include <Nazara/Utility.hpp>
|
||||
#include <Nazara/Utility/Components.hpp>
|
||||
|
|
@ -17,15 +19,19 @@ int main()
|
|||
{
|
||||
Nz::Application<Nz::Graphics> app;
|
||||
|
||||
entt::registry registry;
|
||||
Nz::SystemGraph systemGraph(registry);
|
||||
Nz::RenderSystem& renderSystem = systemGraph.AddSystem<Nz::RenderSystem>();
|
||||
Nz::RenderWindow& mainWindow = renderSystem.CreateWindow(Nz::Graphics::Instance()->GetRenderDevice(), Nz::VideoMode(1280, 720), "Tut01 - Hello world");
|
||||
auto& windowing = app.AddComponent<Nz::AppWindowingComponent>();
|
||||
Nz::Window& mainWindow = windowing.CreateWindow(Nz::VideoMode(1280, 720), "Hello world");
|
||||
|
||||
entt::entity cameraEntity = registry.create();
|
||||
auto& ecs = app.AddComponent<Nz::AppEntitySystemComponent>();
|
||||
|
||||
Nz::RenderSystem& renderSystem = ecs.AddSystem<Nz::RenderSystem>();
|
||||
auto& windowSwapchain = renderSystem.CreateSwapchain(mainWindow);
|
||||
|
||||
entt::handle cameraEntity = ecs.CreateEntity();
|
||||
{
|
||||
registry.emplace<Nz::NodeComponent>(cameraEntity);
|
||||
auto& cameraComponent = registry.emplace<Nz::CameraComponent>(cameraEntity, mainWindow.GetRenderTarget(), Nz::ProjectionType::Orthographic);
|
||||
cameraEntity.emplace<Nz::NodeComponent>();
|
||||
|
||||
auto& cameraComponent = cameraEntity.emplace<Nz::CameraComponent>(&windowSwapchain.GetSwapchain(), Nz::ProjectionType::Orthographic);
|
||||
cameraComponent.UpdateClearColor(Nz::Color(0.46f, 0.48f, 0.84f, 1.f));
|
||||
}
|
||||
|
||||
|
|
@ -37,10 +43,10 @@ int main()
|
|||
std::shared_ptr<Nz::TextSprite> textSprite = std::make_shared<Nz::TextSprite>();
|
||||
textSprite->Update(textDrawer);
|
||||
|
||||
entt::entity textEntity = registry.create();
|
||||
entt::handle textEntity = ecs.CreateEntity();
|
||||
{
|
||||
auto& nodeComponent = registry.emplace<Nz::NodeComponent>(textEntity);
|
||||
auto& gfxComponent = registry.emplace<Nz::GraphicsComponent>(textEntity);
|
||||
auto& nodeComponent = textEntity.emplace<Nz::NodeComponent>();
|
||||
auto& gfxComponent = textEntity.emplace<Nz::GraphicsComponent>();
|
||||
gfxComponent.AttachRenderable(textSprite, 0xFFFFFFFF);
|
||||
|
||||
Nz::Boxf textBox = textSprite->GetAABB();
|
||||
|
|
@ -48,11 +54,5 @@ int main()
|
|||
nodeComponent.SetPosition(windowSize.x / 2 - textBox.width / 2, windowSize.y / 2 - textBox.height / 2);
|
||||
}
|
||||
|
||||
while (mainWindow.IsOpen())
|
||||
{
|
||||
mainWindow.ProcessEvents();
|
||||
systemGraph.Update();
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
return app.Run();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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>
|
||||
|
|
@ -10,21 +10,33 @@
|
|||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/ApplicationComponent.hpp>
|
||||
#include <Nazara/Platform/Config.hpp>
|
||||
#include <Nazara/Platform/Window.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_PLATFORM_API AppWindowingComponent : public ApplicationComponent
|
||||
{
|
||||
public:
|
||||
AppWindowingComponent() = default;
|
||||
using ApplicationComponent::ApplicationComponent;
|
||||
AppWindowingComponent(const AppWindowingComponent&) = delete;
|
||||
AppWindowingComponent(AppWindowingComponent&&) = delete;
|
||||
~AppWindowingComponent() = default;
|
||||
|
||||
template<typename... Args> Window& CreateWindow(Args&&... args);
|
||||
|
||||
inline void DisableQuitOnLastWindowClosed();
|
||||
inline void EnableQuitOnLastWindowClosed(bool enable = true);
|
||||
|
||||
inline bool IsQuitOnLastWindowClosedEnabled() const;
|
||||
|
||||
void Update(Time elapsedTime) override;
|
||||
|
||||
AppWindowingComponent& operator=(const AppWindowingComponent&) = delete;
|
||||
AppWindowingComponent& operator=(AppWindowingComponent&&) = delete;
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<Window>> m_windows;
|
||||
bool m_quitOnLastWindowClosed;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,26 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
template<typename ...Args>
|
||||
Window& AppWindowingComponent::CreateWindow(Args&&... args)
|
||||
{
|
||||
return *m_windows.emplace_back(std::make_unique<Window>(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
inline void AppWindowingComponent::DisableQuitOnLastWindowClosed()
|
||||
{
|
||||
return EnableQuitOnLastWindowClosed(false);
|
||||
}
|
||||
|
||||
inline void AppWindowingComponent::EnableQuitOnLastWindowClosed(bool enable)
|
||||
{
|
||||
m_quitOnLastWindowClosed = enable;
|
||||
}
|
||||
|
||||
inline bool AppWindowingComponent::IsQuitOnLastWindowClosedEnabled() const
|
||||
{
|
||||
return m_quitOnLastWindowClosed;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Platform/DebugOff.hpp>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
// 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
|
||||
{
|
||||
void AppEntitySystemComponent::Update(Time elapsedTime)
|
||||
{
|
||||
m_systemGraph.Update(elapsedTime);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
// Copyright (C) 2022 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/Application.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
Application::Application(int argc, const char** argv) :
|
||||
m_running(true)
|
||||
{
|
||||
}
|
||||
|
||||
int Application::Run()
|
||||
{
|
||||
// Ignore time between creation and Run() call
|
||||
m_clock.Restart();
|
||||
|
||||
while (m_running)
|
||||
{
|
||||
Time elapsedTime = m_clock.Restart();
|
||||
Update(elapsedTime);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Application::Update(Time elapsedTime)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -28,5 +28,10 @@ namespace Nz
|
|||
|
||||
void ApplicationBase::Update(Time elapsedTime)
|
||||
{
|
||||
for (auto& componentPtr : m_components)
|
||||
{
|
||||
if (componentPtr)
|
||||
componentPtr->Update(elapsedTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue