From 7bdadbe8288e661dfe3503a64f289e22b4fcd636 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Tue, 3 Jul 2018 18:42:00 +0800 Subject: [PATCH] =?UTF-8?q?Time=E7=B1=BB=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Base/Time.cpp | 44 +++++++++++++++++--------------------------- core/Base/Window.cpp | 8 ++------ core/e2dbase.h | 23 +++++++++++++++-------- core/e2dmacros.h | 5 +++-- 4 files changed, 37 insertions(+), 43 deletions(-) diff --git a/core/Base/Time.cpp b/core/Base/Time.cpp index b53e49f8..7a1759e0 100644 --- a/core/Base/Time.cpp +++ b/core/Base/Time.cpp @@ -1,76 +1,66 @@ #include "..\e2dbase.h" #include -#include + using namespace std::chrono; // 游戏开始时间 -static steady_clock::time_point s_tStart; +steady_clock::time_point e2d::Time::_start; // 当前时间 -static steady_clock::time_point s_tNow; +steady_clock::time_point e2d::Time::_now; // 上一帧刷新时间 -static steady_clock::time_point s_tLast; +steady_clock::time_point e2d::Time::_last; // 固定的刷新时间 -static steady_clock::time_point s_tFixed; +steady_clock::time_point e2d::Time::_fixedLast; // 每一帧间隔 -static milliseconds s_tExceptedInvertal; +milliseconds e2d::Time::_interval; double e2d::Time::getTotalTime() { - return duration_cast(s_tNow - s_tStart).count() / 1000.0 / 1000.0; -} - -unsigned int e2d::Time::getTotalTimeMilliseconds() -{ - return static_cast(duration_cast(s_tNow - s_tStart).count()); + return duration_cast(_now - _start).count() / 1000.0 / 1000.0; } double e2d::Time::getDeltaTime() { - return duration_cast(s_tNow - s_tLast).count() / 1000.0 / 1000.0; -} - -unsigned int e2d::Time::getDeltaTimeMilliseconds() -{ - return static_cast(duration_cast(s_tNow - s_tLast).count()); + return duration_cast(_now - _last).count() / 1000.0 / 1000.0; } bool e2d::Time::__init() { - s_tStart = s_tFixed = s_tLast = s_tNow = steady_clock::now(); - s_tExceptedInvertal = milliseconds(15); + _start = _fixedLast = _last = _now = steady_clock::now(); + _interval = milliseconds(15); return true; } bool e2d::Time::__isReady() { - return s_tExceptedInvertal < duration_cast(s_tNow - s_tFixed); + return _interval < duration_cast(_now - _fixedLast); } void e2d::Time::__updateNow() { // 刷新时间 - s_tNow = steady_clock::now(); + _now = steady_clock::now(); } void e2d::Time::__updateLast() { - s_tFixed += s_tExceptedInvertal; + _fixedLast += _interval; - s_tLast = s_tNow; - s_tNow = steady_clock::now(); + _last = _now; + _now = steady_clock::now(); } void e2d::Time::__reset() { - s_tLast = s_tFixed = s_tNow = steady_clock::now(); + _last = _fixedLast = _now = steady_clock::now(); } void e2d::Time::__sleep() { // 计算挂起时长 - int nWaitMS = 16 - static_cast(duration_cast(s_tNow - s_tFixed).count()); + int nWaitMS = 16 - static_cast(duration_cast(_now - _fixedLast).count()); if (nWaitMS > 1) { diff --git a/core/Base/Window.cpp b/core/Base/Window.cpp index 43f9b0c9..82524195 100644 --- a/core/Base/Window.cpp +++ b/core/Base/Window.cpp @@ -320,10 +320,7 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPar { UINT width = LOWORD(lParam); UINT height = HIWORD(lParam); - - auto instance = Window::getInstance(); - if (instance) - instance->_size = Size(width, height); + Window::getInstance()->_size = Size(width, height); // 如果程序接收到一个 WM_SIZE 消息,这个方法将调整渲染 // 目标适当。它可能会调用失败,但是这里可以忽略有可能的 @@ -337,8 +334,7 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPar // 处理窗口标题变化消息 case WM_SETTEXT: { - auto instance = Window::getInstance(); - instance->_title = (const wchar_t*)lParam; + Window::getInstance()->_title = (const wchar_t*)lParam; } break; diff --git a/core/e2dbase.h b/core/e2dbase.h index 6cbf82cc..21c193cc 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -168,12 +168,18 @@ private: void __poll(); // Win32 窗口消息回调程序 - static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); + static LRESULT CALLBACK WndProc( + HWND hWnd, + UINT message, + WPARAM wParam, + LPARAM lParam + ); private: HWND _hWnd; Size _size; String _title; + static Window * _instance; }; @@ -184,18 +190,12 @@ class Time friend class Game; public: - // 获取上一帧与当前帧的时间间隔(秒) + // 获取上一帧的时间间隔(秒) static double getDeltaTime(); - // 获取上一帧与当前帧的时间间隔(毫秒) - static unsigned int getDeltaTimeMilliseconds(); - // 获取游戏总时长(秒) static double getTotalTime(); - // 获取游戏总时长(毫秒) - static unsigned int getTotalTimeMilliseconds(); - private: // 初始化计时操作 static bool __init(); @@ -214,6 +214,13 @@ private: // 挂起线程 static void __sleep(); + +private: + static std::chrono::steady_clock::time_point _start; + static std::chrono::steady_clock::time_point _now; + static std::chrono::steady_clock::time_point _last; + static std::chrono::steady_clock::time_point _fixedLast; + static std::chrono::milliseconds _interval; }; diff --git a/core/e2dmacros.h b/core/e2dmacros.h index 238044b9..a71d45cc 100644 --- a/core/e2dmacros.h +++ b/core/e2dmacros.h @@ -42,10 +42,11 @@ #include #include #include -#include -#include #include #include +#include +#include +#include // Import Libraries #pragma comment(lib, "d2d1.lib")