From 55f28806c9ba1b9c883ebecf13cd8fc4b19c7c72 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Thu, 7 Feb 2019 23:54:19 +0800 Subject: [PATCH] Objects have names now minor minor --- src/core/ActionManager.cpp | 11 +++ src/core/ActionManager.h | 5 ++ src/core/Array.h | 4 +- src/core/EventDispatcher.cpp | 6 +- src/core/EventListener.cpp | 12 +-- src/core/EventListener.h | 5 -- src/core/Image.cpp | 2 +- src/core/Music.cpp | 2 +- src/core/Node.cpp | 10 +-- src/core/Node.h | 4 - src/core/Object.cpp | 25 ++++++ src/core/Object.h | 7 ++ src/core/Resource.cpp | 82 ++++++++++++++++++- src/core/Resource.h | 33 +++++--- src/core/Task.cpp | 7 +- src/core/Task.h | 16 ++-- src/core/TaskManager.cpp | 6 +- src/utils/ResLoader.cpp | 151 +++++++++++++++-------------------- src/utils/Transcoder.cpp | 4 +- src/utils/Transcoder.h | 2 +- 20 files changed, 244 insertions(+), 150 deletions(-) diff --git a/src/core/ActionManager.cpp b/src/core/ActionManager.cpp index 5b03a7b7..bd1a3cad 100644 --- a/src/core/ActionManager.cpp +++ b/src/core/ActionManager.cpp @@ -52,6 +52,17 @@ namespace easy2d } } + ActionPtr ActionManager::GetAction(String const & name) + { + if (actions_.IsEmpty()) + return nullptr; + + for (auto action = actions_.First().Get(); action; action = action->NextItem().Get()) + if (action->IsName(name)) + return action; + return nullptr; + } + void ActionManager::ResumeAllActions() { if (actions_.IsEmpty()) diff --git a/src/core/ActionManager.h b/src/core/ActionManager.h index 3d2767e2..b2fdbc15 100644 --- a/src/core/ActionManager.h +++ b/src/core/ActionManager.h @@ -33,6 +33,11 @@ namespace easy2d ActionPtr const& action ); + // 获取动作 + ActionPtr GetAction( + String const& name + ); + // 继续所有暂停动作 void ResumeAllActions(); diff --git a/src/core/Array.h b/src/core/Array.h index 988df825..8081fffd 100644 --- a/src/core/Array.h +++ b/src/core/Array.h @@ -43,11 +43,11 @@ namespace easy2d inline Array() { size_ = capacity_ = 0; data_ = nullptr; } inline Array(std::initializer_list<_Ty> const& list) { size_ = capacity_ = 0; data_ = nullptr; operator=(list); } inline Array(const Array<_Ty>& src) { size_ = capacity_ = 0; data_ = nullptr; operator=(src); } - inline Array(Array<_Ty>&& src) { size_ = capacity_ = 0; data_ = nullptr; operator=(src); } + inline Array(Array<_Ty>&& src) { size_ = capacity_ = 0; data_ = nullptr; operator=(std::forward&&>(src)); } inline ~Array() { if (data_) _destroy_all(); } inline Array<_Ty>& operator=(const Array<_Ty>& src) { if (&src == this) { return *this; } clear(); reserve(src.size_); std::uninitialized_copy(src.begin(), src.end(), begin()); size_ = src.size_; return *this; } - inline Array<_Ty>& operator=(Array<_Ty>&& src) { clear(); size_ = src.size_; capacity_ = src.capacity_; data_ = src.data_; src.data_ = src.size_ = src.capacity_ = 0; return *this; } + inline Array<_Ty>& operator=(Array<_Ty>&& src) { clear(); size_ = src.size_; capacity_ = src.capacity_; data_ = src.data_; src.size_ = src.capacity_ = 0; src.data_ = nullptr; return *this; } inline Array<_Ty>& operator=(std::initializer_list<_Ty> const& list) { clear(); reserve((int)list.size()); std::uninitialized_copy(list.begin(), list.end(), begin()); size_ = (int)list.size(); return *this; } inline bool empty() const { return size_ == 0; } diff --git a/src/core/EventDispatcher.cpp b/src/core/EventDispatcher.cpp index 19150541..1d2edb86 100644 --- a/src/core/EventDispatcher.cpp +++ b/src/core/EventDispatcher.cpp @@ -63,7 +63,7 @@ namespace easy2d { for (auto listener = listeners_.First(); listener; listener = listener->NextItem()) { - if (listener->GetName() == listener_name) + if (listener->IsName(listener_name)) { listener->Start(); } @@ -74,7 +74,7 @@ namespace easy2d { for (auto listener = listeners_.First(); listener; listener = listener->NextItem()) { - if (listener->GetName() == listener_name) + if (listener->IsName(listener_name)) { listener->Stop(); } @@ -88,7 +88,7 @@ namespace easy2d { next = listener->NextItem(); - if (listener->GetName() == listener_name) + if (listener->IsName(listener_name)) { listeners_.Remove(listener); } diff --git a/src/core/EventListener.cpp b/src/core/EventListener.cpp index 47eab905..b1f74013 100644 --- a/src/core/EventListener.cpp +++ b/src/core/EventListener.cpp @@ -26,9 +26,9 @@ namespace easy2d EventListener::EventListener(EventType type, EventCallback const & callback, String const & name) : type_(type) , callback_(callback) - , name_(name) , running_(true) { + SetName(name); } EventListener::~EventListener() @@ -50,14 +50,4 @@ namespace easy2d return running_; } - String const & EventListener::GetName() const - { - return name_; - } - - void EventListener::SetName(String const & name) - { - name_ = name; - } - } diff --git a/src/core/EventListener.h b/src/core/EventListener.h index 222f2093..576ba65d 100644 --- a/src/core/EventListener.h +++ b/src/core/EventListener.h @@ -49,15 +49,10 @@ namespace easy2d void Stop(); - void SetName(String const& name); - bool IsRunning() const; - String const& GetName() const; - protected: bool running_; - String name_; EventType type_; EventCallback callback_; }; diff --git a/src/core/Image.cpp b/src/core/Image.cpp index 43c1c1f6..5a505e40 100644 --- a/src/core/Image.cpp +++ b/src/core/Image.cpp @@ -60,7 +60,7 @@ namespace easy2d HRESULT hr = S_OK; D2DBitmapPtr bitmap; - if (res.IsFile()) + if (res.IsFileType()) { if (!File(res.GetFileName()).Exists()) { diff --git a/src/core/Music.cpp b/src/core/Music.cpp index 899eba80..8f447a3a 100644 --- a/src/core/Music.cpp +++ b/src/core/Music.cpp @@ -58,7 +58,7 @@ namespace easy2d HRESULT hr = S_OK; Transcoder transcoder; - if (res.IsFile()) + if (res.IsFileType()) { if (!File(res.GetFileName()).Exists()) { diff --git a/src/core/Node.cpp b/src/core/Node.cpp index abc73200..51796872 100644 --- a/src/core/Node.cpp +++ b/src/core/Node.cpp @@ -364,9 +364,9 @@ namespace easy2d void Node::SetName(String const& name) { - if (name_ != name) + if (!IsName(name)) { - name_ = name; + Object::SetName(name); hash_name_ = std::hash{}(name); } } @@ -511,7 +511,7 @@ namespace easy2d for (Node* child = children_.First().Get(); child; child = child->NextItem().Get()) { - if (child->hash_name_ == hash_code && child->name_ == name) + if (child->hash_name_ == hash_code && child->IsName(name)) { children.push_back(child); } @@ -525,7 +525,7 @@ namespace easy2d for (Node* child = children_.First().Get(); child; child = child->NextItem().Get()) { - if (child->hash_name_ == hash_code && child->name_ == name) + if (child->hash_name_ == hash_code && child->IsName(name)) { return child; } @@ -582,7 +582,7 @@ namespace easy2d { next = child->NextItem().Get(); - if (child->hash_name_ == hash_code && child->name_ == child_name) + if (child->hash_name_ == hash_code && child->IsName(child_name)) { RemoveChild(child); } diff --git a/src/core/Node.h b/src/core/Node.h index ce949e0e..de59649a 100644 --- a/src/core/Node.h +++ b/src/core/Node.h @@ -61,9 +61,6 @@ namespace easy2d // 获取显示状态 bool IsVisible() const { return visible_; } - // 获取名称 - String const& GetName() const { return name_; } - // 获取名称的 Hash 值 size_t GetHashName() const { return hash_name_; } @@ -377,7 +374,6 @@ namespace easy2d int z_order_; float opacity_; float display_opacity_; - String name_; size_t hash_name_; Transform transform_; Point anchor_; diff --git a/src/core/Object.cpp b/src/core/Object.cpp index 2a956d51..ceb62ac3 100644 --- a/src/core/Object.cpp +++ b/src/core/Object.cpp @@ -31,6 +31,7 @@ namespace easy2d Object::Object() : tracing_leak_(false) , user_data_(nullptr) + , name_(nullptr) { #ifdef E2D_DEBUG @@ -41,6 +42,9 @@ namespace easy2d Object::~Object() { + if (name_) + delete name_; + #ifdef E2D_DEBUG Object::__RemoveObjectFromTracingList(this); @@ -58,6 +62,27 @@ namespace easy2d user_data_ = data; } + void Object::SetName(String const & name) + { + if (IsName(name)) + return; + + if (name.empty()) + { + if (name_) + name_->clear(); + return; + } + + if (!name_) + { + name_ = new (std::nothrow) String(name); + return; + } + + *name_ = name; + } + void Object::StartTracingLeaks() { tracing_leaks = true; diff --git a/src/core/Object.h b/src/core/Object.h index 31e00b3d..24d8d16e 100644 --- a/src/core/Object.h +++ b/src/core/Object.h @@ -36,6 +36,12 @@ namespace easy2d void SetUserData(void* data); + void SetName(String const& name); + + inline String GetName() const { if (name_) return *name_; return String(); } + + inline bool IsName(String const& name) const { return (name_ && (*name_ == name)); } + static void StartTracingLeaks(); static void StopTracingLeaks(); @@ -50,5 +56,6 @@ namespace easy2d private: bool tracing_leak_; void* user_data_; + String* name_; }; } diff --git a/src/core/Resource.cpp b/src/core/Resource.cpp index 94576315..fdbee74f 100644 --- a/src/core/Resource.cpp +++ b/src/core/Resource.cpp @@ -25,8 +25,18 @@ namespace easy2d { Resource::Resource(LPCWSTR file_name) : type_(Type::File) - , file_name_(file_name) + , file_name_(nullptr) { + if (file_name) + file_name_ = new (std::nothrow) String(file_name); + } + + Resource::Resource(String const& file_name) + : type_(Type::File) + , file_name_(nullptr) + { + if (!file_name.empty()) + file_name_ = new (std::nothrow) String(file_name); } Resource::Resource(LPCWSTR name, LPCWSTR type) @@ -36,17 +46,85 @@ namespace easy2d { } + Resource::Resource(Resource const & rhs) + { + operator=(rhs); + } + + Resource::Resource(Resource && rhs) + { + operator=(std::forward(rhs)); + } + + Resource::~Resource() + { + if (IsFileType() && file_name_) + delete file_name_; + } + size_t Resource::GetHashCode() const { if (type_ == Type::File) - return std::hash{}(file_name_); + return std::hash{}(GetFileName()); return std::hash{}(bin_name_); } + Resource & Resource::operator=(Resource const & rhs) + { + if (&rhs != this) + { + if (IsFileType() && file_name_) + { + delete file_name_; + file_name_ = nullptr; + } + + type_ = rhs.type_; + if (IsFileType()) + { + if (rhs.file_name_) + { + file_name_ = new (std::nothrow) String(*rhs.file_name_); + } + } + else + { + bin_name_ = rhs.bin_name_; + bin_type_ = rhs.bin_type_; + } + } + return *this; + } + + Resource & Resource::operator=(Resource && rhs) + { + if (IsFileType() && file_name_) + { + delete file_name_; + file_name_ = nullptr; + } + + type_ = rhs.type_; + if (IsFileType()) + { + file_name_ = rhs.file_name_; + rhs.file_name_ = nullptr; + } + else + { + bin_name_ = rhs.bin_name_; + bin_type_ = rhs.bin_type_; + } + return *this; + } + bool Resource::Load(LPVOID& buffer, DWORD& buffer_size) const { if (type_ != Type::Binary) + { + E2D_ERROR_LOG(L"Only binary resource can be loaded"); return false; + } HGLOBAL res_data; HRSRC res_info; diff --git a/src/core/Resource.h b/src/core/Resource.h index cb1f70e1..47570c15 100644 --- a/src/core/Resource.h +++ b/src/core/Resource.h @@ -20,7 +20,6 @@ #pragma once #include "helper.hpp" -#include "Frames.h" namespace easy2d { @@ -34,23 +33,35 @@ namespace easy2d // class Resource { - public: enum class Type { File, Binary }; + public: Resource( - LPCWSTR file_name /* 文件路径 */ + LPCWSTR file_name /* 文件路径 */ ); Resource( - LPCWSTR name, /* 资源名称 */ - LPCWSTR type /* 资源类型 */ + String const& file_name /* 文件路径 */ ); - inline bool IsFile() const { return type_ == Type::File; } + Resource( + LPCWSTR name, /* 资源名称 */ + LPCWSTR type /* 资源类型 */ + ); - inline Type GetType() const { return type_; } + Resource( + Resource const& rhs + ); - inline LPCWSTR GetFileName() const { return file_name_; } + Resource( + Resource && rhs + ); + + virtual ~Resource(); + + inline bool IsFileType() const { return type_ == Type::File; } + + inline String GetFileName() const { if (file_name_) return *file_name_; return String(); } bool Load( LPVOID& buffer, @@ -59,13 +70,17 @@ namespace easy2d size_t GetHashCode() const; + Resource& operator= (Resource const& rhs); + + Resource& operator= (Resource && rhs); + private: Type type_; union { struct { - LPCWSTR file_name_; + String* file_name_; }; struct diff --git a/src/core/Task.cpp b/src/core/Task.cpp index 53a4086a..94fb5554 100644 --- a/src/core/Task.cpp +++ b/src/core/Task.cpp @@ -33,9 +33,9 @@ namespace easy2d , total_times_(times) , delay_(delay) , callback_(func) - , name_(name) , delta_() { + SetName(name); } void Task::Start() @@ -91,9 +91,4 @@ namespace easy2d return running_; } - String const& Task::GetName() const - { - return name_; - } - } \ No newline at end of file diff --git a/src/core/Task.h b/src/core/Task.h index 7b3cc62c..a5c49d85 100644 --- a/src/core/Task.h +++ b/src/core/Task.h @@ -60,21 +60,17 @@ namespace easy2d // 任务是否正在执行 bool IsRunning() const; - // 获取任务名称 - String const& GetName() const; - protected: void Update(Duration dt, bool& remove_after_update); void Reset(); protected: - bool running_; - int run_times_; - int total_times_; - String name_; - Duration delay_; - Duration delta_; - Callback callback_; + bool running_; + int run_times_; + int total_times_; + Duration delay_; + Duration delta_; + Callback callback_; }; } diff --git a/src/core/TaskManager.cpp b/src/core/TaskManager.cpp index 9999beb1..80d69d2f 100644 --- a/src/core/TaskManager.cpp +++ b/src/core/TaskManager.cpp @@ -59,7 +59,7 @@ namespace easy2d for (auto task = tasks_.First().Get(); task; task = task->NextItem().Get()) { - if (task->GetName() == name) + if (task->IsName(name)) { task->Stop(); } @@ -73,7 +73,7 @@ namespace easy2d for (auto task = tasks_.First().Get(); task; task = task->NextItem().Get()) { - if (task->GetName() == name) + if (task->IsName(name)) { task->Start(); } @@ -89,7 +89,7 @@ namespace easy2d for (auto task = tasks_.First(); task; task = next) { next = task->NextItem(); - if (task->GetName() == name) + if (task->IsName(name)) { tasks_.Remove(task); } diff --git a/src/utils/ResLoader.cpp b/src/utils/ResLoader.cpp index 16e661a3..7473eaed 100644 --- a/src/utils/ResLoader.cpp +++ b/src/utils/ResLoader.cpp @@ -28,16 +28,20 @@ namespace easy2d { namespace { - String Search(LPCWSTR file_name, List const& paths) + Resource LocateRes(Resource const& res, List const& paths) { - for (const auto& path : paths) + if (res.IsFileType()) { - if (modules::Shlwapi::Get().PathFileExistsW((path + file_name).c_str())) + String file_name = res.GetFileName(); + for (const auto& path : paths) { - return path + file_name; + if (modules::Shlwapi::Get().PathFileExistsW((path + file_name).c_str())) + { + return Resource{ path + file_name }; + } } } - return file_name; + return res; } } @@ -46,15 +50,7 @@ namespace easy2d ImagePtr ptr = new (std::nothrow) Image; if (ptr) { - if (image.IsFile()) - { - String path = Search(image.GetFileName(), search_paths_); - ptr->Load(path.c_str()); - } - else - ptr->Load(image); - - if (ptr->IsValid()) + if (ptr->Load(LocateRes(image, search_paths_))) { res_.insert(std::make_pair(id, ptr)); return true; @@ -78,38 +74,28 @@ namespace easy2d if (images.empty()) return 0; - int total = 0; Array image_arr; - image_arr.reserve(images.size()); + for (const auto& image : images) { ImagePtr ptr = new (std::nothrow) Image; if (ptr) { - if (image.IsFile()) - { - String path = Search(image.GetFileName(), search_paths_); - ptr->Load(path.c_str()); - } - else - ptr->Load(image); - - if (ptr->IsValid()) + if (ptr->Load(LocateRes(image, search_paths_))) { image_arr.push_back(ptr); - ++total; } } } - if (total) + if (!image_arr.empty()) { FramesPtr frames = new (std::nothrow) Frames(image_arr); if (frames) { res_.insert(std::make_pair(id, frames)); - return total; + return frames->GetFrames().size(); } } return 0; @@ -134,62 +120,66 @@ namespace easy2d if (cols <= 0 || rows <= 0) return 0; - int total = 0; - FramesPtr frames = new (std::nothrow) Frames; - if (frames) + ImagePtr raw = new (std::nothrow) Image; + if (!raw || !raw->Load(LocateRes(image, search_paths_))) + return 0; + + float raw_width = raw->GetSourceWidth(); + float raw_height = raw->GetSourceHeight(); + float width = raw_width / cols; + float height = raw_height / rows; + + Array image_arr; + image_arr.reserve(rows * cols); + + for (int i = 0; i < rows; i++) { - ImagePtr raw = new (std::nothrow) Image; - if (!raw || !raw->Load(image)) - return 0; - - float raw_width = raw->GetSourceWidth(); - float raw_height = raw->GetSourceHeight(); - float width = raw_width / cols; - float height = raw_height / rows; - - for (int i = 0; i < rows; i++) - { - for (int j = 0; j < cols; j++) - { - ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap()); - if (ptr) - { - ptr->Crop(Rect{ i * width, j * height, width, height }); - frames->Add(ptr); - ++total; - } - } - } - } - if (total) - res_.insert(std::make_pair(id, frames)); - return total; - } - - int ResLoader::AddFrames(String const & id, Resource const & image, Array const & crop_rects) - { - int total = 0; - FramesPtr frames = new (std::nothrow) Frames; - if (frames) - { - ImagePtr raw = new (std::nothrow) Image; - if (!raw || !raw->Load(image)) - return 0; - - for (const auto& rect : crop_rects) + for (int j = 0; j < cols; j++) { ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap()); if (ptr) { - ptr->Crop(rect); - frames->Add(ptr); - ++total; + ptr->Crop(Rect{ i * width, j * height, width, height }); + image_arr.push_back(ptr); } } } - if (total) + + FramesPtr frames = new (std::nothrow) Frames(image_arr); + if (frames) + { res_.insert(std::make_pair(id, frames)); - return total; + return frames->GetFrames().size(); + } + return 0; + } + + int ResLoader::AddFrames(String const & id, Resource const & image, Array const & crop_rects) + { + ImagePtr raw = new (std::nothrow) Image; + if (!raw || !raw->Load(LocateRes(image, search_paths_))) + return 0; + + Array image_arr; + image_arr.reserve(crop_rects.size()); + + for (const auto& rect : crop_rects) + { + ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap()); + if (ptr) + { + ptr->Crop(rect); + image_arr.push_back(ptr); + } + } + + FramesPtr frames = new (std::nothrow) Frames(image_arr); + if (frames) + { + res_.insert(std::make_pair(id, frames)); + return frames->GetFrames().size(); + } + return 0; } bool ResLoader::AddFrames(String const & id, FramesPtr const & frames) @@ -207,16 +197,7 @@ namespace easy2d MusicPtr ptr = new (std::nothrow) Music; if (ptr) { - bool valid = false; - if (music.IsFile()) - { - String path = Search(music.GetFileName(), search_paths_); - valid = ptr->Load(path.c_str()); - } - else - valid = ptr->Load(music); - - if (valid) + if (ptr->Load(LocateRes(music, search_paths_))) { res_.insert(std::make_pair(id, ptr)); return true; diff --git a/src/utils/Transcoder.cpp b/src/utils/Transcoder.cpp index 678a6aac..5ea29b8e 100644 --- a/src/utils/Transcoder.cpp +++ b/src/utils/Transcoder.cpp @@ -49,14 +49,14 @@ namespace easy2d return wave_format_; } - HRESULT Transcoder::LoadMediaFile(LPCWSTR file_path, BYTE** wave_data, UINT32* wave_data_size) + HRESULT Transcoder::LoadMediaFile(String const& file_path, BYTE** wave_data, UINT32* wave_data_size) { HRESULT hr = S_OK; IntrusivePtr reader; hr = modules::MediaFoundation::Get().MFCreateSourceReaderFromURL( - file_path, + file_path.c_str(), nullptr, &reader ); diff --git a/src/utils/Transcoder.h b/src/utils/Transcoder.h index 5b490b32..72f4164b 100644 --- a/src/utils/Transcoder.h +++ b/src/utils/Transcoder.h @@ -39,7 +39,7 @@ namespace easy2d const WAVEFORMATEX* GetWaveFormatEx() const; HRESULT LoadMediaFile( - LPCWSTR file_path, + String const& file_path, BYTE** wave_data, UINT32* wave_data_size );