应用Time和Duration类
This commit is contained in:
parent
7d17c3b225
commit
c3ebe2545a
|
|
@ -24,7 +24,7 @@ bool e2d::Action::isRunning()
|
||||||
void e2d::Action::resume()
|
void e2d::Action::resume()
|
||||||
{
|
{
|
||||||
_running = true;
|
_running = true;
|
||||||
_last = Game::getInstance()->getTotalTime();
|
_last = Game::getInstance()->getTotalDuration().seconds();
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Action::pause()
|
void e2d::Action::pause()
|
||||||
|
|
@ -56,7 +56,7 @@ void e2d::Action::reset()
|
||||||
{
|
{
|
||||||
_initialized = false;
|
_initialized = false;
|
||||||
_done = false;
|
_done = false;
|
||||||
_last = Game::getInstance()->getTotalTime();
|
_last = Game::getInstance()->getTotalDuration().seconds();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool e2d::Action::_isDone()
|
bool e2d::Action::_isDone()
|
||||||
|
|
@ -74,7 +74,7 @@ void e2d::Action::_startWithTarget(Node* target)
|
||||||
void e2d::Action::_init()
|
void e2d::Action::_init()
|
||||||
{
|
{
|
||||||
_initialized = true;
|
_initialized = true;
|
||||||
_last = Game::getInstance()->getTotalTime();
|
_last = Game::getInstance()->getTotalDuration().seconds();
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Action::_update()
|
void e2d::Action::_update()
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,8 @@ void e2d::Animate::_update()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((Game::getInstance()->getTotalTime() - _last) >= _animation->getInterval())
|
auto game = Game::getInstance();
|
||||||
|
while ((game->getTotalDuration().seconds() - _last) >= _animation->getInterval())
|
||||||
{
|
{
|
||||||
auto& frames = _animation->getFrames();
|
auto& frames = _animation->getFrames();
|
||||||
auto target = dynamic_cast<Sprite*>(_target);
|
auto target = dynamic_cast<Sprite*>(_target);
|
||||||
|
|
@ -80,7 +81,7 @@ void e2d::Animate::_update()
|
||||||
void e2d::Animate::_resetTime()
|
void e2d::Animate::_resetTime()
|
||||||
{
|
{
|
||||||
Action::_resetTime();
|
Action::_resetTime();
|
||||||
_last = Game::getInstance()->getTotalTime();
|
_last = Game::getInstance()->getTotalDuration().seconds();
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Animate::reset()
|
void e2d::Animate::reset()
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ void e2d::Delay::_update()
|
||||||
{
|
{
|
||||||
Action::_update();
|
Action::_update();
|
||||||
|
|
||||||
_delta = Game::getInstance()->getTotalTime() - _last;
|
_delta = Game::getInstance()->getTotalDuration().seconds() - _last;
|
||||||
|
|
||||||
if (_delta >= _delay)
|
if (_delta >= _delay)
|
||||||
{
|
{
|
||||||
|
|
@ -42,5 +42,5 @@ void e2d::Delay::_update()
|
||||||
void e2d::Delay::_resetTime()
|
void e2d::Delay::_resetTime()
|
||||||
{
|
{
|
||||||
Action::_resetTime();
|
Action::_resetTime();
|
||||||
_last = Game::getInstance()->getTotalTime() - _delta;
|
_last = Game::getInstance()->getTotalDuration().seconds() - _delta;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ void e2d::FiniteTimeAction::_update()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_delta = std::min((Game::getInstance()->getTotalTime() - _last) / _duration, 1.0);
|
_delta = std::min((Game::getInstance()->getTotalDuration().seconds() - _last) / _duration, 1.0);
|
||||||
|
|
||||||
if (_delta >= 1)
|
if (_delta >= 1)
|
||||||
{
|
{
|
||||||
|
|
@ -40,5 +40,5 @@ void e2d::FiniteTimeAction::_update()
|
||||||
void e2d::FiniteTimeAction::_resetTime()
|
void e2d::FiniteTimeAction::_resetTime()
|
||||||
{
|
{
|
||||||
Action::_resetTime();
|
Action::_resetTime();
|
||||||
_last = Game::getInstance()->getTotalTime() - _delta * _duration;
|
_last = Game::getInstance()->getTotalDuration().seconds() - _delta * _duration;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ e2d::Game::Game()
|
||||||
{
|
{
|
||||||
CoInitialize(nullptr);
|
CoInitialize(nullptr);
|
||||||
|
|
||||||
_start = _last = _now = steady_clock::now();
|
_start = _last = _now = Time::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
e2d::Game::~Game()
|
e2d::Game::~Game()
|
||||||
|
|
@ -67,16 +67,16 @@ void e2d::Game::start()
|
||||||
window->poll();
|
window->poll();
|
||||||
|
|
||||||
// ¿ªÊ¼ÓÎÏ·
|
// ¿ªÊ¼ÓÎÏ·
|
||||||
const milliseconds frameInterval(15LL);
|
Duration frameInterval(15), interval;
|
||||||
milliseconds wait, interval;
|
int wait = 0;
|
||||||
|
|
||||||
_ended = false;
|
_ended = false;
|
||||||
_last = _now = steady_clock::now();
|
_last = _now = Time::now();
|
||||||
|
|
||||||
while (!_ended)
|
while (!_ended)
|
||||||
{
|
{
|
||||||
_now = steady_clock::now();
|
_now = Time::now();
|
||||||
interval = duration_cast<milliseconds>(_now - _last);
|
interval = _now - _last;
|
||||||
|
|
||||||
if (frameInterval < interval)
|
if (frameInterval < interval)
|
||||||
{
|
{
|
||||||
|
|
@ -93,10 +93,10 @@ void e2d::Game::start()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
wait = frameInterval - interval;
|
wait = (frameInterval - interval).milliseconds() - 1;
|
||||||
if (wait.count() > 1LL)
|
if (wait > 1)
|
||||||
{
|
{
|
||||||
std::this_thread::sleep_for(wait - milliseconds(1LL));
|
std::this_thread::sleep_for(milliseconds(wait));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -111,7 +111,7 @@ void e2d::Game::resume()
|
||||||
{
|
{
|
||||||
if (_paused && !_ended)
|
if (_paused && !_ended)
|
||||||
{
|
{
|
||||||
_last = _now = steady_clock::now();
|
_last = _now = Time::now();
|
||||||
Timer::getInstance()->updateTime();
|
Timer::getInstance()->updateTime();
|
||||||
ActionManager::getInstance()->updateTime();
|
ActionManager::getInstance()->updateTime();
|
||||||
}
|
}
|
||||||
|
|
@ -141,9 +141,9 @@ e2d::Config* e2d::Game::getConfig()
|
||||||
return _config;
|
return _config;
|
||||||
}
|
}
|
||||||
|
|
||||||
double e2d::Game::getTotalTime() const
|
e2d::Duration e2d::Game::getTotalDuration() const
|
||||||
{
|
{
|
||||||
return duration_cast<microseconds>(steady_clock::now() - _start).count() / 1000.0 / 1000.0;
|
return std::move(_now - _start);
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Game::quit()
|
void e2d::Game::quit()
|
||||||
|
|
|
||||||
|
|
@ -161,11 +161,12 @@ void e2d::Renderer::_renderFps()
|
||||||
{
|
{
|
||||||
++_renderTimes;
|
++_renderTimes;
|
||||||
|
|
||||||
double fDelay = Game::getInstance()->getTotalTime() - _lastRenderTime;
|
double duration = Game::getInstance()->getTotalDuration().seconds();
|
||||||
|
double fDelay = duration - _lastRenderTime;
|
||||||
if (fDelay >= 0.1)
|
if (fDelay >= 0.1)
|
||||||
{
|
{
|
||||||
_fpsText = String::format(L"FPS: %.1lf", (1 / fDelay) * _renderTimes);
|
_fpsText = String::format(L"FPS: %.1lf", (1 / fDelay) * _renderTimes);
|
||||||
_lastRenderTime = Game::getInstance()->getTotalTime();
|
_lastRenderTime = duration;
|
||||||
_renderTimes = 0;
|
_renderTimes = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,70 @@
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
|
|
||||||
e2d::Duration::Duration()
|
e2d::Duration::Duration()
|
||||||
|
: ms()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
e2d::Duration e2d::Duration::operator+(Duration const & size) const
|
e2d::Duration::Duration(int ms)
|
||||||
|
: ms(ms)
|
||||||
{
|
{
|
||||||
return Duration();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
e2d::Duration e2d::Duration::operator-(Duration const & size) const
|
int e2d::Duration::milliseconds() const
|
||||||
{
|
{
|
||||||
return Duration();
|
return static_cast<int>(ms.count());
|
||||||
|
}
|
||||||
|
|
||||||
|
double e2d::Duration::seconds() const
|
||||||
|
{
|
||||||
|
return ms.count() / 1000.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
double e2d::Duration::minutes() const
|
||||||
|
{
|
||||||
|
return ms.count() / 1000.0 / 60.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool e2d::Duration::operator==(const Duration & other) const
|
||||||
|
{
|
||||||
|
return ms == other.ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool e2d::Duration::operator!=(const Duration & other) const
|
||||||
|
{
|
||||||
|
return ms != other.ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool e2d::Duration::operator>(const Duration & other) const
|
||||||
|
{
|
||||||
|
return ms > other.ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool e2d::Duration::operator>=(const Duration & other) const
|
||||||
|
{
|
||||||
|
return ms >= other.ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool e2d::Duration::operator<(const Duration & other) const
|
||||||
|
{
|
||||||
|
return ms < other.ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool e2d::Duration::operator<=(const Duration & other) const
|
||||||
|
{
|
||||||
|
return ms <= other.ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
e2d::Duration e2d::Duration::operator+(Duration const & other) const
|
||||||
|
{
|
||||||
|
Duration result;
|
||||||
|
result.ms = ms + other.ms;
|
||||||
|
return std::move(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
e2d::Duration e2d::Duration::operator-(Duration const & other) const
|
||||||
|
{
|
||||||
|
Duration result;
|
||||||
|
result.ms = ms - other.ms;
|
||||||
|
return std::move(result);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,12 +7,28 @@ e2d::Time::Time()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
e2d::Time e2d::Time::operator+(Duration const & size) const
|
time_t e2d::Time::getTimeStamp() const
|
||||||
{
|
{
|
||||||
return Time();
|
auto now = time_point_cast<milliseconds>(time).time_since_epoch();
|
||||||
|
return static_cast<time_t>(now.count());
|
||||||
}
|
}
|
||||||
|
|
||||||
e2d::Duration e2d::Time::operator-(Time const & size) const
|
e2d::Time e2d::Time::operator+(Duration const & other) const
|
||||||
{
|
{
|
||||||
return Duration();
|
Time result;
|
||||||
|
result.time = time - other.ms;
|
||||||
|
return std::move(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
e2d::Duration e2d::Time::operator-(Time const & other) const
|
||||||
|
{
|
||||||
|
int ms = static_cast<int>(duration_cast<milliseconds>(time - other.time).count());
|
||||||
|
return std::move(Duration(ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
e2d::Time e2d::Time::now()
|
||||||
|
{
|
||||||
|
Time now;
|
||||||
|
now.time = steady_clock::now();
|
||||||
|
return std::move(now);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ bool e2d::Task::isReady() const
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ((Game::getInstance()->getTotalTime() - _lastTime) >= _delay)
|
if ((Game::getInstance()->getTotalDuration().seconds() - _lastTime) >= _delay)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -80,5 +80,5 @@ e2d::String e2d::Task::getName() const
|
||||||
|
|
||||||
void e2d::Task::updateTime()
|
void e2d::Task::updateTime()
|
||||||
{
|
{
|
||||||
_lastTime = Game::getInstance()->getTotalTime();
|
_lastTime = Game::getInstance()->getTotalDuration().seconds();
|
||||||
}
|
}
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
e2d::Transition::Transition(double duration)
|
e2d::Transition::Transition(double duration)
|
||||||
: _end(false)
|
: _end(false)
|
||||||
, _last(0)
|
, _last()
|
||||||
, _delta(0)
|
, _delta(0)
|
||||||
, _outScene(nullptr)
|
, _outScene(nullptr)
|
||||||
, _inScene(nullptr)
|
, _inScene(nullptr)
|
||||||
|
|
@ -45,7 +45,7 @@ void e2d::Transition::_init(Scene * prev, Scene * next)
|
||||||
throw SystemException(L"场景过渡动画图层创建失败");
|
throw SystemException(L"场景过渡动画图层创建失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
_last = Game::getInstance()->getTotalTime();
|
_last = Game::getInstance()->getTotalDuration();
|
||||||
_outScene = prev;
|
_outScene = prev;
|
||||||
_inScene = next;
|
_inScene = next;
|
||||||
GC::retain(_outScene);
|
GC::retain(_outScene);
|
||||||
|
|
@ -64,7 +64,8 @@ void e2d::Transition::_update()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_delta = std::min((Game::getInstance()->getTotalTime() - _last) / _duration, 1.0);
|
_delta = (Game::getInstance()->getTotalDuration() - _last).seconds() / _duration;
|
||||||
|
_delta = std::min(_delta, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
this->_updateCustom();
|
this->_updateCustom();
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,8 @@ public:
|
||||||
// »ñÈ¡ÓÎÏ·ÅäÖÃ
|
// »ñÈ¡ÓÎÏ·ÅäÖÃ
|
||||||
Config* getConfig();
|
Config* getConfig();
|
||||||
|
|
||||||
double getTotalTime() const;
|
// 获取游戏总时长
|
||||||
|
Duration getTotalDuration() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Game();
|
Game();
|
||||||
|
|
@ -56,12 +57,12 @@ private:
|
||||||
E2D_DISABLE_COPY(Game);
|
E2D_DISABLE_COPY(Game);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _ended;
|
bool _ended;
|
||||||
bool _paused;
|
bool _paused;
|
||||||
Config* _config;
|
Config* _config;
|
||||||
std::chrono::steady_clock::time_point _start;
|
Time _start;
|
||||||
std::chrono::steady_clock::time_point _now;
|
Time _now;
|
||||||
std::chrono::steady_clock::time_point _last;
|
Time _last;
|
||||||
|
|
||||||
static Game * _instance;
|
static Game * _instance;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -410,11 +410,31 @@ class Duration
|
||||||
public:
|
public:
|
||||||
Duration();
|
Duration();
|
||||||
|
|
||||||
Duration operator + (Duration const & size) const;
|
explicit Duration(
|
||||||
Duration operator - (Duration const & size) const;
|
int ms
|
||||||
|
);
|
||||||
|
|
||||||
protected:
|
// 获取毫秒数
|
||||||
std::chrono::milliseconds _duration;
|
int milliseconds() const;
|
||||||
|
|
||||||
|
// 获取秒数
|
||||||
|
double seconds() const;
|
||||||
|
|
||||||
|
// 获取分钟数
|
||||||
|
double minutes() const;
|
||||||
|
|
||||||
|
bool operator== (const Duration &) const;
|
||||||
|
bool operator!= (const Duration &) const;
|
||||||
|
bool operator> (const Duration &) const;
|
||||||
|
bool operator>= (const Duration &) const;
|
||||||
|
bool operator< (const Duration &) const;
|
||||||
|
bool operator<= (const Duration &) const;
|
||||||
|
|
||||||
|
Duration operator + (Duration const & other) const;
|
||||||
|
Duration operator - (Duration const & other) const;
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::chrono::milliseconds ms;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -424,11 +444,19 @@ class Time
|
||||||
public:
|
public:
|
||||||
Time();
|
Time();
|
||||||
|
|
||||||
Time operator + (Duration const & size) const;
|
// 获取时间戳
|
||||||
Duration operator - (Time const & size) const;
|
time_t getTimeStamp() const;
|
||||||
|
|
||||||
protected:
|
// 计算时间间隔后的时间点
|
||||||
std::chrono::steady_clock::time_point _time;
|
Time operator + (Duration const & other) const;
|
||||||
|
// 计算两时间点的时间间隔
|
||||||
|
Duration operator - (Time const & other) const;
|
||||||
|
|
||||||
|
// 获取当前时间
|
||||||
|
static Time now();
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::chrono::steady_clock::time_point time;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,9 +46,9 @@ protected:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool _end;
|
bool _end;
|
||||||
double _last;
|
|
||||||
double _duration;
|
double _duration;
|
||||||
double _delta;
|
double _delta;
|
||||||
|
Duration _last;
|
||||||
Size _windowSize;
|
Size _windowSize;
|
||||||
Scene * _outScene;
|
Scene * _outScene;
|
||||||
Scene * _inScene;
|
Scene * _inScene;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue