Feature: runner settings
This commit is contained in:
parent
ee165a234f
commit
a62f1f4d2d
|
|
@ -28,7 +28,7 @@ void ImGuiModule::SetupModule()
|
||||||
// Setup Dear ImGui style
|
// Setup Dear ImGui style
|
||||||
ImGui::StyleColorsDark();
|
ImGui::StyleColorsDark();
|
||||||
|
|
||||||
window_ = Application::GetInstance().GetMainWindow();
|
window_ = Application::GetInstance().GetWindow();
|
||||||
|
|
||||||
// Setup Platform/Renderer bindings
|
// Setup Platform/Renderer bindings
|
||||||
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
|
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
|
||||||
|
|
|
||||||
|
|
@ -62,14 +62,14 @@ void Button::SetStatus(Status status)
|
||||||
|
|
||||||
if (status == Status::Normal)
|
if (status == Status::Normal)
|
||||||
{
|
{
|
||||||
Application::GetInstance().GetMainWindow()->SetCursor(CursorType::Arrow);
|
Application::GetInstance().GetWindow()->SetCursor(CursorType::Arrow);
|
||||||
|
|
||||||
if (mouse_out_callback_)
|
if (mouse_out_callback_)
|
||||||
mouse_out_callback_(this, GetBoundActor());
|
mouse_out_callback_(this, GetBoundActor());
|
||||||
}
|
}
|
||||||
else if (status == Status::Hover)
|
else if (status == Status::Hover)
|
||||||
{
|
{
|
||||||
Application::GetInstance().GetMainWindow()->SetCursor(CursorType::Hand);
|
Application::GetInstance().GetWindow()->SetCursor(CursorType::Hand);
|
||||||
|
|
||||||
if (old_status != Status::Pressed)
|
if (old_status != Status::Pressed)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,8 @@
|
||||||
// THE SOFTWARE.
|
// THE SOFTWARE.
|
||||||
|
|
||||||
#include <kiwano/platform/Application.h>
|
#include <kiwano/platform/Application.h>
|
||||||
#include <kiwano/platform/Input.h>
|
|
||||||
#include <kiwano/base/Director.h>
|
|
||||||
#include <kiwano/utils/Logger.h>
|
#include <kiwano/utils/Logger.h>
|
||||||
|
#include <kiwano/base/Director.h>
|
||||||
#include <kiwano/render/Renderer.h>
|
#include <kiwano/render/Renderer.h>
|
||||||
#include <kiwano/render/TextureCache.h>
|
#include <kiwano/render/TextureCache.h>
|
||||||
#include <kiwano/utils/ResourceCache.h>
|
#include <kiwano/utils/ResourceCache.h>
|
||||||
|
|
@ -39,9 +38,6 @@ Application::Application()
|
||||||
, is_paused_(false)
|
, is_paused_(false)
|
||||||
, time_scale_(1.f)
|
, time_scale_(1.f)
|
||||||
{
|
{
|
||||||
Use(Renderer::GetInstance());
|
|
||||||
Use(Input::GetInstance());
|
|
||||||
Use(Director::GetInstance());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Application::~Application()
|
Application::~Application()
|
||||||
|
|
@ -49,25 +45,22 @@ Application::~Application()
|
||||||
this->Destroy();
|
this->Destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::Run(RunnerPtr runner, bool debug)
|
void Application::Run(RunnerPtr runner)
|
||||||
{
|
{
|
||||||
KGE_ASSERT(runner);
|
KGE_ASSERT(runner);
|
||||||
runner_ = runner;
|
runner_ = runner;
|
||||||
running_ = true;
|
running_ = true;
|
||||||
is_paused_ = false;
|
is_paused_ = false;
|
||||||
|
|
||||||
|
// Initialize runner
|
||||||
|
runner->InitSettings();
|
||||||
|
|
||||||
// Setup all modules
|
// Setup all modules
|
||||||
for (auto c : modules_)
|
for (auto c : modules_)
|
||||||
{
|
{
|
||||||
c->SetupModule();
|
c->SetupModule();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (debug)
|
|
||||||
{
|
|
||||||
Director::GetInstance().ShowDebugInfo(true);
|
|
||||||
Renderer::GetInstance().GetContext().SetCollectingStatus(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Everything is ready
|
// Everything is ready
|
||||||
runner->OnReady();
|
runner->OnReady();
|
||||||
|
|
||||||
|
|
@ -128,16 +121,19 @@ void Application::Destroy()
|
||||||
runner_ = nullptr;
|
runner_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear all resources
|
// Clear user resources
|
||||||
Director::GetInstance().ClearStages();
|
Director::GetInstance().ClearStages();
|
||||||
ResourceCache::GetInstance().Clear();
|
ResourceCache::GetInstance().Clear();
|
||||||
TextureCache::GetInstance().Clear();
|
|
||||||
|
|
||||||
for (auto iter = modules_.rbegin(); iter != modules_.rend(); ++iter)
|
for (auto iter = modules_.rbegin(); iter != modules_.rend(); ++iter)
|
||||||
{
|
{
|
||||||
(*iter)->DestroyModule();
|
(*iter)->DestroyModule();
|
||||||
}
|
}
|
||||||
modules_.clear();
|
modules_.clear();
|
||||||
|
|
||||||
|
// Clear device resources
|
||||||
|
TextureCache::GetInstance().Clear();
|
||||||
|
Renderer::GetInstance().Destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::Use(Module& module)
|
void Application::Use(Module& module)
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ public:
|
||||||
* @param debug 是否启用调试模式
|
* @param debug 是否启用调试模式
|
||||||
* @note 该函数是阻塞的,应用程序结束时函数返回
|
* @note 该函数是阻塞的,应用程序结束时函数返回
|
||||||
*/
|
*/
|
||||||
void Run(RunnerPtr runner, bool debug = false);
|
void Run(RunnerPtr runner);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \~chinese
|
* \~chinese
|
||||||
|
|
@ -99,9 +99,9 @@ public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \~chinese
|
* \~chinese
|
||||||
* @brief »ñÈ¡Ö÷´°¿Ú
|
* @brief »ñÈ¡´°¿Ú
|
||||||
*/
|
*/
|
||||||
WindowPtr GetMainWindow() const;
|
WindowPtr GetWindow() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \~chinese
|
* \~chinese
|
||||||
|
|
@ -183,10 +183,10 @@ inline RunnerPtr Application::GetRunner() const
|
||||||
return runner_;
|
return runner_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline WindowPtr Application::GetMainWindow() const
|
inline WindowPtr Application::GetWindow() const
|
||||||
{
|
{
|
||||||
KGE_ASSERT(runner_);
|
KGE_ASSERT(runner_);
|
||||||
return runner_->GetMainWindow();
|
return runner_->GetWindow();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline TickerPtr Application::GetFrameTicker() const
|
inline TickerPtr Application::GetFrameTicker() const
|
||||||
|
|
|
||||||
|
|
@ -20,24 +20,25 @@
|
||||||
|
|
||||||
#include <kiwano/utils/Logger.h>
|
#include <kiwano/utils/Logger.h>
|
||||||
#include <kiwano/platform/Runner.h>
|
#include <kiwano/platform/Runner.h>
|
||||||
|
#include <kiwano/platform/Input.h>
|
||||||
#include <kiwano/platform/Application.h>
|
#include <kiwano/platform/Application.h>
|
||||||
|
#include <kiwano/render/Renderer.h>
|
||||||
#define KGE_MAX_SKIP_FRAMES 10
|
#include <kiwano/base/Director.h>
|
||||||
|
|
||||||
namespace kiwano
|
namespace kiwano
|
||||||
{
|
{
|
||||||
|
|
||||||
RunnerPtr Runner::Create(WindowPtr main_window)
|
RunnerPtr Runner::Create(Settings settings)
|
||||||
{
|
{
|
||||||
RunnerPtr ptr = memory::New<Runner>();
|
RunnerPtr ptr = memory::New<Runner>();
|
||||||
if (ptr)
|
if (ptr)
|
||||||
{
|
{
|
||||||
ptr->SetMainWindow(main_window);
|
ptr->SetSettings(settings);
|
||||||
}
|
}
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
RunnerPtr Runner::Create(WindowPtr main_window, Function<void()> on_ready, Function<void()> on_destroy)
|
RunnerPtr Runner::Create(Settings settings, Function<void()> on_ready, Function<void()> on_destroy)
|
||||||
{
|
{
|
||||||
class CallbackRunner : public Runner
|
class CallbackRunner : public Runner
|
||||||
{
|
{
|
||||||
|
|
@ -63,7 +64,7 @@ RunnerPtr Runner::Create(WindowPtr main_window, Function<void()> on_ready, Funct
|
||||||
{
|
{
|
||||||
ptr->on_ready = on_ready;
|
ptr->on_ready = on_ready;
|
||||||
ptr->on_destroy = on_destroy;
|
ptr->on_destroy = on_destroy;
|
||||||
ptr->SetMainWindow(main_window);
|
ptr->SetSettings(settings);
|
||||||
}
|
}
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
@ -72,6 +73,36 @@ Runner::Runner() {}
|
||||||
|
|
||||||
Runner::~Runner() {}
|
Runner::~Runner() {}
|
||||||
|
|
||||||
|
void Runner::InitSettings()
|
||||||
|
{
|
||||||
|
if (settings_.debug_mode)
|
||||||
|
{
|
||||||
|
// Show console window before creating main window
|
||||||
|
Logger::GetInstance().ShowConsole(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create game window
|
||||||
|
WindowPtr window =
|
||||||
|
Window::Create(settings_.title, settings_.width, settings_.height, settings_.icon, settings_.resizable);
|
||||||
|
SetWindow(window);
|
||||||
|
|
||||||
|
// Update renderer settings
|
||||||
|
Renderer::GetInstance().MakeContextForWindow(window);
|
||||||
|
Renderer::GetInstance().SetClearColor(settings_.bg_color);
|
||||||
|
Renderer::GetInstance().SetVSyncEnabled(settings_.vsync_enabled);
|
||||||
|
|
||||||
|
// Use defaut modules
|
||||||
|
Application::GetInstance().Use(Input::GetInstance());
|
||||||
|
Application::GetInstance().Use(Director::GetInstance());
|
||||||
|
|
||||||
|
// Enable debug mode
|
||||||
|
if (settings_.debug_mode)
|
||||||
|
{
|
||||||
|
Director::GetInstance().ShowDebugInfo(true);
|
||||||
|
Renderer::GetInstance().GetContext().SetCollectingStatus(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool Runner::MainLoop(Duration dt)
|
bool Runner::MainLoop(Duration dt)
|
||||||
{
|
{
|
||||||
if (!main_window_)
|
if (!main_window_)
|
||||||
|
|
@ -79,7 +110,7 @@ bool Runner::MainLoop(Duration dt)
|
||||||
|
|
||||||
if (main_window_->ShouldClose())
|
if (main_window_->ShouldClose())
|
||||||
{
|
{
|
||||||
if (this->OnClosing())
|
if (this->OnClose())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
main_window_->SetShouldClose(false);
|
main_window_->SetShouldClose(false);
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,8 @@
|
||||||
#include <kiwano/core/Common.h>
|
#include <kiwano/core/Common.h>
|
||||||
#include <kiwano/core/Time.h>
|
#include <kiwano/core/Time.h>
|
||||||
#include <kiwano/platform/Window.h>
|
#include <kiwano/platform/Window.h>
|
||||||
|
#include <kiwano/render/Color.h>
|
||||||
|
#include <kiwano/render/Texture.h>
|
||||||
|
|
||||||
namespace kiwano
|
namespace kiwano
|
||||||
{
|
{
|
||||||
|
|
@ -30,26 +32,52 @@ class Application;
|
||||||
|
|
||||||
KGE_DECLARE_SMART_PTR(Runner);
|
KGE_DECLARE_SMART_PTR(Runner);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \~chinese
|
||||||
|
* @brief 游戏设置
|
||||||
|
*/
|
||||||
|
struct Settings
|
||||||
|
{
|
||||||
|
uint32_t width; ///< 窗口宽度
|
||||||
|
uint32_t height; ///< 窗口高度
|
||||||
|
String title; ///< 窗口标题
|
||||||
|
uint32_t icon; ///< 窗口图标
|
||||||
|
bool resizable; ///< 窗口大小可调整
|
||||||
|
Color bg_color; ///< 窗口背景色
|
||||||
|
bool vsync_enabled; ///< 垂直同步
|
||||||
|
bool debug_mode; ///< 调试模式
|
||||||
|
|
||||||
|
Settings()
|
||||||
|
: width(800)
|
||||||
|
, height(600)
|
||||||
|
, title("Kiwano")
|
||||||
|
, icon()
|
||||||
|
, resizable(false)
|
||||||
|
, bg_color(Color::Black)
|
||||||
|
, vsync_enabled(true)
|
||||||
|
, debug_mode(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \~chinese
|
* \~chinese
|
||||||
* @brief 程序运行器
|
* @brief 程序运行器
|
||||||
*/
|
*/
|
||||||
class KGE_API Runner : public ObjectBase
|
class KGE_API Runner : public ObjectBase
|
||||||
{
|
{
|
||||||
friend class Application;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 创建程序运行器
|
/// @brief 创建程序运行器
|
||||||
/// @param main_window 主窗口
|
/// @param main_window 主窗口
|
||||||
static RunnerPtr Create(WindowPtr main_window);
|
static RunnerPtr Create(Settings settings);
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 创建程序运行器
|
/// @brief 创建程序运行器
|
||||||
/// @param main_window 主窗口
|
/// @param main_window 主窗口
|
||||||
/// @param on_ready 应用程序初始化完成后执行的回调函数
|
/// @param on_ready 应用程序初始化完成后执行的回调函数
|
||||||
/// @param on_destroy 应用程序销毁时执行的回调函数
|
/// @param on_destroy 应用程序销毁时执行的回调函数
|
||||||
static RunnerPtr Create(WindowPtr main_window, Function<void()> on_ready, Function<void()> on_destroy = nullptr);
|
static RunnerPtr Create(Settings settings, Function<void()> on_ready, Function<void()> on_destroy = nullptr);
|
||||||
|
|
||||||
Runner();
|
Runner();
|
||||||
|
|
||||||
|
|
@ -69,7 +97,7 @@ public:
|
||||||
/// @brief 应用程序关闭处理
|
/// @brief 应用程序关闭处理
|
||||||
/// @details 重载该函数以处理用户关闭应用程序时的行为,如保存用户数据等
|
/// @details 重载该函数以处理用户关闭应用程序时的行为,如保存用户数据等
|
||||||
/// @return 返回true允许用户关闭程序,否则阻止程序关闭
|
/// @return 返回true允许用户关闭程序,否则阻止程序关闭
|
||||||
virtual bool OnClosing();
|
virtual bool OnClose();
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 应用程序主循环
|
/// @brief 应用程序主循环
|
||||||
|
|
@ -79,14 +107,29 @@ public:
|
||||||
virtual bool MainLoop(Duration dt);
|
virtual bool MainLoop(Duration dt);
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief »ñÈ¡Ö÷´°¿Ú
|
/// @brief 获取窗口
|
||||||
WindowPtr GetMainWindow() const;
|
WindowPtr GetWindow() const;
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief ÉèÖÃÖ÷´°¿Ú
|
/// @brief 设置窗口
|
||||||
void SetMainWindow(WindowPtr window);
|
void SetWindow(WindowPtr window);
|
||||||
|
|
||||||
|
/// \~chinese
|
||||||
|
/// @brief 获取设置
|
||||||
|
Settings GetSettings() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/// \~chinese
|
||||||
|
/// @brief 修改设置
|
||||||
|
void SetSettings(Settings settings);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
friend class Application;
|
||||||
|
|
||||||
|
void InitSettings();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Settings settings_;
|
||||||
WindowPtr main_window_;
|
WindowPtr main_window_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -94,19 +137,29 @@ inline void Runner::OnReady() {}
|
||||||
|
|
||||||
inline void Runner::OnDestroy() {}
|
inline void Runner::OnDestroy() {}
|
||||||
|
|
||||||
inline bool Runner::OnClosing()
|
inline bool Runner::OnClose()
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline WindowPtr Runner::GetMainWindow() const
|
inline WindowPtr Runner::GetWindow() const
|
||||||
{
|
{
|
||||||
return main_window_;
|
return main_window_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Runner::SetMainWindow(WindowPtr window)
|
inline void Runner::SetWindow(WindowPtr window)
|
||||||
{
|
{
|
||||||
main_window_ = window;
|
main_window_ = window;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline Settings Runner::GetSettings() const
|
||||||
|
{
|
||||||
|
return settings_;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void Runner::SetSettings(Settings settings)
|
||||||
|
{
|
||||||
|
settings_ = settings;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace kiwano
|
} // namespace kiwano
|
||||||
|
|
|
||||||
|
|
@ -79,13 +79,13 @@ public:
|
||||||
|
|
||||||
void Resize(uint32_t width, uint32_t height) override;
|
void Resize(uint32_t width, uint32_t height) override;
|
||||||
|
|
||||||
protected:
|
|
||||||
RendererImpl();
|
|
||||||
|
|
||||||
void MakeContextForWindow(WindowPtr window) override;
|
void MakeContextForWindow(WindowPtr window) override;
|
||||||
|
|
||||||
void Destroy() override;
|
void Destroy() override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
RendererImpl();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using ID2DDeviceResources = kiwano::graphics::directx::ID2DDeviceResources;
|
using ID2DDeviceResources = kiwano::graphics::directx::ID2DDeviceResources;
|
||||||
using ID3DDeviceResources = kiwano::graphics::directx::ID3DDeviceResources;
|
using ID3DDeviceResources = kiwano::graphics::directx::ID3DDeviceResources;
|
||||||
|
|
|
||||||
|
|
@ -31,22 +31,6 @@ Renderer::Renderer()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::SetupModule()
|
|
||||||
{
|
|
||||||
WindowPtr window = Application::GetInstance().GetMainWindow();
|
|
||||||
MakeContextForWindow(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Renderer::DestroyModule()
|
|
||||||
{
|
|
||||||
Destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Renderer::HandleEvent(Event* evt)
|
|
||||||
{
|
|
||||||
// DO NOTHING
|
|
||||||
}
|
|
||||||
|
|
||||||
void Renderer::BeginDraw()
|
void Renderer::BeginDraw()
|
||||||
{
|
{
|
||||||
KGE_ASSERT(render_ctx_);
|
KGE_ASSERT(render_ctx_);
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ namespace kiwano
|
||||||
* \~chinese
|
* \~chinese
|
||||||
* @brief 渲染器
|
* @brief 渲染器
|
||||||
*/
|
*/
|
||||||
class KGE_API Renderer : public EventModule
|
class KGE_API Renderer : public Noncopyable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
|
|
@ -235,20 +235,17 @@ public:
|
||||||
/// @throw kiwano::SystemError 呈现失败时抛出
|
/// @throw kiwano::SystemError 呈现失败时抛出
|
||||||
virtual void Present() = 0;
|
virtual void Present() = 0;
|
||||||
|
|
||||||
public:
|
/// \~chinese
|
||||||
void SetupModule() override;
|
/// @brief 为窗口创建渲染上下文
|
||||||
|
virtual void MakeContextForWindow(WindowPtr window) = 0;
|
||||||
|
|
||||||
void DestroyModule() override;
|
/// \~chinese
|
||||||
|
/// @brief 销毁渲染器资源
|
||||||
void HandleEvent(Event* evt) override;
|
virtual void Destroy() = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Renderer();
|
Renderer();
|
||||||
|
|
||||||
virtual void MakeContextForWindow(WindowPtr window) = 0;
|
|
||||||
|
|
||||||
virtual void Destroy() = 0;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool vsync_;
|
bool vsync_;
|
||||||
Color clear_color_;
|
Color clear_color_;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue