use application scoped timer
This commit is contained in:
parent
1805c016f8
commit
86bb0e4239
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue