Sdk/Application: Add Console and FPSCounter overlays

This allows any Nazara-powered application to enable a ready-to-use
console or a working FPS counter


Former-commit-id: 274b276313b530ff8c99f3e0fb43c8b78689d787 [formerly 36e3a7e9d0d050af4b217311bde4496597e37c37] [formerly 69146c2fc6887f86b9c39d0265a95a4ba003f41e [formerly 72a66d7a31ceb87197b800813c90d43f73fc26b4]]
Former-commit-id: 3fda987313648101b8a423e5e3aa8b0cd67a1c72 [formerly 5f9d42cdc5ed5443846531bad26059f66670df63]
Former-commit-id: 2ef1496fb4a379e14f9c11c788da306eff465719
This commit is contained in:
Lynix
2016-08-28 18:10:09 +02:00
parent 449bcfc462
commit 6872616ca4
5 changed files with 423 additions and 120 deletions

View File

@@ -4,6 +4,15 @@
#include <NDK/Application.hpp>
#ifndef NDK_SERVER
#include <NDK/Components/CameraComponent.hpp>
#include <NDK/Components/GraphicsComponent.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Systems/RenderSystem.hpp>
#include <NDK/LuaAPI.hpp>
#include <Nazara/Utility/SimpleTextDrawer.hpp>
#endif
namespace Ndk
{
/*!
@@ -15,7 +24,6 @@ namespace Ndk
/*!
* \brief Runs the application by updating worlds, taking care about windows, ...
*/
bool Application::Run()
{
#ifndef NDK_SERVER
@@ -24,7 +32,7 @@ namespace Ndk
auto it = m_windows.begin();
while (it != m_windows.end())
{
Nz::Window& window = **it;
Nz::Window& window = *it->window;
window.ProcessEvents();
@@ -54,8 +62,128 @@ namespace Ndk
for (World& world : m_worlds)
world.Update(m_updateTime);
#ifndef NDK_SERVER
for (WindowInfo& info : m_windows)
{
if (info.fpsCounter)
{
FPSCounterOverlay& fpsCounter = *info.fpsCounter;
fpsCounter.frameCount++;
fpsCounter.elapsedTime += m_updateTime;
if (fpsCounter.elapsedTime >= 1.f)
{
fpsCounter.sprite->Update(Nz::SimpleTextDrawer::Draw("FPS: " + Nz::String::Number(fpsCounter.frameCount), 36));
fpsCounter.frameCount = 0;
fpsCounter.elapsedTime = 0.f;
}
}
info.overlayWorld->Update(m_updateTime);
}
#endif
return true;
}
void Application::SetupConsole(WindowInfo& info)
{
std::unique_ptr<ConsoleOverlay> overlay = std::make_unique<ConsoleOverlay>();
overlay->console = std::make_unique<Console>(*info.overlayWorld, Nz::Vector2f(Nz::Vector2ui(info.window->GetWidth(), info.window->GetHeight() / 4)), overlay->lua);
Console& consoleRef = *overlay->console;
// Redirect logs toward the console
overlay->logSlot.Connect(Nz::Log::OnLogWrite, [&consoleRef] (const Nz::String& str)
{
consoleRef.AddLine(str);
});
LuaAPI::RegisterClasses(overlay->lua);
// Override "print" function to add a line in the console
overlay->lua.PushFunction([&consoleRef] (Nz::LuaInstance& instance)
{
Nz::StringStream stream;
unsigned int argCount = instance.GetStackTop();
instance.GetGlobal("tostring");
for (unsigned int i = 1; i <= argCount; ++i)
{
instance.PushValue(-1); // tostring function
instance.PushValue(i); // argument
instance.Call(1, 1);
std::size_t length;
const char* str = instance.CheckString(-1, &length);
if (i > 1)
stream << '\t';
stream << Nz::String(str, length);
instance.Pop(1);
}
consoleRef.AddLine(stream);
return 0;
});
overlay->lua.SetGlobal("print");
// Define a few base variables to allow our interface to interact with the application
overlay->lua.PushGlobal("Application", Ndk::Application::Instance());
overlay->lua.PushGlobal("Console", consoleRef.CreateHandle());
// Setup a few event callback to handle the console
Nz::EventHandler& eventHandler = info.window->GetEventHandler();
overlay->eventSlot.Connect(eventHandler.OnEvent, [&consoleRef] (const Nz::EventHandler*, const Nz::WindowEvent& event)
{
if (consoleRef.IsVisible())
consoleRef.SendEvent(event);
});
overlay->keyPressedSlot.Connect(eventHandler.OnKeyPressed, [&consoleRef] (const Nz::EventHandler*, const Nz::WindowEvent::KeyEvent& event)
{
if (event.code == Nz::Keyboard::F9)
consoleRef.Show(!consoleRef.IsVisible());
});
overlay->resizedSlot.Connect(eventHandler.OnResized, [&consoleRef] (const Nz::EventHandler*, const Nz::WindowEvent::SizeEvent& event)
{
consoleRef.SetSize({float(event.width), event.height / 4.f});
});
info.console = std::move(overlay);
}
void Application::SetupFPSCounter(WindowInfo& info)
{
std::unique_ptr<FPSCounterOverlay> fpsCounter = std::make_unique<FPSCounterOverlay>();
fpsCounter->sprite = Nz::TextSprite::New();
fpsCounter->entity = info.overlayWorld->CreateEntity();
fpsCounter->entity->AddComponent<NodeComponent>();
fpsCounter->entity->AddComponent<GraphicsComponent>().Attach(fpsCounter->sprite);
info.fpsCounter = std::move(fpsCounter);
}
void Application::SetupOverlay(WindowInfo& info)
{
info.overlayWorld = std::make_unique<World>(false); //< No default system
RenderSystem& renderSystem = info.overlayWorld->AddSystem<RenderSystem>();
renderSystem.ChangeRenderTechnique<Nz::ForwardRenderTechnique>();
renderSystem.SetDefaultBackground(nullptr);
renderSystem.SetGlobalUp(Nz::Vector3f::Down());
EntityHandle viewer = info.overlayWorld->CreateEntity();
CameraComponent& camComponent = viewer->AddComponent<CameraComponent>();
viewer->AddComponent<NodeComponent>();
camComponent.SetProjectionType(Nz::ProjectionType_Orthogonal);
camComponent.SetTarget(info.renderTarget);
}
Application* Application::s_application = nullptr;
}

View File

@@ -17,7 +17,14 @@ namespace Ndk
#ifndef NDK_SERVER
//application.SetMethod("AddWindow", &Application::AddWindow);
application.BindMethod("EnableConsole", &Application::EnableConsole);
application.BindMethod("EnableFPSCounter", &Application::EnableFPSCounter);
application.BindMethod("IsConsoleEnabled", &Application::IsConsoleEnabled);
application.BindMethod("IsFPSCounterEnabled", &Application::IsFPSCounterEnabled);
#endif
application.BindMethod("AddWorld", [] (Nz::LuaInstance& instance, Application* application) -> int
{
instance.Push(application->AddWorld().CreateHandle());