From 5f698fb475beca93fb3671b238762eceea9f5231 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Fri, 7 Sep 2018 00:28:54 +0800 Subject: [PATCH] =?UTF-8?q?Refactoring=20:=20GC=20=E6=9C=BA=E5=88=B6?= =?UTF-8?q?=E9=87=8D=E5=81=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/actions/Animate.cpp | 14 +++-- core/actions/Animation.cpp | 8 +-- core/actions/Callback.cpp | 4 +- core/actions/Delay.cpp | 4 +- core/actions/JumpBy.cpp | 4 +- core/actions/JumpTo.cpp | 2 +- core/actions/Loop.cpp | 6 +- core/actions/MoveBy.cpp | 4 +- core/actions/MoveTo.cpp | 2 +- core/actions/OpacityBy.cpp | 4 +- core/actions/OpacityTo.cpp | 2 +- core/actions/RotateBy.cpp | 4 +- core/actions/RotateTo.cpp | 2 +- core/actions/ScaleBy.cpp | 4 +- core/actions/ScaleTo.cpp | 2 +- core/actions/Sequence.cpp | 8 +-- core/actions/Spawn.cpp | 8 +-- core/e2dimpl.h | 11 ---- core/e2dmodule.h | 24 +------- core/e2dobject.h | 10 ++-- core/e2dtool.h | 3 +- core/e2dutil.h | 39 ++++++------- core/modules/GC.cpp | 80 +-------------------------- core/modules/Game.cpp | 11 +++- core/objects/Collider.cpp | 2 + core/objects/Image.cpp | 11 ++++ core/objects/Node.cpp | 8 +-- core/objects/Object.cpp | 25 --------- core/objects/Scene.cpp | 4 +- core/objects/Sprite.cpp | 12 ++-- core/tools/File.cpp | 72 ++++++++++++------------ core/tools/Player.cpp | 36 +++++++++--- core/transitions/Transition.cpp | 4 +- core/utils/Ref.cpp | 33 +++++++++++ project/vs2012/Easy2D.vcxproj | 2 +- project/vs2012/Easy2D.vcxproj.filters | 34 ++++++------ project/vs2013/Easy2D.vcxproj | 2 +- project/vs2013/Easy2D.vcxproj.filters | 34 ++++++------ project/vs2017/Easy2D.vcxproj | 2 +- project/vs2017/Easy2D.vcxproj.filters | 34 ++++++------ 40 files changed, 256 insertions(+), 319 deletions(-) delete mode 100644 core/objects/Object.cpp create mode 100644 core/utils/Ref.cpp diff --git a/core/actions/Animate.cpp b/core/actions/Animate.cpp index c8882a5e..6ac5c5a3 100644 --- a/core/actions/Animate.cpp +++ b/core/actions/Animate.cpp @@ -16,7 +16,7 @@ e2d::Animate::Animate(Animation * animation) e2d::Animate::~Animate() { - GC::GetInstance()->SafeRelease(animation_); + SafeRelease(animation_); } e2d::Animation * e2d::Animate::GetAnimation() const @@ -26,11 +26,15 @@ e2d::Animation * e2d::Animate::GetAnimation() const void e2d::Animate::SetAnimation(Animation * animation) { - if (animation && animation != animation_ && !animation->GetFrames().empty()) + if (animation && animation != animation_) { - if (animation_) animation_->Release(); + if (animation_) + { + animation_->Release(); + } animation_ = animation; animation_->Retain(); + frame_index_ = 0; } } @@ -92,7 +96,7 @@ e2d::Animate * e2d::Animate::Clone() const { if (animation_) { - return new (e2d::autorelease) Animate(animation_); + return new Animate(animation_); } return nullptr; } @@ -104,7 +108,7 @@ e2d::Animate * e2d::Animate::Reverse() const auto animation = animation_->Reverse(); if (animation) { - return new (e2d::autorelease) Animate(animation); + return new Animate(animation); } } return nullptr; diff --git a/core/actions/Animation.cpp b/core/actions/Animation.cpp index dab6aa79..d57ea29c 100644 --- a/core/actions/Animation.cpp +++ b/core/actions/Animation.cpp @@ -24,9 +24,9 @@ e2d::Animation::Animation(float interval, const Images& frames) e2d::Animation::~Animation() { - for (const auto& frame : frames_) + for (auto frame : frames_) { - GC::GetInstance()->SafeRelease(frame); + SafeRelease(frame); } } @@ -65,7 +65,7 @@ const e2d::Animation::Images& e2d::Animation::GetFrames() const e2d::Animation * e2d::Animation::Clone() const { - auto animation = new (e2d::autorelease) Animation(interval_); + auto animation = new Animation(interval_); if (animation) { for (const auto& frame : frames_) @@ -96,5 +96,5 @@ e2d::Animation * e2d::Animation::Reverse() const } } - return new (e2d::autorelease) Animation(this->GetInterval(), frames); + return new Animation(this->GetInterval(), frames); } diff --git a/core/actions/Callback.cpp b/core/actions/Callback.cpp index 09e746b8..e63ecc6a 100644 --- a/core/actions/Callback.cpp +++ b/core/actions/Callback.cpp @@ -7,12 +7,12 @@ e2d::Callback::Callback(const Function& func) : e2d::Callback * e2d::Callback::Clone() const { - return new (e2d::autorelease) Callback(callback_); + return new Callback(callback_); } e2d::Callback * e2d::Callback::Reverse() const { - return new (e2d::autorelease) Callback(callback_); + return new Callback(callback_); } void e2d::Callback::Init() diff --git a/core/actions/Delay.cpp b/core/actions/Delay.cpp index ca6dd0a5..324c8acd 100644 --- a/core/actions/Delay.cpp +++ b/core/actions/Delay.cpp @@ -8,12 +8,12 @@ e2d::Delay::Delay(float duration) e2d::Delay * e2d::Delay::Clone() const { - return new (e2d::autorelease) Delay(delay_); + return new Delay(delay_); } e2d::Delay * e2d::Delay::Reverse() const { - return new (e2d::autorelease) Delay(delay_); + return new Delay(delay_); } void e2d::Delay::Reset() diff --git a/core/actions/JumpBy.cpp b/core/actions/JumpBy.cpp index 1c1be6a3..d4e9f372 100644 --- a/core/actions/JumpBy.cpp +++ b/core/actions/JumpBy.cpp @@ -11,12 +11,12 @@ e2d::JumpBy::JumpBy(float duration, const Point & vec, float height, int jumps) e2d::JumpBy * e2d::JumpBy::Clone() const { - return new (e2d::autorelease) JumpBy(duration_, delta_pos_, height_, jumps_); + return new JumpBy(duration_, delta_pos_, height_, jumps_); } e2d::JumpBy * e2d::JumpBy::Reverse() const { - return new (e2d::autorelease) JumpBy(duration_, -delta_pos_, height_, jumps_); + return new JumpBy(duration_, -delta_pos_, height_, jumps_); } void e2d::JumpBy::Init() diff --git a/core/actions/JumpTo.cpp b/core/actions/JumpTo.cpp index 9ab39d5e..af33628b 100644 --- a/core/actions/JumpTo.cpp +++ b/core/actions/JumpTo.cpp @@ -9,7 +9,7 @@ e2d::JumpTo::JumpTo(float duration, const Point & pos, float height, int jumps) e2d::JumpTo * e2d::JumpTo::Clone() const { - return new (e2d::autorelease) JumpTo(duration_, end_pos_, height_, jumps_); + return new JumpTo(duration_, end_pos_, height_, jumps_); } void e2d::JumpTo::Init() diff --git a/core/actions/Loop.cpp b/core/actions/Loop.cpp index 84dd422d..9a2b80ef 100644 --- a/core/actions/Loop.cpp +++ b/core/actions/Loop.cpp @@ -17,14 +17,14 @@ e2d::Loop::Loop(Action * action, int times /* = -1 */) e2d::Loop::~Loop() { - GC::GetInstance()->SafeRelease(action_); + SafeRelease(action_); } e2d::Loop * e2d::Loop::Clone() const { if (action_) { - return new (e2d::autorelease) Loop(action_->Clone()); + return new Loop(action_->Clone()); } else { @@ -36,7 +36,7 @@ e2d::Loop * e2d::Loop::Reverse() const { if (action_) { - return new (e2d::autorelease) Loop(action_->Clone()); + return new Loop(action_->Clone()); } else { diff --git a/core/actions/MoveBy.cpp b/core/actions/MoveBy.cpp index 83290e36..21f0d994 100644 --- a/core/actions/MoveBy.cpp +++ b/core/actions/MoveBy.cpp @@ -37,10 +37,10 @@ void e2d::MoveBy::Update() e2d::MoveBy * e2d::MoveBy::Clone() const { - return new (e2d::autorelease) MoveBy(duration_, delta_pos_); + return new MoveBy(duration_, delta_pos_); } e2d::MoveBy * e2d::MoveBy::Reverse() const { - return new (e2d::autorelease) MoveBy(duration_, -delta_pos_); + return new MoveBy(duration_, -delta_pos_); } \ No newline at end of file diff --git a/core/actions/MoveTo.cpp b/core/actions/MoveTo.cpp index df682f44..16f373eb 100644 --- a/core/actions/MoveTo.cpp +++ b/core/actions/MoveTo.cpp @@ -9,7 +9,7 @@ e2d::MoveTo::MoveTo(float duration, Point pos) e2d::MoveTo * e2d::MoveTo::Clone() const { - return new (e2d::autorelease) MoveTo(duration_, end_pos_); + return new MoveTo(duration_, end_pos_); } void e2d::MoveTo::Init() diff --git a/core/actions/OpacityBy.cpp b/core/actions/OpacityBy.cpp index 84e12e5e..a5f6b319 100644 --- a/core/actions/OpacityBy.cpp +++ b/core/actions/OpacityBy.cpp @@ -30,10 +30,10 @@ void e2d::OpacityBy::Update() e2d::OpacityBy * e2d::OpacityBy::Clone() const { - return new (e2d::autorelease) OpacityBy(duration_, delta_val_); + return new OpacityBy(duration_, delta_val_); } e2d::OpacityBy * e2d::OpacityBy::Reverse() const { - return new (e2d::autorelease) OpacityBy(duration_, -delta_val_); + return new OpacityBy(duration_, -delta_val_); } \ No newline at end of file diff --git a/core/actions/OpacityTo.cpp b/core/actions/OpacityTo.cpp index 3717f3f9..9df81f58 100644 --- a/core/actions/OpacityTo.cpp +++ b/core/actions/OpacityTo.cpp @@ -10,7 +10,7 @@ e2d::OpacityTo::OpacityTo(float duration, float opacity) e2d::OpacityTo * e2d::OpacityTo::Clone() const { - return new (e2d::autorelease) OpacityTo(duration_, end_val_); + return new OpacityTo(duration_, end_val_); } void e2d::OpacityTo::Init() diff --git a/core/actions/RotateBy.cpp b/core/actions/RotateBy.cpp index c4024c28..f96d972e 100644 --- a/core/actions/RotateBy.cpp +++ b/core/actions/RotateBy.cpp @@ -30,10 +30,10 @@ void e2d::RotateBy::Update() e2d::RotateBy * e2d::RotateBy::Clone() const { - return new (e2d::autorelease) RotateBy(duration_, delta_val_); + return new RotateBy(duration_, delta_val_); } e2d::RotateBy * e2d::RotateBy::Reverse() const { - return new (e2d::autorelease) RotateBy(duration_, -delta_val_); + return new RotateBy(duration_, -delta_val_); } \ No newline at end of file diff --git a/core/actions/RotateTo.cpp b/core/actions/RotateTo.cpp index 08fa4a6e..0999ce8c 100644 --- a/core/actions/RotateTo.cpp +++ b/core/actions/RotateTo.cpp @@ -10,7 +10,7 @@ e2d::RotateTo::RotateTo(float duration, float rotation) e2d::RotateTo * e2d::RotateTo::Clone() const { - return new (e2d::autorelease) RotateTo(duration_, end_val_); + return new RotateTo(duration_, end_val_); } void e2d::RotateTo::Init() diff --git a/core/actions/ScaleBy.cpp b/core/actions/ScaleBy.cpp index 3ec7504b..f5f8234e 100644 --- a/core/actions/ScaleBy.cpp +++ b/core/actions/ScaleBy.cpp @@ -39,10 +39,10 @@ void e2d::ScaleBy::Update() e2d::ScaleBy * e2d::ScaleBy::Clone() const { - return new (e2d::autorelease) ScaleBy(duration_, delta_x_, delta_y_); + return new ScaleBy(duration_, delta_x_, delta_y_); } e2d::ScaleBy * e2d::ScaleBy::Reverse() const { - return new (e2d::autorelease) ScaleBy(duration_, -delta_x_, -delta_y_); + return new ScaleBy(duration_, -delta_x_, -delta_y_); } \ No newline at end of file diff --git a/core/actions/ScaleTo.cpp b/core/actions/ScaleTo.cpp index 95fc3276..f499b1e2 100644 --- a/core/actions/ScaleTo.cpp +++ b/core/actions/ScaleTo.cpp @@ -17,7 +17,7 @@ e2d::ScaleTo::ScaleTo(float duration, float scale_x, float scale_y) e2d::ScaleTo * e2d::ScaleTo::Clone() const { - return new (e2d::autorelease) ScaleTo(duration_, end_scale_x_, end_scale_y_); + return new ScaleTo(duration_, end_scale_x_, end_scale_y_); } void e2d::ScaleTo::Init() diff --git a/core/actions/Sequence.cpp b/core/actions/Sequence.cpp index b601f567..6bdbff2a 100644 --- a/core/actions/Sequence.cpp +++ b/core/actions/Sequence.cpp @@ -13,9 +13,9 @@ e2d::Sequence::Sequence(const Actions& actions) e2d::Sequence::~Sequence() { - for (const auto& action : actions_) + for (auto action : actions_) { - GC::GetInstance()->SafeRelease(action); + SafeRelease(action); } } @@ -92,7 +92,7 @@ void e2d::Sequence::Add(const Actions& actions) e2d::Sequence * e2d::Sequence::Clone() const { - auto sequence = new (e2d::autorelease) Sequence(); + auto sequence = new Sequence(); for (const auto& action : actions_) { if (action) @@ -105,7 +105,7 @@ e2d::Sequence * e2d::Sequence::Clone() const e2d::Sequence * e2d::Sequence::Reverse() const { - auto sequence = new (e2d::autorelease) Sequence(); + auto sequence = new Sequence(); if (sequence && !actions_.empty()) { std::vector newActions(actions_.size()); diff --git a/core/actions/Spawn.cpp b/core/actions/Spawn.cpp index df68758e..aa9cdec4 100644 --- a/core/actions/Spawn.cpp +++ b/core/actions/Spawn.cpp @@ -11,9 +11,9 @@ e2d::Spawn::Spawn(const Actions& actions) e2d::Spawn::~Spawn() { - for (const auto& action : actions_) + for (auto action : actions_) { - GC::GetInstance()->SafeRelease(action); + SafeRelease(action); } } @@ -90,7 +90,7 @@ void e2d::Spawn::Add(const Actions& actions) e2d::Spawn * e2d::Spawn::Clone() const { - auto spawn = new (e2d::autorelease) Spawn(); + auto spawn = new Spawn(); for (const auto& action : actions_) { if (action) @@ -103,7 +103,7 @@ e2d::Spawn * e2d::Spawn::Clone() const e2d::Spawn * e2d::Spawn::Reverse() const { - auto spawn = new (e2d::autorelease) Spawn(); + auto spawn = new Spawn(); if (spawn && !actions_.empty()) { std::vector newActions(actions_.size()); diff --git a/core/e2dimpl.h b/core/e2dimpl.h index 759b8199..e2332c37 100644 --- a/core/e2dimpl.h +++ b/core/e2dimpl.h @@ -170,17 +170,6 @@ namespace e2d }; - template - inline void SafeRelease(Interface*& p) - { - if (p != nullptr) - { - p->Release(); - p = nullptr; - } - } - - inline void ThrowIfFailed(HRESULT hr) { if (FAILED(hr)) diff --git a/core/e2dmodule.h b/core/e2dmodule.h index b8fc205f..cddfed9b 100644 --- a/core/e2dmodule.h +++ b/core/e2dmodule.h @@ -376,26 +376,9 @@ namespace e2d }; - // 垃圾回收池 + // 垃圾回收 class GC { - public: - // 获取 GC 实例 - static GC * GetInstance(); - - // 自动释放 - void AutoRelease( - Ref* ref - ); - - // 安全地释放对象 - void SafeRelease( - Ref* ref - ); - - // 刷新内存池 - void Flush(); - private: GC(); @@ -403,10 +386,7 @@ namespace e2d E2D_DISABLE_COPY(GC); - private: - bool notifyed_; - bool cleanup_; - std::set pool_; + static GC instance_; }; } diff --git a/core/e2dobject.h b/core/e2dobject.h index 17c95cdf..1033777f 100644 --- a/core/e2dobject.h +++ b/core/e2dobject.h @@ -79,6 +79,11 @@ namespace e2d // 获取 ID2D1Bitmap 对象 ID2D1Bitmap * GetBitmap(); + // 设置 Bitmap + void SetBitmap( + ID2D1Bitmap * bitmap + ); + // 预加载图片资源 static bool Preload( const String& file_name @@ -95,11 +100,6 @@ namespace e2d protected: E2D_DISABLE_COPY(Image); - // 设置 Bitmap - void SetBitmap( - ID2D1Bitmap * bitmap - ); - protected: Rect crop_rect_; ID2D1Bitmap * bitmap_; diff --git a/core/e2dtool.h b/core/e2dtool.h index 67fc5074..35e0b6e7 100644 --- a/core/e2dtool.h +++ b/core/e2dtool.h @@ -50,7 +50,8 @@ namespace e2d // 音乐 - class Music + class Music : + public Ref { public: Music(); diff --git a/core/e2dutil.h b/core/e2dutil.h index 3997c045..e0a212e7 100644 --- a/core/e2dutil.h +++ b/core/e2dutil.h @@ -610,38 +610,31 @@ namespace e2d virtual ~Ref(); // 增加引用计数 - void Retain(); + LONG Retain(); // 减少引用计数 - void Release(); + LONG Release(); // 获取引用计数 - int GetRefCount() const; + LONG GetRefCount() const; protected: E2D_DISABLE_COPY(Ref); - private: - int ref_count_; + protected: + LONG ref_count_; }; + template + inline void SafeRelease(Interface*& p) + { + if (p != nullptr) + { + p->Release(); + p = nullptr; + } + } + + } - - -namespace e2d -{ - struct autorelease_t { }; - - extern autorelease_t const autorelease; -} - -void* operator new( - size_t size, - e2d::autorelease_t const& - ) E2D_NOEXCEPT; - -void operator delete( - void* block, - e2d::autorelease_t const& - ) E2D_NOEXCEPT; diff --git a/core/modules/GC.cpp b/core/modules/GC.cpp index 16fee797..7038a286 100644 --- a/core/modules/GC.cpp +++ b/core/modules/GC.cpp @@ -1,55 +1,17 @@ #include "..\e2dmodule.h" #include "..\e2dtool.h" -#include "..\e2dmanager.h" - -using namespace e2d; - -e2d::autorelease_t const e2d::autorelease = e2d::autorelease_t(); - -void * operator new(size_t size, e2d::autorelease_t const &) E2D_NOEXCEPT -{ - void* p = ::operator new(size, std::nothrow); - if (p) - { - GC::GetInstance()->AutoRelease(static_cast(p)); - } - return p; -} - -void operator delete(void * block, e2d::autorelease_t const &) E2D_NOEXCEPT -{ - ::operator delete (block, std::nothrow); -} -e2d::GC * e2d::GC::GetInstance() -{ - static GC instance_; - return &instance_; -} +e2d::GC e2d::GC::instance_; e2d::GC::GC() - : notifyed_(false) - , cleanup_(false) - , pool_() { } e2d::GC::~GC() { - // 删除所有对象 - cleanup_ = true; - for (const auto& ref : pool_) - { - delete ref; - } - pool_.clear(); - cleanup_ = false; - - // 清除缓存 Image::ClearCache(); - // 清除单例 Player::DestroyInstance(); Audio::DestroyInstance(); Renderer::DestroyInstance(); @@ -57,43 +19,3 @@ e2d::GC::~GC() Window::DestroyInstance(); Game::DestroyInstance(); } - -void e2d::GC::Flush() -{ - if (!notifyed_) - return; - - notifyed_ = false; - for (auto iter = pool_.begin(); iter != pool_.end();) - { - if ((*iter)->GetRefCount() <= 0) - { - delete (*iter); - iter = pool_.erase(iter); - } - else - { - ++iter; - } - } -} - -void e2d::GC::AutoRelease(Ref * ref) -{ - if (ref) - { - pool_.insert(ref); - } -} - -void e2d::GC::SafeRelease(Ref* ref) -{ - if (cleanup_) - return; - - if (ref) - { - ref->Release(); - notifyed_ = true; - } -} diff --git a/core/modules/Game.cpp b/core/modules/Game.cpp index fa56b27e..8809dae8 100644 --- a/core/modules/Game.cpp +++ b/core/modules/Game.cpp @@ -36,6 +36,10 @@ e2d::Game::Game() e2d::Game::~Game() { + SafeRelease(transition_); + SafeRelease(curr_scene_); + SafeRelease(next_scene_); + ::CoUninitialize(); } @@ -75,7 +79,6 @@ void e2d::Game::Start() DrawScene(); window->Poll(); - GC::GetInstance()->Flush(); } else { @@ -122,8 +125,10 @@ void e2d::Game::EnterScene(Scene * scene) if (!scene) return; - // 保存下一场景的指针 - if (next_scene_) next_scene_->Release(); + if (next_scene_) + { + next_scene_->Release(); + } next_scene_ = scene; next_scene_->Retain(); } diff --git a/core/objects/Collider.cpp b/core/objects/Collider.cpp index 047f50e6..3d5951f9 100644 --- a/core/objects/Collider.cpp +++ b/core/objects/Collider.cpp @@ -18,6 +18,8 @@ e2d::Collider::Collider(Node * parent) e2d::Collider::~Collider() { SafeRelease(geometry_); + + CollisionManager::GetInstance()->RemoveCollider(this); } const e2d::Color& e2d::Collider::GetBorderColor() const diff --git a/core/objects/Image.cpp b/core/objects/Image.cpp index c8d73831..c31f7e2b 100644 --- a/core/objects/Image.cpp +++ b/core/objects/Image.cpp @@ -42,6 +42,7 @@ e2d::Image::Image(const String & file_name, const Rect & crop_rect) e2d::Image::~Image() { + SafeRelease(bitmap_); } bool e2d::Image::Open(const Resource& res) @@ -366,8 +367,18 @@ void e2d::Image::ClearCache() void e2d::Image::SetBitmap(ID2D1Bitmap * bitmap) { + if (bitmap_ == bitmap) + return; + + if (bitmap_) + { + bitmap_->Release(); + } + if (bitmap) { + bitmap->AddRef(); + bitmap_ = bitmap; crop_rect_.origin.x = crop_rect_.origin.y = 0; crop_rect_.size.width = bitmap_->GetSize().width; diff --git a/core/objects/Node.cpp b/core/objects/Node.cpp index 11265954..9db5f33f 100644 --- a/core/objects/Node.cpp +++ b/core/objects/Node.cpp @@ -63,14 +63,14 @@ e2d::Node::~Node() { SafeRelease(border_); - for (const auto& action : actions_) + for (auto action : actions_) { - GC::GetInstance()->SafeRelease(action); + SafeRelease(action); } - for (const auto& child : children_) + for (auto child : children_) { - GC::GetInstance()->SafeRelease(child); + SafeRelease(child); } } diff --git a/core/objects/Object.cpp b/core/objects/Object.cpp deleted file mode 100644 index bcbefc3b..00000000 --- a/core/objects/Object.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "..\e2dobject.h" - -e2d::Ref::Ref() - : ref_count_(0) -{ -} - -e2d::Ref::~Ref() -{ -} - -void e2d::Ref::Retain() -{ - ref_count_++; -} - -void e2d::Ref::Release() -{ - ref_count_--; -} - -int e2d::Ref::GetRefCount() const -{ - return ref_count_; -} diff --git a/core/objects/Scene.cpp b/core/objects/Scene.cpp index adc0b9ba..170b4d71 100644 --- a/core/objects/Scene.cpp +++ b/core/objects/Scene.cpp @@ -20,8 +20,8 @@ e2d::Scene::~Scene() { if (root_) { - root_->Release(); root_->SetParentScene(nullptr); + root_->Release(); } } @@ -32,8 +32,8 @@ void e2d::Scene::SetRoot(Node * root) if (root_) { - root_->Release(); root_->SetParentScene(nullptr); + root_->Release(); } if (root) diff --git a/core/objects/Sprite.cpp b/core/objects/Sprite.cpp index 95dd5268..6a13bb76 100644 --- a/core/objects/Sprite.cpp +++ b/core/objects/Sprite.cpp @@ -40,14 +40,18 @@ e2d::Sprite::Sprite(const String & file_name, const Rect & crop_rect) e2d::Sprite::~Sprite() { - GC::GetInstance()->SafeRelease(image_); + SafeRelease(image_); } bool e2d::Sprite::Open(Image * image) { if (image) { - if (image_) image_->Release(); + if (image_) + { + image_->Release(); + } + image_ = image; image_->Retain(); @@ -61,7 +65,7 @@ bool e2d::Sprite::Open(const Resource& res) { if (!image_) { - image_ = new (e2d::autorelease) Image(); + image_ = new Image(); image_->Retain(); } @@ -77,7 +81,7 @@ bool e2d::Sprite::Open(const String & file_name) { if (!image_) { - image_ = new (e2d::autorelease) Image(); + image_ = new Image(); image_->Retain(); } diff --git a/core/tools/File.cpp b/core/tools/File.cpp index 7398b5cd..aaa11f32 100644 --- a/core/tools/File.cpp +++ b/core/tools/File.cpp @@ -174,60 +174,60 @@ e2d::File e2d::File::ShowOpenDialog(const String & title, const String & filter) if (SUCCEEDED(hr)) { - IFileOpenDialog *pFileOpen; + IFileOpenDialog *file_open; hr = ::CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, - IID_IFileOpenDialog, reinterpret_cast(&pFileOpen)); + IID_IFileOpenDialog, reinterpret_cast(&file_open)); if (SUCCEEDED(hr)) { if (!title.IsEmpty()) { - pFileOpen->SetTitle(LPCWSTR(title)); + file_open->SetTitle(LPCWSTR(title)); } if (!filter.IsEmpty()) { - COMDLG_FILTERSPEC rgSpec[] = + COMDLG_FILTERSPEC spec[] = { { L"", LPCWSTR(filter) } }; - pFileOpen->SetFileTypes(1, rgSpec); + file_open->SetFileTypes(1, spec); } else { - COMDLG_FILTERSPEC rgSpec[] = + COMDLG_FILTERSPEC spec[] = { { L"所有文件", L"*.*" } }; - pFileOpen->SetFileTypes(1, rgSpec); + file_open->SetFileTypes(1, spec); } Game::GetInstance()->Pause(); { HWND hWnd = Window::GetInstance()->GetHWnd(); - hr = pFileOpen->Show(hWnd); + hr = file_open->Show(hWnd); } Game::GetInstance()->Resume(); if (SUCCEEDED(hr)) { - IShellItem *pItem; - hr = pFileOpen->GetResult(&pItem); + IShellItem *item; + hr = file_open->GetResult(&item); if (SUCCEEDED(hr)) { - PWSTR pszFilePath; - hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); + PWSTR str_file_path; + hr = item->GetDisplayName(SIGDN_FILESYSPATH, &str_file_path); if (SUCCEEDED(hr)) { - file_path = pszFilePath; - ::CoTaskMemFree(pszFilePath); + file_path = str_file_path; + ::CoTaskMemFree(str_file_path); } - pItem->Release(); + item->Release(); } } - pFileOpen->Release(); + file_open->Release(); } ::CoUninitialize(); } @@ -241,68 +241,68 @@ e2d::File e2d::File::ShowSaveDialog(const String & title, const String& def_file if (SUCCEEDED(hr)) { - IFileSaveDialog *pFileSave; + IFileSaveDialog *file_save; hr = ::CoCreateInstance(CLSID_FileSaveDialog, NULL, CLSCTX_ALL, - IID_IFileSaveDialog, reinterpret_cast(&pFileSave)); + IID_IFileSaveDialog, reinterpret_cast(&file_save)); if (SUCCEEDED(hr)) { if (!title.IsEmpty()) { - pFileSave->SetTitle(LPCWSTR(title)); + file_save->SetTitle(LPCWSTR(title)); } if (!def_file.IsEmpty()) { - pFileSave->SetFileName(LPCWSTR(def_file)); + file_save->SetFileName(LPCWSTR(def_file)); } if (!def_ext.IsEmpty()) { - pFileSave->SetDefaultExtension(LPCWSTR(def_ext)); + file_save->SetDefaultExtension(LPCWSTR(def_ext)); - String spec = L"*." + def_ext; - COMDLG_FILTERSPEC rgSpec[] = + String ext = L"*." + def_ext; + COMDLG_FILTERSPEC spec[] = { - { L"", LPCWSTR(spec) } + { L"", LPCWSTR(ext) } }; - pFileSave->SetFileTypes(1, rgSpec); + file_save->SetFileTypes(1, spec); } else { - COMDLG_FILTERSPEC rgSpec[] = + COMDLG_FILTERSPEC spec[] = { { L"所有文件", L"*.*" } }; - pFileSave->SetFileTypes(1, rgSpec); + file_save->SetFileTypes(1, spec); } Game::GetInstance()->Pause(); { HWND hWnd = Window::GetInstance()->GetHWnd(); - hr = pFileSave->Show(hWnd); + hr = file_save->Show(hWnd); } Game::GetInstance()->Resume(); if (SUCCEEDED(hr)) { - IShellItem *pItem; - hr = pFileSave->GetResult(&pItem); + IShellItem *item; + hr = file_save->GetResult(&item); if (SUCCEEDED(hr)) { - PWSTR pszFilePath; - hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); + PWSTR str_file_path; + hr = item->GetDisplayName(SIGDN_FILESYSPATH, &str_file_path); if (SUCCEEDED(hr)) { - file_path = pszFilePath; - ::CoTaskMemFree(pszFilePath); + file_path = str_file_path; + ::CoTaskMemFree(str_file_path); } - pItem->Release(); + item->Release(); } } - pFileSave->Release(); + file_save->Release(); } ::CoUninitialize(); } diff --git a/core/tools/Player.cpp b/core/tools/Player.cpp index d1aa096a..7bf8d802 100644 --- a/core/tools/Player.cpp +++ b/core/tools/Player.cpp @@ -32,7 +32,7 @@ e2d::Player::~Player() { for (const auto& pair : musics_) { - delete pair.second; + pair.second->Release(); } } } @@ -44,11 +44,20 @@ bool e2d::Player::Preload(const String & file_path) Music * music = new (std::nothrow) Music(); - if (music && music->Open(file_path)) + if (music) { - music->SetVolume(volume_); - musics_.insert(std::make_pair(file_path.GetHash(), music)); - return true; + music->Retain(); + + if (music->Open(file_path)) + { + music->SetVolume(volume_); + musics_.insert(std::make_pair(file_path.GetHash(), music)); + return true; + } + else + { + music->Release(); + } } return false; } @@ -117,11 +126,20 @@ bool e2d::Player::Preload(const Resource& res) Music * music = new (std::nothrow) Music(); - if (music && music->Open(res)) + if (music) { - music->SetVolume(volume_); - musics_.insert(std::make_pair(res.name, music)); - return true; + music->Retain(); + + if (music->Open(res)) + { + music->SetVolume(volume_); + musics_.insert(std::make_pair(res.name, music)); + return true; + } + else + { + music->Release(); + } } return false; } diff --git a/core/transitions/Transition.cpp b/core/transitions/Transition.cpp index d13f4ed7..6ee62b9a 100644 --- a/core/transitions/Transition.cpp +++ b/core/transitions/Transition.cpp @@ -22,8 +22,8 @@ e2d::Transition::~Transition() { SafeRelease(out_layer_); SafeRelease(in_layer_); - GC::GetInstance()->SafeRelease(out_scene_); - GC::GetInstance()->SafeRelease(in_scene_); + SafeRelease(out_scene_); + SafeRelease(in_scene_); } bool e2d::Transition::IsDone() diff --git a/core/utils/Ref.cpp b/core/utils/Ref.cpp new file mode 100644 index 00000000..393fe7f0 --- /dev/null +++ b/core/utils/Ref.cpp @@ -0,0 +1,33 @@ +#include "..\e2dobject.h" + +e2d::Ref::Ref() + : ref_count_(0) +{ +} + +e2d::Ref::~Ref() +{ +} + +LONG e2d::Ref::Retain() +{ + return ::InterlockedIncrement(&ref_count_); +} + +LONG e2d::Ref::Release() +{ + LONG new_count = ::InterlockedDecrement(&ref_count_); + + if (new_count <= 0) + { + delete this; + return 0; + } + + return new_count; +} + +LONG e2d::Ref::GetRefCount() const +{ + return ref_count_; +} diff --git a/project/vs2012/Easy2D.vcxproj b/project/vs2012/Easy2D.vcxproj index b40c4c0a..a361043e 100644 --- a/project/vs2012/Easy2D.vcxproj +++ b/project/vs2012/Easy2D.vcxproj @@ -61,7 +61,6 @@ - @@ -84,6 +83,7 @@ + diff --git a/project/vs2012/Easy2D.vcxproj.filters b/project/vs2012/Easy2D.vcxproj.filters index 79dd0cd0..9c84c07e 100644 --- a/project/vs2012/Easy2D.vcxproj.filters +++ b/project/vs2012/Easy2D.vcxproj.filters @@ -32,20 +32,6 @@ {51864c81-02ee-4043-bf09-9ce3cbe5b6da} - - - - - - - - - - - - - - actions @@ -242,9 +228,6 @@ objects - - objects - objects @@ -254,5 +237,22 @@ objects + + utils + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index 2ff2a4ce..a2ccd3c1 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -205,7 +205,6 @@ - @@ -228,6 +227,7 @@ + diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index 79dd0cd0..9c84c07e 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -32,20 +32,6 @@ {51864c81-02ee-4043-bf09-9ce3cbe5b6da} - - - - - - - - - - - - - - actions @@ -242,9 +228,6 @@ objects - - objects - objects @@ -254,5 +237,22 @@ objects + + utils + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 8f8aeb37..83a4de8f 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -238,7 +238,6 @@ - @@ -261,6 +260,7 @@ + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 79dd0cd0..9c84c07e 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -32,20 +32,6 @@ {51864c81-02ee-4043-bf09-9ce3cbe5b6da} - - - - - - - - - - - - - - actions @@ -242,9 +228,6 @@ objects - - objects - objects @@ -254,5 +237,22 @@ objects + + utils + + + + + + + + + + + + + + + \ No newline at end of file