From 83884015fc1ed68c079036ecfdec2ee82e04b3f7 Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 15 Mar 2016 23:00:03 +0100 Subject: [PATCH 1/6] Add current work Former-commit-id: 06dd34f00951e334b330c067f61f69fc73dcc2d2 --- SDK/include/NDK/Application.hpp | 32 ++++++++++++++++++++++++ SDK/include/NDK/Application.inl | 38 +++++++++++++++++++++++++++- SDK/src/NDK/Application.cpp | 44 +++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/SDK/include/NDK/Application.hpp b/SDK/include/NDK/Application.hpp index 9b6eedae1..644dcc971 100644 --- a/SDK/include/NDK/Application.hpp +++ b/SDK/include/NDK/Application.hpp @@ -8,6 +8,10 @@ #define NDK_APPLICATION_HPP #include +#include +#include +#include +#include namespace Ndk { @@ -15,7 +19,35 @@ namespace Ndk { public: inline Application(); + Application(const Application&) = delete; inline ~Application(); + + #ifndef NDK_SERVER + template T& AddWindow(Args&&... args); + #endif + template World& AddWorld(Args&&... args); + + bool Run(); + + inline void Quit(); + + Application& operator=(const Application&) = delete; + + inline static Application* Instance(); + + private: + #ifndef NDK_SERVER + std::vector> m_windows; + #endif + std::vector m_worlds; + Nz::Clock m_updateClock; + #ifndef NDK_SERVER + bool m_exitOnClosedWindows; + #endif + bool m_shouldQuit; + float m_updateTime; + + static Application* s_application; }; } diff --git a/SDK/include/NDK/Application.inl b/SDK/include/NDK/Application.inl index dafbd54d8..690cf54d3 100644 --- a/SDK/include/NDK/Application.inl +++ b/SDK/include/NDK/Application.inl @@ -3,12 +3,21 @@ // For conditions of distribution and use, see copyright notice in Prerequesites.hpp #include +#include #include namespace Ndk { - inline Application::Application() + inline Application::Application() : + #ifndef NDK_SERVER + m_exitOnClosedWindows(true), + #endif + m_shouldQuit(false), + m_updateTime(0.f) { + NazaraAssert(s_application == nullptr, "You can create only one application instance per program"); + s_application = this; + Nz::ErrorFlags errFlags(Nz::ErrorFlag_ThrowException, true); // Initialisation du SDK @@ -21,5 +30,32 @@ namespace Ndk Sdk::Uninitialize(); // Libération automatique des modules + s_application = nullptr; + } + + template + T& Application::AddWindow(Args&&... args) + { + static_assert(std::is_base_of::value, "Type must inherit Window"); + + m_windows.emplace_back(new T(std::forward(args)...)); + return static_cast(*m_windows.back().get()); + } + + template + World& Application::AddWorld(Args&&... args) + { + m_worlds.emplace_back(std::forward(args)...); + return *m_worlds.back(); + } + + inline void Application::Quit() + { + m_shouldQuit = true; + } + + inline Application* Application::Instance() + { + return s_application; } } diff --git a/SDK/src/NDK/Application.cpp b/SDK/src/NDK/Application.cpp index a2141b0f8..e15ba2f92 100644 --- a/SDK/src/NDK/Application.cpp +++ b/SDK/src/NDK/Application.cpp @@ -3,3 +3,47 @@ // For conditions of distribution and use, see copyright notice in Prerequesites.hpp #include + +namespace Ndk +{ + bool Application::Run() + { + #ifndef NDK_SERVER + bool hasAtLeastOneActiveWindow = false; + + auto it = m_windows.begin(); + while (it != m_windows.end()) + { + Nz::Window& window = **it; + + if (!window.IsOpen(true)) + { + it = m_windows.erase(it); + continue; + } + + hasAtLeastOneActiveWindow = true; + + ++it; + } + #endif + + float elapsedTime = m_updateClock.GetSeconds(); + m_updateClock.Restart(); + + for (World& world : m_worlds) + world.Update(elapsedTime); + + if (m_shouldQuit) + return false; + + #ifndef NDK_SERVER + if (m_exitOnClosedWindows && !hasAtLeastOneActiveWindow) + return false; + #endif + + return true; + } + + Application* Application::s_application = nullptr; +} \ No newline at end of file From 42874b90d93e245230a3d911ee19af920dc04296 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 19 Mar 2016 12:21:07 +0100 Subject: [PATCH 2/6] SDK: Add StateMachine (FSM) Former-commit-id: 8029f7cce71809ff1f661982d39750c48c86431f --- SDK/include/NDK/State.hpp | 28 +++++++++++++++++++++++ SDK/include/NDK/StateMachine.hpp | 39 ++++++++++++++++++++++++++++++++ SDK/include/NDK/StateMachine.inl | 26 +++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 SDK/include/NDK/State.hpp create mode 100644 SDK/include/NDK/StateMachine.hpp create mode 100644 SDK/include/NDK/StateMachine.inl diff --git a/SDK/include/NDK/State.hpp b/SDK/include/NDK/State.hpp new file mode 100644 index 000000000..0a113def3 --- /dev/null +++ b/SDK/include/NDK/State.hpp @@ -0,0 +1,28 @@ +// 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_STATE_HPP +#define NDK_STATE_HPP + +#include + +namespace Ndk +{ + class StateMachine; + + class State + { + public: + State(); + ~State(); + + virtual void Enter(StateMachine& fsm) = 0; + virtual void Leave(StateMachine& fsm) = 0; + virtual bool Update(StateMachine& fsm, float elapsedTime) = 0; + }; +} + +#endif // NDK_STATE_HPP \ No newline at end of file diff --git a/SDK/include/NDK/StateMachine.hpp b/SDK/include/NDK/StateMachine.hpp new file mode 100644 index 000000000..8d05f8335 --- /dev/null +++ b/SDK/include/NDK/StateMachine.hpp @@ -0,0 +1,39 @@ +// 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_STATEMACHINE_HPP +#define NDK_STATEMACHINE_HPP + +#include +#include +#include + +namespace Ndk +{ + class StateMachine + { + public: + inline StateMachine(); + StateMachine(const StateMachine&) = delete; + inline StateMachine(StateMachine&& fsm) = default; + inline ~StateMachine(); + + inline void ChangeState(std::shared_ptr state); + + inline bool Update(float elapsedTime); + + inline StateMachine& operator=(StateMachine&& fsm) = default; + StateMachine& operator=(const StateMachine&) = delete; + + private: + std::shared_ptr m_currentState; + std::shared_ptr m_nextState; + }; +} + +#include + +#endif // NDK_STATEMACHINE_HPP \ No newline at end of file diff --git a/SDK/include/NDK/StateMachine.inl b/SDK/include/NDK/StateMachine.inl new file mode 100644 index 000000000..0a1e195c4 --- /dev/null +++ b/SDK/include/NDK/StateMachine.inl @@ -0,0 +1,26 @@ +// 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 + +namespace Ndk +{ + inline void StateMachine::ChangeState(std::shared_ptr state) + { + m_nextState = std::move(state); + } + + inline bool StateMachine::Update(float elapsedTime) + { + if (m_nextState) + { + m_currentState->Leave(*this); + m_currentState = std::move(m_nextState); + m_currentState->Enter(*this); + } + + return m_currentState->Update(*this, elapsedTime); + } +} From 8aa406a310398ec7b46abae2e6747e479ad4b336 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 19 Mar 2016 14:20:28 +0100 Subject: [PATCH 3/6] Sdk/Application: Fix AddWorld Former-commit-id: 8b4758f0fafd6aae0360d0c87b8d5362e8e8d583 --- SDK/include/NDK/Application.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDK/include/NDK/Application.inl b/SDK/include/NDK/Application.inl index 690cf54d3..1efd10bd4 100644 --- a/SDK/include/NDK/Application.inl +++ b/SDK/include/NDK/Application.inl @@ -46,7 +46,7 @@ namespace Ndk World& Application::AddWorld(Args&&... args) { m_worlds.emplace_back(std::forward(args)...); - return *m_worlds.back(); + return m_worlds.back(); } inline void Application::Quit() From 8d6c8e8a79f7357f6b7cfa266cb4f7efb256ddaf Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 19 Mar 2016 14:20:55 +0100 Subject: [PATCH 4/6] Sdk/Application: Fix errors while exiting Former-commit-id: 5b22aad341730918fbe4f56698a6a10e251256af --- SDK/include/NDK/Application.inl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/SDK/include/NDK/Application.inl b/SDK/include/NDK/Application.inl index 1efd10bd4..ca1fa1ffb 100644 --- a/SDK/include/NDK/Application.inl +++ b/SDK/include/NDK/Application.inl @@ -26,6 +26,11 @@ namespace Ndk inline Application::~Application() { + m_worlds.clear(); + #ifndef NDK_SERVER + m_windows.clear(); + #endif + // Libération du SDK Sdk::Uninitialize(); From 11fe78f7e08194d3d4e4335e0f96b0add4da5e85 Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 25 Mar 2016 21:34:17 +0100 Subject: [PATCH 5/6] Ndk/StateMachine: Add constructor/destructor, should be usable now Former-commit-id: fab76ba97470e8d7a2f4a01a9d1746a8e213892f --- SDK/include/NDK/StateMachine.hpp | 2 +- SDK/include/NDK/StateMachine.inl | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/SDK/include/NDK/StateMachine.hpp b/SDK/include/NDK/StateMachine.hpp index 8d05f8335..8e7ad2f64 100644 --- a/SDK/include/NDK/StateMachine.hpp +++ b/SDK/include/NDK/StateMachine.hpp @@ -16,7 +16,7 @@ namespace Ndk class StateMachine { public: - inline StateMachine(); + inline StateMachine(std::shared_ptr originalState); StateMachine(const StateMachine&) = delete; inline StateMachine(StateMachine&& fsm) = default; inline ~StateMachine(); diff --git a/SDK/include/NDK/StateMachine.inl b/SDK/include/NDK/StateMachine.inl index 0a1e195c4..208a609a3 100644 --- a/SDK/include/NDK/StateMachine.inl +++ b/SDK/include/NDK/StateMachine.inl @@ -2,11 +2,24 @@ // This file is part of the "Nazara Development Kit" // For conditions of distribution and use, see copyright notice in Prerequesites.hpp +#include #include #include namespace Ndk { + inline StateMachine::StateMachine(std::shared_ptr originalState) : + m_currentState(std::move(originalState)) + { + NazaraAssert(m_currentState, "StateMachine must have a state to begin with"); + } + + inline StateMachine::~StateMachine() + { + m_currentState->Leave(*this); + } + + inline void StateMachine::ChangeState(std::shared_ptr state) { m_nextState = std::move(state); From 8ae493b53550ebb07cbb01099100c8d5392a1bff Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 29 Mar 2016 23:42:25 +0200 Subject: [PATCH 6/6] Ndk/StateMachine: Fix constructor not calling State::Enter Former-commit-id: 8e66600c19c53617cab1d889d8fa3c219bc9ea19 --- SDK/include/NDK/StateMachine.inl | 1 + 1 file changed, 1 insertion(+) diff --git a/SDK/include/NDK/StateMachine.inl b/SDK/include/NDK/StateMachine.inl index 208a609a3..fc4c7f787 100644 --- a/SDK/include/NDK/StateMachine.inl +++ b/SDK/include/NDK/StateMachine.inl @@ -12,6 +12,7 @@ namespace Ndk m_currentState(std::move(originalState)) { NazaraAssert(m_currentState, "StateMachine must have a state to begin with"); + m_currentState->Enter(*this); } inline StateMachine::~StateMachine()