Merge branch 'application'

Former-commit-id: e0f348fdcdd66784c9265ca8f8b55bb70fd423fd
This commit is contained in:
Lynix 2016-03-30 19:45:33 +02:00
commit 9b988f8960
6 changed files with 225 additions and 1 deletions

View File

@ -8,6 +8,10 @@
#define NDK_APPLICATION_HPP
#include <NDK/Prerequesites.hpp>
#include <NDK/World.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Utility/Window.hpp>
#include <vector>
namespace Ndk
{
@ -15,7 +19,35 @@ namespace Ndk
{
public:
inline Application();
Application(const Application&) = delete;
inline ~Application();
#ifndef NDK_SERVER
template<typename T, typename... Args> T& AddWindow(Args&&... args);
#endif
template<typename... Args> World& AddWorld(Args&&... args);
bool Run();
inline void Quit();
Application& operator=(const Application&) = delete;
inline static Application* Instance();
private:
#ifndef NDK_SERVER
std::vector<std::unique_ptr<Nz::Window>> m_windows;
#endif
std::vector<World> m_worlds;
Nz::Clock m_updateClock;
#ifndef NDK_SERVER
bool m_exitOnClosedWindows;
#endif
bool m_shouldQuit;
float m_updateTime;
static Application* s_application;
};
}

View File

@ -3,12 +3,21 @@
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
#include <Nazara/Core/ErrorFlags.hpp>
#include <type_traits>
#include <NDK/Sdk.hpp>
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
@ -17,9 +26,41 @@ namespace Ndk
inline Application::~Application()
{
m_worlds.clear();
#ifndef NDK_SERVER
m_windows.clear();
#endif
// Libération du SDK
Sdk::Uninitialize();
// Libération automatique des modules
s_application = nullptr;
}
template<typename T, typename... Args>
T& Application::AddWindow(Args&&... args)
{
static_assert(std::is_base_of<Nz::Window, T>::value, "Type must inherit Window");
m_windows.emplace_back(new T(std::forward<Args>(args)...));
return static_cast<T&>(*m_windows.back().get());
}
template<typename... Args>
World& Application::AddWorld(Args&&... args)
{
m_worlds.emplace_back(std::forward<Args>(args)...);
return m_worlds.back();
}
inline void Application::Quit()
{
m_shouldQuit = true;
}
inline Application* Application::Instance()
{
return s_application;
}
}

28
SDK/include/NDK/State.hpp Normal file
View File

@ -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 <NDK/Prerequesites.hpp>
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

View File

@ -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 <NDK/Prerequesites.hpp>
#include <NDK/State.hpp>
#include <memory>
namespace Ndk
{
class StateMachine
{
public:
inline StateMachine(std::shared_ptr<State> originalState);
StateMachine(const StateMachine&) = delete;
inline StateMachine(StateMachine&& fsm) = default;
inline ~StateMachine();
inline void ChangeState(std::shared_ptr<State> state);
inline bool Update(float elapsedTime);
inline StateMachine& operator=(StateMachine&& fsm) = default;
StateMachine& operator=(const StateMachine&) = delete;
private:
std::shared_ptr<State> m_currentState;
std::shared_ptr<State> m_nextState;
};
}
#include <NDK/StateMachine.inl>
#endif // NDK_STATEMACHINE_HPP

View File

@ -0,0 +1,40 @@
// 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 <Nazara/Core/Error.hpp>
#include <NDK/StateMachine.hpp>
#include <utility>
namespace Ndk
{
inline StateMachine::StateMachine(std::shared_ptr<State> originalState) :
m_currentState(std::move(originalState))
{
NazaraAssert(m_currentState, "StateMachine must have a state to begin with");
m_currentState->Enter(*this);
}
inline StateMachine::~StateMachine()
{
m_currentState->Leave(*this);
}
inline void StateMachine::ChangeState(std::shared_ptr<State> 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);
}
}

View File

@ -3,3 +3,47 @@
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
#include <NDK/Application.hpp>
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;
}