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

@ -68,15 +68,13 @@ void Application::Run(RunnerPtr runner, bool debug)
// Everything is ready
runner->OnReady();
running_ = true;
last_update_time_ = Time::Now();
running_ = true;
timer_ = Timer::Create();
while (running_)
{
const Time now = Time::Now();
const Duration dt = (now - last_update_time_);
last_update_time_ = now;
timer_->Tick();
if (!runner->MainLoop(dt))
if (!runner->MainLoop(timer_->GetDeltaTime()))
running_ = false;
}

View File

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

View File

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