Add current work
Former-commit-id: 06dd34f00951e334b330c067f61f69fc73dcc2d2
This commit is contained in:
parent
3f9a4170f1
commit
83884015fc
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -21,5 +30,32 @@ namespace Ndk
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
Loading…
Reference in New Issue