Time类调整

This commit is contained in:
Nomango 2018-07-03 18:42:00 +08:00
parent abaf59379a
commit 7bdadbe828
4 changed files with 37 additions and 43 deletions

View File

@ -1,76 +1,66 @@
#include "..\e2dbase.h" #include "..\e2dbase.h"
#include <thread> #include <thread>
#include <chrono>
using namespace std::chrono; 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() double e2d::Time::getTotalTime()
{ {
return duration_cast<microseconds>(s_tNow - s_tStart).count() / 1000.0 / 1000.0; return duration_cast<microseconds>(_now - _start).count() / 1000.0 / 1000.0;
}
unsigned int e2d::Time::getTotalTimeMilliseconds()
{
return static_cast<unsigned int>(duration_cast<milliseconds>(s_tNow - s_tStart).count());
} }
double e2d::Time::getDeltaTime() double e2d::Time::getDeltaTime()
{ {
return duration_cast<microseconds>(s_tNow - s_tLast).count() / 1000.0 / 1000.0; return duration_cast<microseconds>(_now - _last).count() / 1000.0 / 1000.0;
}
unsigned int e2d::Time::getDeltaTimeMilliseconds()
{
return static_cast<unsigned int>(duration_cast<milliseconds>(s_tNow - s_tLast).count());
} }
bool e2d::Time::__init() bool e2d::Time::__init()
{ {
s_tStart = s_tFixed = s_tLast = s_tNow = steady_clock::now(); _start = _fixedLast = _last = _now = steady_clock::now();
s_tExceptedInvertal = milliseconds(15); _interval = milliseconds(15);
return true; return true;
} }
bool e2d::Time::__isReady() bool e2d::Time::__isReady()
{ {
return s_tExceptedInvertal < duration_cast<milliseconds>(s_tNow - s_tFixed); return _interval < duration_cast<milliseconds>(_now - _fixedLast);
} }
void e2d::Time::__updateNow() void e2d::Time::__updateNow()
{ {
// 刷新时间 // 刷新时间
s_tNow = steady_clock::now(); _now = steady_clock::now();
} }
void e2d::Time::__updateLast() void e2d::Time::__updateLast()
{ {
s_tFixed += s_tExceptedInvertal; _fixedLast += _interval;
s_tLast = s_tNow; _last = _now;
s_tNow = steady_clock::now(); _now = steady_clock::now();
} }
void e2d::Time::__reset() void e2d::Time::__reset()
{ {
s_tLast = s_tFixed = s_tNow = steady_clock::now(); _last = _fixedLast = _now = steady_clock::now();
} }
void e2d::Time::__sleep() void e2d::Time::__sleep()
{ {
// 计算挂起时长 // 计算挂起时长
int nWaitMS = 16 - static_cast<int>(duration_cast<milliseconds>(s_tNow - s_tFixed).count()); int nWaitMS = 16 - static_cast<int>(duration_cast<milliseconds>(_now - _fixedLast).count());
if (nWaitMS > 1) if (nWaitMS > 1)
{ {

View File

@ -320,10 +320,7 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPar
{ {
UINT width = LOWORD(lParam); UINT width = LOWORD(lParam);
UINT height = HIWORD(lParam); UINT height = HIWORD(lParam);
Window::getInstance()->_size = Size(width, height);
auto instance = Window::getInstance();
if (instance)
instance->_size = Size(width, height);
// 如果程序接收到一个 WM_SIZE 消息,这个方法将调整渲染 // 如果程序接收到一个 WM_SIZE 消息,这个方法将调整渲染
// 目标适当。它可能会调用失败,但是这里可以忽略有可能的 // 目标适当。它可能会调用失败,但是这里可以忽略有可能的
@ -337,8 +334,7 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPar
// 处理窗口标题变化消息 // 处理窗口标题变化消息
case WM_SETTEXT: case WM_SETTEXT:
{ {
auto instance = Window::getInstance(); Window::getInstance()->_title = (const wchar_t*)lParam;
instance->_title = (const wchar_t*)lParam;
} }
break; break;

View File

@ -168,12 +168,18 @@ private:
void __poll(); void __poll();
// Win32 窗口消息回调程序 // 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: private:
HWND _hWnd; HWND _hWnd;
Size _size; Size _size;
String _title; String _title;
static Window * _instance; static Window * _instance;
}; };
@ -184,18 +190,12 @@ class Time
friend class Game; friend class Game;
public: public:
// 获取上一帧与当前帧的时间间隔(秒) // 获取上一帧的时间间隔(秒)
static double getDeltaTime(); static double getDeltaTime();
// 获取上一帧与当前帧的时间间隔(毫秒)
static unsigned int getDeltaTimeMilliseconds();
// 获取游戏总时长(秒) // 获取游戏总时长(秒)
static double getTotalTime(); static double getTotalTime();
// 获取游戏总时长(毫秒)
static unsigned int getTotalTimeMilliseconds();
private: private:
// 初始化计时操作 // 初始化计时操作
static bool __init(); static bool __init();
@ -214,6 +214,13 @@ private:
// 挂起线程 // 挂起线程
static void __sleep(); 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;
}; };

View File

@ -42,10 +42,11 @@
#include <set> #include <set>
#include <stack> #include <stack>
#include <vector> #include <vector>
#include <functional>
#include <sstream>
#include <random> #include <random>
#include <utility> #include <utility>
#include <chrono>
#include <sstream>
#include <functional>
// Import Libraries // Import Libraries
#pragma comment(lib, "d2d1.lib") #pragma comment(lib, "d2d1.lib")