add: time literals & duration to string & duration operation with iostream
This commit is contained in:
		
							parent
							
								
									0bcaf2fa04
								
							
						
					
					
						commit
						a062af4de7
					
				|  | @ -136,7 +136,7 @@ namespace easy2d | |||
| 		::ShowWindow(window::instance.handle, SW_SHOWNORMAL); | ||||
| 		::UpdateWindow(window::instance.handle); | ||||
| 
 | ||||
| 		const int min_interval = 5; | ||||
| 		const int64_t min_interval = 5; | ||||
| 		auto last = time::Now(); | ||||
| 		MSG msg = { 0 }; | ||||
| 
 | ||||
|  | @ -171,8 +171,8 @@ namespace easy2d | |||
| 				// ID2D1HwndRenderTarget 开启了垂直同步,在渲染时会等待显示器刷新,
 | ||||
| 				// 它起到了非常稳定的延时作用,所以大部分时候不需要手动挂起线程进行延时。
 | ||||
| 				// 下面的代码仅在一些情况下(例如窗口最小化时)挂起线程,防止占用过高 CPU 。
 | ||||
| 				int wait = min_interval - dur.Milliseconds(); | ||||
| 				if (wait > 1) | ||||
| 				int64_t wait = min_interval - dur.Milliseconds(); | ||||
| 				if (wait > 1LL) | ||||
| 				{ | ||||
| 					std::this_thread::sleep_for(std::chrono::milliseconds(wait)); | ||||
| 				} | ||||
|  |  | |||
|  | @ -1041,9 +1041,9 @@ namespace easy2d | |||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	void Node::SetVisible(bool value) | ||||
| 	void Node::SetVisible(bool val) | ||||
| 	{ | ||||
| 		visible_ = value; | ||||
| 		visible_ = val; | ||||
| 	} | ||||
| 
 | ||||
| 	void Node::SetName(const String& name) | ||||
|  |  | |||
|  | @ -125,7 +125,7 @@ namespace easy2d | |||
| 
 | ||||
| 		// 设置节点是否显示
 | ||||
| 		void SetVisible( | ||||
| 			bool value | ||||
| 			bool val | ||||
| 		); | ||||
| 
 | ||||
| 		// 设置节点名称
 | ||||
|  |  | |||
|  | @ -50,14 +50,14 @@ namespace easy2d | |||
| 		return Size(width - other.width, height - other.height); | ||||
| 	} | ||||
| 
 | ||||
| 	Size Size::operator*(float value) const | ||||
| 	Size Size::operator*(float val) const | ||||
| 	{ | ||||
| 		return Size(width * value, height * value); | ||||
| 		return Size(width * val, height * val); | ||||
| 	} | ||||
| 
 | ||||
| 	Size Size::operator/(float value) const | ||||
| 	Size Size::operator/(float val) const | ||||
| 	{ | ||||
| 		return Size(width / value, height / value); | ||||
| 		return Size(width / val, height / val); | ||||
| 	} | ||||
| 
 | ||||
| 	Size Size::operator-() const | ||||
|  |  | |||
|  | @ -48,8 +48,8 @@ namespace easy2d | |||
| 
 | ||||
| 		Size operator + (const Size & other) const; | ||||
| 		Size operator - (const Size & other) const; | ||||
| 		Size operator * (float value) const; | ||||
| 		Size operator / (float value) const; | ||||
| 		Size operator * (float val) const; | ||||
| 		Size operator / (float val) const; | ||||
| 		Size operator - () const; | ||||
| 		bool operator== (const Size& other) const; | ||||
| 	}; | ||||
|  |  | |||
|  | @ -223,9 +223,9 @@ namespace easy2d | |||
| 		style_.color = color; | ||||
| 	} | ||||
| 
 | ||||
| 	void Text::SetItalic(bool value) | ||||
| 	void Text::SetItalic(bool val) | ||||
| 	{ | ||||
| 		font_.italic = value; | ||||
| 		font_.italic = val; | ||||
| 		Reset(); | ||||
| 	} | ||||
| 
 | ||||
|  |  | |||
|  | @ -164,7 +164,7 @@ namespace easy2d | |||
| 
 | ||||
| 		// 设置文字斜体(默认值为 false)
 | ||||
| 		void SetItalic( | ||||
| 			bool value | ||||
| 			bool val | ||||
| 		); | ||||
| 
 | ||||
| 		// 打开或关闭文本自动换行(默认为关闭)
 | ||||
|  |  | |||
|  | @ -20,6 +20,25 @@ | |||
| 
 | ||||
| #pragma once | ||||
| 
 | ||||
| #ifndef __cplusplus | ||||
| #	error Easy2D only supports C++  | ||||
| #endif | ||||
| 
 | ||||
| #ifndef _MSC_VER | ||||
| #   error Easy2D only supports MSVC compiler | ||||
| #endif | ||||
| 
 | ||||
| #ifndef VS_VER | ||||
| #	define VS_VER _MSC_VER | ||||
| #	define VS_2013 1800 | ||||
| #	define VS_2015 1900 | ||||
| #	define VS_2017 1900 | ||||
| #endif | ||||
| 
 | ||||
| #if VS_VER < VS_2013 | ||||
| #	error Easy2D only supports Visual Studio 2013 and above | ||||
| #endif | ||||
| 
 | ||||
| #ifndef WINVER | ||||
| #	define WINVER 0x0700	   // Allow use of features specific to Windows 7 or later
 | ||||
| #endif | ||||
|  | @ -63,7 +82,7 @@ | |||
| #include <algorithm> | ||||
| 
 | ||||
| 
 | ||||
| #if _MSC_VER >= 1900  // >= Visual Studio 2015
 | ||||
| #if VS_VER >= VS_2015 | ||||
| #	define E2D_NOEXCEPT noexcept | ||||
| #else | ||||
| #	define E2D_NOEXCEPT throw() | ||||
|  |  | |||
|  | @ -32,7 +32,7 @@ namespace easy2d | |||
| 	{ | ||||
| 		_D2D_Resource D2D = { 0 }; | ||||
| 
 | ||||
| 		void Initialize(HWND hwnd) | ||||
| 		void easy2d::render::Initialize(HWND hwnd) | ||||
| 		{ | ||||
| 			if (D2D.Factory) | ||||
| 				return; | ||||
|  | @ -106,7 +106,7 @@ namespace easy2d | |||
| 			CreateDeviceResources(hwnd); | ||||
| 		} | ||||
| 
 | ||||
| 		void CreateDeviceResources(HWND hwnd) | ||||
| 		void easy2d::render::CreateDeviceResources(HWND hwnd) | ||||
| 		{ | ||||
| 			if (!D2D.HwndRenderTarget) | ||||
| 			{ | ||||
|  | @ -155,7 +155,7 @@ namespace easy2d | |||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		void Uninitialize() | ||||
| 		void easy2d::render::Uninitialize() | ||||
| 		{ | ||||
| 			SafeRelease(D2D.TextRenderer); | ||||
| 			SafeRelease(D2D.SolidColorBrush); | ||||
|  | @ -224,8 +224,9 @@ namespace easy2d | |||
| 		void GraphicsDevice::DrawDebugInfo() | ||||
| 		{ | ||||
| 			static int render_times_ = 0; | ||||
| 			static time::TimePoint last_render_time_ = time::Now(); | ||||
| 			int duration = (time::Now() - last_render_time_).Milliseconds(); | ||||
| 			static auto last_render_time_ = time::Now(); | ||||
| 
 | ||||
| 			int64_t duration = (time::Now() - last_render_time_).Milliseconds(); | ||||
| 
 | ||||
| 			if (!fps_text_format_) | ||||
| 			{ | ||||
|  | @ -250,10 +251,10 @@ namespace easy2d | |||
| 			} | ||||
| 
 | ||||
| 			++render_times_; | ||||
| 			if (duration >= 100) | ||||
| 			if (duration >= 100LL) | ||||
| 			{ | ||||
| 				wchar_t fps_text[12] = {}; | ||||
| 				int len = swprintf_s(fps_text, L"FPS: %.1f", 1000.f / duration * render_times_); | ||||
| 				int len = swprintf_s(fps_text, L"FPS: %.1f", 1000.f / static_cast<float>(duration) * render_times_); | ||||
| 
 | ||||
| 				last_render_time_ = time::Now(); | ||||
| 				render_times_ = 0; | ||||
|  |  | |||
|  | @ -19,6 +19,7 @@ | |||
| // THE SOFTWARE.
 | ||||
| 
 | ||||
| #include "time.h" | ||||
| #include <chrono> | ||||
| #include <regex> | ||||
| 
 | ||||
| namespace easy2d | ||||
|  | @ -35,58 +36,61 @@ namespace easy2d | |||
| 		{ | ||||
| 		} | ||||
| 
 | ||||
| 		TimePoint::TimePoint(std::chrono::steady_clock::time_point time) | ||||
| 			: time_(time) | ||||
| 		TimePoint::TimePoint(const Duration& dur_since_epoch) | ||||
| 			: dur_since_epoch_(dur_since_epoch) | ||||
| 		{ | ||||
| 		} | ||||
| 
 | ||||
| 		TimePoint::TimePoint(int64_t dur_since_epoch) | ||||
| 			: dur_since_epoch_(dur_since_epoch) | ||||
| 		{ | ||||
| 		} | ||||
| 
 | ||||
| 		TimePoint::TimePoint(const TimePoint & other) | ||||
| 			: time_(other.time_) | ||||
| 			: dur_since_epoch_(other.dur_since_epoch_) | ||||
| 		{ | ||||
| 		} | ||||
| 
 | ||||
| 		TimePoint::TimePoint(TimePoint && other) | ||||
| 			: time_(std::move(other.time_)) | ||||
| 			: dur_since_epoch_(std::move(other.dur_since_epoch_)) | ||||
| 		{ | ||||
| 		} | ||||
| 
 | ||||
| 		time_t TimePoint::GetTimeStamp() const | ||||
| 		{ | ||||
| 			auto& duration = time_point_cast<milliseconds>(time_).time_since_epoch(); | ||||
| 			return static_cast<time_t>(duration.count()); | ||||
| 			return static_cast<time_t>(dur_since_epoch_.Seconds()); | ||||
| 		} | ||||
| 
 | ||||
| 		bool TimePoint::IsZero() const | ||||
| 		{ | ||||
| 			return time_.time_since_epoch().count() == 0LL; | ||||
| 			return !!dur_since_epoch_.Milliseconds(); | ||||
| 		} | ||||
| 
 | ||||
| 		TimePoint TimePoint::operator+(const Duration & other) const | ||||
| 		TimePoint TimePoint::operator+(const Duration & dur) const | ||||
| 		{ | ||||
| 			return TimePoint(time_ + milliseconds(other.Milliseconds())); | ||||
| 			return TimePoint(dur_since_epoch_ + dur); | ||||
| 		} | ||||
| 
 | ||||
| 		TimePoint TimePoint::operator-(const Duration & other) const | ||||
| 		TimePoint TimePoint::operator-(const Duration & dur) const | ||||
| 		{ | ||||
| 			return TimePoint(time_ - milliseconds(other.Milliseconds())); | ||||
| 			return TimePoint(dur_since_epoch_ - dur); | ||||
| 		} | ||||
| 
 | ||||
| 		TimePoint & TimePoint::operator+=(const Duration & other) | ||||
| 		{ | ||||
| 			time_ += milliseconds(other.Milliseconds()); | ||||
| 			dur_since_epoch_ += other; | ||||
| 			return (*this); | ||||
| 		} | ||||
| 
 | ||||
| 		TimePoint & TimePoint::operator-=(const Duration &other) | ||||
| 		{ | ||||
| 			time_ -= milliseconds(other.Milliseconds()); | ||||
| 			dur_since_epoch_ -= other; | ||||
| 			return (*this); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration TimePoint::operator-(const TimePoint & other) const | ||||
| 		{ | ||||
| 			auto ms = duration_cast<milliseconds>(time_ - other.time_).count(); | ||||
| 			return Duration(static_cast<int>(ms)); | ||||
| 			return dur_since_epoch_ - other.dur_since_epoch_; | ||||
| 		} | ||||
| 
 | ||||
| 		TimePoint& TimePoint::operator=(const TimePoint & other) E2D_NOEXCEPT | ||||
|  | @ -94,7 +98,7 @@ namespace easy2d | |||
| 			if (this == &other) | ||||
| 				return *this; | ||||
| 
 | ||||
| 			time_ = other.time_; | ||||
| 			dur_since_epoch_ = other.dur_since_epoch_; | ||||
| 			return *this; | ||||
| 		} | ||||
| 
 | ||||
|  | @ -103,7 +107,7 @@ namespace easy2d | |||
| 			if (this == &other) | ||||
| 				return *this; | ||||
| 
 | ||||
| 			time_ = std::move(other.time_); | ||||
| 			dur_since_epoch_ = std::move(other.dur_since_epoch_); | ||||
| 			return *this; | ||||
| 		} | ||||
| 
 | ||||
|  | @ -119,7 +123,7 @@ namespace easy2d | |||
| 
 | ||||
| 		namespace | ||||
| 		{ | ||||
| 			const auto duration_regex = std::wregex(L"[-+]?([0-9]*(\\.[0-9]*)?[a-z]+)+"); | ||||
| 			const auto duration_regex = std::wregex(LR"([-+]?([0-9]*(\.[0-9]*)?[a-z]+)+)"); | ||||
| 
 | ||||
| 			typedef std::map<std::wstring, Duration> UnitMap; | ||||
| 			const auto unit_map = UnitMap | ||||
|  | @ -136,12 +140,12 @@ namespace easy2d | |||
| 		{ | ||||
| 		} | ||||
| 
 | ||||
| 		Duration::Duration(int milliseconds) | ||||
| 		Duration::Duration(int64_t milliseconds) | ||||
| 			: milliseconds_(milliseconds) | ||||
| 		{ | ||||
| 		} | ||||
| 
 | ||||
| 		int Duration::Milliseconds() const | ||||
| 		int64_t Duration::Milliseconds() const | ||||
| 		{ | ||||
| 			return milliseconds_; | ||||
| 		} | ||||
|  | @ -167,6 +171,37 @@ namespace easy2d | |||
| 			return static_cast<float>(hour) + static_cast<float>(ms) / (60 * 60 * 1000.f); | ||||
| 		} | ||||
| 
 | ||||
| 		std::wstring easy2d::time::Duration::ToString() const | ||||
| 		{ | ||||
| 			std::wstring result; | ||||
| 			int64_t ms = milliseconds_ % Second.milliseconds_; | ||||
| 			int64_t sec = milliseconds_ / Second.milliseconds_; | ||||
| 			int64_t min = milliseconds_ / Minute.milliseconds_; | ||||
| 			int64_t hour = milliseconds_ / Hour.milliseconds_; | ||||
| 
 | ||||
| 			min -= hour * 60; | ||||
| 			sec -= (hour * 60 * 60 + min * 60); | ||||
| 
 | ||||
| 			auto float_to_str = [](float val) -> std::wstring | ||||
| 			{ | ||||
| 				wchar_t buf[10] = {}; | ||||
| 				::swprintf_s(buf, L"%.2f", val); | ||||
| 				return std::wstring(buf); | ||||
| 			}; | ||||
| 
 | ||||
| 			if (milliseconds_ < 0) | ||||
| 				result.append(L"-"); | ||||
| 
 | ||||
| 			result.append(std::to_wstring(hour)) | ||||
| 				.append(L"h") | ||||
| 				.append(std::to_wstring(min)) | ||||
| 				.append(L"m") | ||||
| 				.append(float_to_str(static_cast<float>(sec) + static_cast<float>(ms) / 1000.f)) | ||||
| 				.append(L"s"); | ||||
| 
 | ||||
| 			return result; | ||||
| 		} | ||||
| 
 | ||||
| 		bool Duration::operator==(const Duration & other) const | ||||
| 		{ | ||||
| 			return milliseconds_ == other.milliseconds_; | ||||
|  | @ -212,34 +247,54 @@ namespace easy2d | |||
| 			return Duration(-milliseconds_); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration Duration::operator*(int value) const | ||||
| 		Duration Duration::operator*(int val) const | ||||
| 		{ | ||||
| 			return Duration(milliseconds_ * value); | ||||
| 			return Duration(milliseconds_ * val); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration Duration::operator/(int value) const | ||||
| 		Duration Duration::operator/(int val) const | ||||
| 		{ | ||||
| 			return Duration(milliseconds_ / value); | ||||
| 			return Duration(milliseconds_ / val); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration Duration::operator*(float value) const | ||||
| 		Duration easy2d::time::Duration::operator*(unsigned long long val) const | ||||
| 		{ | ||||
| 			return Duration(static_cast<int>(milliseconds_ * value)); | ||||
| 			return Duration(static_cast<int64_t>(milliseconds_ * val)); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration Duration::operator/(float value) const | ||||
| 		Duration easy2d::time::Duration::operator/(unsigned long long val) const | ||||
| 		{ | ||||
| 			return Duration(static_cast<int>(milliseconds_ / value)); | ||||
| 			return Duration(static_cast<int64_t>(milliseconds_ / val)); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration Duration::operator*(double value) const | ||||
| 		Duration Duration::operator*(float val) const | ||||
| 		{ | ||||
| 			return Duration(static_cast<int>(milliseconds_ * value)); | ||||
| 			return Duration(static_cast<int64_t>(milliseconds_ * val)); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration Duration::operator/(double value) const | ||||
| 		Duration Duration::operator/(float val) const | ||||
| 		{ | ||||
| 			return Duration(static_cast<int>(milliseconds_ / value)); | ||||
| 			return Duration(static_cast<int64_t>(milliseconds_ / val)); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration Duration::operator*(double val) const | ||||
| 		{ | ||||
| 			return Duration(static_cast<int64_t>(milliseconds_ * val)); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration Duration::operator*(long double val) const | ||||
| 		{ | ||||
| 			return Duration(static_cast<int64_t>(milliseconds_ * val)); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration Duration::operator/(double val) const | ||||
| 		{ | ||||
| 			return Duration(static_cast<int64_t>(milliseconds_ / val)); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration Duration::operator/(long double val) const | ||||
| 		{ | ||||
| 			return Duration(static_cast<int64_t>(milliseconds_ / val)); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration & Duration::operator+=(const Duration &other) | ||||
|  | @ -254,70 +309,127 @@ namespace easy2d | |||
| 			return (*this); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration & Duration::operator*=(int value) | ||||
| 		Duration & Duration::operator*=(int val) | ||||
| 		{ | ||||
| 			milliseconds_ *= value; | ||||
| 			milliseconds_ *= val; | ||||
| 			return (*this); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration & Duration::operator/=(int value) | ||||
| 		Duration & Duration::operator/=(int val) | ||||
| 		{ | ||||
| 			milliseconds_ /= value; | ||||
| 			milliseconds_ = static_cast<int64_t>(milliseconds_ / val); | ||||
| 			return (*this); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration & Duration::operator*=(float value) | ||||
| 		Duration & easy2d::time::Duration::operator*=(unsigned long long val) | ||||
| 		{ | ||||
| 			milliseconds_ = static_cast<int>(milliseconds_ * value); | ||||
| 			milliseconds_ = static_cast<int64_t>(milliseconds_ * val); | ||||
| 			return (*this); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration & Duration::operator/=(float value) | ||||
| 		Duration & easy2d::time::Duration::operator/=(unsigned long long val) | ||||
| 		{ | ||||
| 			milliseconds_ = static_cast<int>(milliseconds_ / value); | ||||
| 			milliseconds_ = static_cast<int64_t>(milliseconds_ * val); | ||||
| 			return (*this); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration & Duration::operator*=(double value) | ||||
| 		Duration & Duration::operator*=(float val) | ||||
| 		{ | ||||
| 			milliseconds_ = static_cast<int>(milliseconds_ * value); | ||||
| 			milliseconds_ = static_cast<int64_t>(milliseconds_ * val); | ||||
| 			return (*this); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration & Duration::operator/=(double value) | ||||
| 		Duration & Duration::operator/=(float val) | ||||
| 		{ | ||||
| 			milliseconds_ = static_cast<int>(milliseconds_ / value); | ||||
| 			milliseconds_ = static_cast<int64_t>(milliseconds_ / val); | ||||
| 			return (*this); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration operator*(int value, const Duration & dur) | ||||
| 		Duration & Duration::operator*=(double val) | ||||
| 		{ | ||||
| 			return dur * value; | ||||
| 			milliseconds_ = static_cast<int64_t>(milliseconds_ * val); | ||||
| 			return (*this); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration operator/(int value, const Duration & dur) | ||||
| 		Duration & Duration::operator*=(long double val) | ||||
| 		{ | ||||
| 			return dur / value; | ||||
| 			milliseconds_ = static_cast<int64_t>(milliseconds_ * val); | ||||
| 			return (*this); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration operator*(float value, const Duration & dur) | ||||
| 		Duration & Duration::operator/=(double val) | ||||
| 		{ | ||||
| 			return dur * value; | ||||
| 			milliseconds_ = static_cast<int64_t>(milliseconds_ / val); | ||||
| 			return (*this); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration operator/(float value, const Duration & dur) | ||||
| 		Duration & Duration::operator/=(long double val) | ||||
| 		{ | ||||
| 			return dur / value; | ||||
| 			milliseconds_ = static_cast<int64_t>(milliseconds_ / val); | ||||
| 			return (*this); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration operator*(double value, const Duration & dur) | ||||
| 		Duration easy2d::time::operator*(int val, const Duration & dur) | ||||
| 		{ | ||||
| 			return dur * value; | ||||
| 			return dur * val; | ||||
| 		} | ||||
| 
 | ||||
| 		Duration operator/(double value, const Duration & dur) | ||||
| 		Duration easy2d::time::operator*(unsigned long long val, const Duration & dur) | ||||
| 		{ | ||||
| 			return dur / value; | ||||
| 			return dur / val; | ||||
| 		} | ||||
| 
 | ||||
| 		Duration easy2d::time::operator/(int val, const Duration & dur) | ||||
| 		{ | ||||
| 			return dur / val; | ||||
| 		} | ||||
| 
 | ||||
| 		Duration easy2d::time::operator/(unsigned long long val, const Duration & dur) | ||||
| 		{ | ||||
| 			return dur * val; | ||||
| 		} | ||||
| 
 | ||||
| 		Duration easy2d::time::operator*(float val, const Duration & dur) | ||||
| 		{ | ||||
| 			return dur * val; | ||||
| 		} | ||||
| 
 | ||||
| 		Duration easy2d::time::operator/(float val, const Duration & dur) | ||||
| 		{ | ||||
| 			return dur / val; | ||||
| 		} | ||||
| 
 | ||||
| 		Duration easy2d::time::operator*(double val, const Duration & dur) | ||||
| 		{ | ||||
| 			return dur * val; | ||||
| 		} | ||||
| 
 | ||||
| 		Duration easy2d::time::operator/(double val, const Duration & dur) | ||||
| 		{ | ||||
| 			return dur / val; | ||||
| 		} | ||||
| 
 | ||||
| 		Duration easy2d::time::operator*(long double val, const Duration & dur) | ||||
| 		{ | ||||
| 			return dur * val; | ||||
| 		} | ||||
| 
 | ||||
| 		Duration easy2d::time::operator/(long double val, const Duration & dur) | ||||
| 		{ | ||||
| 			return dur / val; | ||||
| 		} | ||||
| 
 | ||||
| 		std::wostream & easy2d::time::operator<<(std::wostream & out, const Duration & dur) | ||||
| 		{ | ||||
| 			return out << dur.ToString(); | ||||
| 		} | ||||
| 
 | ||||
| 		std::wistream & easy2d::time::operator>>(std::wistream & in, Duration & dur) | ||||
| 		{ | ||||
| 			std::wstring str; | ||||
| 			in >> str; | ||||
| 			dur = time::ParseDuration(str); | ||||
| 			return in; | ||||
| 		} | ||||
| 
 | ||||
| 
 | ||||
|  | @ -325,12 +437,16 @@ namespace easy2d | |||
| 		// Functions
 | ||||
| 		//-------------------------------------------------------
 | ||||
| 
 | ||||
| 		TimePoint Now() | ||||
| 		TimePoint easy2d::time::Now() | ||||
| 		{ | ||||
| 			return TimePoint(steady_clock::now()); | ||||
| 			return TimePoint( | ||||
| 				static_cast<int64_t>( | ||||
| 					duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count() | ||||
| 				) | ||||
| 			); | ||||
| 		} | ||||
| 
 | ||||
| 		Duration ParseDuration(const std::wstring & str) | ||||
| 		Duration easy2d::time::ParseDuration(const std::wstring & str) | ||||
| 		{ | ||||
| 			size_t len = str.length(); | ||||
| 			size_t pos = 0; | ||||
|  |  | |||
|  | @ -20,7 +20,7 @@ | |||
| 
 | ||||
| #pragma once | ||||
| #include "macros.h" | ||||
| #include <chrono> | ||||
| #include <cstdint> | ||||
| 
 | ||||
| namespace easy2d | ||||
| { | ||||
|  | @ -34,18 +34,22 @@ namespace easy2d | |||
| 		//         1.5 小时: time::Hour * 1.5
 | ||||
| 		//         3 小时 45 分 15 秒: time::Hour * 3 + time::Minute * 45 + time::Second * 15
 | ||||
| 		//     时间段格式化: auto d = time::ParseDuration(L"1h35m");  // 1小时35分钟
 | ||||
| 		// 
 | ||||
| 		//     在 VS2015 及更高版本可以使用 time literals:
 | ||||
| 		//         5 秒: 5_s
 | ||||
| 		//         1.5 小时: 1.5_h
 | ||||
| 		//         3 小时 45 分 15 秒: 3_h + 45_m + 15_s
 | ||||
| 		//
 | ||||
| 		class Duration | ||||
| 		{ | ||||
| 		public: | ||||
| 			Duration(); | ||||
| 
 | ||||
| 			explicit Duration( | ||||
| 				int milliseconds | ||||
| 				int64_t milliseconds | ||||
| 			); | ||||
| 
 | ||||
| 			// 转化为毫秒
 | ||||
| 			int Milliseconds() const; | ||||
| 			int64_t Milliseconds() const; | ||||
| 
 | ||||
| 			// 转化为秒
 | ||||
| 			float Seconds() const; | ||||
|  | @ -56,6 +60,9 @@ namespace easy2d | |||
| 			// 转化为小时
 | ||||
| 			float Hours() const; | ||||
| 
 | ||||
| 			// 转为字符串
 | ||||
| 			std::wstring ToString() const; | ||||
| 
 | ||||
| 			bool operator== (const Duration &) const; | ||||
| 			bool operator!= (const Duration &) const; | ||||
| 			bool operator> (const Duration &) const; | ||||
|  | @ -67,30 +74,45 @@ namespace easy2d | |||
| 			Duration operator - (const Duration &) const; | ||||
| 			Duration operator - () const; | ||||
| 			Duration operator * (int) const; | ||||
| 			Duration operator * (unsigned long long) const; | ||||
| 			Duration operator * (float) const; | ||||
| 			Duration operator * (double) const; | ||||
| 			Duration operator * (long double) const; | ||||
| 			Duration operator / (int) const; | ||||
| 			Duration operator / (unsigned long long) const; | ||||
| 			Duration operator / (float) const; | ||||
| 			Duration operator / (double) const; | ||||
| 			Duration operator / (long double) const; | ||||
| 
 | ||||
| 			Duration& operator += (const Duration &); | ||||
| 			Duration& operator -= (const Duration &); | ||||
| 			Duration& operator *= (int); | ||||
| 			Duration& operator *= (unsigned long long); | ||||
| 			Duration& operator *= (float); | ||||
| 			Duration& operator *= (double); | ||||
| 			Duration& operator *= (long double); | ||||
| 			Duration& operator /= (int); | ||||
| 			Duration& operator /= (unsigned long long); | ||||
| 			Duration& operator /= (float); | ||||
| 			Duration& operator /= (double); | ||||
| 			Duration& operator /= (long double); | ||||
| 
 | ||||
| 			friend Duration operator* (int, const Duration &); | ||||
| 			friend Duration operator* (unsigned long long, const Duration &); | ||||
| 			friend Duration operator* (float, const Duration &); | ||||
| 			friend Duration operator* (double, const Duration &); | ||||
| 			friend Duration operator* (long double, const Duration &); | ||||
| 			friend Duration operator/ (int, const Duration &); | ||||
| 			friend Duration operator/ (unsigned long long, const Duration &); | ||||
| 			friend Duration operator/ (float, const Duration &); | ||||
| 			friend Duration operator/ (double, const Duration &); | ||||
| 			friend Duration operator/ (long double, const Duration &); | ||||
| 
 | ||||
| 			friend std::wostream& operator<< (std::wostream &, const Duration &); | ||||
| 			friend std::wistream& operator>> (std::wistream &, Duration &); | ||||
| 
 | ||||
| 		private: | ||||
| 			int milliseconds_; | ||||
| 			int64_t milliseconds_; | ||||
| 		}; | ||||
| 
 | ||||
| 		extern const Duration Millisecond;	// 毫秒
 | ||||
|  | @ -118,7 +140,11 @@ namespace easy2d | |||
| 			TimePoint(); | ||||
| 
 | ||||
| 			explicit TimePoint( | ||||
| 				std::chrono::steady_clock::time_point | ||||
| 				const Duration& dur_since_epoch | ||||
| 			); | ||||
| 
 | ||||
| 			explicit TimePoint( | ||||
| 				int64_t dur_since_epoch | ||||
| 			); | ||||
| 
 | ||||
| 			TimePoint( | ||||
|  | @ -147,7 +173,7 @@ namespace easy2d | |||
| 			TimePoint& operator = (TimePoint &&) E2D_NOEXCEPT; | ||||
| 
 | ||||
| 		private: | ||||
| 			std::chrono::steady_clock::time_point time_; | ||||
| 			Duration dur_since_epoch_; | ||||
| 		}; | ||||
| 
 | ||||
| 		// 获取当前时间
 | ||||
|  | @ -159,4 +185,59 @@ namespace easy2d | |||
| 		// 允许的时间单位有 "ms", "s", "m", "h"
 | ||||
| 		Duration ParseDuration(const std::wstring& parse_str); | ||||
| 	} | ||||
| } | ||||
| } | ||||
| 
 | ||||
| #if VS_VER >= VS_2015 | ||||
| 
 | ||||
| namespace easy2d | ||||
| { | ||||
| 	inline namespace literals | ||||
| 	{ | ||||
| 		inline const easy2d::time::Duration operator "" _ms(long double val) | ||||
| 		{ | ||||
| 			return easy2d::time::Millisecond * val; | ||||
| 		} | ||||
| 
 | ||||
| 		inline const easy2d::time::Duration operator "" _s(long double val) | ||||
| 		{ | ||||
| 			return easy2d::time::Second * val; | ||||
| 		} | ||||
| 
 | ||||
| 		inline const easy2d::time::Duration operator "" _m(long double val) | ||||
| 		{ | ||||
| 			return easy2d::time::Minute * val; | ||||
| 		} | ||||
| 
 | ||||
| 		inline const easy2d::time::Duration operator "" _h(long double val) | ||||
| 		{ | ||||
| 			return easy2d::time::Hour * val; | ||||
| 		} | ||||
| 
 | ||||
| 		inline const easy2d::time::Duration operator "" _ms(unsigned long long val) | ||||
| 		{ | ||||
| 			return easy2d::time::Millisecond * val; | ||||
| 		} | ||||
| 
 | ||||
| 		inline const easy2d::time::Duration operator "" _s(unsigned long long val) | ||||
| 		{ | ||||
| 			return easy2d::time::Second * val; | ||||
| 		} | ||||
| 
 | ||||
| 		inline const easy2d::time::Duration operator "" _m(unsigned long long val) | ||||
| 		{ | ||||
| 			return easy2d::time::Minute * val; | ||||
| 		} | ||||
| 
 | ||||
| 		inline const easy2d::time::Duration operator "" _h(unsigned long long val) | ||||
| 		{ | ||||
| 			return easy2d::time::Hour * val; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	namespace time | ||||
| 	{ | ||||
| 		using namespace easy2d::literals; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
|  |  | |||
|  | @ -25,16 +25,6 @@ | |||
| 
 | ||||
| #pragma once | ||||
| 
 | ||||
| #ifndef __cplusplus | ||||
| #	error 仅能在 C++ 环境下使用 Easy2D | ||||
| #endif | ||||
| 
 | ||||
| #ifndef _MSC_VER | ||||
| #   error 仅支持在 MSVC 环境下编译 | ||||
| #elif _MSC_VER < 1800 | ||||
| #	error Easy2D 不支持 Visual Studio 2013 以下版本 | ||||
| #endif | ||||
| 
 | ||||
| 
 | ||||
| //
 | ||||
| // base
 | ||||
|  |  | |||
|  | @ -25,40 +25,40 @@ namespace easy2d | |||
| { | ||||
| 	namespace math | ||||
| 	{ | ||||
| 		inline int Abs(int v) { return ::abs(v); } | ||||
| 		inline int Abs(int val) { return ::abs(val); } | ||||
| 
 | ||||
| 		inline float Abs(float v) { return ::fabsf(v); } | ||||
| 		inline float Abs(float val) { return ::fabsf(val); } | ||||
| 
 | ||||
| 		inline double Abs(double v) { return ::fabs(v); } | ||||
| 		inline double Abs(double val) { return ::fabs(val); } | ||||
| 
 | ||||
| 		inline float Sqrt(float v) { return ::sqrtf(v); } | ||||
| 		inline float Sqrt(float val) { return ::sqrtf(val); } | ||||
| 
 | ||||
| 		inline double Sqrt(double v) { return ::sqrt(v); } | ||||
| 		inline double Sqrt(double val) { return ::sqrt(val); } | ||||
| 
 | ||||
| 		inline int Sign(int v) { return v < 0 ? -1 : 1; } | ||||
| 		inline int Sign(int val) { return val < 0 ? -1 : 1; } | ||||
| 
 | ||||
| 		inline float Sign(float v) { return v < 0 ? -1.f : 1.f; } | ||||
| 		inline float Sign(float val) { return val < 0 ? -1.f : 1.f; } | ||||
| 
 | ||||
| 		inline double Sign(double v) { return v < 0 ? -1.0 : 1.0; } | ||||
| 		inline double Sign(double val) { return val < 0 ? -1.0 : 1.0; } | ||||
| 
 | ||||
| 		inline float Sin(float v) { return ::sinf(v); } | ||||
| 		inline float Sin(float val) { return ::sinf(val); } | ||||
| 
 | ||||
| 		inline double Sin(double v) { return ::sin(v); } | ||||
| 		inline double Sin(double val) { return ::sin(val); } | ||||
| 
 | ||||
| 		inline float Cos(float v) { return ::cosf(v); } | ||||
| 		inline float Cos(float val) { return ::cosf(val); } | ||||
| 
 | ||||
| 		inline double Cos(double v) { return ::cos(v); } | ||||
| 		inline double Cos(double val) { return ::cos(val); } | ||||
| 
 | ||||
| 		inline float Tan(float v) { return ::tanf(v); } | ||||
| 		inline float Tan(float val) { return ::tanf(val); } | ||||
| 
 | ||||
| 		inline double Tan(double v) { return ::tan(v); } | ||||
| 		inline double Tan(double val) { return ::tan(val); } | ||||
| 
 | ||||
| 		inline float Ceil(float v) { return ceil(v); } | ||||
| 		inline float Ceil(float val) { return ceil(val); } | ||||
| 
 | ||||
| 		inline double Ceil(double v) { return ceil(v); } | ||||
| 		inline double Ceil(double val) { return ceil(val); } | ||||
| 
 | ||||
| 		inline float Floor(float v) { return floor(v); } | ||||
| 		inline float Floor(float val) { return floor(val); } | ||||
| 
 | ||||
| 		inline double Floor(double v) { return floor(v); } | ||||
| 		inline double Floor(double val) { return floor(val); } | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -59,14 +59,14 @@ namespace easy2d | |||
| 				return Vector2(x - other.x, y - other.y); | ||||
| 			} | ||||
| 
 | ||||
| 			inline Vector2 operator * (float value) const | ||||
| 			inline Vector2 operator * (float val) const | ||||
| 			{ | ||||
| 				return Vector2(x * value, y * value); | ||||
| 				return Vector2(x * val, y * val); | ||||
| 			} | ||||
| 
 | ||||
| 			inline Vector2 operator / (float value) const | ||||
| 			inline Vector2 operator / (float val) const | ||||
| 			{ | ||||
| 				return Vector2(x / value, y / value); | ||||
| 				return Vector2(x / val, y / val); | ||||
| 			} | ||||
| 
 | ||||
| 			inline Vector2 operator - () const | ||||
|  |  | |||
|  | @ -44,56 +44,56 @@ namespace easy2d | |||
| 		return temp[0] == L'\0'; | ||||
| 	} | ||||
| 
 | ||||
| 	bool Data::SaveInt(int value) | ||||
| 	bool Data::SaveInt(int val) | ||||
| 	{ | ||||
| 		BOOL ret = ::WritePrivateProfileStringW( | ||||
| 			field_.c_str(), | ||||
| 			key_.c_str(), | ||||
| 			std::to_wstring(value).c_str(), | ||||
| 			std::to_wstring(val).c_str(), | ||||
| 			data_path_.c_str() | ||||
| 		); | ||||
| 		return ret == TRUE; | ||||
| 	} | ||||
| 
 | ||||
| 	bool Data::SaveFloat(float value) | ||||
| 	bool Data::SaveFloat(float val) | ||||
| 	{ | ||||
| 		BOOL ret = ::WritePrivateProfileStringW( | ||||
| 			field_.c_str(), | ||||
| 			key_.c_str(), | ||||
| 			std::to_wstring(value).c_str(), | ||||
| 			std::to_wstring(val).c_str(), | ||||
| 			data_path_.c_str() | ||||
| 		); | ||||
| 		return ret == TRUE; | ||||
| 	} | ||||
| 
 | ||||
| 	bool Data::SaveDouble(double value) | ||||
| 	bool Data::SaveDouble(double val) | ||||
| 	{ | ||||
| 		BOOL ret = ::WritePrivateProfileStringW( | ||||
| 			field_.c_str(), | ||||
| 			key_.c_str(), | ||||
| 			std::to_wstring(value).c_str(), | ||||
| 			std::to_wstring(val).c_str(), | ||||
| 			data_path_.c_str() | ||||
| 		); | ||||
| 		return ret == TRUE; | ||||
| 	} | ||||
| 
 | ||||
| 	bool Data::SaveBool(bool value) | ||||
| 	bool Data::SaveBool(bool val) | ||||
| 	{ | ||||
| 		BOOL ret = ::WritePrivateProfileStringW( | ||||
| 			field_.c_str(), | ||||
| 			key_.c_str(), | ||||
| 			(value ? L"1" : L"0"), | ||||
| 			(val ? L"1" : L"0"), | ||||
| 			data_path_.c_str() | ||||
| 		); | ||||
| 		return ret == TRUE; | ||||
| 	} | ||||
| 
 | ||||
| 	bool Data::SaveString(const String& value) | ||||
| 	bool Data::SaveString(const String& val) | ||||
| 	{ | ||||
| 		BOOL ret = ::WritePrivateProfileStringW( | ||||
| 			field_.c_str(), | ||||
| 			key_.c_str(), | ||||
| 			value.c_str(), | ||||
| 			val.c_str(), | ||||
| 			data_path_.c_str() | ||||
| 		); | ||||
| 		return ret == TRUE; | ||||
|  |  | |||
|  | @ -37,27 +37,27 @@ namespace easy2d | |||
| 
 | ||||
| 		// 保存 int 类型的值
 | ||||
| 		bool SaveInt( | ||||
| 			int value | ||||
| 			int val | ||||
| 		); | ||||
| 
 | ||||
| 		// 保存 float 类型的值
 | ||||
| 		bool SaveFloat( | ||||
| 			float value | ||||
| 			float val | ||||
| 		); | ||||
| 
 | ||||
| 		// 保存 double 类型的值
 | ||||
| 		bool SaveDouble( | ||||
| 			double value | ||||
| 			double val | ||||
| 		); | ||||
| 
 | ||||
| 		// 保存 bool 类型的值
 | ||||
| 		bool SaveBool( | ||||
| 			bool value | ||||
| 			bool val | ||||
| 		); | ||||
| 
 | ||||
| 		// 保存 String 类型的值
 | ||||
| 		bool SaveString( | ||||
| 			const String& value | ||||
| 			const String& val | ||||
| 		); | ||||
| 
 | ||||
| 		// 获取 int 类型的值
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue