From 380bde7932214e0dcfe1985f0908c4cdbf240942 Mon Sep 17 00:00:00 2001 From: Haibo Date: Sun, 28 Oct 2018 23:24:01 +0800 Subject: [PATCH] remove easy2d::String class & refactory easy2d::Resource class refactoring: move Random class to utils.h --- core/actions/Action.cpp | 4 +- core/e2daction.h | 6 +- core/e2dmodule.h | 6 +- core/e2dobject.h | 84 ++-- core/e2dtool.h | 92 ++--- core/e2dutil.h | 202 ++-------- core/modules/Game.cpp | 8 +- core/modules/Graphics.cpp | 8 +- core/objects/Image.cpp | 81 ++-- core/objects/Node.cpp | 40 +- core/objects/Sprite.cpp | 12 +- core/objects/Task.cpp | 6 +- core/objects/Text.cpp | 22 +- core/tools/Data.cpp | 86 ++--- core/tools/File.cpp | 93 ++--- core/tools/Music.cpp | 52 +-- core/tools/Path.cpp | 58 +-- core/tools/Player.cpp | 94 ++--- core/utils/Font.cpp | 9 +- core/{tools => utils}/Random.cpp | 0 core/utils/Resource.cpp | 75 +++- core/utils/String.cpp | 527 -------------------------- project/vs2012/Easy2D.vcxproj | 3 +- project/vs2012/Easy2D.vcxproj.filters | 9 +- project/vs2013/Easy2D.vcxproj | 3 +- project/vs2013/Easy2D.vcxproj.filters | 9 +- project/vs2017/Easy2D.vcxproj | 3 +- project/vs2017/Easy2D.vcxproj.filters | 9 +- 28 files changed, 462 insertions(+), 1139 deletions(-) rename core/{tools => utils}/Random.cpp (100%) delete mode 100644 core/utils/String.cpp diff --git a/core/actions/Action.cpp b/core/actions/Action.cpp index 4a0963d7..5f6aa5ce 100644 --- a/core/actions/Action.cpp +++ b/core/actions/Action.cpp @@ -52,12 +52,12 @@ void easy2d::Action::Stop() done_ = true; } -const easy2d::String& easy2d::Action::GetName() const +const std::wstring& easy2d::Action::GetName() const { return name_; } -void easy2d::Action::SetName(const String& name) +void easy2d::Action::SetName(const std::wstring& name) { name_ = name; } diff --git a/core/e2daction.h b/core/e2daction.h index e82c863c..2e91d35a 100644 --- a/core/e2daction.h +++ b/core/e2daction.h @@ -60,11 +60,11 @@ namespace easy2d virtual void Stop(); // 获取动作名称 - virtual const String& GetName() const; + virtual const std::wstring& GetName() const; // 设置动作名称 virtual void SetName( - const String& name + const std::wstring& name ); // 获取动作的拷贝 @@ -100,7 +100,7 @@ namespace easy2d E2D_DISABLE_COPY(Action); protected: - String name_; + std::wstring name_; bool running_; bool done_; bool initialized_; diff --git a/core/e2dmodule.h b/core/e2dmodule.h index 585f1af0..45a7bcca 100644 --- a/core/e2dmodule.h +++ b/core/e2dmodule.h @@ -232,7 +232,7 @@ namespace easy2d // 设置窗体标题 void SetTitle( - const String& title /* 窗体标题 */ + const std::wstring& title /* 窗体标题 */ ); // 设置窗体图标 @@ -246,7 +246,7 @@ namespace easy2d ); // 获取窗体标题 - const String& GetTitle() const; + const std::wstring& GetTitle() const; // 获取窗体宽度 int GetWidth() const; @@ -310,7 +310,7 @@ namespace easy2d private: HWND hwnd_; - String title_; + std::wstring title_; int width_; int height_; int icon_; diff --git a/core/e2dobject.h b/core/e2dobject.h index 6f14e27f..08264dc8 100644 --- a/core/e2dobject.h +++ b/core/e2dobject.h @@ -37,20 +37,20 @@ namespace easy2d Image(); explicit Image( - const Resource& res + Resource& res ); explicit Image( - const Resource& res, + Resource& res, const Rect& crop_rect /* 裁剪矩形 */ ); explicit Image( - const String& file_name + const std::wstring& file_name ); explicit Image( - const String& file_name, + const std::wstring& file_name, const Rect& crop_rect /* 裁剪矩形 */ ); @@ -58,12 +58,12 @@ namespace easy2d // 加载图片资源 bool Load( - const Resource& res + Resource& res ); // 加载图片资源 bool Load( - const String& file_name + const std::wstring& file_name ); // 将图片裁剪为矩形 @@ -112,12 +112,12 @@ namespace easy2d // 缓存 Bitmap 资源 static bool CacheBitmap( - const String& file_name + const std::wstring& file_name ); // 缓存 Bitmap 资源 static bool CacheBitmap( - const Resource& res + Resource& res ); // 设置 Bitmap @@ -203,15 +203,15 @@ namespace easy2d public: explicit Task( - const Function& func, /* 执行函数 */ - const String& name = L"" /* 任务名称 */ + const Function& func, /* 执行函数 */ + const std::wstring& name = L"" /* 任务名称 */ ); explicit Task( - const Function& func, /* 执行函数 */ - float delay, /* 时间间隔(秒) */ - int times = -1, /* 执行次数(设 -1 为永久执行) */ - const String& name = L"" /* 任务名称 */ + const Function& func, /* 执行函数 */ + float delay, /* 时间间隔(秒) */ + int times = -1, /* 执行次数(设 -1 为永久执行) */ + const std::wstring& name = L"" /* 任务名称 */ ); // 启动任务 @@ -224,7 +224,7 @@ namespace easy2d bool IsRunning() const; // 获取任务名称 - const String& GetName() const; + const std::wstring& GetName() const; // 任务是否就绪 bool IsReady() const; @@ -240,7 +240,7 @@ namespace easy2d bool stopped_; int run_times_; int total_times_; - String name_; + std::wstring name_; Duration delay_; Time last_time_; Function callback_; @@ -294,7 +294,7 @@ namespace easy2d bool IsVisible() const; // 获取节点名称 - const String& GetName() const; + const std::wstring& GetName() const; // 获取节点名称的 Hash 值 size_t GetHashName() const; @@ -366,7 +366,7 @@ namespace easy2d // 设置节点名称 void SetName( - const String& name + const std::wstring& name ); // 设置节点横坐标 @@ -542,12 +542,12 @@ namespace easy2d // 获取所有名称相同的子节点 Nodes GetChildren( - const String& name + const std::wstring& name ) const; // 获取名称相同的子节点 Node* GetChild( - const String& name + const std::wstring& name ) const; // 获取所有子节点 @@ -563,7 +563,7 @@ namespace easy2d // 移除所有名称相同的子节点 void RemoveChildren( - const String& child_name + const std::wstring& child_name ); // 移除所有节点 @@ -579,17 +579,17 @@ namespace easy2d // 继续动作 void ResumeAction( - const String& name + const std::wstring& name ); // 暂停动作 void PauseAction( - const String& name + const std::wstring& name ); // 停止动作 void StopAction( - const String& name + const std::wstring& name ); // 继续所有暂停动作 @@ -611,17 +611,17 @@ namespace easy2d // 启动任务 void StartTasks( - const String& task_name + const std::wstring& task_name ); // 停止任务 void StopTasks( - const String& task_name + const std::wstring& task_name ); // 移除任务 void RemoveTasks( - const String& task_name + const std::wstring& task_name ); // 启动所有任务 @@ -682,7 +682,7 @@ namespace easy2d void UpdateTime(); private: - String name_; + std::wstring name_; size_t hash_name_; Transform transform_; float display_opacity_; @@ -716,20 +716,20 @@ namespace easy2d ); explicit Sprite( - const Resource& res + Resource& res ); explicit Sprite( - const Resource& res, + Resource& res, const Rect& crop_rect /* 裁剪矩形 */ ); explicit Sprite( - const String& file_name + const std::wstring& file_name ); explicit Sprite( - const String& file_name, + const std::wstring& file_name, const Rect& crop_rect /* 裁剪矩形 */ ); @@ -737,12 +737,12 @@ namespace easy2d // 加载图片文件 bool Load( - const Resource& res + Resource& res ); // 加载图片文件 bool Load( - const String& file_name + const std::wstring& file_name ); // 加载图片 @@ -820,15 +820,15 @@ namespace easy2d Text(); explicit Text( - const String& text, /* 文字内容 */ - const Font& font = Font(), /* 字体 */ - const Style& style = Style() /* 文本样式 */ + const std::wstring& text, /* 文字内容 */ + const Font& font = Font(), /* 字体 */ + const Style& style = Style() /* 文本样式 */ ); virtual ~Text(); // 获取文本 - const String& GetText() const; + const std::wstring& GetText() const; // 获取字体 const Font& GetFont() const; @@ -837,7 +837,7 @@ namespace easy2d const Style& GetStyle() const; // 获取字体族 - const String& GetFontFamily() const; + const std::wstring& GetFontFamily() const; // 获取当前字号 float GetFontSize() const; @@ -874,7 +874,7 @@ namespace easy2d // 设置文本 void SetText( - const String& text + const std::wstring& text ); // 设置文本样式 @@ -889,7 +889,7 @@ namespace easy2d // 设置字体族 void SetFontFamily( - const String& family + const std::wstring& family ); // 设置字号(默认值为 22) @@ -978,7 +978,7 @@ namespace easy2d void CreateLayout(); private: - String text_; + std::wstring text_; Font font_; Style style_; IDWriteTextFormat* text_format_; diff --git a/core/e2dtool.h b/core/e2dtool.h index f922ac12..4e8f047c 100644 --- a/core/e2dtool.h +++ b/core/e2dtool.h @@ -78,28 +78,28 @@ namespace easy2d Music(); Music( - const easy2d::String& file_path /* 音乐文件路径 */ + const std::wstring& file_path /* 音乐文件路径 */ ); Music( - const Resource& res /* 音乐资源 */ + Resource& res /* 音乐资源 */ ); virtual ~Music(); // 打开音乐文件 bool Load( - const easy2d::String& file_path /* 音乐文件路径 */ + const std::wstring& file_path /* 音乐文件路径 */ ); // 打开音乐资源 bool Load( - const Resource& res /* 音乐资源 */ + Resource& res /* 音乐资源 */ ); // 播放 bool Play( - int loop_count = 0 /* 播放循环次数 (-1 为循环播放) */ + int loop_count = 0 /* 播放循环次数 (-1 为循环播放) */ ); // 暂停 @@ -150,64 +150,64 @@ namespace easy2d // 预加载音乐资源 bool Load( - const String& file_path /* 音乐文件路径 */ + const std::wstring& file_path /* 音乐文件路径 */ ); // 播放音乐 bool Play( - const String& file_path, /* 音乐文件路径 */ - int loop_count = 0 /* 播放循环次数 (-1 为循环播放) */ + const std::wstring& file_path, /* 音乐文件路径 */ + int loop_count = 0 /* 播放循环次数 (-1 为循环播放) */ ); // 暂停音乐 void Pause( - const String& file_path /* 音乐文件路径 */ + const std::wstring& file_path /* 音乐文件路径 */ ); // 继续播放音乐 void Resume( - const String& file_path /* 音乐文件路径 */ + const std::wstring& file_path /* 音乐文件路径 */ ); // 停止音乐 void Stop( - const String& file_path /* 音乐文件路径 */ + const std::wstring& file_path /* 音乐文件路径 */ ); // 获取音乐播放状态 bool IsPlaying( - const String& file_path /* 音乐文件路径 */ + const std::wstring& file_path /* 音乐文件路径 */ ); // 预加载音乐资源 bool Load( - const Resource& res /* 音乐资源 */ + Resource& res /* 音乐资源 */ ); // 播放音乐 bool Play( - const Resource& res, /* 音乐资源 */ - int loop_count = 0 /* 播放循环次数 (-1 为循环播放) */ + Resource& res, /* 音乐资源 */ + int loop_count = 0 /* 播放循环次数 (-1 为循环播放) */ ); // 暂停音乐 void Pause( - const Resource& res /* 音乐资源 */ + Resource& res /* 音乐资源 */ ); // 继续播放音乐 void Resume( - const Resource& res /* 音乐资源 */ + Resource& res /* 音乐资源 */ ); // 停止音乐 void Stop( - const Resource& res /* 音乐资源 */ + Resource& res /* 音乐资源 */ ); // 获取音乐播放状态 bool IsPlaying( - const Resource& res /* 音乐资源 */ + Resource& res /* 音乐资源 */ ); // 获取音量 @@ -215,7 +215,7 @@ namespace easy2d // 设置音量 void SetVolume( - float volume /* 1.0 为原始音量 */ + float volume /* 1.0 为原始音量 */ ); // 暂停所有音乐 @@ -245,8 +245,8 @@ namespace easy2d { public: Data( - const String& key, /* 键值 */ - const String& field = L"Defalut" /* 字段名称 */ + const std::wstring& key, /* 键值 */ + const std::wstring& field = L"Defalut" /* 字段名称 */ ); // 该数据是否存在 @@ -272,9 +272,9 @@ namespace easy2d bool value ); - // 保存 String 类型的值 + // 保存 std::wstring 类型的值 bool SaveString( - const String& value + const std::wstring& value ); // 获取 int 类型的值 @@ -290,12 +290,12 @@ namespace easy2d bool GetBool() const; // 获取 字符串 类型的值 - String GetString(); + std::wstring GetString(); protected: - String key_; - String field_; - const String& data_path_; + std::wstring key_; + std::wstring field_; + const std::wstring& data_path_; }; @@ -306,14 +306,14 @@ namespace easy2d File(); File( - const String& file_name + const std::wstring& file_name ); virtual ~File(); // 打开文件 bool Open( - const String& file_name + const std::wstring& file_name ); // 文件是否存在 @@ -323,39 +323,39 @@ namespace easy2d bool Delete(); // 获取文件路径 - const String& GetPath() const; + const std::wstring& GetPath() const; // 获取文件扩展名 - String GetExtension() const; + std::wstring GetExtension() const; // 释放资源到临时文件目录 static File Extract( - const Resource& res, /* 资源 */ - const String& dest_file_name /* 目标文件名 */ + Resource& res, /* 资源 */ + const std::wstring& dest_file_name /* 目标文件名 */ ); // 添加文件搜索路径 static void AddSearchPath( - const String& path + const std::wstring& path ); // 弹出打开文件对话框 static File ShowOpenDialog( - const String& title = L"打开", /* 对话框标题 */ - const String& filter = L"" /* 筛选扩展名,例如 "*.jpg;*.jpeg" */ + const std::wstring& title = L"打开", /* 对话框标题 */ + const std::wstring& filter = L"" /* 筛选扩展名,例如 "*.jpg;*.jpeg" */ ); // 弹出保存文件对话框 static File ShowSaveDialog( - const String& title = L"保存", /* 对话框标题 */ - const String& def_file = L"", /* 默认保存的文件名 */ - const String& def_ext = L"" /* 默认追加的扩展名,例如 "txt" */ + const std::wstring& title = L"保存", /* 对话框标题 */ + const std::wstring& def_file = L"", /* 默认保存的文件名 */ + const std::wstring& def_ext = L"" /* 默认追加的扩展名,例如 "txt" */ ); protected: - String file_path_; + std::wstring file_path_; - static std::list search_paths_; + static std::list search_paths_; }; @@ -364,16 +364,16 @@ namespace easy2d { public: // 获取数据的默认保存路径 - static const String& GetDataPath(); + static const std::wstring& GetDataPath(); // 获取临时文件目录 - static const String& GetTemporaryPath(); + static const std::wstring& GetTemporaryPath(); // 获取 LocalAppData 目录 - static const String& GetLocalAppDataPath(); + static const std::wstring& GetLocalAppDataPath(); // 获取当前程序的运行路径 - static const String& GetExeFilePath(); + static const std::wstring& GetExeFilePath(); }; } // end of easy2d namespace diff --git a/core/e2dutil.h b/core/e2dutil.h index 64be5bd8..7aa5ba09 100644 --- a/core/e2dutil.h +++ b/core/e2dutil.h @@ -137,171 +137,6 @@ namespace easy2d }; - // 字符串 - class String - { - public: - String(); - - String( - const String & - ); - - String( - const char * - ); - - String( - const wchar_t * - ); - - String( - String && - ); - - ~String(); - - // 获取字符串长度 - int Length() const; - - // 获取该字符串的 Hash 值 - size_t GetHash() const; - - // 判断字符串是否为空 - bool IsEmpty() const; - - // 获取指定位置字符 - const wchar_t& At( - size_t index - ) const; - - // 比较字符串 - int Compare( - const String & str - ) const; - - // 截取字符串 - String Subtract( - int offset, /* 偏移量 */ - int count = -1 /* 截取字符数量 */ - ) const; - - // 插入字符串 - void Insert( - const String & str, - int pos - ); - - // 替换字符串中的指定内容 - void Replace( - const String & from, /* 需替换内容 */ - const String & to /* 替换成内容 */ - ); - - // 删除字符串中的指定内容 - void Erase( - int offset, /* 偏移量 */ - int count /* 删除字符数量 */ - ); - - // 搜索字符串 - int Find( - const String & str, /* 查找内容 */ - int offset = 0 /* 偏移量 */ - ) const; - - // 清空字符串 - void Clear(); - - // 获取大写字符串 - String ToUpper() const; - - // 获取小写字符串 - String ToLower() const; - - // 将字符串转化为 int 型 - int ToInt() const; - - // 将字符串转化为 float 型 - float ToFloat() const; - - // 将字符串转化为 double 型 - double ToDouble() const; - - // 将字符串转化为 bool 型 - bool ToBool() const; - - // 数字类型转字符串 - static String Parse(int value); - static String Parse(unsigned int value); - static String Parse(unsigned long long value); - static String Parse(float value); - static String Parse(double value); - - // 格式化字符串 - static String Format(const char * format, ...); - static String Format(const wchar_t * format, ...); - - // 赋值运算符 - String& operator= (const String &); - String& operator= (const char *); - String& operator= (const wchar_t *); - - // 运算符 - String& operator+= (const String &); - String& operator+= (const char *); - String& operator+= (const wchar_t *); - String operator+ (const String &) const; - String operator+ (const char *) const; - String operator+ (const wchar_t *) const; - - // 友元运算符 - friend String operator+ (const char *, const String &); - friend String operator+ (const wchar_t*, const String &); - - // 类型转换操作符 - E2D_OP_EXPLICIT operator const wchar_t* () const; - E2D_OP_EXPLICIT operator wchar_t* () const; - E2D_OP_EXPLICIT operator std::wstring() const; - E2D_OP_EXPLICIT operator std::string() const; - - // 比较运算符 - bool operator== (const String &) const; - bool operator== (const char *) const; - bool operator== (const wchar_t *) const; - bool operator!= (const String &) const; - bool operator!= (const char *) const; - bool operator!= (const wchar_t *) const; - bool operator> (const String &) const; - bool operator>= (const String &) const; - bool operator< (const String &) const; - bool operator<= (const String &) const; - - // << 运算符(后接字符串) - String& operator<< (const String &); - String& operator<< (const char *); - String& operator<< (char *); - String& operator<< (const wchar_t *); - String& operator<< (wchar_t *); - String& operator<< (int value); - String& operator<< (unsigned int value); - String& operator<< (float value); - String& operator<< (double value); - - // 其他运算符 - wchar_t& operator[] (size_t); - - friend std::ostream& operator<< (std::ostream &, const String &); - friend std::wostream& operator<< (std::wostream &, const String &); - - friend std::istream& operator>> (std::istream &, String &); - friend std::wistream& operator>> (std::wistream &, String &); - - private: - std::wstring string_; - }; - - // 颜色 class Color { @@ -608,10 +443,10 @@ namespace easy2d class Font { public: - String family; // 字体族 - float size; // 字号 - UINT weight; // 粗细值 - bool italic; // 是否斜体 + std::wstring family; // 字体族 + float size; // 字号 + UINT weight; // 粗细值 + bool italic; // 是否斜体 public: // 字体粗细值 @@ -629,10 +464,8 @@ namespace easy2d }; public: - Font(); - explicit Font( - const String& family, + const std::wstring& family = L"", float size = 22, UINT weight = Font::Weight::Normal, bool italic = false @@ -645,13 +478,28 @@ namespace easy2d { public: Resource( - int resource_id, /* 资源名称 */ - const String& resource_type /* 资源类型 */ + LPCWSTR name, /* 资源名称 */ + LPCWSTR type /* 资源类型 */ ); - public: - int id; - String type; + bool Load(); + + LPCWSTR GetName() const; + + LPCWSTR GetType() const; + + LPVOID GetData() const; + + DWORD GetDataSize() const; + + size_t GetHashCode() const; + + private: + bool loaded_; + LPCWSTR name_; + LPCWSTR type_; + LPVOID data_; + DWORD data_size_; }; diff --git a/core/modules/Game.cpp b/core/modules/Game.cpp index 7733db8d..f47c4068 100644 --- a/core/modules/Game.cpp +++ b/core/modules/Game.cpp @@ -302,7 +302,7 @@ void easy2d::Game::Init() hwnd_ = ::CreateWindowEx( NULL, REGISTER_CLASS, - (LPCTSTR)title_, + title_.c_str(), WINDOW_STYLE, int(client_rect.origin.x), int(client_rect.origin.y), @@ -421,7 +421,7 @@ HWND easy2d::Game::GetHWnd() const return hwnd_; } -const easy2d::String& easy2d::Game::GetTitle() const +const std::wstring& easy2d::Game::GetTitle() const { return title_; } @@ -448,13 +448,13 @@ void easy2d::Game::SetSize(int width, int height) } } -void easy2d::Game::SetTitle(const String& title) +void easy2d::Game::SetTitle(const std::wstring& title) { title_ = title; if (hwnd_) { - ::SetWindowText(hwnd_, (LPCWSTR)title); + ::SetWindowText(hwnd_, title.c_str()); } } diff --git a/core/modules/Graphics.cpp b/core/modules/Graphics.cpp index 3c044218..1474096d 100644 --- a/core/modules/Graphics.cpp +++ b/core/modules/Graphics.cpp @@ -660,7 +660,9 @@ void easy2d::Graphics::DrawDebugInfo() ++render_times_; if (duration >= 100) { - String fps_text = String::Format(L"FPS: %.1f", (1000.f / duration * render_times_)); + wchar_t fps_text[12] = {}; + int len = swprintf_s(fps_text, L"FPS: %.1f", 1000.f / duration * render_times_); + last_render_time_ = Time::Now(); render_times_ = 0; @@ -668,8 +670,8 @@ void easy2d::Graphics::DrawDebugInfo() ThrowIfFailed( write_factory_->CreateTextLayout( - (const wchar_t *)fps_text, - (UINT32)fps_text.Length(), + fps_text, + static_cast(len), fps_text_format_, 0, 0, diff --git a/core/objects/Image.cpp b/core/objects/Image.cpp index 784fd811..f67d7839 100644 --- a/core/objects/Image.cpp +++ b/core/objects/Image.cpp @@ -30,14 +30,14 @@ easy2d::Image::Image() { } -easy2d::Image::Image(const Resource& res) +easy2d::Image::Image(Resource& res) : bitmap_(nullptr) , crop_rect_() { this->Load(res); } -easy2d::Image::Image(const Resource& res, const Rect& crop_rect) +easy2d::Image::Image(Resource& res, const Rect& crop_rect) : bitmap_(nullptr) , crop_rect_() { @@ -45,14 +45,14 @@ easy2d::Image::Image(const Resource& res, const Rect& crop_rect) this->Crop(crop_rect); } -easy2d::Image::Image(const String & file_name) +easy2d::Image::Image(const std::wstring & file_name) : bitmap_(nullptr) , crop_rect_() { this->Load(file_name); } -easy2d::Image::Image(const String & file_name, const Rect & crop_rect) +easy2d::Image::Image(const std::wstring & file_name, const Rect & crop_rect) : bitmap_(nullptr) , crop_rect_() { @@ -65,7 +65,7 @@ easy2d::Image::~Image() SafeRelease(bitmap_); } -bool easy2d::Image::Load(const Resource& res) +bool easy2d::Image::Load(Resource& res) { if (!Image::CacheBitmap(res)) { @@ -73,15 +73,15 @@ bool easy2d::Image::Load(const Resource& res) return false; } - this->SetBitmap(bitmap_cache_.at(res.id)); + this->SetBitmap(bitmap_cache_.at(res.GetHashCode())); return true; } -bool easy2d::Image::Load(const String & file_name) +bool easy2d::Image::Load(const std::wstring & file_name) { - E2D_WARNING_IF(file_name.IsEmpty(), "Image Load failed! Invalid file name."); + E2D_WARNING_IF(file_name.empty(), "Image Load failed! Invalid file name."); - if (file_name.IsEmpty()) + if (file_name.empty()) return false; if (!Image::CacheBitmap(file_name)) @@ -90,7 +90,7 @@ bool easy2d::Image::Load(const String & file_name) return false; } - this->SetBitmap(bitmap_cache_.at(file_name.GetHash())); + this->SetBitmap(bitmap_cache_.at(std::hash{}(file_name))); return true; } @@ -182,9 +182,10 @@ ID2D1Bitmap * easy2d::Image::GetBitmap() const return bitmap_; } -bool easy2d::Image::CacheBitmap(const Resource& res) +bool easy2d::Image::CacheBitmap(Resource& res) { - if (bitmap_cache_.find(res.id) != bitmap_cache_.end()) + size_t hash_code = res.GetHashCode(); + if (bitmap_cache_.find(hash_code) != bitmap_cache_.end()) { return true; } @@ -199,43 +200,9 @@ bool easy2d::Image::CacheBitmap(const Resource& res) IWICStream* stream = nullptr; IWICFormatConverter* converter = nullptr; ID2D1Bitmap* bitmap = nullptr; - HRSRC res_handle = nullptr; - HGLOBAL res_data_handle = nullptr; - LPVOID image_file = nullptr; - DWORD image_file_size = 0; - // 定位资源 - res_handle = ::FindResourceW( - hinstance, - MAKEINTRESOURCE(res.id), - (LPCWSTR)res.type - ); - - hr = res_handle ? S_OK : E_FAIL; - - if (SUCCEEDED(hr)) - { - // 加载资源 - res_data_handle = ::LoadResource(hinstance, res_handle); - - hr = res_data_handle ? S_OK : E_FAIL; - } - - if (SUCCEEDED(hr)) - { - // 获取文件指针,并锁定资源 - image_file = ::LockResource(res_data_handle); - - hr = image_file ? S_OK : E_FAIL; - } - - if (SUCCEEDED(hr)) - { - // 计算大小 - image_file_size = ::SizeofResource(hinstance, res_handle); - - hr = image_file_size ? S_OK : E_FAIL; - } + // 加载资源 + hr = res.Load() ? S_OK : E_FAIL; if (SUCCEEDED(hr)) { @@ -247,8 +214,8 @@ bool easy2d::Image::CacheBitmap(const Resource& res) { // 初始化流 hr = stream->InitializeFromMemory( - reinterpret_cast(image_file), - image_file_size + static_cast(res.GetData()), + res.GetDataSize() ); } @@ -300,7 +267,7 @@ bool easy2d::Image::CacheBitmap(const Resource& res) if (SUCCEEDED(hr)) { - bitmap_cache_.insert(std::make_pair(res.id, bitmap)); + bitmap_cache_.insert(std::make_pair(hash_code, bitmap)); } // 释放相关资源 @@ -312,10 +279,10 @@ bool easy2d::Image::CacheBitmap(const Resource& res) return SUCCEEDED(hr); } -bool easy2d::Image::CacheBitmap(const String & file_name) +bool easy2d::Image::CacheBitmap(const std::wstring & file_name) { - size_t hash = file_name.GetHash(); - if (bitmap_cache_.find(hash) != bitmap_cache_.end()) + size_t hash_code = std::hash{}(file_name); + if (bitmap_cache_.find(hash_code) != bitmap_cache_.end()) return true; File image_file; @@ -324,7 +291,7 @@ bool easy2d::Image::CacheBitmap(const String & file_name) // 用户输入的路径不一定是完整路径,因为用户可能通过 File::AddSearchPath 添加 // 默认搜索路径,所以需要通过 File::GetPath 获取完整路径 - String image_file_path = image_file.GetPath(); + std::wstring image_file_path = image_file.GetPath(); Graphics* graphics_device = Device::GetGraphics(); IWICImagingFactory* imaging_factory = graphics_device->GetImagingFactory(); @@ -337,7 +304,7 @@ bool easy2d::Image::CacheBitmap(const String & file_name) // 创建解码器 HRESULT hr = imaging_factory->CreateDecoderFromFilename( - (LPCWSTR)image_file_path, + image_file_path.c_str(), nullptr, GENERIC_READ, WICDecodeMetadataCacheOnLoad, @@ -381,7 +348,7 @@ bool easy2d::Image::CacheBitmap(const String & file_name) if (SUCCEEDED(hr)) { - bitmap_cache_.insert(std::make_pair(hash, bitmap)); + bitmap_cache_.insert(std::make_pair(hash_code, bitmap)); } // 释放相关资源 diff --git a/core/objects/Node.cpp b/core/objects/Node.cpp index 8372c828..7ad699c0 100644 --- a/core/objects/Node.cpp +++ b/core/objects/Node.cpp @@ -326,7 +326,7 @@ bool easy2d::Node::IsVisible() const return visible_; } -const easy2d::String& easy2d::Node::GetName() const +const std::wstring& easy2d::Node::GetName() const { return name_; } @@ -653,15 +653,15 @@ easy2d::Scene * easy2d::Node::GetParentScene() const return parent_scene_; } -easy2d::Node::Nodes easy2d::Node::GetChildren(const String& name) const +easy2d::Node::Nodes easy2d::Node::GetChildren(const std::wstring& name) const { Nodes children; - size_t hash = name.GetHash(); + size_t hash_code = std::hash{}(name); for (const auto& child : children_) { // 不同的名称可能会有相同的 Hash 值,但是先比较 Hash 可以提升搜索速度 - if (child->hash_name_ == hash && child->name_ == name) + if (child->hash_name_ == hash_code && child->name_ == name) { children.push_back(child); } @@ -669,14 +669,14 @@ easy2d::Node::Nodes easy2d::Node::GetChildren(const String& name) const return std::move(children); } -easy2d::Node * easy2d::Node::GetChild(const String& name) const +easy2d::Node * easy2d::Node::GetChild(const std::wstring& name) const { - size_t hash = name.GetHash(); + size_t hash_code = std::hash{}(name); for (const auto& child : children_) { // 不同的名称可能会有相同的 Hash 值,但是先比较 Hash 可以提升搜索速度 - if (child->hash_name_ == hash && child->name_ == name) + if (child->hash_name_ == hash_code && child->name_ == name) { return child; } @@ -731,17 +731,17 @@ bool easy2d::Node::RemoveChild(Node * child) return false; } -void easy2d::Node::RemoveChildren(const String& child_name) +void easy2d::Node::RemoveChildren(const std::wstring& child_name) { if (children_.empty()) { return; } - size_t hash = child_name.GetHash(); + size_t hash_code = std::hash{}(child_name); for (auto iter = children_.begin(); iter != children_.end();) { - if ((*iter)->hash_name_ == hash && (*iter)->name_ == child_name) + if ((*iter)->hash_name_ == hash_code && (*iter)->name_ == child_name) { (*iter)->parent_ = nullptr; if ((*iter)->parent_scene_) @@ -792,7 +792,7 @@ void easy2d::Node::RunAction(Action * action) } } -void easy2d::Node::ResumeAction(const String& name) +void easy2d::Node::ResumeAction(const std::wstring& name) { if (actions_.empty()) return; @@ -806,7 +806,7 @@ void easy2d::Node::ResumeAction(const String& name) } } -void easy2d::Node::PauseAction(const String& name) +void easy2d::Node::PauseAction(const std::wstring& name) { if (actions_.empty()) return; @@ -820,7 +820,7 @@ void easy2d::Node::PauseAction(const String& name) } } -void easy2d::Node::StopAction(const String& name) +void easy2d::Node::StopAction(const std::wstring& name) { if (actions_.empty()) return; @@ -926,7 +926,7 @@ void easy2d::Node::AddTask(Task * task) } } -void easy2d::Node::StopTasks(const String& name) +void easy2d::Node::StopTasks(const std::wstring& name) { for (const auto& task : tasks_) { @@ -937,7 +937,7 @@ void easy2d::Node::StopTasks(const String& name) } } -void easy2d::Node::StartTasks(const String& name) +void easy2d::Node::StartTasks(const std::wstring& name) { for (const auto& task : tasks_) { @@ -948,7 +948,7 @@ void easy2d::Node::StartTasks(const String& name) } } -void easy2d::Node::RemoveTasks(const String& name) +void easy2d::Node::RemoveTasks(const std::wstring& name) { for (const auto& task : tasks_) { @@ -1044,16 +1044,14 @@ void easy2d::Node::SetVisible(bool value) visible_ = value; } -void easy2d::Node::SetName(const String& name) +void easy2d::Node::SetName(const std::wstring& name) { - E2D_WARNING_IF(name.IsEmpty(), "Invalid Node name."); - - if (!name.IsEmpty() && name_ != name) + if (name_ != name) { // 保存节点名 name_ = name; // 保存节点 Hash 名 - hash_name_ = name.GetHash(); + hash_name_ = std::hash{}(name); } } diff --git a/core/objects/Sprite.cpp b/core/objects/Sprite.cpp index 910ffde5..62a31984 100644 --- a/core/objects/Sprite.cpp +++ b/core/objects/Sprite.cpp @@ -32,26 +32,26 @@ easy2d::Sprite::Sprite(Image * image) Load(image); } -easy2d::Sprite::Sprite(const Resource& res) +easy2d::Sprite::Sprite(Resource& res) : image_(nullptr) { Load(res); } -easy2d::Sprite::Sprite(const Resource& res, const Rect& crop_rect) +easy2d::Sprite::Sprite(Resource& res, const Rect& crop_rect) : image_(nullptr) { Load(res); Crop(crop_rect); } -easy2d::Sprite::Sprite(const String & file_name) +easy2d::Sprite::Sprite(const std::wstring & file_name) : image_(nullptr) { Load(file_name); } -easy2d::Sprite::Sprite(const String & file_name, const Rect & crop_rect) +easy2d::Sprite::Sprite(const std::wstring & file_name, const Rect & crop_rect) : image_(nullptr) { Load(file_name); @@ -81,7 +81,7 @@ bool easy2d::Sprite::Load(Image * image) return false; } -bool easy2d::Sprite::Load(const Resource& res) +bool easy2d::Sprite::Load(Resource& res) { if (!image_) { @@ -97,7 +97,7 @@ bool easy2d::Sprite::Load(const Resource& res) return false; } -bool easy2d::Sprite::Load(const String & file_name) +bool easy2d::Sprite::Load(const std::wstring & file_name) { if (!image_) { diff --git a/core/objects/Task.cpp b/core/objects/Task.cpp index 3a77e60a..95d32bda 100644 --- a/core/objects/Task.cpp +++ b/core/objects/Task.cpp @@ -21,7 +21,7 @@ #include "..\e2dobject.h" -easy2d::Task::Task(const Function & func, const String & name) +easy2d::Task::Task(const Function & func, const std::wstring & name) : running_(true) , stopped_(false) , run_times_(0) @@ -32,7 +32,7 @@ easy2d::Task::Task(const Function & func, const String & name) { } -easy2d::Task::Task(const Function & func, float delay, int times, const String & name) +easy2d::Task::Task(const Function & func, float delay, int times, const std::wstring & name) : running_(true) , stopped_(false) , run_times_(0) @@ -103,7 +103,7 @@ bool easy2d::Task::IsRunning() const return running_; } -const easy2d::String& easy2d::Task::GetName() const +const std::wstring& easy2d::Task::GetName() const { return name_; } diff --git a/core/objects/Text.cpp b/core/objects/Text.cpp index ecd001e6..a4f6d078 100644 --- a/core/objects/Text.cpp +++ b/core/objects/Text.cpp @@ -79,7 +79,7 @@ easy2d::Text::Text() { } -easy2d::Text::Text(const String & text, const Font & font, const Style & style) +easy2d::Text::Text(const std::wstring & text, const Font & font, const Style & style) : font_(font) , style_(style) , text_layout_(nullptr) @@ -95,7 +95,7 @@ easy2d::Text::~Text() SafeRelease(text_layout_); } -const easy2d::String& easy2d::Text::GetText() const +const std::wstring& easy2d::Text::GetText() const { return text_; } @@ -110,7 +110,7 @@ const easy2d::Text::Style& easy2d::Text::GetStyle() const return style_; } -const easy2d::String& easy2d::Text::GetFontFamily() const +const std::wstring& easy2d::Text::GetFontFamily() const { return font_.family; } @@ -179,7 +179,7 @@ bool easy2d::Text::outline() const return style_.outline; } -void easy2d::Text::SetText(const String& text) +void easy2d::Text::SetText(const std::wstring& text) { text_ = text; Reset(); @@ -197,7 +197,7 @@ void easy2d::Text::SetFont(const Font & font) Reset(); } -void easy2d::Text::SetFontFamily(const String& family) +void easy2d::Text::SetFontFamily(const std::wstring& family) { font_.family = family; Reset(); @@ -344,7 +344,7 @@ void easy2d::Text::CreateFormat() ThrowIfFailed( Device::GetGraphics()->GetWriteFactory()->CreateTextFormat( - (const wchar_t *)font_.family, + font_.family.c_str(), nullptr, DWRITE_FONT_WEIGHT(font_.weight), font_.italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL, @@ -388,7 +388,7 @@ void easy2d::Text::CreateLayout() SafeRelease(text_layout_); // 文本为空字符串时,重置属性 - if (text_.IsEmpty()) + if (text_.empty()) { this->SetSize(0, 0); return; @@ -400,7 +400,7 @@ void easy2d::Text::CreateLayout() return; } - UINT32 length = (UINT32)text_.Length(); + UINT32 length = static_cast(text_.size()); auto writeFactory = Device::GetGraphics()->GetWriteFactory(); // 对文本自动换行情况下进行处理 @@ -408,7 +408,7 @@ void easy2d::Text::CreateLayout() { ThrowIfFailed( writeFactory->CreateTextLayout( - (const wchar_t *)text_, + text_.c_str(), length, text_format_, style_.wrap_width, @@ -427,7 +427,7 @@ void easy2d::Text::CreateLayout() // 为防止文本对齐问题,根据先创建 layout 以获取宽度 ThrowIfFailed( writeFactory->CreateTextLayout( - (const wchar_t *)text_, + text_.c_str(), length, text_format_, 0, @@ -446,7 +446,7 @@ void easy2d::Text::CreateLayout() SafeRelease(text_layout_); ThrowIfFailed( writeFactory->CreateTextLayout( - (const wchar_t *)text_, + text_.c_str(), length, text_format_, GetTransform().size.width, diff --git a/core/tools/Data.cpp b/core/tools/Data.cpp index e66dbcf1..7f505892 100644 --- a/core/tools/Data.cpp +++ b/core/tools/Data.cpp @@ -21,7 +21,7 @@ #include "..\e2dtool.h" -easy2d::Data::Data(const String & key, const String & field) +easy2d::Data::Data(const std::wstring & key, const std::wstring & field) : key_(key) , field_(field) , data_path_(Path::GetDataPath()) @@ -32,12 +32,12 @@ bool easy2d::Data::Exists() const { wchar_t temp[256] = { 0 }; ::GetPrivateProfileStringW( - (LPCWSTR)field_, - (LPCWSTR)key_, + field_.c_str(), + key_.c_str(), L"", temp, 255, - (LPCWSTR)data_path_ + data_path_.c_str() ); return temp[0] == L'\0'; } @@ -45,102 +45,102 @@ bool easy2d::Data::Exists() const bool easy2d::Data::SaveInt(int value) { BOOL ret = ::WritePrivateProfileStringW( - (LPCWSTR)field_, - (LPCWSTR)key_, - (LPCWSTR)String::Parse(value), - (LPCWSTR)data_path_ + field_.c_str(), + key_.c_str(), + std::to_wstring(value).c_str(), + data_path_.c_str() ); - return ret != 0; + return ret == TRUE; } bool easy2d::Data::SaveFloat(float value) { BOOL ret = ::WritePrivateProfileStringW( - (LPCWSTR)field_, - (LPCWSTR)key_, - (LPCWSTR)String::Parse(value), - (LPCWSTR)data_path_ + field_.c_str(), + key_.c_str(), + std::to_wstring(value).c_str(), + data_path_.c_str() ); - return ret != 0; + return ret == TRUE; } bool easy2d::Data::SaveDouble(double value) { BOOL ret = ::WritePrivateProfileStringW( - (LPCWSTR)field_, - (LPCWSTR)key_, - (LPCWSTR)String::Parse(value), - (LPCWSTR)data_path_ + field_.c_str(), + key_.c_str(), + std::to_wstring(value).c_str(), + data_path_.c_str() ); - return ret != 0; + return ret == TRUE; } bool easy2d::Data::SaveBool(bool value) { BOOL ret = ::WritePrivateProfileStringW( - (LPCWSTR)field_, - (LPCWSTR)key_, + field_.c_str(), + key_.c_str(), (value ? L"1" : L"0"), - (LPCWSTR)data_path_ + data_path_.c_str() ); - return ret != 0; + return ret == TRUE; } -bool easy2d::Data::SaveString(const String& value) +bool easy2d::Data::SaveString(const std::wstring& value) { BOOL ret = ::WritePrivateProfileStringW( - (LPCWSTR)field_, - (LPCWSTR)key_, - (LPCWSTR)value, - (LPCWSTR)data_path_ + field_.c_str(), + key_.c_str(), + value.c_str(), + data_path_.c_str() ); - return ret != 0; + return ret == TRUE; } int easy2d::Data::GetInt() const { return ::GetPrivateProfileIntW( - (LPCWSTR)field_, - (LPCWSTR)key_, + field_.c_str(), + key_.c_str(), 0, - (LPCWSTR)data_path_ + data_path_.c_str() ); } float easy2d::Data::GetFloat() const { wchar_t temp[32] = { 0 }; - ::GetPrivateProfileStringW((LPCWSTR)field_, (LPCWSTR)key_, L"0.0", temp, 31, (LPCWSTR)data_path_); + ::GetPrivateProfileStringW(field_.c_str(), key_.c_str(), L"0.0", temp, 31, data_path_.c_str()); return std::stof(temp); } double easy2d::Data::GetDouble() const { wchar_t temp[32] = { 0 }; - ::GetPrivateProfileStringW((LPCWSTR)field_, (LPCWSTR)key_, L"0.0", temp, 31, (LPCWSTR)data_path_); + ::GetPrivateProfileStringW(field_.c_str(), key_.c_str(), L"0.0", temp, 31, data_path_.c_str()); return std::stod(temp); } bool easy2d::Data::GetBool() const { int nValue = ::GetPrivateProfileIntW( - (LPCWSTR)field_, - (LPCWSTR)key_, + field_.c_str(), + key_.c_str(), 0, - (LPCWSTR)data_path_); - return nValue != 0; + data_path_.c_str()); + return nValue == TRUE; } -easy2d::String easy2d::Data::GetString() +std::wstring easy2d::Data::GetString() { wchar_t temp[256] = { 0 }; ::GetPrivateProfileStringW( - (LPCWSTR)field_, - (LPCWSTR)key_, + field_.c_str(), + key_.c_str(), L"", temp, 255, - (LPCWSTR)data_path_ + data_path_.c_str() ); return temp; -} \ No newline at end of file +} diff --git a/core/tools/File.cpp b/core/tools/File.cpp index a46f55bf..e2d87e6b 100644 --- a/core/tools/File.cpp +++ b/core/tools/File.cpp @@ -20,16 +20,17 @@ #include "..\e2dtool.h" #include "..\e2dmodule.h" +#include #include -std::list easy2d::File::search_paths_; +std::list easy2d::File::search_paths_; easy2d::File::File() : file_path_() { } -easy2d::File::File(const String & file_name) +easy2d::File::File(const std::wstring & file_name) : file_path_(file_name) { this->Open(file_name); @@ -39,14 +40,14 @@ easy2d::File::~File() { } -bool easy2d::File::Open(const String & file_name) +bool easy2d::File::Open(const std::wstring & file_name) { - if (file_name.IsEmpty()) + if (file_name.empty()) return false; - auto FindFile = [](const String & path) -> bool + auto FindFile = [](const std::wstring & path) -> bool { - if (::PathFileExists((const wchar_t*)path)) + if (::PathFileExists(path.c_str())) return true; return false; }; @@ -70,44 +71,44 @@ bool easy2d::File::Open(const String & file_name) bool easy2d::File::Exists() const { - if (::PathFileExists(static_cast(file_path_))) + if (::PathFileExists(file_path_.c_str())) return true; return false; } -const easy2d::String& easy2d::File::GetPath() const +const std::wstring& easy2d::File::GetPath() const { return file_path_; } -easy2d::String easy2d::File::GetExtension() const +std::wstring easy2d::File::GetExtension() const { - String file_ext; + std::wstring file_ext; // 找到文件名中的最后一个 '.' 的位置 - size_t pos = file_path_.operator std::wstring().find_last_of(L'.'); + size_t pos = file_path_.find_last_of(L'.'); // 判断 pos 是否是有效位置 if (pos != std::wstring::npos) { // 截取扩展名 - file_ext = file_path_.Subtract(static_cast(pos)); + file_ext = file_path_.substr(pos); // 转换为小写字母 - file_ext = file_ext.ToLower(); + std::transform(file_ext.begin(), file_ext.end(), file_ext.begin(), std::towlower); } return std::move(file_ext); } bool easy2d::File::Delete() { - if (::DeleteFile(static_cast(file_path_))) + if (::DeleteFile(file_path_.c_str())) return true; return false; } -easy2d::File easy2d::File::Extract(const Resource& res, const String& dest_file_name) +easy2d::File easy2d::File::Extract(Resource& res, const std::wstring& dest_file_name) { File file; HANDLE file_handle = ::CreateFile( - static_cast(dest_file_name), + dest_file_name.c_str(), GENERIC_WRITE, NULL, NULL, @@ -119,16 +120,11 @@ easy2d::File easy2d::File::Extract(const Resource& res, const String& dest_file_ if (file_handle == INVALID_HANDLE_VALUE) return std::move(file); - // 查找资源文件中、加载资源到内存、得到资源大小 - HRSRC res_info = ::FindResource(NULL, MAKEINTRESOURCE(res.id), static_cast(res.type)); - HGLOBAL res_data = ::LoadResource(NULL, res_info); - DWORD res_size = ::SizeofResource(NULL, res_info); - - if (res_info && res_data && res_size) + if (res.Load()) { // 写入文件 DWORD written_bytes = 0; - ::WriteFile(file_handle, res_data, res_size, &written_bytes, NULL); + ::WriteFile(file_handle, res.GetData(), res.GetDataSize(), &written_bytes, NULL); ::CloseHandle(file_handle); file.Open(dest_file_name); @@ -136,20 +132,25 @@ easy2d::File easy2d::File::Extract(const Resource& res, const String& dest_file_ else { ::CloseHandle(file_handle); - ::DeleteFile(static_cast(dest_file_name)); + ::DeleteFile(dest_file_name.c_str()); } - ::FreeResource(res_data); return std::move(file); } -void easy2d::File::AddSearchPath(const String & path) +void easy2d::File::AddSearchPath(const std::wstring & path) { - String tmp = path; - tmp.Replace(L"/", L"\\"); - if (tmp.At(tmp.Length() - 1) != L'\\') + std::wstring tmp = path; + size_t pos = 0; + while ((pos = tmp.find(L"/", pos)) != std::wstring::npos) { - tmp << L"\\"; + tmp.replace(pos, 1, L"\\"); + pos++; + } + + if (tmp.at(tmp.length() - 1) != L'\\') + { + tmp.append(L"\\"); } auto iter = std::find(search_paths_.cbegin(), search_paths_.cend(), tmp); if (iter == search_paths_.cend()) @@ -158,9 +159,9 @@ void easy2d::File::AddSearchPath(const String & path) } } -easy2d::File easy2d::File::ShowOpenDialog(const String & title, const String & filter) +easy2d::File easy2d::File::ShowOpenDialog(const std::wstring & title, const std::wstring & filter) { - String file_path; + std::wstring file_path; HRESULT hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) @@ -172,16 +173,16 @@ easy2d::File easy2d::File::ShowOpenDialog(const String & title, const String & f if (SUCCEEDED(hr)) { - if (!title.IsEmpty()) + if (!title.empty()) { - file_open->SetTitle(LPCWSTR(title)); + file_open->SetTitle(title.c_str()); } - if (!filter.IsEmpty()) + if (!filter.empty()) { COMDLG_FILTERSPEC spec[] = { - { L"", LPCWSTR(filter) } + { L"", filter.c_str() } }; file_open->SetFileTypes(1, spec); } @@ -220,9 +221,9 @@ easy2d::File easy2d::File::ShowOpenDialog(const String & title, const String & f return std::move(File(file_path)); } -easy2d::File easy2d::File::ShowSaveDialog(const String & title, const String& def_file, const String & def_ext) +easy2d::File easy2d::File::ShowSaveDialog(const std::wstring & title, const std::wstring& def_file, const std::wstring & def_ext) { - String file_path; + std::wstring file_path; HRESULT hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) @@ -234,24 +235,24 @@ easy2d::File easy2d::File::ShowSaveDialog(const String & title, const String& de if (SUCCEEDED(hr)) { - if (!title.IsEmpty()) + if (!title.empty()) { - file_save->SetTitle(LPCWSTR(title)); + file_save->SetTitle(title.c_str()); } - if (!def_file.IsEmpty()) + if (!def_file.empty()) { - file_save->SetFileName(LPCWSTR(def_file)); + file_save->SetFileName(def_file.c_str()); } - if (!def_ext.IsEmpty()) + if (!def_ext.empty()) { - file_save->SetDefaultExtension(LPCWSTR(def_ext)); + file_save->SetDefaultExtension(def_ext.c_str()); - String ext = L"*." + def_ext; + std::wstring ext = L"*." + def_ext; COMDLG_FILTERSPEC spec[] = { - { L"", LPCWSTR(ext) } + { L"", ext.c_str() } }; file_save->SetFileTypes(1, spec); } diff --git a/core/tools/Music.cpp b/core/tools/Music.cpp index 3c42fe33..fe16a2b3 100644 --- a/core/tools/Music.cpp +++ b/core/tools/Music.cpp @@ -100,50 +100,20 @@ namespace easy2d return SUCCEEDED(hr); } - bool LoadMediaResource(LPCWSTR res_name, LPCWSTR res_type, BYTE** wave_data, UINT32* wave_data_size) + bool LoadMediaResource(Resource& res, BYTE** wave_data, UINT32* wave_data_size) { HRESULT hr = S_OK; - HRSRC res_info; - HGLOBAL res_data; - DWORD res_size; - LPVOID res; HINSTANCE hinstance = GetModuleHandle(nullptr); IStream* stream = nullptr; IMFByteStream* byte_stream = nullptr; IMFSourceReader* reader = nullptr; - res_info = FindResourceW(hinstance, res_name, res_type); - if (res_info == nullptr) - { - TraceError(L"FindResource"); - return false; - } - - res_data = LoadResource(hinstance, res_info); - if (res_data == nullptr) - { - TraceError(L"LoadResource"); - return false; - } - - res_size = SizeofResource(hinstance, res_info); - if (res_size == 0) - { - TraceError(L"SizeofResource"); - return false; - } - - res = LockResource(res_data); - if (res == nullptr) - { - TraceError(L"LockResource"); - return false; - } + if (!res.Load()) { return false; } stream = SHCreateMemStream( - static_cast(res), - static_cast(res_size) + static_cast(res.GetData()), + static_cast(res.GetDataSize()) ); if (stream == nullptr) @@ -346,7 +316,7 @@ easy2d::Music::Music() { } -easy2d::Music::Music(const easy2d::String& file_path) +easy2d::Music::Music(const std::wstring& file_path) : opened_(false) , playing_(false) , wave_data_(nullptr) @@ -356,7 +326,7 @@ easy2d::Music::Music(const easy2d::String& file_path) Load(file_path); } -easy2d::Music::Music(const Resource& res) +easy2d::Music::Music(Resource& res) : opened_(false) , playing_(false) , wave_data_(nullptr) @@ -371,7 +341,7 @@ easy2d::Music::~Music() Close(); } -bool easy2d::Music::Load(const easy2d::String & file_path) +bool easy2d::Music::Load(const std::wstring & file_path) { if (opened_) { @@ -387,10 +357,10 @@ bool easy2d::Music::Load(const easy2d::String & file_path) // 用户输入的路径不一定是完整路径,因为用户可能通过 File::AddSearchPath 添加 // 默认搜索路径,所以需要通过 File::GetPath 获取完整路径 - String music_file_path = music_file.GetPath(); + std::wstring music_file_path = music_file.GetPath(); Transcoder transcoder; - if (!transcoder.LoadMediaFile((LPCWSTR)music_file_path, &wave_data_, &size_)) + if (!transcoder.LoadMediaFile(music_file_path.c_str(), &wave_data_, &size_)) { return false; } @@ -411,7 +381,7 @@ bool easy2d::Music::Load(const easy2d::String & file_path) return true; } -bool easy2d::Music::Load(const Resource& res) +bool easy2d::Music::Load(Resource& res) { if (opened_) { @@ -419,7 +389,7 @@ bool easy2d::Music::Load(const Resource& res) } Transcoder transcoder; - if (!transcoder.LoadMediaResource(MAKEINTRESOURCE(res.id), (LPCWSTR)res.type, &wave_data_, &size_)) + if (!transcoder.LoadMediaResource(res, &wave_data_, &size_)) { return false; } diff --git a/core/tools/Path.cpp b/core/tools/Path.cpp index 146e4741..4ad87fb4 100644 --- a/core/tools/Path.cpp +++ b/core/tools/Path.cpp @@ -26,17 +26,17 @@ namespace { // 创建指定文件夹 - bool CreateFolder(const easy2d::String & dir_path) + bool CreateFolder(const std::wstring & dir_path) { - if (dir_path.IsEmpty() || dir_path.Length() >= MAX_PATH) + if (dir_path.empty() || dir_path.size() >= MAX_PATH) return false; wchar_t tmp_dir_path[MAX_PATH] = { 0 }; - int length = dir_path.Length(); + size_t length = dir_path.length(); - for (int i = 0; i < length; ++i) + for (size_t i = 0; i < length; ++i) { - tmp_dir_path[i] = dir_path.At(i); + tmp_dir_path[i] = dir_path.at(i); if (tmp_dir_path[i] == L'\\' || tmp_dir_path[i] == L'/' || i == (length - 1)) { if (::_waccess(tmp_dir_path, 0) != 0) @@ -53,19 +53,22 @@ namespace } -const easy2d::String& easy2d::Path::GetDataPath() +const std::wstring& easy2d::Path::GetDataPath() { - static String data_path; - if (data_path.IsEmpty()) + static std::wstring data_path; + if (data_path.empty()) { // 设置数据的保存路径 - String local_app_data_path = Path::GetLocalAppDataPath(); - String title = Game::GetInstance()->GetTitle(); - String folder_name = String::Parse(title.GetHash()); + std::wstring local_app_data_path = Path::GetLocalAppDataPath(); + std::wstring title = Game::GetInstance()->GetTitle(); + std::wstring folder_name = std::to_wstring(std::hash{}(title)); - if (!local_app_data_path.IsEmpty()) + if (!local_app_data_path.empty()) { - data_path = local_app_data_path + L"\\Easy2DGameData\\" << folder_name << L"\\"; + data_path.append(local_app_data_path) + .append(L"\\Easy2DGameData\\") + .append(folder_name) + .append(L"\\"); File file(data_path); if (!file.Exists() && !CreateFolder(data_path)) @@ -73,24 +76,27 @@ const easy2d::String& easy2d::Path::GetDataPath() data_path = L""; } } - data_path << L"Data.ini"; + data_path.append(L"Data.ini"); } return data_path; } -const easy2d::String& easy2d::Path::GetTemporaryPath() +const std::wstring& easy2d::Path::GetTemporaryPath() { - static String temp_path; - if (temp_path.IsEmpty()) + static std::wstring temp_path; + if (temp_path.empty()) { // 设置临时文件保存路径 wchar_t path[_MAX_PATH]; - String title = Game::GetInstance()->GetTitle(); - String folder_name = String::Parse(title.GetHash()); + std::wstring title = Game::GetInstance()->GetTitle(); + std::wstring folder_name = std::to_wstring(std::hash{}(title)); if (0 != ::GetTempPath(_MAX_PATH, path)) { - temp_path << path << L"\\Easy2DGameTemp\\" << folder_name << L"\\"; + temp_path.append(path) + .append(L"\\Easy2DGameTemp\\") + .append(folder_name) + .append(L"\\"); File file(temp_path); if (!file.Exists() && !CreateFolder(temp_path)) @@ -102,10 +108,10 @@ const easy2d::String& easy2d::Path::GetTemporaryPath() return temp_path; } -const easy2d::String& easy2d::Path::GetLocalAppDataPath() +const std::wstring& easy2d::Path::GetLocalAppDataPath() { - static String local_app_data_path; - if (local_app_data_path.IsEmpty()) + static std::wstring local_app_data_path; + if (local_app_data_path.empty()) { // 获取 AppData/Local 文件夹的路径 wchar_t path[MAX_PATH] = { 0 }; @@ -116,10 +122,10 @@ const easy2d::String& easy2d::Path::GetLocalAppDataPath() return local_app_data_path; } -const easy2d::String& easy2d::Path::GetExeFilePath() +const std::wstring& easy2d::Path::GetExeFilePath() { - static String exe_file_path; - if (exe_file_path.IsEmpty()) + static std::wstring exe_file_path; + if (exe_file_path.empty()) { TCHAR path[_MAX_PATH] = { 0 }; if (::GetModuleFileName(nullptr, path, _MAX_PATH) != 0) diff --git a/core/tools/Player.cpp b/core/tools/Player.cpp index 2264a3fa..4c990fe0 100644 --- a/core/tools/Player.cpp +++ b/core/tools/Player.cpp @@ -12,9 +12,9 @@ easy2d::Player::~Player() { } -bool easy2d::Player::Load(const String & file_path) +bool easy2d::Player::Load(const std::wstring & file_path) { - if (file_path.IsEmpty()) + if (file_path.empty()) return false; Music * music = new (std::nothrow) Music(); @@ -24,7 +24,9 @@ bool easy2d::Player::Load(const String & file_path) if (music->Load(file_path)) { music->SetVolume(volume_); - musics_.insert(std::make_pair(file_path.GetHash(), music)); + + size_t hash_code = std::hash{}(file_path); + musics_.insert(std::make_pair(hash_code, music)); return true; } else @@ -35,14 +37,14 @@ bool easy2d::Player::Load(const String & file_path) return false; } -bool easy2d::Player::Play(const String & file_path, int loop_count) +bool easy2d::Player::Play(const std::wstring & file_path, int loop_count) { - if (file_path.IsEmpty()) + if (file_path.empty()) return false; if (Load(file_path)) { - auto music = musics_[file_path.GetHash()]; + auto music = musics_[std::hash{}(file_path)]; if (music->Play(loop_count)) { return true; @@ -51,50 +53,51 @@ bool easy2d::Player::Play(const String & file_path, int loop_count) return false; } -void easy2d::Player::Pause(const String & file_path) +void easy2d::Player::Pause(const std::wstring & file_path) { - if (file_path.IsEmpty()) + if (file_path.empty()) return; - size_t hash = file_path.GetHash(); - if (musics_.end() != musics_.find(hash)) - musics_[hash]->Pause(); + size_t hash_code = std::hash{}(file_path); + if (musics_.end() != musics_.find(hash_code)) + musics_[hash_code]->Pause(); } -void easy2d::Player::Resume(const String & file_path) +void easy2d::Player::Resume(const std::wstring & file_path) { - if (file_path.IsEmpty()) + if (file_path.empty()) return; - size_t hash = file_path.GetHash(); - if (musics_.end() != musics_.find(hash)) - musics_[hash]->Resume(); + size_t hash_code = std::hash{}(file_path); + if (musics_.end() != musics_.find(hash_code)) + musics_[hash_code]->Resume(); } -void easy2d::Player::Stop(const String & file_path) +void easy2d::Player::Stop(const std::wstring & file_path) { - if (file_path.IsEmpty()) + if (file_path.empty()) return; - size_t hash = file_path.GetHash(); - if (musics_.end() != musics_.find(hash)) - musics_[hash]->Stop(); + size_t hash_code = std::hash{}(file_path); + if (musics_.end() != musics_.find(hash_code)) + musics_[hash_code]->Stop(); } -bool easy2d::Player::IsPlaying(const String & file_path) +bool easy2d::Player::IsPlaying(const std::wstring & file_path) { - if (file_path.IsEmpty()) + if (file_path.empty()) return false; - size_t hash = file_path.GetHash(); - if (musics_.end() != musics_.find(hash)) - return musics_[hash]->IsPlaying(); + size_t hash_code = std::hash{}(file_path); + if (musics_.end() != musics_.find(hash_code)) + return musics_[hash_code]->IsPlaying(); return false; } -bool easy2d::Player::Load(const Resource& res) +bool easy2d::Player::Load(Resource& res) { - if (musics_.end() != musics_.find(res.id)) + size_t hash_code = res.GetHashCode(); + if (musics_.end() != musics_.find(hash_code)) return true; Music * music = new (std::nothrow) Music(); @@ -104,7 +107,7 @@ bool easy2d::Player::Load(const Resource& res) if (music->Load(res)) { music->SetVolume(volume_); - musics_.insert(std::make_pair(res.id, music)); + musics_.insert(std::make_pair(hash_code, music)); return true; } else @@ -115,11 +118,12 @@ bool easy2d::Player::Load(const Resource& res) return false; } -bool easy2d::Player::Play(const Resource& res, int loop_count) +bool easy2d::Player::Play(Resource& res, int loop_count) { if (Load(res)) { - auto music = musics_[res.id]; + size_t hash_code = res.GetHashCode(); + auto music = musics_[hash_code]; if (music->Play(loop_count)) { return true; @@ -128,28 +132,32 @@ bool easy2d::Player::Play(const Resource& res, int loop_count) return false; } -void easy2d::Player::Pause(const Resource& res) +void easy2d::Player::Pause(Resource& res) { - if (musics_.end() != musics_.find(res.id)) - musics_[res.id]->Pause(); + size_t hash_code = res.GetHashCode(); + if (musics_.end() != musics_.find(hash_code)) + musics_[hash_code]->Pause(); } -void easy2d::Player::Resume(const Resource& res) +void easy2d::Player::Resume(Resource& res) { - if (musics_.end() != musics_.find(res.id)) - musics_[res.id]->Resume(); + size_t hash_code = res.GetHashCode(); + if (musics_.end() != musics_.find(hash_code)) + musics_[hash_code]->Resume(); } -void easy2d::Player::Stop(const Resource& res) +void easy2d::Player::Stop(Resource& res) { - if (musics_.end() != musics_.find(res.id)) - musics_[res.id]->Stop(); + size_t hash_code = res.GetHashCode(); + if (musics_.end() != musics_.find(hash_code)) + musics_[hash_code]->Stop(); } -bool easy2d::Player::IsPlaying(const Resource& res) +bool easy2d::Player::IsPlaying(Resource& res) { - if (musics_.end() != musics_.find(res.id)) - return musics_[res.id]->IsPlaying(); + size_t hash_code = res.GetHashCode(); + if (musics_.end() != musics_.find(hash_code)) + return musics_[hash_code]->IsPlaying(); return false; } diff --git a/core/utils/Font.cpp b/core/utils/Font.cpp index df5b7ada..549e1ad2 100644 --- a/core/utils/Font.cpp +++ b/core/utils/Font.cpp @@ -20,15 +20,8 @@ #include "..\e2dutil.h" -easy2d::Font::Font() - : family("") - , size(22) - , weight(Font::Weight::Normal) - , italic(false) -{ -} -easy2d::Font::Font(const String & family, float size, UINT weight, bool italic) +easy2d::Font::Font(const std::wstring & family, float size, UINT weight, bool italic) : family(family) , size(size) , weight(weight) diff --git a/core/tools/Random.cpp b/core/utils/Random.cpp similarity index 100% rename from core/tools/Random.cpp rename to core/utils/Random.cpp diff --git a/core/utils/Resource.cpp b/core/utils/Resource.cpp index 552a28fd..558baa70 100644 --- a/core/utils/Resource.cpp +++ b/core/utils/Resource.cpp @@ -21,8 +21,77 @@ #include "..\e2dtool.h" -easy2d::Resource::Resource(int resource_id, const String & resource_type) - : id(resource_id) - , type(resource_type) +easy2d::Resource::Resource(LPCWSTR name, LPCWSTR type) + : name_(name) + , type_(type) + , data_(nullptr) + , data_size_(0) + , loaded_(false) { } + +LPCWSTR easy2d::Resource::GetName() const +{ + return name_; +} + +LPCWSTR easy2d::Resource::GetType() const +{ + return type_; +} + +LPVOID easy2d::Resource::GetData() const +{ + return data_; +} + +DWORD easy2d::Resource::GetDataSize() const +{ + return data_size_; +} + +size_t easy2d::Resource::GetHashCode() const +{ + return std::hash{}(name_); +} + +bool easy2d::Resource::Load() +{ + if (!loaded_) + { + HRSRC res_info; + HGLOBAL res_data; + HINSTANCE hinstance = GetModuleHandle(NULL); + + res_info = FindResourceW(hinstance, name_, type_); + if (res_info == nullptr) + { + E2D_WARNING("FindResource"); + return false; + } + + res_data = LoadResource(hinstance, res_info); + if (res_data == nullptr) + { + E2D_WARNING("LoadResource"); + return false; + } + + data_size_ = SizeofResource(hinstance, res_info); + if (data_size_ == 0) + { + E2D_WARNING("SizeofResource"); + return false; + } + + data_ = LockResource(res_data); + if (data_ == nullptr) + { + E2D_WARNING("LockResource"); + return false; + } + + loaded_ = true; + } + return true; +} diff --git a/core/utils/String.cpp b/core/utils/String.cpp deleted file mode 100644 index b0e81b47..00000000 --- a/core/utils/String.cpp +++ /dev/null @@ -1,527 +0,0 @@ -// Copyright (c) 2016-2018 Easy2D - Nomango -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#include "..\e2dutil.h" -#include -#include -#include -#pragma comment(lib, "comsuppw.lib") - - -easy2d::String::String() - : string_(L"") -{ -} - -easy2d::String::String(const wchar_t *str) - : string_(str) -{ -} - -easy2d::String::String(const char *cstr) - : string_(static_cast(_bstr_t(cstr))) -{ -} - -easy2d::String::String(easy2d::String && str) -{ - string_ = std::move(str.string_); -} - -easy2d::String::String(const easy2d::String &str) - : string_(str.string_) -{ -} - -easy2d::String::~String() -{ - string_.clear(); -} - -easy2d::String &easy2d::String::operator=(const wchar_t *str) -{ - string_ = str; - return (*this); -} - -easy2d::String & easy2d::String::operator=(const char *cstr) -{ - string_ = static_cast(_bstr_t(cstr)); - return (*this); -} - -easy2d::String easy2d::String::Parse(int value) -{ - String tmp; - tmp.string_ = std::to_wstring(value); - return std::move(tmp); -} - -easy2d::String easy2d::String::Parse(unsigned int value) -{ - String tmp; - tmp.string_ = std::to_wstring(value); - return std::move(tmp); -} - -easy2d::String easy2d::String::Parse(unsigned long long value) -{ - String tmp; - tmp.string_ = std::to_wstring(value); - return std::move(tmp); -} - -easy2d::String easy2d::String::Parse(float value) -{ - String tmp; - tmp.string_ = std::to_wstring(value); - return std::move(tmp); -} - -easy2d::String easy2d::String::Parse(double value) -{ - String tmp; - tmp.string_ = std::to_wstring(value); - return std::move(tmp); -} - -easy2d::String easy2d::String::Format(const char * format, ...) -{ - std::string tmp; - - va_list marker; - va_start(marker, format); - - size_t nu_of_chars = _vscprintf(format, marker); - - if (nu_of_chars > tmp.capacity()) - { - tmp.resize(nu_of_chars + 1); - } - - vsprintf_s(const_cast(tmp.data()), tmp.capacity(), format, marker); - - va_end(marker); - - String str = tmp.c_str(); - return std::move(str); -} - -easy2d::String easy2d::String::Format(const wchar_t * format, ...) -{ - std::wstring tmp; - - va_list marker; - va_start(marker, format); - - size_t nu_of_chars = _vscwprintf(format, marker); - - if (nu_of_chars > tmp.capacity()) - { - tmp.resize(nu_of_chars + 1); - } - - vswprintf_s(const_cast(tmp.data()), tmp.capacity(), format, marker); - - va_end(marker); - - String str = tmp.c_str(); - return std::move(str); -} - -easy2d::String & easy2d::String::operator=(const String &str) -{ - string_ = str.string_; - return (*this); -} - -bool easy2d::String::operator==(const wchar_t *str) const -{ - if (str) - { - return (string_.compare(str) == 0); - } - else - { - return false; - } -} - -bool easy2d::String::operator==(const char *str) const -{ - if (str) - { - String temp(str); - return (string_ == temp.string_); - } - else - { - return false; - } -} - -bool easy2d::String::operator ==(const easy2d::String &str) const -{ - return string_ == str.string_; -} - -bool easy2d::String::operator!=(const wchar_t *str) const -{ - if (str) - { - return (string_.compare(str) != 0); - } - else - { - return true; - } -} - -bool easy2d::String::operator!=(const char *str) const -{ - if (str) - { - String temp(str); - return (string_ != temp.string_); - } - else - { - return true; - } -} - -bool easy2d::String::operator!=(const easy2d::String &str) const -{ - return string_ != str.string_; -} - -wchar_t &easy2d::String::operator[](size_t index) -{ - return string_[index]; -} - -easy2d::String easy2d::String::operator+(const wchar_t *str) const -{ - String temp; - temp.string_ = string_ + str; - return std::move(temp); -} - -easy2d::String easy2d::String::operator+(const char *str) const -{ - String temp; - temp.string_ = string_ + static_cast(_bstr_t(str)); - return std::move(temp); -} - -easy2d::String easy2d::String::operator+(const easy2d::String &str) const -{ - String temp; - temp.string_ = string_ + str.string_; - return std::move(temp); -} - -easy2d::String easy2d::operator+(const wchar_t *str1, const easy2d::String &str2) -{ - String temp; - temp.string_ = str1 + str2.string_; - return std::move(temp); -} - -easy2d::String easy2d::operator+(const char *str1, const String &str2) -{ - String temp; - temp.string_ = static_cast(_bstr_t(str1)) + str2.string_; - return std::move(temp); -} - -easy2d::String & easy2d::String::operator+=(const wchar_t *str) -{ - string_ += str; - return (*this); -} - -easy2d::String & easy2d::String::operator+=(const char *str) -{ - string_ += static_cast(_bstr_t(str)); - return (*this); -} - -easy2d::String & easy2d::String::operator+=(const String &str) -{ - string_ += str.string_; - return (*this); -} - -bool easy2d::String::operator>(const String &str) const -{ - return string_ > str.string_; -} - -bool easy2d::String::operator>=(const String &str) const -{ - return string_ >= str.string_; -} - -bool easy2d::String::operator<(const String &str) const -{ - return string_ < str.string_; -} - -bool easy2d::String::operator<=(const String &str) const -{ - return string_ <= str.string_; -} - -easy2d::String & easy2d::String::operator<<(const String &str) -{ - string_ += str.string_; - return (*this); -} - -easy2d::String & easy2d::String::operator<<(const wchar_t *str) -{ - string_ += str; - return (*this); -} - -easy2d::String & easy2d::String::operator<<(wchar_t *str) -{ - string_ += str; - return (*this); -} - -easy2d::String & easy2d::String::operator<<(const char * cstr) -{ - string_ += static_cast(_bstr_t(cstr)); - return (*this); -} - -easy2d::String & easy2d::String::operator<<(char * cstr) -{ - string_ += static_cast(_bstr_t(cstr)); - return (*this); -} - -easy2d::String & easy2d::String::operator<<(int value) -{ - (*this) += String::Parse(value); - return (*this); -} - -easy2d::String & easy2d::String::operator<<(unsigned int value) -{ - (*this) += String::Parse(value); - return (*this); -} - -easy2d::String & easy2d::String::operator<<(float value) -{ - (*this) += String::Parse(value); - return (*this); -} - -easy2d::String & easy2d::String::operator<<(double value) -{ - (*this) += String::Parse(value); - return (*this); -} - -easy2d::String::operator const wchar_t*() const -{ - return string_.c_str(); -} - -easy2d::String::operator wchar_t*() const -{ - return const_cast(string_.c_str()); -} - -easy2d::String::operator std::wstring() const -{ - return string_; -} - -easy2d::String::operator std::string() const -{ - std::string str = static_cast(_bstr_t(string_.c_str())); - return std::move(str); -} - -bool easy2d::String::IsEmpty() const -{ - return string_.empty(); -} - -int easy2d::String::Length() const -{ - return static_cast(string_.size()); -} - -size_t easy2d::String::GetHash() const -{ - std::hash hash; - return hash(string_); -} - -const wchar_t& easy2d::String::At(size_t index) const -{ - return string_.at(index); -} - -int easy2d::String::Compare(const String & str) const -{ - return string_.compare(str.string_); -} - -easy2d::String easy2d::String::ToUpper() const -{ - String str(*this); - std::transform(str.string_.begin(), str.string_.end(), str.string_.begin(), std::towupper); - return std::move(str); -} - -easy2d::String easy2d::String::ToLower() const -{ - easy2d::String str(*this); - std::transform(str.string_.begin(), str.string_.end(), str.string_.begin(), std::towlower); - return std::move(str); -} - -int easy2d::String::ToInt() const -{ - if (string_.empty()) - { - return 0; - } - return std::stoi(string_, 0, 10); -} - -float easy2d::String::ToFloat() const -{ - if (string_.empty()) - { - return 0.f; - } - return std::stof(string_, 0); -} - -double easy2d::String::ToDouble() const -{ - if (string_.empty()) - { - return 0.0; - } - return std::stod(string_, 0); -} - -bool easy2d::String::ToBool() const -{ - if (string_.empty()) - { - return false; - } - - if (string_.compare(L"0") == 0 || string_.compare(L"false") == 0) - { - return false; - } - return true; -} - -easy2d::String easy2d::String::Subtract(int offset, int count) const -{ - String tmp; - int length = static_cast(string_.size()); - - if (length == 0 || offset >= length) - return std::move(tmp); - - offset = offset >= 0 ? offset : 0; - - if (count < 0 || (offset + count) > length) - count = length - offset; - - tmp.string_ = string_.substr(offset, count); - return std::move(tmp); -} - -void easy2d::String::Insert(const String & str, int pos) -{ - string_.insert(size_t(pos), str.string_); -} - -void easy2d::String::Replace(const String & from, const String & to) -{ - if (from.string_.empty()) - return; - - size_t start_pos = 0; - while ((start_pos = string_.find((const wchar_t *)from, start_pos)) != std::wstring::npos) - { - string_.replace(start_pos, from.string_.length(), (const wchar_t *)to); - start_pos += to.string_.length(); - } -} - -void easy2d::String::Erase(int offset, int count) -{ - string_.erase(size_t(offset), size_t(count)); -} - -int easy2d::String::Find(const String & str, int offset) const -{ - size_t index; - if ((index = string_.find(str.string_, size_t(offset))) == std::wstring::npos) - return -1; - return static_cast(index); -} - -void easy2d::String::Clear() -{ - string_.clear(); -} - -std::wostream & easy2d::operator<<(std::wostream &cout, const String &str) -{ - cout << str.string_; - return cout; -} - -std::wistream & easy2d::operator>>(std::wistream &cin, String &str) -{ - cin >> str.string_; - return cin; -} - -std::ostream & easy2d::operator<<(std::ostream &cout, const String &str) -{ - std::string cstr = static_cast(_bstr_t(str.string_.c_str())); - cout << cstr; - return cout; -} - -std::istream & easy2d::operator>>(std::istream &cin, String &str) -{ - std::string temp; - cin >> temp; - str.string_ = static_cast(_bstr_t(temp.c_str())); - return cin; -} \ No newline at end of file diff --git a/project/vs2012/Easy2D.vcxproj b/project/vs2012/Easy2D.vcxproj index d93370d8..2df2f82b 100644 --- a/project/vs2012/Easy2D.vcxproj +++ b/project/vs2012/Easy2D.vcxproj @@ -61,7 +61,6 @@ - @@ -73,11 +72,11 @@ + - diff --git a/project/vs2012/Easy2D.vcxproj.filters b/project/vs2012/Easy2D.vcxproj.filters index 7354ab3b..3ff6de27 100644 --- a/project/vs2012/Easy2D.vcxproj.filters +++ b/project/vs2012/Easy2D.vcxproj.filters @@ -123,9 +123,6 @@ utils - - utils - utils @@ -156,9 +153,6 @@ tools - - tools - transitions @@ -210,6 +204,9 @@ modules + + utils + diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index d16cb5ec..0febad40 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -205,7 +205,6 @@ - @@ -217,11 +216,11 @@ + - diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index 7354ab3b..3ff6de27 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -123,9 +123,6 @@ utils - - utils - utils @@ -156,9 +153,6 @@ tools - - tools - transitions @@ -210,6 +204,9 @@ modules + + utils + diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 7e2f10a6..d62bab54 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -238,7 +238,6 @@ - @@ -250,11 +249,11 @@ + - diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 7354ab3b..3ff6de27 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -123,9 +123,6 @@ utils - - utils - utils @@ -156,9 +153,6 @@ tools - - tools - transitions @@ -210,6 +204,9 @@ modules + + utils +