From ccba1363a02aea01f098131a82f171179f1d247f Mon Sep 17 00:00:00 2001 From: Haibo Date: Wed, 14 Nov 2018 01:34:41 +0800 Subject: [PATCH] add: use intrusive pointers to solve memory management problems minor improves --- core/base/Action.cpp | 106 ------------ core/base/{Action.h => Action.hpp} | 97 +++++------ core/base/ActionCombined.cpp | 132 ++++++-------- core/base/ActionCombined.h | 44 +++-- core/base/ActionFiniteTime.cpp | 190 ++++++++++----------- core/base/ActionFiniteTime.h | 84 ++++----- core/base/Animation.cpp | 78 ++++----- core/base/Animation.h | 28 ++- core/base/BaseTypes.h | 2 +- core/base/CallFunc.cpp | 8 +- core/base/CallFunc.h | 10 +- core/base/Game.cpp | 74 ++++---- core/base/Game.h | 36 ++-- core/base/Image.cpp | 5 +- core/base/Image.h | 1 - core/base/Input.cpp | 2 +- core/base/Input.h | 2 +- core/base/IntrusivePtr.hpp | 176 +++++++++++++++++++ core/base/Music.h | 1 - core/base/Node.cpp | 123 +++---------- core/base/Node.h | 28 +-- core/base/RefCounter.cpp | 14 +- core/base/{RefCounter.h => RefCounter.hpp} | 29 +++- core/base/Scene.cpp | 23 +-- core/base/Scene.h | 10 +- core/base/Sprite.cpp | 37 ++-- core/base/Sprite.h | 8 +- core/base/Task.cpp | 12 +- core/base/Task.h | 32 ++-- core/base/Transition.cpp | 30 ++-- core/base/Transition.h | 50 +++--- core/base/audio.cpp | 4 +- core/base/audio.h | 8 +- core/base/base.h | 64 +++++++ core/base/modules.cpp | 2 +- core/base/modules.h | 2 +- core/base/render.cpp | 7 +- core/base/render.h | 7 +- core/base/time.h | 5 + core/base/window.cpp | 2 +- core/base/window.h | 2 +- core/easy2d.h | 13 +- core/ui/Button.cpp | 20 ++- core/ui/Button.h | 40 ++--- core/ui/Menu.cpp | 8 +- core/ui/Menu.h | 11 +- core/utils/Player.cpp | 19 +-- core/utils/Player.h | 4 +- project/vs2013/Easy2D.vcxproj | 7 +- project/vs2013/Easy2D.vcxproj.filters | 21 +-- project/vs2015/Easy2D.vcxproj | 7 +- project/vs2015/Easy2D.vcxproj.filters | 21 +-- project/vs2017/Easy2D.vcxproj | 9 +- project/vs2017/Easy2D.vcxproj.filters | 21 +-- 54 files changed, 856 insertions(+), 920 deletions(-) delete mode 100644 core/base/Action.cpp rename core/base/{Action.h => Action.hpp} (59%) create mode 100644 core/base/IntrusivePtr.hpp rename core/base/{RefCounter.h => RefCounter.hpp} (73%) diff --git a/core/base/Action.cpp b/core/base/Action.cpp deleted file mode 100644 index 508a7cb7..00000000 --- a/core/base/Action.cpp +++ /dev/null @@ -1,106 +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 "Action.h" -#include "base.h" - -namespace easy2d -{ - Action::Action() - : running_(false), done_(false), initialized_(false), target_(nullptr) - { - } - - Action::~Action() - { - } - - bool Action::IsRunning() - { - return running_; - } - - void Action::Resume() - { - running_ = true; - } - - void Action::Pause() - { - running_ = false; - } - - void Action::Stop() - { - done_ = true; - } - - const String& Action::GetName() const - { - return name_; - } - - void Action::SetName(const String& name) - { - name_ = name; - } - - Node * Action::GetTarget() - { - return target_; - } - - void Action::Reset() - { - initialized_ = false; - done_ = false; - started_ = time::Now(); - } - - bool Action::IsDone() const - { - return done_; - } - - void Action::StartWithTarget(Node* target) - { - target_ = target; - running_ = true; - this->Reset(); - } - - void Action::Initialize() - { - initialized_ = true; - started_ = time::Now(); - } - - void Action::Update() - { - if (!initialized_) - { - Initialize(); - } - } - - void Action::ResetTime() - { - } -} diff --git a/core/base/Action.h b/core/base/Action.hpp similarity index 59% rename from core/base/Action.h rename to core/base/Action.hpp index 671fcaba..a79ca1da 100644 --- a/core/base/Action.h +++ b/core/base/Action.hpp @@ -21,85 +21,86 @@ #pragma once #include "base.h" #include "time.h" -#include "RefCounter.h" namespace easy2d { - class Node; - class Loop; - class Sequence; - class Spawn; - - // 基础动作 class Action : public RefCounter { + E2D_DISABLE_COPY(Action); + + friend class Node; friend class Loop; friend class Sequence; friend class Spawn; - E2D_DISABLE_COPY(Action); - public: - Action(); + Action() : running_(false), done_(false), initialized_(false) {} - virtual ~Action(); + virtual ~Action() {} // 获取动作运行状态 - virtual bool IsRunning(); + inline bool IsRunning() { return running_; } // 继续动作 - virtual void Resume(); + inline void Resume() { running_ = true; } // 暂停动作 - virtual void Pause(); + inline void Pause() { running_ = false; } // 停止动作 - virtual void Stop(); + inline void Stop() { done_ = true; } // 获取动作名称 - virtual const String& GetName() const; + inline const String& GetName() const { return name_; } // 设置动作名称 - virtual void SetName( - const String& name - ); + inline void SetName(const String& name) { name_ = name; } + + inline bool IsDone() const { return done_; } // 获取动作的拷贝 - virtual Action * Clone() const = 0; + virtual spAction Clone() const = 0; // 获取动作的倒转 - virtual Action * Reverse() const = 0; + virtual spAction Reverse() const = 0; // 重置动作 - virtual void Reset(); - - // 获取该动作的执行目标 - virtual Node * GetTarget(); - - // 开始动作 - virtual void StartWithTarget( - Node* target - ); - - // 初始化动作 - virtual void Initialize(); - - // 更新动作 - virtual void Update(); - - // 重置动作时间 - virtual void ResetTime(); - - // 获取动作结束状态 - virtual bool IsDone() const; + virtual void Reset() + { + initialized_ = false; + done_ = false; + started_ = time::Now(); + } protected: - String name_; - bool running_; - bool done_; - bool initialized_; - Node* target_; - time::TimePoint started_; + virtual void Start() + { + running_ = true; + this->Reset(); + } + + virtual void Init(Node* target) + { + initialized_ = true; + started_ = time::Now(); + } + + virtual void Update(Node* target) + { + if (!initialized_) + { + Init(target); + } + } + + virtual void ResetTime() {} + + protected: + String name_; + bool running_; + bool done_; + bool initialized_; + TimePoint started_; }; } diff --git a/core/base/ActionCombined.cpp b/core/base/ActionCombined.cpp index bc2df50c..47aacf0f 100644 --- a/core/base/ActionCombined.cpp +++ b/core/base/ActionCombined.cpp @@ -27,30 +27,25 @@ namespace easy2d // Loop //------------------------------------------------------- - Loop::Loop(Action * action, int times) + Loop::Loop(spAction const& action, int times) : action_(action) , times_(0) , total_times_(times) { - E2D_WARNING_IF(action == nullptr, "Loop NULL pointer exception!"); + E2D_WARNING_IF(!action, "Loop NULL pointer exception!"); - if (action) - { - action_ = action; - action_->Retain(); - } + action_ = action; } Loop::~Loop() { - SafeRelease(action_); } - Loop * Loop::Clone() const + spAction Loop::Clone() const { if (action_) { - return new Loop(action_->Clone()); + return new (std::nothrow) Loop(action_->Clone()); } else { @@ -58,11 +53,11 @@ namespace easy2d } } - Loop * Loop::Reverse() const + spAction Loop::Reverse() const { if (action_) { - return new Loop(action_->Clone()); + return new (std::nothrow) Loop(action_->Clone()); } else { @@ -70,20 +65,19 @@ namespace easy2d } } - void Loop::Initialize() + void Loop::Init(Node* target) { - Action::Initialize(); + Action::Init(target); if (action_) { - action_->target_ = target_; - action_->Initialize(); + action_->Init(target); } } - void Loop::Update() + void Loop::Update(Node* target) { - Action::Update(); + Action::Update(target); if (times_ == total_times_) { @@ -93,7 +87,7 @@ namespace easy2d if (action_) { - action_->Update(); + action_->Update(target); if (action_->IsDone()) { @@ -140,33 +134,20 @@ namespace easy2d Sequence::~Sequence() { - for (auto action : actions_) - { - SafeRelease(action); - } } - void Sequence::Initialize() + void Sequence::Init(Node* target) { - Action::Initialize(); - // 将所有动作与目标绑定 - if (target_) - { - for (const auto& action : actions_) - { - action->target_ = target_; - } - } - // 初始化第一个动作 - actions_[0]->Initialize(); + Action::Init(target); + actions_[0]->Init(target); } - void Sequence::Update() + void Sequence::Update(Node* target) { - Action::Update(); + Action::Update(target); auto &action = actions_[action_index_]; - action->Update(); + action->Update(target); if (action->IsDone()) { @@ -177,7 +158,7 @@ namespace easy2d } else { - actions_[action_index_]->Initialize(); + actions_[action_index_]->Init(target); } } } @@ -200,12 +181,11 @@ namespace easy2d } } - void Sequence::Add(Action * action) + void Sequence::Add(spAction const& action) { if (action) { actions_.push_back(action); - action->Retain(); } } @@ -217,30 +197,32 @@ namespace easy2d } } - Sequence * Sequence::Clone() const + spAction Sequence::Clone() const { - auto sequence = new Sequence(); - for (const auto& action : actions_) + auto sequence = new (std::nothrow) Sequence(); + if (sequence) { - if (action) + for (const auto& action : actions_) { - sequence->Add(action->Clone()); + if (action) + { + sequence->Add(action->Clone()); + } } } return sequence; } - Sequence * Sequence::Reverse() const + spAction Sequence::Reverse() const { - auto sequence = new Sequence(); + auto sequence = new (std::nothrow) Sequence(); if (sequence && !actions_.empty()) { - std::vector newActions(actions_.size()); - for (auto iter = actions_.crbegin(), iterCrend = actions_.crend(); iter != iterCrend; ++iter) + for (auto iter = actions_.crbegin(), crend = actions_.crend(); iter != crend; ++iter) { - newActions.push_back((*iter)->Reverse()); + if (*iter) + sequence->Add((*iter)->Reverse()); } - sequence->Add(newActions); } return sequence; } @@ -261,29 +243,24 @@ namespace easy2d Spawn::~Spawn() { - for (auto action : actions_) - { - SafeRelease(action); - } } - void Spawn::Initialize() + void Spawn::Init(Node* target) { - Action::Initialize(); + Action::Init(target); - if (target_) + if (target) { for (const auto& action : actions_) { - action->target_ = target_; - action->Initialize(); + action->Init(target); } } } - void Spawn::Update() + void Spawn::Update(Node* target) { - Action::Update(); + Action::Update(target); size_t done_num = 0; for (const auto& action : actions_) @@ -294,7 +271,7 @@ namespace easy2d } else { - action->Update(); + action->Update(target); } } @@ -321,12 +298,11 @@ namespace easy2d } } - void Spawn::Add(Action * action) + void Spawn::Add(spAction const& action) { if (action) { actions_.push_back(action); - action->Retain(); } } @@ -338,30 +314,32 @@ namespace easy2d } } - Spawn * Spawn::Clone() const + spAction Spawn::Clone() const { - auto spawn = new Spawn(); - for (const auto& action : actions_) + auto spawn = new (std::nothrow) Spawn(); + if (spawn) { - if (action) + for (const auto& action : actions_) { - spawn->Add(action->Clone()); + if (action) + { + spawn->Add(action->Clone()); + } } } return spawn; } - Spawn * Spawn::Reverse() const + spAction Spawn::Reverse() const { - auto spawn = new Spawn(); + auto spawn = new (std::nothrow) Spawn(); if (spawn && !actions_.empty()) { - std::vector newActions(actions_.size()); - for (auto iter = actions_.crbegin(), iterCrend = actions_.crend(); iter != iterCrend; ++iter) + for (auto iter = actions_.crbegin(), crend = actions_.crend(); iter != crend; ++iter) { - newActions.push_back((*iter)->Reverse()); + if (*iter) + spawn->Add((*iter)->Reverse()); } - spawn->Add(newActions); } return spawn; } diff --git a/core/base/ActionCombined.h b/core/base/ActionCombined.h index abb87d3e..39bd6397 100644 --- a/core/base/ActionCombined.h +++ b/core/base/ActionCombined.h @@ -19,7 +19,7 @@ // THE SOFTWARE. #pragma once -#include "Action.h" +#include "Action.hpp" namespace easy2d { @@ -31,35 +31,35 @@ namespace easy2d public: explicit Loop( - Action * action, /* 执行循环的动作 */ - int times = -1 /* 循环次数 */ + spAction const& action, /* 执行循环的动作 */ + int times = -1 /* 循环次数 */ ); virtual ~Loop(); // 获取该动作的拷贝对象 - virtual Loop * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual Loop * Reverse() const override; + virtual spAction Reverse() const override; // 重置动作 virtual void Reset() override; protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; // 更新动作 - virtual void Update() override; + virtual void Update(Node* target) override; // 重置动作时间 virtual void ResetTime() override; protected: - Action * action_; - int times_; - int total_times_; + spAction action_; + int times_; + int total_times_; }; @@ -70,8 +70,6 @@ namespace easy2d E2D_DISABLE_COPY(Sequence); public: - typedef std::vector Actions; - Sequence(); explicit Sequence( @@ -82,7 +80,7 @@ namespace easy2d // 在结尾添加动作 void Add( - Action * action + spAction const& action ); // 在结尾添加多个动作 @@ -91,20 +89,20 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual Sequence * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual Sequence * Reverse() const; + virtual spAction Reverse() const override; // 重置动作 virtual void Reset() override; protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; // 更新动作 - virtual void Update() override; + virtual void Update(Node* target) override; // 重置动作时间 virtual void ResetTime() override; @@ -122,8 +120,6 @@ namespace easy2d E2D_DISABLE_COPY(Spawn); public: - typedef std::vector Actions; - Spawn(); explicit Spawn( @@ -134,7 +130,7 @@ namespace easy2d // 在结尾添加动作 void Add( - Action * action + spAction const& action ); // 在结尾添加多个动作 @@ -143,20 +139,20 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual Spawn * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual Spawn * Reverse() const; + virtual spAction Reverse() const; // 重置动作 virtual void Reset() override; protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; // 更新动作 - virtual void Update() override; + virtual void Update(Node* target) override; // 重置动作时间 virtual void ResetTime() override; diff --git a/core/base/ActionFiniteTime.cpp b/core/base/ActionFiniteTime.cpp index 8aadd7cc..22653f70 100644 --- a/core/base/ActionFiniteTime.cpp +++ b/core/base/ActionFiniteTime.cpp @@ -41,14 +41,14 @@ namespace easy2d delta_ = 0; } - void FiniteTimeAction::Initialize() + void FiniteTimeAction::Init(Node* target) { - Action::Initialize(); + Action::Init(target); } - void FiniteTimeAction::Update() + void FiniteTimeAction::Update(Node* target) { - Action::Update(); + Action::Update(target); if (duration_ == 0) { @@ -83,41 +83,41 @@ namespace easy2d delta_pos_ = vector; } - void MoveBy::Initialize() + void MoveBy::Init(Node* target) { - FiniteTimeAction::Initialize(); + FiniteTimeAction::Init(target); - if (target_) + if (target) { - prev_pos_ = start_pos_ = target_->GetPosition(); + prev_pos_ = start_pos_ = target->GetPosition(); } } - void MoveBy::Update() + void MoveBy::Update(Node* target) { - FiniteTimeAction::Update(); + FiniteTimeAction::Update(target); - if (target_) + if (target) { - Point currentPos = target_->GetPosition(); + Point currentPos = target->GetPosition(); Point diff = currentPos - prev_pos_; start_pos_ = start_pos_ + diff; Point newPos = start_pos_ + (delta_pos_ * delta_); - target_->SetPosition(newPos); + target->SetPosition(newPos); prev_pos_ = newPos; } } - MoveBy * MoveBy::Clone() const + spAction MoveBy::Clone() const { - return new MoveBy(duration_, delta_pos_); + return new (std::nothrow) MoveBy(duration_, delta_pos_); } - MoveBy * MoveBy::Reverse() const + spAction MoveBy::Reverse() const { - return new MoveBy(duration_, -delta_pos_); + return new (std::nothrow) MoveBy(duration_, -delta_pos_); } MoveTo::MoveTo(float duration, Point pos) @@ -126,14 +126,14 @@ namespace easy2d end_pos_ = pos; } - MoveTo * MoveTo::Clone() const + spAction MoveTo::Clone() const { - return new MoveTo(duration_, end_pos_); + return new (std::nothrow) MoveTo(duration_, end_pos_); } - void MoveTo::Initialize() + void MoveTo::Init(Node* target) { - MoveBy::Initialize(); + MoveBy::Init(target); delta_pos_ = end_pos_ - start_pos_; } @@ -150,44 +150,44 @@ namespace easy2d { } - JumpBy * JumpBy::Clone() const + spAction JumpBy::Clone() const { - return new JumpBy(duration_, delta_pos_, height_, jumps_); + return new (std::nothrow) JumpBy(duration_, delta_pos_, height_, jumps_); } - JumpBy * JumpBy::Reverse() const + spAction JumpBy::Reverse() const { - return new JumpBy(duration_, -delta_pos_, height_, jumps_); + return new (std::nothrow) JumpBy(duration_, -delta_pos_, height_, jumps_); } - void JumpBy::Initialize() + void JumpBy::Init(Node* target) { - FiniteTimeAction::Initialize(); + FiniteTimeAction::Init(target); - if (target_) + if (target) { - prev_pos_ = start_pos_ = target_->GetPosition(); + prev_pos_ = start_pos_ = target->GetPosition(); } } - void JumpBy::Update() + void JumpBy::Update(Node* target) { - FiniteTimeAction::Update(); + FiniteTimeAction::Update(target); - if (target_) + if (target) { float frac = fmod(delta_ * jumps_, 1.f); float x = delta_pos_.x * delta_; float y = height_ * 4 * frac * (1 - frac); y += delta_pos_.y * delta_; - Point currentPos = target_->GetPosition(); + Point currentPos = target->GetPosition(); Point diff = currentPos - prev_pos_; start_pos_ = diff + start_pos_; Point newPos = start_pos_ + Point(x, y); - target_->SetPosition(newPos); + target->SetPosition(newPos); prev_pos_ = newPos; } @@ -199,14 +199,14 @@ namespace easy2d { } - JumpTo * JumpTo::Clone() const + spAction JumpTo::Clone() const { - return new JumpTo(duration_, end_pos_, height_, jumps_); + return new (std::nothrow) JumpTo(duration_, end_pos_, height_, jumps_); } - void JumpTo::Initialize() + void JumpTo::Init(Node* target) { - JumpBy::Initialize(); + JumpBy::Init(target); delta_pos_ = end_pos_ - start_pos_; } @@ -229,35 +229,35 @@ namespace easy2d delta_y_ = scale_y; } - void ScaleBy::Initialize() + void ScaleBy::Init(Node* target) { - FiniteTimeAction::Initialize(); + FiniteTimeAction::Init(target); - if (target_) + if (target) { - start_scale_x_ = target_->GetScaleX(); - start_scale_y_ = target_->GetScaleY(); + start_scale_x_ = target->GetScaleX(); + start_scale_y_ = target->GetScaleY(); } } - void ScaleBy::Update() + void ScaleBy::Update(Node* target) { - FiniteTimeAction::Update(); + FiniteTimeAction::Update(target); - if (target_) + if (target) { - target_->SetScale(start_scale_x_ + delta_x_ * delta_, start_scale_y_ + delta_y_ * delta_); + target->SetScale(start_scale_x_ + delta_x_ * delta_, start_scale_y_ + delta_y_ * delta_); } } - ScaleBy * ScaleBy::Clone() const + spAction ScaleBy::Clone() const { - return new ScaleBy(duration_, delta_x_, delta_y_); + return new (std::nothrow) ScaleBy(duration_, delta_x_, delta_y_); } - ScaleBy * ScaleBy::Reverse() const + spAction ScaleBy::Reverse() const { - return new ScaleBy(duration_, -delta_x_, -delta_y_); + return new (std::nothrow) ScaleBy(duration_, -delta_x_, -delta_y_); } ScaleTo::ScaleTo(float duration, float scale) @@ -274,14 +274,14 @@ namespace easy2d end_scale_y_ = scale_y; } - ScaleTo * ScaleTo::Clone() const + spAction ScaleTo::Clone() const { - return new ScaleTo(duration_, end_scale_x_, end_scale_y_); + return new (std::nothrow) ScaleTo(duration_, end_scale_x_, end_scale_y_); } - void ScaleTo::Initialize() + void ScaleTo::Init(Node* target) { - ScaleBy::Initialize(); + ScaleBy::Init(target); delta_x_ = end_scale_x_ - start_scale_x_; delta_y_ = end_scale_y_ - start_scale_y_; } @@ -297,34 +297,34 @@ namespace easy2d delta_val_ = opacity; } - void OpacityBy::Initialize() + void OpacityBy::Init(Node* target) { - FiniteTimeAction::Initialize(); + FiniteTimeAction::Init(target); - if (target_) + if (target) { - start_val_ = target_->GetOpacity(); + start_val_ = target->GetOpacity(); } } - void OpacityBy::Update() + void OpacityBy::Update(Node* target) { - FiniteTimeAction::Update(); + FiniteTimeAction::Update(target); - if (target_) + if (target) { - target_->SetOpacity(start_val_ + delta_val_ * delta_); + target->SetOpacity(start_val_ + delta_val_ * delta_); } } - OpacityBy * OpacityBy::Clone() const + spAction OpacityBy::Clone() const { - return new OpacityBy(duration_, delta_val_); + return new (std::nothrow) OpacityBy(duration_, delta_val_); } - OpacityBy * OpacityBy::Reverse() const + spAction OpacityBy::Reverse() const { - return new OpacityBy(duration_, -delta_val_); + return new (std::nothrow) OpacityBy(duration_, -delta_val_); } OpacityTo::OpacityTo(float duration, float opacity) @@ -333,14 +333,14 @@ namespace easy2d end_val_ = opacity; } - OpacityTo * OpacityTo::Clone() const + spAction OpacityTo::Clone() const { - return new OpacityTo(duration_, end_val_); + return new (std::nothrow) OpacityTo(duration_, end_val_); } - void OpacityTo::Initialize() + void OpacityTo::Init(Node* target) { - OpacityBy::Initialize(); + OpacityBy::Init(target); delta_val_ = end_val_ - start_val_; } @@ -365,34 +365,34 @@ namespace easy2d delta_val_ = rotation; } - void RotateBy::Initialize() + void RotateBy::Init(Node* target) { - FiniteTimeAction::Initialize(); + FiniteTimeAction::Init(target); - if (target_) + if (target) { - start_val_ = target_->GetRotation(); + start_val_ = target->GetRotation(); } } - void RotateBy::Update() + void RotateBy::Update(Node* target) { - FiniteTimeAction::Update(); + FiniteTimeAction::Update(target); - if (target_) + if (target) { - target_->SetRotation(start_val_ + delta_val_ * delta_); + target->SetRotation(start_val_ + delta_val_ * delta_); } } - RotateBy * RotateBy::Clone() const + spAction RotateBy::Clone() const { - return new RotateBy(duration_, delta_val_); + return new (std::nothrow) RotateBy(duration_, delta_val_); } - RotateBy * RotateBy::Reverse() const + spAction RotateBy::Reverse() const { - return new RotateBy(duration_, -delta_val_); + return new (std::nothrow) RotateBy(duration_, -delta_val_); } RotateTo::RotateTo(float duration, float rotation) @@ -401,14 +401,14 @@ namespace easy2d end_val_ = rotation; } - RotateTo * RotateTo::Clone() const + spAction RotateTo::Clone() const { - return new RotateTo(duration_, end_val_); + return new (std::nothrow) RotateTo(duration_, end_val_); } - void RotateTo::Initialize() + void RotateTo::Init(Node* target) { - RotateBy::Initialize(); + RotateBy::Init(target); delta_val_ = end_val_ - start_val_; } @@ -429,14 +429,14 @@ namespace easy2d delta_ = 0; } - void Delay::Initialize() + void Delay::Init(Node* target) { - Action::Initialize(); + Action::Init(target); } - void Delay::Update() + void Delay::Update(Node* target) { - Action::Update(); + Action::Update(target); delta_ = (time::Now() - started_).Seconds(); @@ -452,13 +452,13 @@ namespace easy2d started_ = time::Now() - time::Second * delta_; } - Delay * Delay::Clone() const + spAction Delay::Clone() const { - return new Delay(delay_); + return new (std::nothrow) Delay(delay_); } - Delay * Delay::Reverse() const + spAction Delay::Reverse() const { - return new Delay(delay_); + return new (std::nothrow) Delay(delay_); } } diff --git a/core/base/ActionFiniteTime.h b/core/base/ActionFiniteTime.h index 30b00596..bb30f6f4 100644 --- a/core/base/ActionFiniteTime.h +++ b/core/base/ActionFiniteTime.h @@ -19,7 +19,7 @@ // THE SOFTWARE. #pragma once -#include "Action.h" +#include "Action.hpp" namespace easy2d { @@ -40,10 +40,10 @@ namespace easy2d protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; // 更新动作 - virtual void Update() override; + virtual void Update(Node* target) override; // 重置动作时间 virtual void ResetTime() override; @@ -67,17 +67,17 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual MoveBy * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual MoveBy * Reverse() const override; + virtual spAction Reverse() const override; protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; // 更新动作 - virtual void Update() override; + virtual void Update(Node* target) override; protected: Point start_pos_; @@ -99,10 +99,10 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual MoveTo * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual MoveTo * Reverse() const override + virtual spAction Reverse() const override { E2D_WARNING("Reverse() not supported in MoveTo"); return nullptr; @@ -110,7 +110,7 @@ namespace easy2d protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; protected: Point end_pos_; @@ -132,17 +132,17 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual JumpBy * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual JumpBy * Reverse() const override; + virtual spAction Reverse() const override; protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; // 更新动作 - virtual void Update() override; + virtual void Update(Node* target) override; protected: Point start_pos_; @@ -168,10 +168,10 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual JumpTo * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual JumpTo * Reverse() const override + virtual spAction Reverse() const override { E2D_WARNING("Reverse() not supported in JumpTo"); return nullptr; @@ -179,7 +179,7 @@ namespace easy2d protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; protected: Point end_pos_; @@ -205,17 +205,17 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ScaleBy * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual ScaleBy * Reverse() const override; + virtual spAction Reverse() const override; protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; // 更新动作 - virtual void Update() override; + virtual void Update(Node* target) override; protected: float start_scale_x_; @@ -244,10 +244,10 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ScaleTo * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual ScaleTo * Reverse() const override + virtual spAction Reverse() const override { E2D_WARNING("Reverse() not supported in ScaleTo"); return nullptr; @@ -255,7 +255,7 @@ namespace easy2d protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; protected: float end_scale_x_; @@ -276,17 +276,17 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual OpacityBy * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual OpacityBy * Reverse() const override; + virtual spAction Reverse() const override; protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; // 更新动作 - virtual void Update() override; + virtual void Update(Node* target) override; protected: float start_val_; @@ -307,10 +307,10 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual OpacityTo * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual OpacityTo * Reverse() const override + virtual spAction Reverse() const override { E2D_WARNING("Reverse() not supported in OpacityTo"); return nullptr; @@ -318,7 +318,7 @@ namespace easy2d protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; protected: float end_val_; @@ -366,17 +366,17 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual RotateBy * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual RotateBy * Reverse() const override; + virtual spAction Reverse() const override; protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; // 更新动作 - virtual void Update() override; + virtual void Update(Node* target) override; protected: float start_val_; @@ -397,10 +397,10 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual RotateTo * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual RotateTo * Reverse() const override + virtual spAction Reverse() const override { E2D_WARNING("Reverse() not supported in RotateTo"); return nullptr; @@ -408,7 +408,7 @@ namespace easy2d protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; protected: float end_val_; @@ -427,20 +427,20 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual Delay * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual Delay * Reverse() const override; + virtual spAction Reverse() const override; // 重置动作 virtual void Reset() override; protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; // 更新动作 - virtual void Update() override; + virtual void Update(Node* target) override; // 重置动作时间 virtual void ResetTime() override; diff --git a/core/base/Animation.cpp b/core/base/Animation.cpp index 6670e0df..100c91ea 100644 --- a/core/base/Animation.cpp +++ b/core/base/Animation.cpp @@ -33,7 +33,7 @@ namespace easy2d { } - Animate::Animate(Animation * animation) + Animate::Animate(spAnimation const& animation) : frame_index_(0) , animation_(nullptr) { @@ -42,43 +42,37 @@ namespace easy2d Animate::~Animate() { - SafeRelease(animation_); } - Animation * Animate::GetAnimation() const + spAnimation Animate::GetAnimation() const { return animation_; } - void Animate::SetAnimation(Animation * animation) + void Animate::SetAnimation(spAnimation const& animation) { if (animation && animation != animation_) { - if (animation_) - { - animation_->Release(); - } animation_ = animation; - animation_->Retain(); frame_index_ = 0; } } - void Animate::Initialize() + void Animate::Init(Node* target) { - Action::Initialize(); + Action::Init(target); - auto target = dynamic_cast(target_); - if (target && animation_) + auto sprite_target = dynamic_cast(target); + if (sprite_target && animation_) { - target->Load(animation_->GetFrames()[frame_index_]); + sprite_target->Load(animation_->GetFrames()[frame_index_]); ++frame_index_; } } - void Animate::Update() + void Animate::Update(Node* target) { - Action::Update(); + Action::Update(target); if (!animation_) { @@ -89,11 +83,11 @@ namespace easy2d while ((time::Now() - started_).Seconds() >= animation_->GetInterval()) { auto& frames = animation_->GetFrames(); - auto target = dynamic_cast(target_); + auto sprite_target = dynamic_cast(target); - if (target) + if (sprite_target) { - target->Load(frames[frame_index_]); + sprite_target->Load(frames[frame_index_]); } started_ += time::Second * animation_->GetInterval(); @@ -118,23 +112,23 @@ namespace easy2d frame_index_ = 0; } - Animate * Animate::Clone() const + spAction Animate::Clone() const { if (animation_) { - return new Animate(animation_); + return new (std::nothrow) Animate(animation_); } return nullptr; } - Animate * Animate::Reverse() const + spAction Animate::Reverse() const { if (animation_) { auto animation = animation_->Reverse(); if (animation) { - return new Animate(animation); + return new (std::nothrow) Animate(animation); } } return nullptr; @@ -169,10 +163,6 @@ namespace easy2d Animation::~Animation() { - for (auto frame : frames_) - { - SafeRelease(frame); - } } void Animation::SetInterval(float interval) @@ -180,13 +170,12 @@ namespace easy2d interval_ = std::max(interval, 0.f); } - void Animation::Add(Image * frame) + void Animation::Add(spImage const& frame) { - E2D_WARNING_IF(frame == nullptr, "Animation::Add failed, frame Is nullptr."); + E2D_WARNING_IF(!frame, "Animation::Add failed, frame Is nullptr."); if (frame) { frames_.push_back(frame); - frame->Retain(); } } @@ -203,14 +192,14 @@ namespace easy2d return interval_; } - const Animation::Images& Animation::GetFrames() const + const Images& Animation::GetFrames() const { return frames_; } - Animation * Animation::Clone() const + spAnimation Animation::Clone() const { - auto animation = new Animation(interval_); + auto animation = new (std::nothrow) Animation(interval_); if (animation) { for (const auto& frame : frames_) @@ -221,26 +210,17 @@ namespace easy2d return animation; } - Animation * Animation::Reverse() const + spAnimation Animation::Reverse() const { - auto& oldFrames = this->GetFrames(); - Images frames(oldFrames.size()); - - if (!oldFrames.empty()) + auto animation = new (std::nothrow) Animation(interval_); + if (!frames_.empty()) { - for (auto iter = oldFrames.crbegin(), - iterCrend = oldFrames.crend(); - iter != iterCrend; - ++iter) + for (auto iter = frames_.crbegin(), crend = frames_.crend(); iter != crend; ++iter) { - Image* frame = *iter; - if (frame) - { - frames.push_back(frame); - } + if (*iter) + animation->Add(*iter); } } - - return new Animation(this->GetInterval(), frames); + return animation; } } \ No newline at end of file diff --git a/core/base/Animation.h b/core/base/Animation.h index 0ad819ac..da88ad36 100644 --- a/core/base/Animation.h +++ b/core/base/Animation.h @@ -19,7 +19,7 @@ // THE SOFTWARE. #pragma once -#include "Action.h" +#include "Action.hpp" #include "Image.h" namespace easy2d @@ -31,8 +31,6 @@ namespace easy2d E2D_DISABLE_COPY(Animation); public: - typedef std::vector Images; - Animation(); explicit Animation( @@ -52,7 +50,7 @@ namespace easy2d // 添加关键帧 void Add( - Image * frame /* 关键帧 */ + spImage const& frame /* 关键帧 */ ); // 添加多个关键帧 @@ -72,10 +70,10 @@ namespace easy2d ); // 获取帧动画的拷贝对象 - Animation * Clone() const; + spAnimation Clone() const; // 获取帧动画的倒转 - Animation * Reverse() const; + spAnimation Reverse() const; protected: float interval_; @@ -93,40 +91,40 @@ namespace easy2d Animate(); explicit Animate( - Animation * animation + spAnimation const& animation ); virtual ~Animate(); // 获取动画 - virtual Animation * GetAnimation() const; + spAnimation GetAnimation() const; // 设置动画 - virtual void SetAnimation( - Animation * animation + void SetAnimation( + spAnimation const& animation ); // 获取该动作的拷贝对象 - virtual Animate * Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual Animate * Reverse() const override; + virtual spAction Reverse() const override; // 重置动作 virtual void Reset() override; protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node* target) override; // 更新动作 - virtual void Update() override; + virtual void Update(Node* target) override; // 重置动作时间 virtual void ResetTime() override; protected: UINT frame_index_; - Animation * animation_; + spAnimation animation_; }; } diff --git a/core/base/BaseTypes.h b/core/base/BaseTypes.h index 26bacbc9..f9af103d 100644 --- a/core/base/BaseTypes.h +++ b/core/base/BaseTypes.h @@ -20,10 +20,10 @@ #pragma once #include "macros.h" -#include "../math/vector.hpp" #include "Color.h" #include "Size.h" #include "Rect.hpp" +#include "../math/vector.hpp" namespace easy2d { diff --git a/core/base/CallFunc.cpp b/core/base/CallFunc.cpp index 2edcc816..f9e1f10c 100644 --- a/core/base/CallFunc.cpp +++ b/core/base/CallFunc.cpp @@ -27,21 +27,21 @@ namespace easy2d { } - CallFunc * CallFunc::Clone() const + spAction CallFunc::Clone() const { return new CallFunc(callback_); } - CallFunc * CallFunc::Reverse() const + spAction CallFunc::Reverse() const { return new CallFunc(callback_); } - void CallFunc::Initialize() + void CallFunc::Init(Node*) { } - void CallFunc::Update() + void CallFunc::Update(Node*) { callback_(); this->Stop(); diff --git a/core/base/CallFunc.h b/core/base/CallFunc.h index e90c27b4..fa8e5fb1 100644 --- a/core/base/CallFunc.h +++ b/core/base/CallFunc.h @@ -19,7 +19,7 @@ // THE SOFTWARE. #pragma once -#include "Action.h" +#include "Action.hpp" #include namespace easy2d @@ -38,17 +38,17 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual CallFunc *Clone() const override; + virtual spAction Clone() const override; // 获取该动作的倒转 - virtual CallFunc *Reverse() const override; + virtual spAction Reverse() const override; protected: // 初始化动作 - virtual void Initialize() override; + virtual void Init(Node*) override; // 更新动作 - virtual void Update() override; + virtual void Update(Node*) override; protected: Callback callback_; diff --git a/core/base/Game.cpp b/core/base/Game.cpp index 95d2d44c..74cd2f8f 100644 --- a/core/base/Game.cpp +++ b/core/base/Game.cpp @@ -39,33 +39,37 @@ namespace easy2d , curr_scene_(nullptr) , next_scene_(nullptr) , transition_(nullptr) - , debug_mode_(false) + , debug_enabled_(false) + , initialized_(false) { ::CoInitialize(nullptr); } + Game::Game(Options const & options) + : Game() + { + Init(options); + } + Game::~Game() { - SafeRelease(transition_); - SafeRelease(curr_scene_); - SafeRelease(next_scene_); - ::CoUninitialize(); } - void Game::Initialize(const Options& options) + void Game::Init(const Options& options) { - debug_mode_ = options.debug; + if (initialized_) + return; - Window::Instance().Initialize(options.title, options.width, options.height, options.icon, debug_mode_); - devices::Graphics::Instance().Initialize(Window::Instance().GetHandle(), debug_mode_); - devices::Input::Instance().Initialize(debug_mode_); - devices::Audio::Instance().Initialize(debug_mode_); + debug_enabled_ = options.debug; + + Window::Instance().Init(options.title, options.width, options.height, options.icon, debug_enabled_); + devices::Graphics::Instance().Init(Window::Instance().GetHandle(), debug_enabled_); + devices::Input::Instance().Init(debug_enabled_); + devices::Audio::Instance().Init(debug_enabled_); - // 若开启了调试模式,打开控制台 HWND console = ::GetConsoleWindow(); - // 关闭控制台 - if (debug_mode_) + if (debug_enabled_) { if (console == nullptr) { @@ -97,6 +101,8 @@ namespace easy2d GWLP_USERDATA, PtrToUlong(this) ); + + initialized_ = true; } void Game::Run() @@ -110,12 +116,13 @@ namespace easy2d next_scene_ = nullptr; } - ::ShowWindow(Window::Instance().GetHandle(), SW_SHOWNORMAL); - ::UpdateWindow(Window::Instance().GetHandle()); + const auto& window = Window::Instance(); + ::ShowWindow(window.GetHandle(), SW_SHOWNORMAL); + ::UpdateWindow(window.GetHandle()); const int64_t min_interval = 5; auto last = time::Now(); - MSG msg = { 0 }; + MSG msg = {}; while (!quit_) { @@ -128,12 +135,11 @@ namespace easy2d last = now; devices::Input::Instance().Update( - Window::Instance().GetHandle(), - Window::Instance().GetContentScaleX(), - Window::Instance().GetContentScaleY() + window.GetHandle(), + window.GetContentScaleX(), + window.GetContentScaleY() ); - OnUpdate(dt); UpdateScene(dt); DrawScene(); @@ -162,9 +168,9 @@ namespace easy2d quit_ = true; } - void Game::EnterScene(Scene * scene, Transition * transition) + void Game::EnterScene(spScene const& scene, spTransition const& transition) { - if (scene == nullptr) + if (!scene) { E2D_WARNING("Next scene is null pointer!"); return; @@ -172,45 +178,37 @@ namespace easy2d if (curr_scene_ == scene) { return; } - if (next_scene_) - { - next_scene_->Release(); - } next_scene_ = scene; - next_scene_->Retain(); if (transition) { if (transition_) { transition_->Stop(); - transition_->Release(); } transition_ = transition; - transition_->Retain(); - - transition_->Initialize(curr_scene_, next_scene_, this); + transition_->Init(curr_scene_, next_scene_); } } - Scene * Game::GetCurrentScene() + spScene const& Game::GetCurrentScene() { return curr_scene_; } bool Game::IsTransitioning() const { - return transition_ != nullptr; + return transition_; } void Game::UpdateScene(float dt) { - auto update = [&](Scene * scene) -> void + auto update = [&](spScene const& scene) -> void { if (scene) { scene->OnUpdate(dt); - Node * root = scene->GetRoot(); + spNode const& root = scene->GetRoot(); if (root) { root->UpdateChildren(dt); @@ -227,7 +225,6 @@ namespace easy2d if (transition_->IsDone()) { - transition_->Release(); transition_ = nullptr; } else @@ -241,7 +238,6 @@ namespace easy2d if (curr_scene_) { curr_scene_->OnExit(); - curr_scene_->Release(); } next_scene_->OnEnter(); @@ -265,7 +261,7 @@ namespace easy2d curr_scene_->Draw(); } - if (debug_mode_) + if (debug_enabled_) { if (curr_scene_ && curr_scene_->GetRoot()) { diff --git a/core/base/Game.h b/core/base/Game.h index 5741968d..38e512b0 100644 --- a/core/base/Game.h +++ b/core/base/Game.h @@ -21,12 +21,11 @@ #pragma once #include "base.h" #include "window.h" +#include "Scene.h" +#include "Transition.h" namespace easy2d { - class Scene; - class Transition; - struct Options { String title; /* 标题 */ @@ -52,10 +51,11 @@ namespace easy2d public: Game(); - virtual ~Game(); + Game( + Options const& options + ); - // 更新时 - virtual void OnUpdate(float dt) {} + virtual ~Game(); // 退出时 virtual void OnExit() {} @@ -65,8 +65,8 @@ namespace easy2d virtual bool OnClose() { return true; } // 初始化 - void Initialize( - const Options& options /* 属性 */ + void Init( + Options const& options ); // 运行 @@ -77,29 +77,27 @@ namespace easy2d // 切换场景 void EnterScene( - Scene * scene, /* 场景 */ - Transition * transition = nullptr /* 场景动画 */ + spScene const& scene, /* 场景 */ + spTransition const& transition = nullptr /* 场景动画 */ ); // 获取当前场景 - Scene * GetCurrentScene(); + spScene const& GetCurrentScene(); - // 是否正在进行场景过渡 bool IsTransitioning() const; - // 渲染场景画面 void DrawScene(); - // 更新场景 void UpdateScene( float dt ); private: - bool debug_mode_; - bool quit_; - Scene* curr_scene_; - Scene* next_scene_; - Transition* transition_; + bool initialized_; + bool debug_enabled_; + bool quit_; + spScene curr_scene_; + spScene next_scene_; + spTransition transition_; }; } diff --git a/core/base/Image.cpp b/core/base/Image.cpp index e88d2cd2..0c4ba8f6 100644 --- a/core/base/Image.cpp +++ b/core/base/Image.cpp @@ -192,10 +192,7 @@ namespace easy2d if (bitmap_ == bitmap) return; - if (bitmap_) - { - bitmap_->Release(); - } + SafeRelease(bitmap_); if (bitmap) { diff --git a/core/base/Image.h b/core/base/Image.h index cd902348..2ec999a4 100644 --- a/core/base/Image.h +++ b/core/base/Image.h @@ -21,7 +21,6 @@ #pragma once #include "base.h" #include "Resource.h" -#include "RefCounter.h" namespace easy2d { diff --git a/core/base/Input.cpp b/core/base/Input.cpp index e6666ce4..a6a4e95e 100644 --- a/core/base/Input.cpp +++ b/core/base/Input.cpp @@ -34,7 +34,7 @@ namespace easy2d { } - void InputDevice::Initialize(bool debug) + void InputDevice::Init(bool debug) { if (initialized) return; diff --git a/core/base/Input.h b/core/base/Input.h index b098b4d4..2e875570 100644 --- a/core/base/Input.h +++ b/core/base/Input.h @@ -33,7 +33,7 @@ namespace easy2d E2D_DISABLE_COPY(InputDevice); public: - void Initialize(bool debug); + void Init(bool debug); // 检测键盘某按键是否正被按下 bool IsDown( diff --git a/core/base/IntrusivePtr.hpp b/core/base/IntrusivePtr.hpp new file mode 100644 index 00000000..378818dd --- /dev/null +++ b/core/base/IntrusivePtr.hpp @@ -0,0 +1,176 @@ +// Copyright (c) 2016-2018 Easy2D - Nomango +// +// Permission is hereby granted, free of charge, to any person obtaining lhs 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. + +#pragma once +#include + +namespace easy2d +{ + template + class IntrusivePtr + { + using ElemType = T; + + ElemType* ptr_{ nullptr }; + + public: + IntrusivePtr() {} + + IntrusivePtr(nullptr_t) {} + + IntrusivePtr(ElemType* p) : ptr_(p) + { + IntrusivePtrAddRef(ptr_); + } + + IntrusivePtr(const IntrusivePtr& other) : ptr_(other.ptr_) + { + IntrusivePtrAddRef(ptr_); + } + + template + IntrusivePtr(const IntrusivePtr& other) : ptr_(other.Get()) + { + IntrusivePtrAddRef(ptr_); + } + + IntrusivePtr(IntrusivePtr&& other) : ptr_(::std::move(other.ptr_)) {} + + ~IntrusivePtr() + { + IntrusivePtrRelease(ptr_); + } + + inline ElemType* Get() const { return ptr_; } + + inline void Swap(IntrusivePtr& other) + { + ::std::swap(ptr_, other.ptr_); + } + + inline ElemType* operator ->() const + { + assert(ptr_ && ptr_->GetRefCount() > 0 && + "Invalid pointer!"); + return ptr_; + } + + inline ElemType& operator *() const + { + assert(ptr_ && ptr_->GetRefCount() > 0 && + "Invalid pointer!"); + return *ptr_; + } + + inline operator bool() const { return ptr_ != nullptr; } + + inline bool operator !() const { return ptr_ == 0; } + + inline IntrusivePtr& operator =(const IntrusivePtr& other) + { + IntrusivePtr(other).Swap(*this); + return *this; + } + + inline IntrusivePtr& operator =(ElemType* p) + { + IntrusivePtr(p).Swap(*this); + return *this; + } + + inline IntrusivePtr& operator =(nullptr_t) + { + IntrusivePtr{}.Swap(*this); + return *this; + } + }; + + template + inline bool operator==(IntrusivePtr const& lhs, IntrusivePtr const& rhs) + { + return lhs.Get() == rhs.Get(); + } + + template + inline bool operator!=(IntrusivePtr const& lhs, IntrusivePtr const& rhs) + { + return lhs.Get() != rhs.Get(); + } + + template + inline bool operator<(IntrusivePtr const& lhs, IntrusivePtr const& rhs) + { + return lhs.Get() < rhs.Get(); + } + + template + inline bool operator==(IntrusivePtr const& lhs, T* rhs) + { + return lhs.Get() == rhs; + } + + template + inline bool operator!=(IntrusivePtr const& lhs, T* rhs) + { + return lhs.Get() != rhs; + } + + template + inline bool operator==(T* lhs, IntrusivePtr const& rhs) + { + return lhs == rhs.Get(); + } + + template + inline bool operator!=(T* lhs, IntrusivePtr const& rhs) + { + return lhs != rhs.Get(); + } + + template + inline bool operator==(IntrusivePtr const& lhs, nullptr_t) + { + return !static_cast(lhs); + } + + template + inline bool operator!=(IntrusivePtr const& lhs, nullptr_t) + { + return static_cast(lhs); + } + + template + inline bool operator==(nullptr_t, IntrusivePtr const& rhs) + { + return !static_cast(rhs); + } + + template + inline bool operator!=(nullptr_t, IntrusivePtr const& rhs) + { + return static_cast(rhs); + } + + template + inline IntrusivePtr make_intrusive(T* ptr) + { + return IntrusivePtr(ptr); + } +} diff --git a/core/base/Music.h b/core/base/Music.h index efb4c1cb..b79c280f 100644 --- a/core/base/Music.h +++ b/core/base/Music.h @@ -21,7 +21,6 @@ #pragma once #include "base.h" #include "audio.h" -#include "RefCounter.h" #include "Resource.h" #include diff --git a/core/base/Node.cpp b/core/base/Node.cpp index 28ee8856..a48ada18 100644 --- a/core/base/Node.cpp +++ b/core/base/Node.cpp @@ -21,7 +21,7 @@ #include "Node.h" #include "Scene.h" #include "Task.h" -#include "Action.h" +#include "Action.hpp" #include "time.h" #include "render.h" #include @@ -31,7 +31,6 @@ namespace easy2d Node::Node() : visible_(true) , parent_(nullptr) - , parent_scene_(nullptr) , hash_name_(0) , clip_enabled_(false) , dirty_sort_(false) @@ -53,21 +52,6 @@ namespace easy2d Node::~Node() { SafeRelease(border_); - - for (auto action : actions_) - { - SafeRelease(action); - } - - for (auto task : tasks_) - { - SafeRelease(task); - } - - for (auto child : children_) - { - SafeRelease(child); - } } void Node::Visit() @@ -93,7 +77,7 @@ namespace easy2d std::sort( std::begin(children_), std::end(children_), - [](Node * n1, Node * n2) { return n1->GetOrder() < n2->GetOrder(); } + [](spNode const& n1, spNode const& n2) { return n1->GetOrder() < n2->GetOrder(); } ); dirty_sort_ = false; @@ -202,11 +186,6 @@ namespace easy2d initial_matrix_ = initial_matrix_ * parent_->initial_matrix_; final_matrix_ = final_matrix_ * parent_->initial_matrix_; } - else if (parent_scene_) - { - initial_matrix_ = initial_matrix_ * parent_scene_->GetTransform(); - final_matrix_ = final_matrix_ * parent_scene_->GetTransform(); - } // 重新构造轮廓 SafeRelease(border_); @@ -269,25 +248,24 @@ namespace easy2d if (actions_.empty()) return; - std::vector currActions; + std::vector currActions; currActions.reserve(actions_.size()); std::copy_if( actions_.begin(), actions_.end(), std::back_inserter(currActions), - [](Action* action) { return action->IsRunning() && !action->IsDone(); } + [](spAction action) { return action->IsRunning() && !action->IsDone(); } ); // 遍历所有正在运行的动作 for (const auto& action : currActions) - action->Update(); + action->Update(this); // 清除完成的动作 for (auto iter = actions_.begin(); iter != actions_.end();) { if ((*iter)->IsDone()) { - (*iter)->Release(); iter = actions_.erase(iter); } else @@ -574,18 +552,18 @@ namespace easy2d border_color_ = color; } - void Node::AddChild(Node * child, int order) + void Node::AddChild(spNode const& child, int order) { - E2D_WARNING_IF(child == nullptr, "Node::AddChild NULL pointer exception."); + E2D_WARNING_IF(!child, "Node::AddChild NULL pointer exception."); if (child) { - if (child->parent_ != nullptr) + if (child->parent_) { throw std::runtime_error("节点已有父节点, 不能再添加到其他节点"); } - for (Node * parent = this; parent != nullptr; parent = parent->GetParent()) + for (Node * parent = this; parent; parent = parent->GetParent().Get()) { if (child == parent) { @@ -593,14 +571,9 @@ namespace easy2d } } - child->Retain(); children_.push_back(child); child->SetOrder(order); child->parent_ = this; - if (this->parent_scene_) - { - child->SetParentScene(this->parent_scene_); - } // 更新子节点透明度 child->UpdateOpacity(); @@ -619,17 +592,12 @@ namespace easy2d } } - Node * Node::GetParent() const + spNode Node::GetParent() const { return parent_; } - Scene * Node::GetParentScene() const - { - return parent_scene_; - } - - Node::Nodes Node::GetChildren(const String& name) const + Nodes Node::GetChildren(const String& name) const { Nodes children; size_t hash_code = std::hash{}(name); @@ -645,7 +613,7 @@ namespace easy2d return children; } - Node * Node::GetChild(const String& name) const + spNode Node::GetChild(const String& name) const { size_t hash_code = std::hash{}(name); @@ -660,7 +628,7 @@ namespace easy2d return nullptr; } - const std::vector& Node::GetAllChildren() const + const std::vector& Node::GetAllChildren() const { return children_; } @@ -678,9 +646,9 @@ namespace easy2d } } - bool Node::RemoveChild(Node * child) + bool Node::RemoveChild(spNode const& child) { - E2D_WARNING_IF(child == nullptr, "Node::RemoveChildren NULL pointer exception."); + E2D_WARNING_IF(!child, "Node::RemoveChildren NULL pointer exception."); if (children_.empty()) { @@ -694,13 +662,6 @@ namespace easy2d { children_.erase(iter); child->parent_ = nullptr; - - if (child->parent_scene_) - { - child->SetParentScene(nullptr); - } - - child->Release(); return true; } } @@ -720,11 +681,6 @@ namespace easy2d if ((*iter)->hash_name_ == hash_code && (*iter)->name_ == child_name) { (*iter)->parent_ = nullptr; - if ((*iter)->parent_scene_) - { - (*iter)->SetParentScene(nullptr); - } - (*iter)->Release(); iter = children_.erase(iter); } else @@ -736,34 +692,20 @@ namespace easy2d void Node::RemoveAllChildren() { - // 所有节点的引用计数减一 - for (const auto& child : children_) - { - child->Release(); - } - // 清空储存节点的容器 children_.clear(); } - void Node::RunAction(Action * action) + void Node::RunAction(spAction const& action) { - E2D_WARNING_IF(action == nullptr, "Action NULL pointer exception!"); + E2D_WARNING_IF(!action, "Action NULL pointer exception!"); if (action) { - if (action->GetTarget() == nullptr) + auto iter = std::find(actions_.begin(), actions_.end(), action); + if (iter == actions_.end()) { - auto iter = std::find(actions_.begin(), actions_.end(), action); - if (iter == actions_.end()) - { - action->Retain(); - action->StartWithTarget(this); - actions_.push_back(action); - } - } - else - { - throw std::runtime_error("该 Action 已有执行目标"); + action->Start(); + actions_.push_back(action); } } } @@ -828,7 +770,7 @@ namespace easy2d return ret != 0; } - bool Node::Intersects(Node * node) + bool Node::Intersects(spNode const& node) { if (transform_.size.width == 0.f || transform_.size.height == 0.f || node->transform_.size.width == 0.f || node->transform_.size.height == 0.f) return false; @@ -883,19 +825,18 @@ namespace easy2d } } - const Node::Actions & Node::GetAllActions() const + const Actions& Node::GetAllActions() const { return actions_; } - void Node::AddTask(Task * task) + void Node::AddTask(spTask const& task) { if (task) { auto iter = std::find(tasks_.begin(), tasks_.end(), task); if (iter == tasks_.end()) { - task->Retain(); task->last_time_ = time::Now(); tasks_.push_back(task); } @@ -959,7 +900,7 @@ namespace easy2d } } - const Node::Tasks & Node::GetAllTasks() const + const Tasks & Node::GetAllTasks() const { return tasks_; } @@ -969,13 +910,13 @@ namespace easy2d if (tasks_.empty()) return; - std::vector currTasks; + std::vector currTasks; currTasks.reserve(tasks_.size()); std::copy_if( tasks_.begin(), tasks_.end(), std::back_inserter(currTasks), - [](Task* task) { return task->IsReady() && !task->stopped_; } + [](spTask const& task) { return task->IsReady() && !task->stopped_; } ); // 遍历就绪的任务 @@ -987,7 +928,6 @@ namespace easy2d { if ((*iter)->stopped_) { - (*iter)->Release(); iter = tasks_.erase(iter); } else @@ -1030,13 +970,4 @@ namespace easy2d hash_name_ = std::hash{}(name); } } - - void Node::SetParentScene(Scene * scene) - { - parent_scene_ = scene; - for (const auto& child : children_) - { - child->SetParentScene(scene); - } - } } \ No newline at end of file diff --git a/core/base/Node.h b/core/base/Node.h index bf18acc7..2e5ca350 100644 --- a/core/base/Node.h +++ b/core/base/Node.h @@ -20,7 +20,6 @@ #pragma once #include "base.h" -#include "RefCounter.h" #include "KeyEvent.h" #include "MouseEvent.h" #include "../math/Transform.h" @@ -43,10 +42,6 @@ namespace easy2d E2D_DISABLE_COPY(Node); public: - typedef std::vector Nodes; - typedef std::vector Actions; - typedef std::vector Tasks; - Node(); virtual ~Node(); @@ -121,10 +116,7 @@ namespace easy2d float GetDisplayOpacity() const; // 获取父节点 - Node * GetParent() const; - - // 获取节点所在场景 - Scene * GetParentScene() const; + spNode GetParent() const; // 设置节点是否显示 void SetVisible( @@ -292,12 +284,12 @@ namespace easy2d // 判断两物体是否相交 bool Intersects( - Node * node + spNode const& node ); // 添加子节点 void AddChild( - Node * child, + spNode const& child, int order = 0 /* 渲染顺序 */ ); @@ -313,7 +305,7 @@ namespace easy2d ) const; // 获取名称相同的子节点 - Node* GetChild( + spNode GetChild( const String& name ) const; @@ -325,7 +317,7 @@ namespace easy2d // 移除子节点 bool RemoveChild( - Node * child + spNode const& child ); // 移除所有名称相同的子节点 @@ -341,7 +333,7 @@ namespace easy2d // 执行动作 void RunAction( - Action * action + spAction const& action ); // 继续动作 @@ -373,7 +365,7 @@ namespace easy2d // 添加任务 void AddTask( - Task * task + spTask const& task ); // 启动任务 @@ -423,11 +415,6 @@ namespace easy2d // 渲染节点边缘 void DrawBorder(); - // 设置节点所在场景 - void SetParentScene( - Scene * scene - ); - // 更新子节点 void UpdateChildren(float dt); @@ -456,7 +443,6 @@ namespace easy2d bool clip_enabled_; bool dirty_sort_; bool dirty_transform_; - Scene* parent_scene_; Node* parent_; Color border_color_; Actions actions_; diff --git a/core/base/RefCounter.cpp b/core/base/RefCounter.cpp index 0a57a084..4614adf5 100644 --- a/core/base/RefCounter.cpp +++ b/core/base/RefCounter.cpp @@ -23,8 +23,8 @@ namespace easy2d { RefCounter::RefCounter() + : ref_count_(0) { - ref_count_ = 0; } RefCounter::~RefCounter() @@ -33,20 +33,12 @@ namespace easy2d long RefCounter::Retain() { - return ::InterlockedIncrement(&ref_count_); + } long RefCounter::Release() { - long new_count = ::InterlockedDecrement(&ref_count_); - - if (new_count <= 0) - { - delete this; - return 0; - } - - return new_count; + } long RefCounter::GetRefCount() const diff --git a/core/base/RefCounter.h b/core/base/RefCounter.hpp similarity index 73% rename from core/base/RefCounter.h rename to core/base/RefCounter.hpp index 2c573b1e..225be28a 100644 --- a/core/base/RefCounter.h +++ b/core/base/RefCounter.hpp @@ -23,24 +23,39 @@ namespace easy2d { - // 引用计数 class RefCounter { - public: - RefCounter(); + E2D_DISABLE_COPY(RefCounter); - virtual ~RefCounter(); + public: + RefCounter() : ref_count_(0) {} + + virtual ~RefCounter() {} // 增加引用计数 - long Retain(); + inline void Retain() { ::InterlockedIncrement(&ref_count_); } // 减少引用计数 - long Release(); + inline void Release() + { + if (::InterlockedDecrement(&ref_count_) <= 0) + delete this; + } // 获取引用计数 - long GetRefCount() const; + inline long GetRefCount() const { return ref_count_; } private: long ref_count_; }; + + inline void IntrusivePtrAddRef(RefCounter* ptr) + { + if (ptr) ptr->Retain(); + } + + inline void IntrusivePtrRelease(RefCounter* ptr) + { + if (ptr) ptr->Release(); + } } diff --git a/core/base/Scene.cpp b/core/base/Scene.cpp index ef4e4569..72b273f3 100644 --- a/core/base/Scene.cpp +++ b/core/base/Scene.cpp @@ -29,7 +29,7 @@ namespace easy2d { } - Scene::Scene(Node * root) + Scene::Scene(spNode const& root) : root_(nullptr) , transform_() { @@ -38,34 +38,17 @@ namespace easy2d Scene::~Scene() { - if (root_) - { - root_->SetParentScene(nullptr); - root_->Release(); - } } - void Scene::SetRoot(Node * root) + void Scene::SetRoot(spNode const& root) { if (root_ == root) return; - if (root_) - { - root_->SetParentScene(nullptr); - root_->Release(); - } - - if (root) - { - root->Retain(); - root->SetParentScene(this); - } - root_ = root; } - Node * Scene::GetRoot() const + spNode const& Scene::GetRoot() const { return root_; } diff --git a/core/base/Scene.h b/core/base/Scene.h index 1de206b5..125e4593 100644 --- a/core/base/Scene.h +++ b/core/base/Scene.h @@ -19,7 +19,7 @@ // THE SOFTWARE. #pragma once -#include "RefCounter.h" +#include "base.h" #include "KeyEvent.h" #include "MouseEvent.h" #include "../math/Matrix.hpp" @@ -38,7 +38,7 @@ namespace easy2d Scene(); explicit Scene( - Node * root + spNode const& root ); virtual ~Scene(); @@ -54,11 +54,11 @@ namespace easy2d // 设置根节点 void SetRoot( - Node * root + spNode const& root ); // 获取根节点 - Node* GetRoot() const; + spNode const& GetRoot() const; // 渲染场景 void Draw(); @@ -82,7 +82,7 @@ namespace easy2d const math::Matrix& GetTransform() const; private: - Node* root_; + spNode root_; math::Matrix transform_; }; } diff --git a/core/base/Sprite.cpp b/core/base/Sprite.cpp index 7ddaa0af..7a894191 100644 --- a/core/base/Sprite.cpp +++ b/core/base/Sprite.cpp @@ -29,7 +29,7 @@ namespace easy2d { } - Sprite::Sprite(Image * image) + Sprite::Sprite(spImage const& image) : image_(nullptr) { Load(image); @@ -63,20 +63,13 @@ namespace easy2d Sprite::~Sprite() { - SafeRelease(image_); } - bool Sprite::Load(Image * image) + bool Sprite::Load(spImage const& image) { if (image) { - if (image_) - { - image_->Release(); - } - image_ = image; - image_->Retain(); Node::SetSize(image_->GetWidth(), image_->GetHeight()); return true; @@ -88,14 +81,16 @@ namespace easy2d { if (!image_) { - image_ = new Image(); - image_->Retain(); + image_ = new (std::nothrow) Image(); } - if (image_->Load(res)) + if (image_) { - Node::SetSize(image_->GetWidth(), image_->GetHeight()); - return true; + if (image_->Load(res)) + { + Node::SetSize(image_->GetWidth(), image_->GetHeight()); + return true; + } } return false; } @@ -104,14 +99,16 @@ namespace easy2d { if (!image_) { - image_ = new Image(); - image_->Retain(); + image_ = new (std::nothrow) Image(); } - if (image_->Load(file_name)) + if (image_) { - Node::SetSize(image_->GetWidth(), image_->GetHeight()); - return true; + if (image_->Load(file_name)) + { + Node::SetSize(image_->GetWidth(), image_->GetHeight()); + return true; + } } return false; } @@ -125,7 +122,7 @@ namespace easy2d ); } - Image * Sprite::GetImage() const + spImage const& Sprite::GetImage() const { return image_; } diff --git a/core/base/Sprite.h b/core/base/Sprite.h index 2c8123ca..1d36d564 100644 --- a/core/base/Sprite.h +++ b/core/base/Sprite.h @@ -34,7 +34,7 @@ namespace easy2d Sprite(); explicit Sprite( - Image * image + spImage const& image ); explicit Sprite( @@ -69,7 +69,7 @@ namespace easy2d // 加载图片 bool Load( - Image * image + spImage const& image ); // 将图片裁剪为矩形 @@ -78,12 +78,12 @@ namespace easy2d ); // 获取 Image 对象 - Image * GetImage() const; + spImage const& GetImage() const; // 渲染精灵 virtual void OnDraw() const override; private: - Image* image_; + spImage image_; }; } diff --git a/core/base/Task.cpp b/core/base/Task.cpp index 23fb92d5..e14beaf4 100644 --- a/core/base/Task.cpp +++ b/core/base/Task.cpp @@ -23,22 +23,16 @@ namespace easy2d { Task::Task(const Callback & func, const String & name) - : running_(true) - , stopped_(false) - , run_times_(0) - , total_times_(-1) - , delay_() - , callback_(func) - , name_(name) + : Task(func, Duration{}, -1, name) { } - Task::Task(const Callback & func, float delay, int times, const String & name) + Task::Task(Callback const& func, Duration const& delay, int times, const String & name) : running_(true) , stopped_(false) , run_times_(0) - , delay_(time::Second * std::max(delay, 0.f)) , total_times_(times) + , delay_(delay) , callback_(func) , name_(name) { diff --git a/core/base/Task.h b/core/base/Task.h index 3780cf7f..05a44b3e 100644 --- a/core/base/Task.h +++ b/core/base/Task.h @@ -21,13 +21,10 @@ #pragma once #include "base.h" #include "time.h" -#include "RefCounter.h" #include namespace easy2d { - class Node; - // 定时任务 class Task : public RefCounter @@ -38,14 +35,14 @@ namespace easy2d public: explicit Task( - const Callback& func, /* 执行函数 */ + const Callback& func, /* 执行函数 */ const String& name = L"" /* 任务名称 */ ); explicit Task( - const Callback& func, /* 执行函数 */ - float delay, /* 时间间隔(秒) */ - int times = -1, /* 执行次数(设 -1 为永久执行) */ + Callback const& func, /* 执行函数 */ + Duration const& delay, /* 时间间隔(秒) */ + int times = -1, /* 执行次数(设 -1 为永久执行) */ const String& name = L"" /* 任务名称 */ ); @@ -61,24 +58,21 @@ namespace easy2d // 获取任务名称 const String& GetName() const; - // 任务是否就绪 + protected: bool IsReady() const; - // 执行任务 void Update(); - // 重置计时 void ResetTime(); private: - bool running_; - bool stopped_; - int run_times_; - int total_times_; - String name_; - time::Duration delay_; - time::TimePoint last_time_; - Callback callback_; - Node * target_; + bool running_; + bool stopped_; + int run_times_; + int total_times_; + String name_; + Duration delay_; + TimePoint last_time_; + Callback callback_; }; } diff --git a/core/base/Transition.cpp b/core/base/Transition.cpp index 556c5ee7..fed26552 100644 --- a/core/base/Transition.cpp +++ b/core/base/Transition.cpp @@ -50,8 +50,6 @@ namespace easy2d { SafeRelease(out_layer_); SafeRelease(in_layer_); - SafeRelease(out_scene_); - SafeRelease(in_scene_); } bool Transition::IsDone() @@ -59,18 +57,12 @@ namespace easy2d return done_; } - void Transition::Initialize(Scene * prev, Scene * next, Game * game) + void Transition::Init(spScene const& prev, spScene const& next) { started_ = time::Now(); out_scene_ = prev; in_scene_ = next; - if (out_scene_) - out_scene_->Retain(); - - if (in_scene_) - in_scene_->Retain(); - if (in_scene_) { ThrowIfFailed( @@ -153,9 +145,9 @@ namespace easy2d { } - void BoxTransition::Initialize(Scene * prev, Scene * next, Game * game) + void BoxTransition::Init(spScene const& prev, spScene const& next) { - Transition::Initialize(prev, next, game); + Transition::Init(prev, next); in_layer_prop_.opacity = 0; } @@ -195,9 +187,9 @@ namespace easy2d { } - void EmergeTransition::Initialize(Scene * prev, Scene * next, Game * game) + void EmergeTransition::Init(spScene const& prev, spScene const& next) { - Transition::Initialize(prev, next, game); + Transition::Init(prev, next); out_layer_prop_.opacity = 1; in_layer_prop_.opacity = 0; @@ -220,9 +212,9 @@ namespace easy2d { } - void FadeTransition::Initialize(Scene * prev, Scene * next, Game * game) + void FadeTransition::Init(spScene const& prev, spScene const& next) { - Transition::Initialize(prev, next, game); + Transition::Init(prev, next); out_layer_prop_.opacity = 1; in_layer_prop_.opacity = 0; @@ -254,9 +246,9 @@ namespace easy2d { } - void MoveTransition::Initialize(Scene * prev, Scene * next, Game * game) + void MoveTransition::Init(spScene const& prev, spScene const& next) { - Transition::Initialize(prev, next, game); + Transition::Init(prev, next); switch (direction_) { @@ -344,9 +336,9 @@ namespace easy2d { } - void RotationTransition::Initialize(Scene * prev, Scene * next, Game * game) + void RotationTransition::Init(spScene const& prev, spScene const& next) { - Transition::Initialize(prev, next, game); + Transition::Init(prev, next); if (out_scene_) { diff --git a/core/base/Transition.h b/core/base/Transition.h index e1e558b6..484196db 100644 --- a/core/base/Transition.h +++ b/core/base/Transition.h @@ -21,11 +21,9 @@ #pragma once #include "base.h" #include "time.h" -#include "RefCounter.h" namespace easy2d { - class Game; class Scene; // 场景过渡 @@ -46,10 +44,9 @@ namespace easy2d protected: // 初始化场景过渡动画 - virtual void Initialize( - Scene * prev, - Scene * next, - Game * game + virtual void Init( + spScene const& prev, + spScene const& next ); // 更新场景过渡动画 @@ -68,10 +65,10 @@ namespace easy2d bool done_; float duration_; float process_; - time::TimePoint started_; + TimePoint started_; Size window_size_; - Scene* out_scene_; - Scene* in_scene_; + spScene out_scene_; + spScene in_scene_; ID2D1Layer* out_layer_; ID2D1Layer* in_layer_; LayerProperties out_layer_prop_; @@ -92,10 +89,9 @@ namespace easy2d // 更新动画 virtual void Update() override; - virtual void Initialize( - Scene * prev, - Scene * next, - Game * game + virtual void Init( + spScene const& prev, + spScene const& next ) override; }; @@ -112,10 +108,9 @@ namespace easy2d protected: virtual void Update() override; - virtual void Initialize( - Scene * prev, - Scene * next, - Game * game + virtual void Init( + spScene const& prev, + spScene const& next ) override; }; @@ -132,10 +127,9 @@ namespace easy2d protected: virtual void Update() override; - virtual void Initialize( - Scene * prev, - Scene * next, - Game * game + virtual void Init( + spScene const& prev, + spScene const& next ) override; }; @@ -153,10 +147,9 @@ namespace easy2d protected: virtual void Update() override; - virtual void Initialize( - Scene * prev, - Scene * next, - Game * game + virtual void Init( + spScene const& prev, + spScene const& next ) override; virtual void Reset() override; @@ -181,10 +174,9 @@ namespace easy2d protected: virtual void Update() override; - virtual void Initialize( - Scene * prev, - Scene * next, - Game * game + virtual void Init( + spScene const& prev, + spScene const& next ) override; virtual void Reset() override; diff --git a/core/base/audio.cpp b/core/base/audio.cpp index 07d026b6..86e61317 100644 --- a/core/base/audio.cpp +++ b/core/base/audio.cpp @@ -165,7 +165,7 @@ namespace easy2d , mastering_voice_(nullptr) , initialized(false) { - modules::Initialize(); + modules::Init(); } AudioDevice::~AudioDevice() @@ -185,7 +185,7 @@ namespace easy2d modules::Destroy(); } - void AudioDevice::Initialize(bool debug) + void AudioDevice::Init(bool debug) { if (initialized) return; diff --git a/core/base/audio.h b/core/base/audio.h index e761e1c8..21c8f8b1 100644 --- a/core/base/audio.h +++ b/core/base/audio.h @@ -81,7 +81,7 @@ namespace easy2d E2D_DISABLE_COPY(AudioDevice); public: - void Initialize(bool debug); + void Init(bool debug); // 开启设备 void Open(); @@ -106,10 +106,10 @@ namespace easy2d ~AudioDevice(); protected: - bool initialized; - IXAudio2 * x_audio2_; + bool initialized; + IXAudio2* x_audio2_; IXAudio2MasteringVoice* mastering_voice_; - std::list voice_cache_; + std::list voice_cache_; }; E2D_DECLARE_SINGLETON_TYPE(AudioDevice, Audio); diff --git a/core/base/base.h b/core/base/base.h index 23344fd5..b577377f 100644 --- a/core/base/base.h +++ b/core/base/base.h @@ -20,10 +20,74 @@ #pragma once #include "BaseTypes.h" +#include "IntrusivePtr.hpp" +#include "RefCounter.hpp" #include + +#ifndef E2D_DECLARE_SMART_PTR +#define E2D_DECLARE_SMART_PTR(class_name)\ + class class_name;\ + using sp##class_name = ::easy2d::IntrusivePtr< class_name > + +#define E2D_DECLARE_NS_SMART_PTR(ns_name, class_name)\ + namespace ns_name\ + {\ + class class_name; \ + using sp##class_name = ::easy2d::IntrusivePtr< class_name >;\ + } +#endif + + namespace easy2d { + E2D_DECLARE_SMART_PTR(Image); + E2D_DECLARE_SMART_PTR(Music); + E2D_DECLARE_SMART_PTR(Task); + + E2D_DECLARE_SMART_PTR(Node); + E2D_DECLARE_SMART_PTR(Scene); + E2D_DECLARE_SMART_PTR(Sprite); + E2D_DECLARE_SMART_PTR(Text); + E2D_DECLARE_SMART_PTR(Canvas); + + E2D_DECLARE_SMART_PTR(Action); + E2D_DECLARE_SMART_PTR(MoveBy); + E2D_DECLARE_SMART_PTR(MoveTo); + E2D_DECLARE_SMART_PTR(JumpBy); + E2D_DECLARE_SMART_PTR(JumpTo); + E2D_DECLARE_SMART_PTR(ScaleBy); + E2D_DECLARE_SMART_PTR(ScaleTo); + E2D_DECLARE_SMART_PTR(OpacityBy); + E2D_DECLARE_SMART_PTR(OpacityTo); + E2D_DECLARE_SMART_PTR(FadeIn); + E2D_DECLARE_SMART_PTR(FadeOut); + E2D_DECLARE_SMART_PTR(RotateBy); + E2D_DECLARE_SMART_PTR(RotateTo); + E2D_DECLARE_SMART_PTR(Delay); + E2D_DECLARE_SMART_PTR(Animation); + E2D_DECLARE_SMART_PTR(Animate); + E2D_DECLARE_SMART_PTR(CallFunc); + E2D_DECLARE_SMART_PTR(Loop); + E2D_DECLARE_SMART_PTR(Sequence); + E2D_DECLARE_SMART_PTR(Spawn); + + E2D_DECLARE_SMART_PTR(Transition); + E2D_DECLARE_SMART_PTR(FadeTransition); + E2D_DECLARE_SMART_PTR(EmergeTransition); + E2D_DECLARE_SMART_PTR(BoxTransition); + E2D_DECLARE_SMART_PTR(MoveTransition); + E2D_DECLARE_SMART_PTR(RotationTransition); + + E2D_DECLARE_NS_SMART_PTR(ui, Button); + E2D_DECLARE_NS_SMART_PTR(ui, Menu); + + using Images = ::std::vector< spImage >; + using Nodes = ::std::vector< spNode >; + using Actions = ::std::vector< spAction >; + using Tasks = ::std::vector< spTask >; + + template inline void SafeRelease(Interface*& p) { diff --git a/core/base/modules.cpp b/core/base/modules.cpp index a27f7f58..fa8b8ef3 100644 --- a/core/base/modules.cpp +++ b/core/base/modules.cpp @@ -38,7 +38,7 @@ namespace easy2d Module_XAudio2 XAudio2; Module_MediaFoundation MediaFoundation; - void Initialize() + void Init() { initialize_count++; if (initialize_count > 1) diff --git a/core/base/modules.h b/core/base/modules.h index 14c2e27a..4bcd6c30 100644 --- a/core/base/modules.h +++ b/core/base/modules.h @@ -31,7 +31,7 @@ namespace easy2d { // modules can be initialized multiple times, // but it needs to be destroyed every time - void Initialize(); + void Init(); void Destroy(); diff --git a/core/base/render.cpp b/core/base/render.cpp index 4a1d16a0..0a5f522e 100644 --- a/core/base/render.cpp +++ b/core/base/render.cpp @@ -22,6 +22,7 @@ #include "time.h" #include "base.h" #include "modules.h" +#include "Image.h" #pragma comment(lib, "d2d1.lib") #pragma comment(lib, "dwrite.lib") @@ -38,7 +39,7 @@ namespace easy2d { ZeroMemory(&d2d, sizeof(D2DResources)); - modules::Initialize(); + modules::Init(); } GraphicsDevice::~GraphicsDevice() @@ -63,7 +64,7 @@ namespace easy2d modules::Destroy(); } - void GraphicsDevice::Initialize(HWND hwnd, bool debug) + void GraphicsDevice::Init(HWND hwnd, bool debug) { if (initialized) return; @@ -302,7 +303,7 @@ namespace easy2d } HRESULT GraphicsDevice::DrawImage( - Image * image, + spImage const& image, float opacity, const Rect & dest_rect, const Rect & source_rect diff --git a/core/base/render.h b/core/base/render.h index 9dcb8709..c6eebec4 100644 --- a/core/base/render.h +++ b/core/base/render.h @@ -23,12 +23,13 @@ #include "Singleton.hpp" #include "Font.h" #include "Resource.h" -#include "Image.h" #include "TextRenderer.h" #include "../math/Matrix.hpp" namespace easy2d { + class Image; + namespace devices { struct D2DResources @@ -52,7 +53,7 @@ namespace easy2d E2D_DISABLE_COPY(GraphicsDevice); public: - void Initialize(HWND hwnd, bool debug); + void Init(HWND hwnd, bool debug); // 开始渲染 void BeginDraw(HWND hwnd); @@ -121,7 +122,7 @@ namespace easy2d ); HRESULT DrawImage( - Image* image, + spImage const& image, float opacity, const Rect& dest_rect, const Rect& source_rect diff --git a/core/base/time.h b/core/base/time.h index 08d67265..1650eaa9 100644 --- a/core/base/time.h +++ b/core/base/time.h @@ -187,6 +187,11 @@ namespace easy2d } } +namespace easy2d +{ + using namespace time; +} + #if VS_VER >= VS_2015 namespace easy2d diff --git a/core/base/window.cpp b/core/base/window.cpp index 468e6d43..b75c8451 100644 --- a/core/base/window.cpp +++ b/core/base/window.cpp @@ -56,7 +56,7 @@ namespace easy2d ::DestroyWindow(handle); } - void WindowImpl::Initialize(String title, int width, int height, LPCWSTR icon, bool debug) + void WindowImpl::Init(String title, int width, int height, LPCWSTR icon, bool debug) { if (initialized) return; diff --git a/core/base/window.h b/core/base/window.h index 5f9663a2..230a540f 100644 --- a/core/base/window.h +++ b/core/base/window.h @@ -33,7 +33,7 @@ namespace easy2d E2D_DISABLE_COPY(WindowImpl); public: - void Initialize( + void Init( String title, int width, int height, diff --git a/core/easy2d.h b/core/easy2d.h index 09cb6870..d2aad23d 100644 --- a/core/easy2d.h +++ b/core/easy2d.h @@ -46,21 +46,22 @@ #include "base/Color.h" #include "base/Resource.h" -#include "base/RefCounter.h" +#include "base/RefCounter.hpp" +#include "base/IntrusivePtr.hpp" #include "base/Image.h" -#include "base/Scene.h" #include "base/Node.h" +#include "base/Scene.h" #include "base/Sprite.h" -#include "base/Task.h" #include "base/Text.h" -#include "base/Action.h" +#include "base/Canvas.h" +#include "base/Music.h" +#include "base/Task.h" +#include "base/Action.hpp" #include "base/ActionCombined.h" #include "base/ActionFiniteTime.h" #include "base/Animation.h" #include "base/CallFunc.h" -#include "base/Canvas.h" #include "base/Transition.h" -#include "base/Music.h" #include "base/KeyEvent.h" #include "base/MouseEvent.h" diff --git a/core/ui/Button.cpp b/core/ui/Button.cpp index 87ae9e7b..bcb0d6dc 100644 --- a/core/ui/Button.cpp +++ b/core/ui/Button.cpp @@ -53,7 +53,7 @@ namespace easy2d { } - Button::Button(Node * normal, const Callback& func) + Button::Button(spNode const& normal, const Callback& func) : callback_(nullptr) , status_(Status::Normal) , enabled_(true) @@ -67,7 +67,7 @@ namespace easy2d this->SetCallbackOnClick(func); } - Button::Button(Node * normal, Node * selected, const Callback& func) + Button::Button(spNode const& normal, spNode const& selected, const Callback& func) : callback_(nullptr) , status_(Status::Normal) , enabled_(true) @@ -82,7 +82,7 @@ namespace easy2d this->SetCallbackOnClick(func); } - Button::Button(Node * normal, Node * mouseover, Node * selected, const Callback& func) + Button::Button(spNode const& normal, spNode const& mouseover, spNode const& selected, const Callback& func) : callback_(nullptr) , status_(Status::Normal) , enabled_(true) @@ -98,7 +98,7 @@ namespace easy2d this->SetCallbackOnClick(func); } - Button::Button(Node * normal, Node * mouseover, Node * selected, Node * disabled, const Callback& func) + Button::Button(spNode const& normal, spNode const& mouseover, spNode const& selected, spNode const& disabled, const Callback& func) : callback_(nullptr) , status_(Status::Normal) , enabled_(true) @@ -115,12 +115,16 @@ namespace easy2d this->SetCallbackOnClick(func); } + Button::~Button() + { + } + bool Button::IsEnable() const { return enabled_; } - void Button::SetNormal(Node * normal) + void Button::SetNormal(spNode const& normal) { SET_BUTTON_NODE(normal_, normal); if (normal) @@ -129,17 +133,17 @@ namespace easy2d } } - void Button::SetMouseOver(Node * mouseover) + void Button::SetMouseOver(spNode const& mouseover) { SET_BUTTON_NODE(mouseover_, mouseover); } - void Button::SetSelected(Node * selected) + void Button::SetSelected(spNode const& selected) { SET_BUTTON_NODE(selected_, selected); } - void Button::SetDisabled(Node * disabled) + void Button::SetDisabled(spNode const& disabled) { SET_BUTTON_NODE(disabled_, disabled); } diff --git a/core/ui/Button.h b/core/ui/Button.h index 3efe2f95..9542a962 100644 --- a/core/ui/Button.h +++ b/core/ui/Button.h @@ -31,37 +31,39 @@ namespace easy2d { E2D_DISABLE_COPY(Button); - typedef std::function Callback; + using Callback = std::function; public: Button(); explicit Button( - Node * normal, /* 普通状态 */ + spNode const& normal, /* 普通状态 */ const Callback& func = nullptr /* 按钮点击后的回调函数 */ ); explicit Button( - Node * normal, /* 普通状态 */ - Node * selected, /* 鼠标按下状态 */ + spNode const& normal, /* 普通状态 */ + spNode const& selected, /* 鼠标按下状态 */ const Callback& func = nullptr /* 按钮点击后的回调函数 */ ); explicit Button( - Node * normal, /* 普通状态 */ - Node * mouseover, /* 鼠标移入状态 */ - Node * selected, /* 鼠标按下状态 */ + spNode const& normal, /* 普通状态 */ + spNode const& mouseover, /* 鼠标移入状态 */ + spNode const& selected, /* 鼠标按下状态 */ const Callback& func = nullptr /* 按钮点击后的回调函数 */ ); explicit Button( - Node * normal, /* 普通状态 */ - Node * mouseover, /* 鼠标移入状态 */ - Node * selected, /* 鼠标移入状态 */ - Node * disabled, /* 按钮禁用状态 */ + spNode const& normal, /* 普通状态 */ + spNode const& mouseover, /* 鼠标移入状态 */ + spNode const& selected, /* 鼠标移入状态 */ + spNode const& disabled, /* 按钮禁用状态 */ const Callback& func = nullptr /* 按钮点击后的回调函数 */ ); + virtual ~Button(); + // 获取按钮状态是启用还是禁用 bool IsEnable() const; @@ -72,22 +74,22 @@ namespace easy2d // 设置一般情况下显示的按钮 virtual void SetNormal( - Node * normal + spNode const& normal ); // 设置鼠标移入按钮时显示的按钮 virtual void SetMouseOver( - Node * mouseover + spNode const& mouseover ); // 设置鼠标按下按钮时显示的按钮 virtual void SetSelected( - Node * selected + spNode const& selected ); // 设置按钮被禁用时显示的按钮 virtual void SetDisabled( - Node * disabled + spNode const& disabled ); // 设置按钮点击后的回调函数 @@ -124,10 +126,10 @@ namespace easy2d virtual void Visit() override; private: - Node * normal_; - Node * mouseover_; - Node * selected_; - Node * disabled_; + spNode normal_; + spNode mouseover_; + spNode selected_; + spNode disabled_; bool enabled_; bool is_selected_; Status status_; diff --git a/core/ui/Menu.cpp b/core/ui/Menu.cpp index 6e46bfb9..dabe52f6 100644 --- a/core/ui/Menu.cpp +++ b/core/ui/Menu.cpp @@ -29,7 +29,7 @@ namespace easy2d { } - Menu::Menu(const std::vector& buttons) + Menu::Menu(const std::vector& buttons) : enabled_(true) { for (const auto& button : buttons) @@ -61,7 +61,7 @@ namespace easy2d } } - void Menu::AddButton(Button * button) + void Menu::AddButton(spButton const& button) { if (button) { @@ -71,7 +71,7 @@ namespace easy2d } } - bool Menu::RemoveButton(Button * button) + bool Menu::RemoveButton(spButton const& button) { if (buttons_.empty()) { @@ -94,7 +94,7 @@ namespace easy2d return false; } - const std::vector& Menu::GetAllButtons() const + const std::vector& Menu::GetAllButtons() const { return buttons_; } diff --git a/core/ui/Menu.h b/core/ui/Menu.h index c2b4ac5a..b3923404 100644 --- a/core/ui/Menu.h +++ b/core/ui/Menu.h @@ -19,7 +19,6 @@ // THE SOFTWARE. #pragma once -#include "../base/Node.h" #include "Button.h" namespace easy2d @@ -36,7 +35,7 @@ namespace easy2d Menu(); explicit Menu( - const std::vector& buttons /* 按钮数组 */ + const std::vector& buttons /* 按钮数组 */ ); // 获取菜单是否禁用 @@ -52,20 +51,20 @@ namespace easy2d // 添加按钮 void AddButton( - Button * button + spButton const& button ); // 移除按钮 bool RemoveButton( - Button * button + spButton const& button ); // 获取所有按钮 - const std::vector& GetAllButtons() const; + const std::vector& GetAllButtons() const; private: bool enabled_; - std::vector buttons_; + std::vector buttons_; }; } } \ No newline at end of file diff --git a/core/utils/Player.cpp b/core/utils/Player.cpp index ed84d6d7..264cf26e 100644 --- a/core/utils/Player.cpp +++ b/core/utils/Player.cpp @@ -38,7 +38,7 @@ namespace easy2d if (file_path.empty()) return false; - Music * music = new (std::nothrow) Music(); + spMusic music = new (std::nothrow) Music(); if (music) { @@ -50,10 +50,6 @@ namespace easy2d musics_cache_.insert(std::make_pair(hash_code, music)); return true; } - else - { - music->Release(); - } } return false; } @@ -121,7 +117,7 @@ namespace easy2d if (musics_cache_.end() != musics_cache_.find(hash_code)) return true; - Music * music = new (std::nothrow) Music(); + spMusic music = new (std::nothrow) Music(); if (music) { @@ -131,10 +127,6 @@ namespace easy2d musics_cache_.insert(std::make_pair(hash_code, music)); return true; } - else - { - music->Release(); - } } return false; } @@ -222,13 +214,6 @@ namespace easy2d void Player::ClearCache() { - if (musics_cache_.empty()) - return; - - for (const auto& pair : musics_cache_) - { - pair.second->Release(); - } musics_cache_.clear(); } } \ No newline at end of file diff --git a/core/utils/Player.h b/core/utils/Player.h index 54ef2d98..d61b2452 100644 --- a/core/utils/Player.h +++ b/core/utils/Player.h @@ -24,8 +24,6 @@ namespace easy2d { - class Music; - // 音乐播放器 class Player { @@ -120,6 +118,6 @@ namespace easy2d protected: float volume_; - std::map musics_cache_; + std::map musics_cache_; }; } diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index b09ada35..c30e174f 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -19,7 +19,7 @@ - + @@ -33,6 +33,7 @@ + @@ -41,7 +42,7 @@ - + @@ -69,7 +70,6 @@ - @@ -86,7 +86,6 @@ - diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index 668bcfe5..040a1ad8 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -2,9 +2,6 @@ - - base - base @@ -59,9 +56,6 @@ base - - base - base @@ -143,6 +137,15 @@ base + + base + + + base + + + base + @@ -159,9 +162,6 @@ - - base - base @@ -201,9 +201,6 @@ base - - base - base diff --git a/project/vs2015/Easy2D.vcxproj b/project/vs2015/Easy2D.vcxproj index b4b79ab0..3b1fa467 100644 --- a/project/vs2015/Easy2D.vcxproj +++ b/project/vs2015/Easy2D.vcxproj @@ -19,7 +19,7 @@ - + @@ -33,6 +33,7 @@ + @@ -41,7 +42,7 @@ - + @@ -69,7 +70,6 @@ - @@ -86,7 +86,6 @@ - diff --git a/project/vs2015/Easy2D.vcxproj.filters b/project/vs2015/Easy2D.vcxproj.filters index 668bcfe5..040a1ad8 100644 --- a/project/vs2015/Easy2D.vcxproj.filters +++ b/project/vs2015/Easy2D.vcxproj.filters @@ -2,9 +2,6 @@ - - base - base @@ -59,9 +56,6 @@ base - - base - base @@ -143,6 +137,15 @@ base + + base + + + base + + + base + @@ -159,9 +162,6 @@ - - base - base @@ -201,9 +201,6 @@ base - - base - base diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index f22ae099..b4ec43f5 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -19,7 +19,7 @@ - + @@ -33,6 +33,7 @@ + @@ -41,7 +42,7 @@ - + @@ -69,7 +70,6 @@ - @@ -86,7 +86,6 @@ - @@ -198,7 +197,7 @@ Disabled WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) false - None + EditAndContinue false true diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 668bcfe5..040a1ad8 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -2,9 +2,6 @@ - - base - base @@ -59,9 +56,6 @@ base - - base - base @@ -143,6 +137,15 @@ base + + base + + + base + + + base + @@ -159,9 +162,6 @@ - - base - base @@ -201,9 +201,6 @@ base - - base - base