ANSI String support
This commit is contained in:
		
							parent
							
								
									22fba22dcc
								
							
						
					
					
						commit
						d0733b01e2
					
				|  | @ -12,7 +12,7 @@ static bool s_bInitialized = false; | ||||||
| static e2d::String s_sAppName; | static e2d::String s_sAppName; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| bool e2d::Game::init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID, LPCTSTR sAppname) | bool e2d::Game::init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID, const String & sAppname) | ||||||
| { | { | ||||||
| 	if (s_bInitialized) | 	if (s_bInitialized) | ||||||
| 	{ | 	{ | ||||||
|  | @ -63,7 +63,7 @@ bool e2d::Game::init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIco | ||||||
| 		// 重设 Client 大小
 | 		// 重设 Client 大小
 | ||||||
| 		Window::setSize(nWidth, nHeight); | 		Window::setSize(nWidth, nHeight); | ||||||
| 		// 设置 AppName
 | 		// 设置 AppName
 | ||||||
| 		s_sAppName = (sAppname != nullptr) ? sAppname : Window::getTitle(); | 		s_sAppName = sAppname.isEmpty() ? Window::getTitle() : sAppname; | ||||||
| 		// 标志初始化成功
 | 		// 标志初始化成功
 | ||||||
| 		s_bInitialized = true; | 		s_bInitialized = true; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -9,7 +9,7 @@ static HWND s_HWnd = nullptr; | ||||||
| static bool s_bShowConsole = false; | static bool s_bShowConsole = false; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| bool e2d::Window::__init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID /*= nullptr*/) | bool e2d::Window::__init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID /*= nullptr*/) | ||||||
| { | { | ||||||
| 	// ע˛á´°żÚŔŕ
 | 	// ע˛á´°żÚŔŕ
 | ||||||
| 	WNDCLASSEX wcex = { sizeof(WNDCLASSEX) }; | 	WNDCLASSEX wcex = { sizeof(WNDCLASSEX) }; | ||||||
|  |  | ||||||
|  | @ -13,12 +13,12 @@ e2d::Image::Image() | ||||||
| { | { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::Image::Image(LPCTSTR strFileName) | e2d::Image::Image(const String & strFileName) | ||||||
| { | { | ||||||
| 	this->loadFrom(strFileName); | 	this->loadFrom(strFileName); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::Image::Image(LPCTSTR strFileName, double nClipX, double nClipY, double nClipWidth, double nClipHeight) | e2d::Image::Image(const String & strFileName, double nClipX, double nClipY, double nClipWidth, double nClipHeight) | ||||||
| { | { | ||||||
| 	this->loadFrom(strFileName); | 	this->loadFrom(strFileName); | ||||||
| 	this->clip(nClipX, nClipY, nClipWidth, nClipHeight); | 	this->clip(nClipX, nClipY, nClipWidth, nClipHeight); | ||||||
|  |  | ||||||
|  | @ -1,5 +1,22 @@ | ||||||
| #include "..\ecommon.h" | #include "..\ecommon.h" | ||||||
| #include <iomanip> | #include <iomanip> | ||||||
|  | #include <comutil.h> | ||||||
|  | #pragma comment(lib, "comsuppw.lib") | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | // 将 Unicode 字符串转化为 ANSI 字符串
 | ||||||
|  | static std::string ConvertWide2Ansi(const wchar_t* wstr) | ||||||
|  | { | ||||||
|  | 	std::string str = static_cast<char*>(_bstr_t(wstr)); | ||||||
|  | 	return std::move(str); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // 将 ANSI 字符串转化为 Unicode 字符串
 | ||||||
|  | static std::wstring ConvertAnsi2Wide(const char* cstr) | ||||||
|  | { | ||||||
|  | 	std::wstring str = static_cast<wchar_t*>(_bstr_t(cstr)); | ||||||
|  | 	return std::move(str); | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| e2d::String::String() | e2d::String::String() | ||||||
|  | @ -12,6 +29,16 @@ e2d::String::String(const wchar_t *str) | ||||||
| { | { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | e2d::String::String(const char *cstr) | ||||||
|  | 	: m_str(ConvertAnsi2Wide(cstr)) | ||||||
|  | { | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | e2d::String::String(const std::string &str) | ||||||
|  | 	: m_str(ConvertAnsi2Wide(str.c_str())) | ||||||
|  | { | ||||||
|  | } | ||||||
|  | 
 | ||||||
| e2d::String::String(e2d::String && str) | e2d::String::String(e2d::String && str) | ||||||
| { | { | ||||||
| 	m_str = std::move(str.m_str); | 	m_str = std::move(str.m_str); | ||||||
|  | @ -38,6 +65,12 @@ e2d::String &e2d::String::operator=(const wchar_t *str) | ||||||
| 	return *this; | 	return *this; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | e2d::String & e2d::String::operator=(const char *cstr) | ||||||
|  | { | ||||||
|  | 	m_str = ConvertAnsi2Wide(cstr); | ||||||
|  | 	return *this; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| e2d::String & e2d::String::operator=(const String &str) | e2d::String & e2d::String::operator=(const String &str) | ||||||
| { | { | ||||||
| 	m_str = str.m_str; | 	m_str = str.m_str; | ||||||
|  | @ -50,6 +83,12 @@ e2d::String & e2d::String::operator=(const std::wstring &str) | ||||||
| 	return *this; | 	return *this; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | e2d::String & e2d::String::operator=(const std::string &str) | ||||||
|  | { | ||||||
|  | 	m_str = ConvertAnsi2Wide(str.c_str()); | ||||||
|  | 	return *this; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| bool e2d::String::operator==(const wchar_t *str) | bool e2d::String::operator==(const wchar_t *str) | ||||||
| { | { | ||||||
| 	if (str) | 	if (str) | ||||||
|  | @ -62,6 +101,19 @@ bool e2d::String::operator==(const wchar_t *str) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | bool e2d::String::operator==(const char *str) | ||||||
|  | { | ||||||
|  | 	if (str) | ||||||
|  | 	{ | ||||||
|  | 		String temp(str); | ||||||
|  | 		return (m_str == temp.m_str); | ||||||
|  | 	} | ||||||
|  | 	else | ||||||
|  | 	{ | ||||||
|  | 		return false; | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
| bool e2d::String::operator ==(const e2d::String &str) | bool e2d::String::operator ==(const e2d::String &str) | ||||||
| { | { | ||||||
| 	return m_str == str.m_str; | 	return m_str == str.m_str; | ||||||
|  | @ -84,6 +136,19 @@ bool e2d::String::operator!=(const wchar_t *str) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | bool e2d::String::operator!=(const char *str) | ||||||
|  | { | ||||||
|  | 	if (str) | ||||||
|  | 	{ | ||||||
|  | 		String temp(str); | ||||||
|  | 		return (m_str != temp.m_str); | ||||||
|  | 	} | ||||||
|  | 	else | ||||||
|  | 	{ | ||||||
|  | 		return true; | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
| bool e2d::String::operator!=(const e2d::String &str) | bool e2d::String::operator!=(const e2d::String &str) | ||||||
| { | { | ||||||
| 	return m_str != str.m_str; | 	return m_str != str.m_str; | ||||||
|  | @ -106,10 +171,10 @@ e2d::String e2d::String::operator+(const wchar_t *str) | ||||||
| 	return std::move(temp); | 	return std::move(temp); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::String e2d::String::operator+(const wchar_t x) | e2d::String e2d::String::operator+(const char *str) | ||||||
| { | { | ||||||
| 	String temp; | 	String temp(str); | ||||||
| 	temp.m_str = m_str + x; | 	temp.m_str += m_str; | ||||||
| 	return std::move(temp); | 	return std::move(temp); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -127,13 +192,6 @@ e2d::String e2d::String::operator+(const std::wstring &str) | ||||||
| 	return std::move(temp); | 	return std::move(temp); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::String e2d::operator+(const wchar_t ch, const e2d::String &str) |  | ||||||
| { |  | ||||||
| 	String temp; |  | ||||||
| 	temp.m_str = ch + str.m_str; |  | ||||||
| 	return std::move(temp); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| e2d::String e2d::operator+(const wchar_t *str1, const e2d::String &str2) | e2d::String e2d::operator+(const wchar_t *str1, const e2d::String &str2) | ||||||
| { | { | ||||||
| 	String temp; | 	String temp; | ||||||
|  | @ -141,6 +199,13 @@ e2d::String e2d::operator+(const wchar_t *str1, const e2d::String &str2) | ||||||
| 	return std::move(temp); | 	return std::move(temp); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | e2d::String e2d::operator+(const char *str1, const String &str2) | ||||||
|  | { | ||||||
|  | 	String temp(str1); | ||||||
|  | 	temp.m_str += str2.m_str; | ||||||
|  | 	return std::move(temp); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| e2d::String e2d::operator+(const std::wstring &str1, const e2d::String &str2) | e2d::String e2d::operator+(const std::wstring &str1, const e2d::String &str2) | ||||||
| { | { | ||||||
| 	String temp; | 	String temp; | ||||||
|  | @ -148,28 +213,58 @@ e2d::String e2d::operator+(const std::wstring &str1, const e2d::String &str2) | ||||||
| 	return std::move(temp); | 	return std::move(temp); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::String &e2d::String::operator+=(const wchar_t x) | e2d::String & e2d::String::operator+=(const wchar_t *str) | ||||||
| { |  | ||||||
| 	m_str += x; |  | ||||||
| 	return *this; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| e2d::String &e2d::String::operator+=(const wchar_t *str) |  | ||||||
| { | { | ||||||
| 	m_str += str; | 	m_str += str; | ||||||
| 	return *this; | 	return (*this); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::String &e2d::String::operator+=(const e2d::String &str) | e2d::String & e2d::String::operator+=(const char *str) | ||||||
|  | { | ||||||
|  | 	m_str += ::ConvertAnsi2Wide(str); | ||||||
|  | 	return (*this); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | e2d::String & e2d::String::operator+=(const String &str) | ||||||
| { | { | ||||||
| 	m_str += str.m_str; | 	m_str += str.m_str; | ||||||
| 	return *this; | 	return (*this); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::String & e2d::String::operator+=(const std::wstring &str) | e2d::String & e2d::String::operator+=(const std::wstring &str) | ||||||
| { | { | ||||||
| 	m_str += str; | 	m_str += str; | ||||||
| 	return *this; | 	return (*this); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool e2d::String::operator>(const String &str) const | ||||||
|  | { | ||||||
|  | 	return m_str > str.m_str; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool e2d::String::operator>=(const String &str) const | ||||||
|  | { | ||||||
|  | 	return m_str >= str.m_str; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool e2d::String::operator<(const String &str) const | ||||||
|  | { | ||||||
|  | 	return m_str < str.m_str; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool e2d::String::operator<=(const String &str) const | ||||||
|  | { | ||||||
|  | 	return m_str <= str.m_str; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | e2d::String & e2d::String::operator<<(const char * value) | ||||||
|  | { | ||||||
|  | 	return this->append(value); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | e2d::String & e2d::String::operator<<(char * value) | ||||||
|  | { | ||||||
|  | 	return this->append(value); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::String::operator const wchar_t*() const | e2d::String::operator const wchar_t*() const | ||||||
|  | @ -177,6 +272,16 @@ e2d::String::operator const wchar_t*() const | ||||||
| 	return m_str.c_str(); | 	return m_str.c_str(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | e2d::String::operator wchar_t*() const | ||||||
|  | { | ||||||
|  | 	return const_cast<wchar_t*>(m_str.c_str()); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | e2d::String::operator const char*() const | ||||||
|  | { | ||||||
|  | 	return ::ConvertWide2Ansi(m_str.c_str()).c_str(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| e2d::String::operator bool() const | e2d::String::operator bool() const | ||||||
| { | { | ||||||
| 	return getLength() != 0; | 	return getLength() != 0; | ||||||
|  | @ -204,12 +309,6 @@ unsigned int e2d::String::getHash() const | ||||||
| 	return hash; | 	return hash; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| std::wistream & e2d::operator>>(std::wistream &cin, e2d::String &str) |  | ||||||
| { |  | ||||||
| 	cin >> str.m_str; |  | ||||||
| 	return cin; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| e2d::String e2d::String::toUpper() const | e2d::String e2d::String::toUpper() const | ||||||
| { | { | ||||||
| 	String str(*this); | 	String str(*this); | ||||||
|  | @ -299,17 +398,52 @@ int e2d::String::findLastOf(const wchar_t ch) const | ||||||
| 	return index; | 	return index; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::String & e2d::String::append(const wchar_t ch) |  | ||||||
| { |  | ||||||
| 	return (*this) += ch; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| e2d::String & e2d::String::append(const wchar_t * str) | e2d::String & e2d::String::append(const wchar_t * str) | ||||||
| { | { | ||||||
| 	return (*this) += str; | 	m_str += str; | ||||||
|  | 	return *this; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | e2d::String & e2d::String::append(const char * str) | ||||||
|  | { | ||||||
|  | 	m_str += ::ConvertAnsi2Wide(str); | ||||||
|  | 	return *this; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | e2d::String & e2d::String::append(char * str) | ||||||
|  | { | ||||||
|  | 	m_str += ::ConvertAnsi2Wide(str); | ||||||
|  | 	return *this; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::String & e2d::String::append(const e2d::String & str) | e2d::String & e2d::String::append(const e2d::String & str) | ||||||
| { | { | ||||||
| 	return (*this) += str; | 	m_str += str.m_str; | ||||||
|  | 	return (*this); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | std::wostream & e2d::operator<<(std::wostream &cout, String &str) | ||||||
|  | { | ||||||
|  | 	cout << str.m_str; | ||||||
|  | 	return cout; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | std::wistream & e2d::operator>>(std::wistream &cin, e2d::String &str) | ||||||
|  | { | ||||||
|  | 	cin >> str.m_str; | ||||||
|  | 	return cin; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | std::ostream & e2d::operator<<(std::ostream &cout, String &str) | ||||||
|  | { | ||||||
|  | 	cout << ::ConvertWide2Ansi(str.m_str.c_str()); | ||||||
|  | 	return cout; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | std::istream & e2d::operator>>(std::istream &cin, e2d::String &str) | ||||||
|  | { | ||||||
|  | 	std::string temp; | ||||||
|  | 	cin >> temp; | ||||||
|  | 	str.m_str = ::ConvertAnsi2Wide(temp.c_str()); | ||||||
|  | 	return cin; | ||||||
| } | } | ||||||
|  | @ -12,13 +12,13 @@ e2d::Sprite::Sprite(Image * image) | ||||||
| 	loadFrom(image); | 	loadFrom(image); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::Sprite::Sprite(LPCTSTR imageFileName) | e2d::Sprite::Sprite(const String & imageFileName) | ||||||
| 	: m_pImage(nullptr) | 	: m_pImage(nullptr) | ||||||
| { | { | ||||||
| 	loadFrom(imageFileName); | 	loadFrom(imageFileName); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::Sprite::Sprite(LPCTSTR imageFileName, double x, double y, double width, double height) | e2d::Sprite::Sprite(const String & imageFileName, double x, double y, double width, double height) | ||||||
| 	: m_pImage(nullptr) | 	: m_pImage(nullptr) | ||||||
| { | { | ||||||
| 	loadFrom(imageFileName); | 	loadFrom(imageFileName); | ||||||
|  | @ -42,7 +42,7 @@ void e2d::Sprite::loadFrom(Image * image) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void e2d::Sprite::loadFrom(LPCTSTR imageFileName) | void e2d::Sprite::loadFrom(const String & imageFileName) | ||||||
| { | { | ||||||
| 	loadFrom(new Image(imageFileName)); | 	loadFrom(new Image(imageFileName)); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -81,7 +81,7 @@ e2d::String e2d::File::getSaveFilePath(const String & title, const String & defE | ||||||
| { | { | ||||||
| 	// 弹出保存对话框
 | 	// 弹出保存对话框
 | ||||||
| 	OPENFILENAME ofn = { 0 }; | 	OPENFILENAME ofn = { 0 }; | ||||||
| 	TCHAR strFilename[MAX_PATH] = { 0 };				// 用于接收文件名
 | 	wchar_t strFilename[MAX_PATH] = { 0 };				// 用于接收文件名
 | ||||||
| 	ofn.lStructSize = sizeof(OPENFILENAME);				// 结构体大小
 | 	ofn.lStructSize = sizeof(OPENFILENAME);				// 结构体大小
 | ||||||
| 	ofn.hwndOwner = Window::getHWnd();					// 窗口句柄
 | 	ofn.hwndOwner = Window::getHWnd();					// 窗口句柄
 | ||||||
| 	ofn.lpstrFilter = L"所有文件\0*.*\0\0";				// 设置过滤
 | 	ofn.lpstrFilter = L"所有文件\0*.*\0\0";				// 设置过滤
 | ||||||
|  |  | ||||||
|  | @ -10,13 +10,13 @@ using namespace e2d; | ||||||
| #define SAFE_DELETE_ARRAY(p) { if (p) { delete[] (p);   (p)=nullptr; } } | #define SAFE_DELETE_ARRAY(p) { if (p) { delete[] (p);   (p)=nullptr; } } | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| inline bool TraceError(LPCTSTR sPrompt) | inline bool TraceError(wchar_t* sPrompt) | ||||||
| { | { | ||||||
| 	WARN_IF(true, "Music error: %s failed!", sPrompt); | 	WARN_IF(true, "Music error: %s failed!", sPrompt); | ||||||
| 	return false; | 	return false; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| inline bool TraceError(LPCTSTR sPrompt, HRESULT hr) | inline bool TraceError(wchar_t* sPrompt, HRESULT hr) | ||||||
| { | { | ||||||
| 	WARN_IF(true, "Music error: %s (%#X)", sPrompt, hr); | 	WARN_IF(true, "Music error: %s (%#X)", sPrompt, hr); | ||||||
| 	return false; | 	return false; | ||||||
|  | @ -40,7 +40,7 @@ Music::~Music() | ||||||
| 	_close(); | 	_close(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool Music::_open(LPCWSTR strFileName) | bool Music::_open(const String & strFileName) | ||||||
| { | { | ||||||
| 	if (m_bOpened) | 	if (m_bOpened) | ||||||
| 	{ | 	{ | ||||||
|  | @ -48,7 +48,7 @@ bool Music::_open(LPCWSTR strFileName) | ||||||
| 		return false; | 		return false; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if (strFileName == nullptr) | 	if (strFileName.isEmpty()) | ||||||
| 	{ | 	{ | ||||||
| 		WARN_IF(true, L"Music::_open Invalid file name."); | 		WARN_IF(true, L"Music::_open Invalid file name."); | ||||||
| 		return false; | 		return false; | ||||||
|  | @ -65,7 +65,7 @@ bool Music::_open(LPCWSTR strFileName) | ||||||
| 	wchar_t strFilePath[MAX_PATH]; | 	wchar_t strFilePath[MAX_PATH]; | ||||||
| 	if (!_findMediaFileCch(strFilePath, MAX_PATH, strFileName)) | 	if (!_findMediaFileCch(strFilePath, MAX_PATH, strFileName)) | ||||||
| 	{ | 	{ | ||||||
| 		WARN_IF(true, L"Failed to find media file: %s", strFileName); | 		WARN_IF(true, L"Failed to find media file: %s", (const wchar_t*)strFileName); | ||||||
| 		return false; | 		return false; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -399,17 +399,17 @@ bool Music::_read(BYTE* pBuffer, DWORD dwSizeToRead) | ||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool Music::_findMediaFileCch(WCHAR* strDestPath, int cchDest, LPCWSTR strFilename) | bool Music::_findMediaFileCch(wchar_t* strDestPath, int cchDest, const String & strFilename) | ||||||
| { | { | ||||||
| 	bool bFound = false; | 	bool bFound = false; | ||||||
| 
 | 
 | ||||||
| 	if (nullptr == strFilename || strFilename[0] == 0 || nullptr == strDestPath || cchDest < 10) | 	if (strFilename.isEmpty() || nullptr == strDestPath || cchDest < 10) | ||||||
| 		return false; | 		return false; | ||||||
| 
 | 
 | ||||||
| 	// Get the exe name, and exe path
 | 	// Get the exe name, and exe path
 | ||||||
| 	WCHAR strExePath[MAX_PATH] = { 0 }; | 	wchar_t strExePath[MAX_PATH] = { 0 }; | ||||||
| 	WCHAR strExeName[MAX_PATH] = { 0 }; | 	wchar_t strExeName[MAX_PATH] = { 0 }; | ||||||
| 	WCHAR* strLastSlash = nullptr; | 	wchar_t* strLastSlash = nullptr; | ||||||
| 	GetModuleFileName(HINST_THISCOMPONENT, strExePath, MAX_PATH); | 	GetModuleFileName(HINST_THISCOMPONENT, strExePath, MAX_PATH); | ||||||
| 	strExePath[MAX_PATH - 1] = 0; | 	strExePath[MAX_PATH - 1] = 0; | ||||||
| 	strLastSlash = wcsrchr(strExePath, TEXT('\\')); | 	strLastSlash = wcsrchr(strExePath, TEXT('\\')); | ||||||
|  | @ -431,13 +431,13 @@ bool Music::_findMediaFileCch(WCHAR* strDestPath, int cchDest, LPCWSTR strFilena | ||||||
| 		return true; | 		return true; | ||||||
| 
 | 
 | ||||||
| 	// Search all parent directories starting at .\ and using strFilename as the leaf name
 | 	// Search all parent directories starting at .\ and using strFilename as the leaf name
 | ||||||
| 	WCHAR strLeafName[MAX_PATH] = { 0 }; | 	wchar_t strLeafName[MAX_PATH] = { 0 }; | ||||||
| 	wcscpy_s(strLeafName, MAX_PATH, strFilename); | 	wcscpy_s(strLeafName, MAX_PATH, strFilename); | ||||||
| 
 | 
 | ||||||
| 	WCHAR strFullPath[MAX_PATH] = { 0 }; | 	wchar_t strFullPath[MAX_PATH] = { 0 }; | ||||||
| 	WCHAR strFullFileName[MAX_PATH] = { 0 }; | 	wchar_t strFullFileName[MAX_PATH] = { 0 }; | ||||||
| 	WCHAR strSearch[MAX_PATH] = { 0 }; | 	wchar_t strSearch[MAX_PATH] = { 0 }; | ||||||
| 	WCHAR* strFilePart = nullptr; | 	wchar_t* strFilePart = nullptr; | ||||||
| 
 | 
 | ||||||
| 	GetFullPathName(L".", MAX_PATH, strFullPath, &strFilePart); | 	GetFullPathName(L".", MAX_PATH, strFullPath, &strFilePart); | ||||||
| 	if (strFilePart == nullptr) | 	if (strFilePart == nullptr) | ||||||
|  |  | ||||||
|  | @ -14,11 +14,11 @@ class Game | ||||||
| public: | public: | ||||||
| 	// 初始化游戏
 | 	// 初始化游戏
 | ||||||
| 	static bool init( | 	static bool init( | ||||||
| 		LPCTSTR sTitle,				/* ´°¿Ú±êÌâ */ | 		const String & sTitle,			/* ´°¿Ú±êÌâ */ | ||||||
| 		UINT32 nWidth,					/* 窗口宽度 */ | 		UINT32 nWidth,					/* 窗口宽度 */ | ||||||
| 		UINT32 nHeight,					/* 窗口高度 */ | 		UINT32 nHeight,					/* 窗口高度 */ | ||||||
| 		LPCTSTR pIconID = nullptr,	/* ´°¿Úͼ±ê */ | 		LPCTSTR pIconID = L"",			/* ´°¿Úͼ±ê */ | ||||||
| 		LPCTSTR sAppname = nullptr	/* AppName */ | 		const String & sAppname = L""	/* AppName */ | ||||||
| 	); | 	); | ||||||
| 
 | 
 | ||||||
| 	// 启动游戏
 | 	// 启动游戏
 | ||||||
|  | @ -89,7 +89,7 @@ public: | ||||||
| private: | private: | ||||||
| 	// 初始化窗口
 | 	// 初始化窗口
 | ||||||
| 	static bool __init( | 	static bool __init( | ||||||
| 		LPCTSTR sTitle, | 		const String & sTitle, | ||||||
| 		UINT32 nWidth, | 		UINT32 nWidth, | ||||||
| 		UINT32 nHeight, | 		UINT32 nHeight, | ||||||
| 		LPCTSTR pIconID | 		LPCTSTR pIconID | ||||||
|  |  | ||||||
							
								
								
									
										140
									
								
								core/ecommon.h
								
								
								
								
							
							
						
						
									
										140
									
								
								core/ecommon.h
								
								
								
								
							|  | @ -99,47 +99,6 @@ public: | ||||||
| 
 | 
 | ||||||
| 	~String(); | 	~String(); | ||||||
| 
 | 
 | ||||||
| 	String& operator= (const wchar_t *); |  | ||||||
| 	String& operator= (const String &); |  | ||||||
| 	String& operator= (const std::wstring &); |  | ||||||
| 
 |  | ||||||
| 	bool operator== (const wchar_t *); |  | ||||||
| 	bool operator== (const String &); |  | ||||||
| 	bool operator== (const std::wstring &); |  | ||||||
| 
 |  | ||||||
| 	bool operator!= (const wchar_t *); |  | ||||||
| 	bool operator!= (const String &); |  | ||||||
| 	bool operator!= (const std::wstring &); |  | ||||||
| 
 |  | ||||||
| 	wchar_t &operator[] (int); |  | ||||||
| 
 |  | ||||||
| 	String operator+ (const wchar_t); |  | ||||||
| 	String operator+ (const wchar_t *); |  | ||||||
| 	String operator+ (const String &); |  | ||||||
| 	String operator+ (const std::wstring &); |  | ||||||
| 
 |  | ||||||
| 	friend String operator+ (const wchar_t, const String &); |  | ||||||
| 	friend String operator+ (const wchar_t*, const String &); |  | ||||||
| 	friend String operator+ (const std::wstring &, const String &); |  | ||||||
| 
 |  | ||||||
| 	String& operator+= (const wchar_t); |  | ||||||
| 	String& operator+= (const wchar_t *); |  | ||||||
| 	String& operator+= (const String &); |  | ||||||
| 	String& operator+= (const std::wstring &); |  | ||||||
| 
 |  | ||||||
| 	template<typename T> |  | ||||||
| 	String& operator<< (const T value) |  | ||||||
| 	{ |  | ||||||
| 		std::wostringstream ss; |  | ||||||
| 		ss << value; |  | ||||||
| 		return (*this) += ss.str(); |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	operator const wchar_t* () const; |  | ||||||
| 	operator bool () const; |  | ||||||
| 
 |  | ||||||
| 	friend std::wistream& operator>> (std::wistream &, String &); |  | ||||||
| 
 |  | ||||||
| 	// 判断字符串是否为空
 | 	// 判断字符串是否为空
 | ||||||
| 	bool isEmpty() const; | 	bool isEmpty() const; | ||||||
| 
 | 
 | ||||||
|  | @ -149,30 +108,41 @@ public: | ||||||
| 	// 获取该字符串的散列值
 | 	// 获取该字符串的散列值
 | ||||||
| 	unsigned int getHash() const; | 	unsigned int getHash() const; | ||||||
| 
 | 
 | ||||||
| 	// 后接字符
 | 	// 后接字符串
 | ||||||
| 	String &append(const wchar_t ch); | 	String& append( | ||||||
|  | 		const wchar_t *str | ||||||
|  | 	); | ||||||
| 
 | 
 | ||||||
| 	// 后接字符串
 | 	// 后接字符串
 | ||||||
| 	String &append(const wchar_t *str); | 	String& append( | ||||||
| 
 | 		const String &str | ||||||
| 	// 后接字符串
 | 	); | ||||||
| 	String &append(const String &str); |  | ||||||
| 
 | 
 | ||||||
| 	// 后接字符串
 | 	// 后接字符串
 | ||||||
| 	template<typename T> | 	template<typename T> | ||||||
| 	String &append(const T &value) | 	String& append(const T &value) | ||||||
| 	{ | 	{ | ||||||
| 		return (*this) += value; | 		std::wostringstream ss; | ||||||
|  | 		ss << value; | ||||||
|  | 		m_str += ss.str(); | ||||||
|  | 		return (*this); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// 获取裁剪字符串
 | 	// 获取裁剪字符串
 | ||||||
| 	String subtract(int offset, int count = -1) const; | 	String subtract( | ||||||
|  | 		int offset,		/* 偏移量 */ | ||||||
|  | 		int count = -1	/* 截取字符数量 */ | ||||||
|  | 	) const; | ||||||
| 
 | 
 | ||||||
| 	// 获取字符串中第一个特定字符的下标
 | 	// 获取字符串中第一个特定字符的下标
 | ||||||
| 	int findFirstOf(const wchar_t ch) const; | 	int findFirstOf( | ||||||
|  | 		const wchar_t ch | ||||||
|  | 	) const; | ||||||
| 
 | 
 | ||||||
| 	// 获取字符串中最后一个特定字符的下标
 | 	// 获取字符串中最后一个特定字符的下标
 | ||||||
| 	int findLastOf(const wchar_t ch) const; | 	int findLastOf( | ||||||
|  | 		const wchar_t ch | ||||||
|  | 	) const; | ||||||
| 
 | 
 | ||||||
| 	// 获取大写字符串
 | 	// 获取大写字符串
 | ||||||
| 	String toUpper() const; | 	String toUpper() const; | ||||||
|  | @ -198,6 +168,68 @@ public: | ||||||
| 		return ss.str(); | 		return ss.str(); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	static String toString(const wchar_t * str) { return std::move(String(str)); } | ||||||
|  | 	static String toString(const char * str) { return std::move(String(str)); } | ||||||
|  | 
 | ||||||
|  | 	String& operator= (const wchar_t *); | ||||||
|  | 	String& operator= (const String &); | ||||||
|  | 	String& operator= (const std::wstring &); | ||||||
|  | 
 | ||||||
|  | 	bool operator== (const wchar_t *); | ||||||
|  | 	bool operator== (const String &); | ||||||
|  | 	bool operator== (const std::wstring &); | ||||||
|  | 
 | ||||||
|  | 	bool operator!= (const wchar_t *); | ||||||
|  | 	bool operator!= (const String &); | ||||||
|  | 	bool operator!= (const std::wstring &); | ||||||
|  | 
 | ||||||
|  | 	wchar_t &operator[] (int); | ||||||
|  | 
 | ||||||
|  | 	String operator+ (const wchar_t *); | ||||||
|  | 	String operator+ (const String &); | ||||||
|  | 	String operator+ (const std::wstring &); | ||||||
|  | 
 | ||||||
|  | 	friend String operator+ (const wchar_t*, const String &); | ||||||
|  | 	friend String operator+ (const std::wstring &, const String &); | ||||||
|  | 
 | ||||||
|  | 	String& operator+= (const wchar_t *); | ||||||
|  | 	String& operator+= (const String &); | ||||||
|  | 	String& operator+= (const std::wstring &); | ||||||
|  | 
 | ||||||
|  | 	bool operator> (const String &) const; | ||||||
|  | 	bool operator>= (const String &) const; | ||||||
|  | 	bool operator< (const String &) const; | ||||||
|  | 	bool operator<= (const String &) const; | ||||||
|  | 
 | ||||||
|  | 	template<typename T> | ||||||
|  | 	String& operator<< (const T value) { return this->append<>(value); } | ||||||
|  | 
 | ||||||
|  | 	operator const wchar_t* () const; | ||||||
|  | 	operator wchar_t* () const; | ||||||
|  | 	operator bool () const; | ||||||
|  | 
 | ||||||
|  | 	friend std::wostream& operator<< (std::wostream &, String &); | ||||||
|  | 	friend std::wistream& operator>> (std::wistream &, String &); | ||||||
|  | 
 | ||||||
|  | 	// 为 ANSI 作出的适应
 | ||||||
|  | 	String(const char *); | ||||||
|  | 	String(const std::string &); | ||||||
|  | 	operator const char* () const; | ||||||
|  | 	String& operator= (const char *); | ||||||
|  | 	String& operator= (const std::string &); | ||||||
|  | 	bool operator== (const char *); | ||||||
|  | 	bool operator!= (const char *); | ||||||
|  | 	String operator+ (const char *); | ||||||
|  | 	friend String operator+ (const char *, const String &); | ||||||
|  | 	String& operator+= (const char *); | ||||||
|  | 	String& operator<< (const char *); | ||||||
|  | 	String& operator<< (char *); | ||||||
|  | 	String& append(const char *); | ||||||
|  | 	String& append(char *); | ||||||
|  | 
 | ||||||
|  | 	friend std::ostream& operator<< (std::ostream &, String &); | ||||||
|  | 	friend std::istream& operator>> (std::istream &, String &); | ||||||
|  | 
 | ||||||
| private: | private: | ||||||
| 	std::wstring m_str; | 	std::wstring m_str; | ||||||
| }; | }; | ||||||
|  | @ -501,12 +533,12 @@ public: | ||||||
| 
 | 
 | ||||||
| 	// 从本地文件中读取资源
 | 	// 从本地文件中读取资源
 | ||||||
| 	Image( | 	Image( | ||||||
| 		LPCTSTR strFilePath	/* 图片文件路径 */ | 		const String & strFilePath	/* 图片文件路径 */ | ||||||
| 	); | 	); | ||||||
| 
 | 
 | ||||||
| 	// 从本地文件中读取资源
 | 	// 从本地文件中读取资源
 | ||||||
| 	Image( | 	Image( | ||||||
| 		LPCTSTR strFilePath,/* 图片文件路径 */ | 		const String & strFilePath,/* 图片文件路径 */ | ||||||
| 		double nClipX,		/* 裁剪位置 X 坐标 */ | 		double nClipX,		/* 裁剪位置 X 坐标 */ | ||||||
| 		double nClipY,		/* 裁剪位置 Y 坐标 */ | 		double nClipY,		/* 裁剪位置 Y 坐标 */ | ||||||
| 		double nClipWidth,	/* 裁剪宽度 */ | 		double nClipWidth,	/* 裁剪宽度 */ | ||||||
|  |  | ||||||
|  | @ -422,12 +422,12 @@ public: | ||||||
| 
 | 
 | ||||||
| 	// 从文件图片创建精灵
 | 	// 从文件图片创建精灵
 | ||||||
| 	Sprite( | 	Sprite( | ||||||
| 		LPCTSTR imageFileName | 		const String & imageFileName | ||||||
| 	); | 	); | ||||||
| 
 | 
 | ||||||
| 	// 从文件图片创建精灵并裁剪
 | 	// 从文件图片创建精灵并裁剪
 | ||||||
| 	Sprite( | 	Sprite( | ||||||
| 		LPCTSTR imageFileName, | 		const String & imageFileName, | ||||||
| 		double x, | 		double x, | ||||||
| 		double y, | 		double y, | ||||||
| 		double width, | 		double width, | ||||||
|  | @ -443,7 +443,7 @@ public: | ||||||
| 
 | 
 | ||||||
| 	// 从本地文件加载图片
 | 	// 从本地文件加载图片
 | ||||||
| 	virtual void loadFrom( | 	virtual void loadFrom( | ||||||
| 		LPCTSTR imageFileName | 		const String & imageFileName | ||||||
| 	); | 	); | ||||||
| 
 | 
 | ||||||
| 	// 裁剪图片
 | 	// 裁剪图片
 | ||||||
|  | @ -472,25 +472,25 @@ public: | ||||||
| 	Text(); | 	Text(); | ||||||
| 
 | 
 | ||||||
| 	Text( | 	Text( | ||||||
| 		const String & text | 		const String & text	/* 文字内容 */ | ||||||
| 	); | 	); | ||||||
| 
 | 
 | ||||||
| 	Text( | 	Text( | ||||||
| 		Font * font | 		Font * font			/* 字体样式 */ | ||||||
| 	); | 	); | ||||||
| 
 | 
 | ||||||
| 	Text( | 	Text( | ||||||
| 		const String & text, | 		const String & text,/* 文字内容 */ | ||||||
| 		Font * font | 		Font * font			/* 字体样式 */ | ||||||
| 	); | 	); | ||||||
| 
 | 
 | ||||||
| 	Text( | 	Text( | ||||||
| 		const String & text, | 		const String & text,					/* 文字内容*/ | ||||||
| 		String fontFamily, | 		String fontFamily,						/* 字体 */ | ||||||
| 		double fontSize = 22, | 		double fontSize = 22,					/* 字号 */ | ||||||
| 		UINT32 color = Color::WHITE, | 		UINT32 color = Color::WHITE,			/* 颜色 */ | ||||||
| 		UINT32 fontWeight = FontWeight::REGULAR, | 		UINT32 fontWeight = FontWeight::REGULAR,/* 粗细值 */ | ||||||
| 		bool italic = false | 		bool italic = false						/* 斜体 */ | ||||||
| 	); | 	); | ||||||
| 
 | 
 | ||||||
| 	virtual ~Text(); | 	virtual ~Text(); | ||||||
|  |  | ||||||
|  | @ -242,7 +242,7 @@ protected: | ||||||
| 
 | 
 | ||||||
| 	virtual ~Music(); | 	virtual ~Music(); | ||||||
| 
 | 
 | ||||||
| 	bool _open(LPCWSTR strFileName); | 	bool _open(const String & strFileName); | ||||||
| 
 | 
 | ||||||
| 	void _close(); | 	void _close(); | ||||||
| 
 | 
 | ||||||
|  | @ -252,7 +252,7 @@ protected: | ||||||
| 
 | 
 | ||||||
| 	bool _read(BYTE* pBuffer, DWORD dwSizeToRead); | 	bool _read(BYTE* pBuffer, DWORD dwSizeToRead); | ||||||
| 
 | 
 | ||||||
| 	bool _findMediaFileCch(WCHAR* strDestPath, int cchDest, LPCWSTR strFilename); | 	bool _findMediaFileCch(wchar_t* strDestPath, int cchDest, const String & strFilename); | ||||||
| 
 | 
 | ||||||
| protected: | protected: | ||||||
| 	bool m_bOpened; | 	bool m_bOpened; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue