use application scoped timer

This commit is contained in:
Nomango 2020-05-20 00:55:51 +08:00
parent 1805c016f8
commit 86bb0e4239
3 changed files with 30 additions and 19 deletions

View File

@ -69,14 +69,12 @@ void Application::Run(RunnerPtr runner, bool debug)
runner->OnReady(); runner->OnReady();
running_ = true; running_ = true;
last_update_time_ = Time::Now(); timer_ = Timer::Create();
while (running_) while (running_)
{ {
const Time now = Time::Now(); timer_->Tick();
const Duration dt = (now - last_update_time_);
last_update_time_ = now;
if (!runner->MainLoop(dt)) if (!runner->MainLoop(timer_->GetDeltaTime()))
running_ = false; running_ = false;
} }

View File

@ -23,6 +23,7 @@
#include <kiwano/core/Common.h> #include <kiwano/core/Common.h>
#include <kiwano/core/Module.h> #include <kiwano/core/Module.h>
#include <kiwano/core/Time.h> #include <kiwano/core/Time.h>
#include <kiwano/core/Timer.h>
#include <kiwano/core/Singleton.h> #include <kiwano/core/Singleton.h>
#include <kiwano/core/event/Event.h> #include <kiwano/core/event/Event.h>
#include <kiwano/platform/Runner.h> #include <kiwano/platform/Runner.h>
@ -77,6 +78,12 @@ public:
*/ */
WindowPtr GetMainWindow() const; WindowPtr GetMainWindow() const;
/**
* \~chinese
* @brief
*/
TimerPtr GetTimer() const;
/** /**
* \~chinese * \~chinese
* @brief ÊÇ·ñÕýÔÚÔËÐÐ * @brief ÊÇ·ñÕýÔÚÔËÐÐ
@ -142,17 +149,11 @@ public:
*/ */
void Destroy(); void Destroy();
/**
* \~chinese
* @brief
*/
Time GetLastUpdateTime() const;
private: private:
bool running_; bool running_;
float time_scale_; float time_scale_;
RunnerPtr runner_; RunnerPtr runner_;
Time last_update_time_; TimerPtr timer_;
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_;
@ -169,14 +170,14 @@ inline WindowPtr Application::GetMainWindow() const
return runner_->GetMainWindow(); return runner_->GetMainWindow();
} }
inline TimerPtr Application::GetTimer() const
{
return timer_;
}
inline bool Application::IsRunning() const inline bool Application::IsRunning() const
{ {
return running_; return running_;
} }
inline Time Application::GetLastUpdateTime() const
{
return last_update_time_;
}
} // namespace kiwano } // namespace kiwano

View File

@ -502,9 +502,21 @@ LRESULT WindowWin32Impl::MessageProc(HWND hwnd, UINT32 msg, WPARAM wparam, LPARA
case WM_ACTIVATE: case WM_ACTIVATE:
{ {
bool active = (LOWORD(wparam) != WA_INACTIVE);
WindowFocusChangedEventPtr evt = new WindowFocusChangedEvent; WindowFocusChangedEventPtr evt = new WindowFocusChangedEvent;
evt->focus = (LOWORD(wparam) != WA_INACTIVE); evt->focus = active;
this->PushEvent(evt); this->PushEvent(evt);
// Pause game when window is inactive
TimerPtr timer = Application::GetInstance().GetTimer();
if (timer)
{
if (active)
timer->Resume();
else
timer->Pause();
}
} }
break; break;