[deploy] update Runner

This commit is contained in:
Nomango 2020-03-31 14:07:13 +08:00
parent a6961c0d20
commit a1ed803bc1
5 changed files with 29 additions and 44 deletions

View File

@ -63,17 +63,22 @@ void Application::Run(RunnerPtr runner, bool debug)
} }
// Everything is ready // Everything is ready
runner->Ready(); runner->OnReady();
quiting_ = false; quiting_ = false;
last_update_time_ = Time::Now();
while (!quiting_) while (!quiting_)
{ {
if (!runner->MainLoop()) const Time now = Time::Now();
const Duration dt = (now - last_update_time_);
last_update_time_ = now;
if (!runner->MainLoop(dt))
quiting_ = true; quiting_ = true;
} }
// Destroy all resources // Destroy all resources
runner->Destroy(); runner->OnDestroy();
this->Destroy(); this->Destroy();
} }

View File

@ -136,10 +136,17 @@ public:
*/ */
void Destroy(); void Destroy();
/**
* \~chinese
* @brief
*/
Time GetLastUpdateTime() const;
private: private:
bool quiting_; bool quiting_;
float time_scale_; float time_scale_;
RunnerPtr runner_; RunnerPtr runner_;
Time last_update_time_;
List<Module*> modules_; List<Module*> modules_;
std::mutex perform_mutex_; std::mutex perform_mutex_;
Queue<Function<void()>> functions_to_perform_; Queue<Function<void()>> functions_to_perform_;
@ -156,4 +163,9 @@ inline WindowPtr Application::GetMainWindow() const
return runner_->GetMainWindow(); return runner_->GetMainWindow();
} }
inline Time Application::GetLastUpdateTime() const
{
return last_update_time_;
}
} // namespace kiwano } // namespace kiwano

View File

@ -69,7 +69,7 @@ Runner::Runner() {}
Runner::~Runner() {} Runner::~Runner() {}
bool Runner::MainLoop() bool Runner::MainLoop(Duration dt)
{ {
if (main_window_->ShouldClose()) if (main_window_->ShouldClose())
{ {
@ -88,30 +88,9 @@ bool Runner::MainLoop()
} }
// Update & render // Update & render
const Time now = Time::Now();
const Duration dt = (now - last_update_time_);
last_update_time_ = now;
app.Update(dt); app.Update(dt);
app.Render(); app.Render();
return true; return true;
} }
void Runner::Ready()
{
OnReady();
last_update_time_ = Time::Now();
}
void Runner::Destroy()
{
OnDestroy();
if (main_window_)
{
main_window_->Destroy();
main_window_.Reset();
}
}
} // namespace kiwano } // namespace kiwano

View File

@ -73,9 +73,10 @@ public:
/// \~chinese /// \~chinese
/// @brief 应用程序主循环 /// @brief 应用程序主循环
/// @details 重载该函数以 /// @param dt 时间间隔
/// @details 重载该函数以控制程序主循环
/// @return 返回false退出主循环否则继续运行主循环 /// @return 返回false退出主循环否则继续运行主循环
virtual bool MainLoop(); virtual bool MainLoop(Duration dt);
/// \~chinese /// \~chinese
/// @brief 获取主窗口 /// @brief 获取主窗口
@ -85,18 +86,8 @@ public:
/// @brief 设置主窗口 /// @brief 设置主窗口
void SetMainWindow(WindowPtr window); void SetMainWindow(WindowPtr window);
/// \~chinese
/// @brief 获取上一次更新时间
Time GetLastUpdateTime() const;
private:
void Ready();
void Destroy();
private: private:
WindowPtr main_window_; WindowPtr main_window_;
Time last_update_time_;
}; };
inline void Runner::OnReady() {} inline void Runner::OnReady() {}
@ -118,9 +109,4 @@ inline void Runner::SetMainWindow(WindowPtr window)
main_window_ = window; main_window_ = window;
} }
inline Time Runner::GetLastUpdateTime() const
{
return last_update_time_;
}
} // namespace kiwano } // namespace kiwano

View File

@ -205,7 +205,10 @@ WindowWin32Impl::WindowWin32Impl()
key_map_[VK_F1 + i] = KeyCode(size_t(KeyCode::F1) + i); key_map_[VK_F1 + i] = KeyCode(size_t(KeyCode::F1) + i);
} }
WindowWin32Impl::~WindowWin32Impl() {} WindowWin32Impl::~WindowWin32Impl()
{
this->Destroy();
}
void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable,
bool fullscreen) bool fullscreen)