Time类调整
This commit is contained in:
parent
abaf59379a
commit
7bdadbe828
|
|
@ -1,76 +1,66 @@
|
|||
#include "..\e2dbase.h"
|
||||
#include <thread>
|
||||
#include <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()
|
||||
{
|
||||
return duration_cast<microseconds>(s_tNow - s_tStart).count() / 1000.0 / 1000.0;
|
||||
}
|
||||
|
||||
unsigned int e2d::Time::getTotalTimeMilliseconds()
|
||||
{
|
||||
return static_cast<unsigned int>(duration_cast<milliseconds>(s_tNow - s_tStart).count());
|
||||
return duration_cast<microseconds>(_now - _start).count() / 1000.0 / 1000.0;
|
||||
}
|
||||
|
||||
double e2d::Time::getDeltaTime()
|
||||
{
|
||||
return duration_cast<microseconds>(s_tNow - s_tLast).count() / 1000.0 / 1000.0;
|
||||
}
|
||||
|
||||
unsigned int e2d::Time::getDeltaTimeMilliseconds()
|
||||
{
|
||||
return static_cast<unsigned int>(duration_cast<milliseconds>(s_tNow - s_tLast).count());
|
||||
return duration_cast<microseconds>(_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<milliseconds>(s_tNow - s_tFixed);
|
||||
return _interval < duration_cast<milliseconds>(_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<int>(duration_cast<milliseconds>(s_tNow - s_tFixed).count());
|
||||
int nWaitMS = 16 - static_cast<int>(duration_cast<milliseconds>(_now - _fixedLast).count());
|
||||
|
||||
if (nWaitMS > 1)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -42,10 +42,11 @@
|
|||
#include <set>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <sstream>
|
||||
#include <random>
|
||||
#include <utility>
|
||||
#include <chrono>
|
||||
#include <sstream>
|
||||
#include <functional>
|
||||
|
||||
// Import Libraries
|
||||
#pragma comment(lib, "d2d1.lib")
|
||||
|
|
|
|||
Loading…
Reference in New Issue