update: class Duration
This commit is contained in:
parent
05849422a4
commit
e7cc0773e6
|
|
@ -90,7 +90,7 @@ void easy2d::Animate::Update()
|
||||||
target->Load(frames[frame_index_]);
|
target->Load(frames[frame_index_]);
|
||||||
}
|
}
|
||||||
|
|
||||||
started_ += Duration(animation_->GetInterval());
|
started_ += Duration::Second * animation_->GetInterval();
|
||||||
++frame_index_;
|
++frame_index_;
|
||||||
|
|
||||||
if (frame_index_ == frames.size())
|
if (frame_index_ == frames.size())
|
||||||
|
|
|
||||||
|
|
@ -62,5 +62,5 @@ void easy2d::Delay::Update()
|
||||||
void easy2d::Delay::ResetTime()
|
void easy2d::Delay::ResetTime()
|
||||||
{
|
{
|
||||||
Action::ResetTime();
|
Action::ResetTime();
|
||||||
started_ = Time::Now() - Duration(delta_);
|
started_ = Time::Now() - Duration::Second * delta_;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,5 +60,5 @@ void easy2d::FiniteTimeAction::Update()
|
||||||
void easy2d::FiniteTimeAction::ResetTime()
|
void easy2d::FiniteTimeAction::ResetTime()
|
||||||
{
|
{
|
||||||
Action::ResetTime();
|
Action::ResetTime();
|
||||||
started_ = Time::Now() - Duration(delta_ * duration_);
|
started_ = Time::Now() - Duration::Second * (delta_ * duration_);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -513,19 +513,31 @@ namespace easy2d
|
||||||
// 时间段
|
// 时间段
|
||||||
class Duration
|
class Duration
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
static const Duration Millisecond; // 毫秒
|
||||||
|
static const Duration Second; // 秒
|
||||||
|
static const Duration Minute; // 分钟
|
||||||
|
static const Duration Hour; // 小时
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Duration();
|
Duration();
|
||||||
|
|
||||||
explicit Duration(
|
explicit Duration(
|
||||||
float seconds
|
int64_t milliseconds
|
||||||
);
|
);
|
||||||
|
|
||||||
// 获取毫秒数
|
// 获取毫秒数
|
||||||
int Milliseconds() const;
|
int64_t Milliseconds() const;
|
||||||
|
|
||||||
// 获取秒数
|
// 获取秒数
|
||||||
float Seconds() const;
|
float Seconds() const;
|
||||||
|
|
||||||
|
// 获取分钟数
|
||||||
|
float Minutes() const;
|
||||||
|
|
||||||
|
// 获取小时数
|
||||||
|
float Hours() 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;
|
||||||
|
|
@ -535,12 +547,20 @@ namespace easy2d
|
||||||
|
|
||||||
Duration operator + (Duration const &) const;
|
Duration operator + (Duration const &) const;
|
||||||
Duration operator - (Duration const &) const;
|
Duration operator - (Duration const &) const;
|
||||||
|
Duration operator * (int) const;
|
||||||
|
Duration operator * (float) const;
|
||||||
|
Duration operator / (int) const;
|
||||||
|
Duration operator / (float) const;
|
||||||
|
|
||||||
Duration& operator += (Duration const &);
|
Duration& operator += (Duration const &);
|
||||||
Duration& operator -= (Duration const &);
|
Duration& operator -= (Duration const &);
|
||||||
|
Duration& operator *= (int);
|
||||||
|
Duration& operator *= (float);
|
||||||
|
Duration& operator /= (int);
|
||||||
|
Duration& operator /= (float);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::chrono::milliseconds duration_ms_;
|
int64_t milliseconds_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -553,7 +573,7 @@ namespace easy2d
|
||||||
// 获取时间戳
|
// 获取时间戳
|
||||||
time_t GetTimeStamp() const;
|
time_t GetTimeStamp() const;
|
||||||
|
|
||||||
// 是否是
|
// 是否是零时
|
||||||
bool IsZero() const;
|
bool IsZero() const;
|
||||||
|
|
||||||
Time operator + (Duration const &) const;
|
Time operator + (Duration const &) const;
|
||||||
|
|
|
||||||
|
|
@ -20,80 +20,148 @@
|
||||||
|
|
||||||
#include "..\e2dutil.h"
|
#include "..\e2dutil.h"
|
||||||
|
|
||||||
using namespace std::chrono;
|
|
||||||
|
const easy2d::Duration easy2d::Duration::Millisecond = easy2d::Duration(1);
|
||||||
|
const easy2d::Duration easy2d::Duration::Second = 1000 * easy2d::Duration::Millisecond;
|
||||||
|
const easy2d::Duration easy2d::Duration::Minute = 60 * easy2d::Duration::Second;
|
||||||
|
const easy2d::Duration easy2d::Duration::Hour = 60 * easy2d::Duration::Minute;
|
||||||
|
|
||||||
easy2d::Duration::Duration()
|
easy2d::Duration::Duration()
|
||||||
: duration_ms_()
|
: milliseconds_(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
easy2d::Duration::Duration(float seconds)
|
easy2d::Duration::Duration(int64_t milliseconds)
|
||||||
: duration_ms_(static_cast<long long>(seconds * 1000.f))
|
: milliseconds_(milliseconds)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
int easy2d::Duration::Milliseconds() const
|
int64_t easy2d::Duration::Milliseconds() const
|
||||||
{
|
{
|
||||||
return static_cast<int>(duration_ms_.count());
|
return milliseconds_;
|
||||||
}
|
}
|
||||||
|
|
||||||
float easy2d::Duration::Seconds() const
|
float easy2d::Duration::Seconds() const
|
||||||
{
|
{
|
||||||
return duration_ms_.count() / 1000.f;
|
int64_t sec = milliseconds_ / Second.milliseconds_;
|
||||||
|
int64_t ms = milliseconds_ % Second.milliseconds_;
|
||||||
|
return static_cast<float>(sec) + static_cast<float>(ms) / 1000.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
float easy2d::Duration::Minutes() const
|
||||||
|
{
|
||||||
|
int64_t min = milliseconds_ / Minute.milliseconds_;
|
||||||
|
int64_t ms = milliseconds_ % Minute.milliseconds_;
|
||||||
|
return static_cast<float>(min) + static_cast<float>(ms) / (60 * 1000.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
float easy2d::Duration::Hours() const
|
||||||
|
{
|
||||||
|
int64_t hour = milliseconds_ / Hour.milliseconds_;
|
||||||
|
int64_t ms = milliseconds_ % Hour.milliseconds_;
|
||||||
|
return static_cast<float>(hour) + static_cast<float>(ms) / (60 * 60 * 1000.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool easy2d::Duration::operator==(const Duration & other) const
|
bool easy2d::Duration::operator==(const Duration & other) const
|
||||||
{
|
{
|
||||||
return duration_ms_ == other.duration_ms_;
|
return milliseconds_ == other.milliseconds_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool easy2d::Duration::operator!=(const Duration & other) const
|
bool easy2d::Duration::operator!=(const Duration & other) const
|
||||||
{
|
{
|
||||||
return duration_ms_ != other.duration_ms_;
|
return milliseconds_ != other.milliseconds_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool easy2d::Duration::operator>(const Duration & other) const
|
bool easy2d::Duration::operator>(const Duration & other) const
|
||||||
{
|
{
|
||||||
return duration_ms_ > other.duration_ms_;
|
return milliseconds_ > other.milliseconds_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool easy2d::Duration::operator>=(const Duration & other) const
|
bool easy2d::Duration::operator>=(const Duration & other) const
|
||||||
{
|
{
|
||||||
return duration_ms_ >= other.duration_ms_;
|
return milliseconds_ >= other.milliseconds_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool easy2d::Duration::operator<(const Duration & other) const
|
bool easy2d::Duration::operator<(const Duration & other) const
|
||||||
{
|
{
|
||||||
return duration_ms_ < other.duration_ms_;
|
return milliseconds_ < other.milliseconds_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool easy2d::Duration::operator<=(const Duration & other) const
|
bool easy2d::Duration::operator<=(const Duration & other) const
|
||||||
{
|
{
|
||||||
return duration_ms_ <= other.duration_ms_;
|
return milliseconds_ <= other.milliseconds_;
|
||||||
}
|
}
|
||||||
|
|
||||||
easy2d::Duration easy2d::Duration::operator+(Duration const & other) const
|
easy2d::Duration easy2d::Duration::operator+(Duration const & other) const
|
||||||
{
|
{
|
||||||
Duration d;
|
Duration d(milliseconds_ + other.milliseconds_);
|
||||||
d.duration_ms_ = duration_ms_ + other.duration_ms_;
|
|
||||||
return std::move(d);
|
return std::move(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
easy2d::Duration easy2d::Duration::operator-(Duration const & other) const
|
easy2d::Duration easy2d::Duration::operator-(Duration const & other) const
|
||||||
{
|
{
|
||||||
Duration d;
|
Duration d(milliseconds_ - other.milliseconds_);
|
||||||
d.duration_ms_ = duration_ms_ - other.duration_ms_;
|
return std::move(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
easy2d::Duration easy2d::Duration::operator*(int value) const
|
||||||
|
{
|
||||||
|
Duration d(milliseconds_ * value);
|
||||||
|
return std::move(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
easy2d::Duration easy2d::Duration::operator/(int value) const
|
||||||
|
{
|
||||||
|
Duration d(milliseconds_ / value);
|
||||||
|
return std::move(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
easy2d::Duration easy2d::Duration::operator*(float value) const
|
||||||
|
{
|
||||||
|
int64_t ms = static_cast<int64_t>(milliseconds_ * value);
|
||||||
|
Duration d(ms);
|
||||||
|
return std::move(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
easy2d::Duration easy2d::Duration::operator/(float value) const
|
||||||
|
{
|
||||||
|
int64_t ms = static_cast<int64_t>(milliseconds_ / value);
|
||||||
|
Duration d(ms);
|
||||||
return std::move(d);
|
return std::move(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
easy2d::Duration & easy2d::Duration::operator+=(Duration const &other)
|
easy2d::Duration & easy2d::Duration::operator+=(Duration const &other)
|
||||||
{
|
{
|
||||||
duration_ms_ += other.duration_ms_;
|
milliseconds_ += other.milliseconds_;
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
easy2d::Duration & easy2d::Duration::operator-=(Duration const &other)
|
easy2d::Duration & easy2d::Duration::operator-=(Duration const &other)
|
||||||
{
|
{
|
||||||
duration_ms_ -= other.duration_ms_;
|
milliseconds_ -= other.milliseconds_;
|
||||||
|
return (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
easy2d::Duration & easy2d::Duration::operator*=(int value)
|
||||||
|
{
|
||||||
|
milliseconds_ *= value;
|
||||||
|
return (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
easy2d::Duration & easy2d::Duration::operator/=(int value)
|
||||||
|
{
|
||||||
|
milliseconds_ /= value;
|
||||||
|
return (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
easy2d::Duration & easy2d::Duration::operator*=(float value)
|
||||||
|
{
|
||||||
|
milliseconds_ *= value;
|
||||||
|
return (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
easy2d::Duration & easy2d::Duration::operator/=(float value)
|
||||||
|
{
|
||||||
|
milliseconds_ /= value;
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,8 @@ easy2d::Time & easy2d::Time::operator-=(Duration const &other)
|
||||||
easy2d::Duration easy2d::Time::operator-(Time const & other) const
|
easy2d::Duration easy2d::Time::operator-(Time const & other) const
|
||||||
{
|
{
|
||||||
auto ms = duration_cast<milliseconds>(time_ - other.time_).count();
|
auto ms = duration_cast<milliseconds>(time_ - other.time_).count();
|
||||||
return std::move(Duration(static_cast<float>(ms) / 1000.f));
|
Duration d(static_cast<int64_t>(ms));
|
||||||
|
return std::move(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
easy2d::Time easy2d::Time::Now()
|
easy2d::Time easy2d::Time::Now()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue