From 5a877ccf7b764ecb5007dbf9eeea1db18f7891bc Mon Sep 17 00:00:00 2001 From: Nomango Date: Wed, 22 Jul 2020 21:08:48 +0800 Subject: [PATCH] remove Create functions --- projects/kiwano/kiwano.vcxproj | 3 +- projects/kiwano/kiwano.vcxproj.filters | 7 +- src/kiwano-audio/Sound.cpp | 22 +- src/kiwano-audio/Sound.h | 4 +- src/kiwano-audio/SoundPlayer.cpp | 10 +- src/kiwano-audio/SoundPlayer.h | 4 - src/kiwano-imgui/ImGuiLayer.cpp | 22 +- src/kiwano-imgui/ImGuiLayer.h | 8 +- src/kiwano-network/HttpModule.cpp | 2 +- src/kiwano-network/HttpRequest.cpp | 39 +-- src/kiwano-network/HttpRequest.h | 13 +- src/kiwano-physics/Fixture.cpp | 10 +- src/kiwano-physics/Joint.cpp | 165 ++-------- src/kiwano-physics/Joint.h | 44 +-- src/kiwano-physics/PhysicBody.cpp | 36 +- src/kiwano-physics/PhysicBody.h | 15 +- src/kiwano-physics/PhysicWorld.cpp | 22 +- src/kiwano-physics/PhysicWorld.h | 8 +- src/kiwano/2d/Actor.cpp | 26 +- src/kiwano/2d/Actor.h | 4 - src/kiwano/2d/Canvas.cpp | 20 +- src/kiwano/2d/Canvas.h | 5 +- src/kiwano/2d/DebugActor.cpp | 4 +- src/kiwano/2d/GifSprite.cpp | 54 ++- src/kiwano/2d/GifSprite.h | 10 +- src/kiwano/2d/LayerActor.cpp | 6 - src/kiwano/2d/LayerActor.h | 4 - src/kiwano/2d/ShapeActor.cpp | 140 +++----- src/kiwano/2d/ShapeActor.h | 52 ++- src/kiwano/2d/Sprite.cpp | 83 ++--- src/kiwano/2d/Sprite.h | 14 +- src/kiwano/2d/Stage.cpp | 10 +- src/kiwano/2d/Stage.h | 4 - src/kiwano/2d/TextActor.cpp | 27 +- src/kiwano/2d/TextActor.h | 8 +- src/kiwano/2d/Transition.cpp | 61 +--- src/kiwano/2d/Transition.h | 40 +-- src/kiwano/2d/action/Action.cpp | 3 +- src/kiwano/2d/action/Action.h | 10 +- src/kiwano/2d/action/ActionDelay.cpp | 25 +- src/kiwano/2d/action/ActionDelay.h | 6 +- src/kiwano/2d/action/ActionGroup.cpp | 28 +- src/kiwano/2d/action/ActionGroup.h | 12 +- src/kiwano/2d/action/ActionScheduler.cpp | 4 +- src/kiwano/2d/action/ActionScheduler.h | 2 +- src/kiwano/2d/action/ActionTween.cpp | 307 +++++++----------- src/kiwano/2d/action/ActionTween.h | 104 +++--- src/kiwano/2d/action/ActionWalk.cpp | 44 +-- src/kiwano/2d/action/ActionWalk.h | 9 +- src/kiwano/2d/action/Animation.cpp | 41 +-- src/kiwano/2d/action/Animation.h | 11 +- src/kiwano/base/Director.cpp | 7 +- src/kiwano/base/ObjectBase.h | 2 +- src/kiwano/base/ObjectPool.cpp | 5 - src/kiwano/base/ObjectPool.h | 2 - src/kiwano/base/RefPtr.h | 79 +++++ src/kiwano/base/component/Button.cpp | 22 +- src/kiwano/base/component/Button.h | 5 +- src/kiwano/core/Cloneable.h | 3 +- .../core/{RefPtr.hpp => RefBasePtr.hpp} | 100 ++---- src/kiwano/event/Event.h | 14 +- src/kiwano/event/EventDispatcher.cpp | 8 +- src/kiwano/event/EventDispatcher.h | 4 +- src/kiwano/event/EventListener.cpp | 36 +- src/kiwano/event/EventListener.h | 54 +-- src/kiwano/kiwano.h | 2 +- src/kiwano/platform/Application.cpp | 2 +- src/kiwano/platform/Runner.cpp | 42 +-- src/kiwano/platform/Runner.h | 17 +- src/kiwano/platform/win32/ComPtr.hpp | 4 +- src/kiwano/platform/win32/WindowImpl.cpp | 2 +- src/kiwano/render/Brush.cpp | 30 +- src/kiwano/render/Brush.h | 6 +- src/kiwano/render/DirectX/RendererImpl.cpp | 8 +- src/kiwano/render/Font.cpp | 20 +- src/kiwano/render/Font.h | 4 +- src/kiwano/render/Frame.cpp | 29 +- src/kiwano/render/Frame.h | 6 +- src/kiwano/render/FrameSequence.cpp | 30 +- src/kiwano/render/FrameSequence.h | 8 +- src/kiwano/render/GifImage.cpp | 22 +- src/kiwano/render/GifImage.h | 4 +- src/kiwano/render/Shape.cpp | 10 +- src/kiwano/render/ShapeMaker.cpp | 14 +- src/kiwano/render/ShapeMaker.h | 4 - src/kiwano/render/StrokeStyle.cpp | 43 +-- src/kiwano/render/StrokeStyle.h | 16 +- src/kiwano/render/TextLayout.cpp | 22 +- src/kiwano/render/TextLayout.h | 10 +- src/kiwano/render/Texture.cpp | 22 +- src/kiwano/render/Texture.h | 4 +- src/kiwano/render/TextureCache.cpp | 2 +- src/kiwano/utils/ConfigIni.cpp | 14 +- src/kiwano/utils/ConfigIni.h | 12 +- src/kiwano/utils/EventTicker.cpp | 12 +- src/kiwano/utils/EventTicker.h | 6 +- src/kiwano/utils/Logger.cpp | 17 +- src/kiwano/utils/Logger.h | 4 +- src/kiwano/utils/ResourceCache.cpp | 16 +- src/kiwano/utils/Task.cpp | 37 +-- src/kiwano/utils/Task.h | 8 +- src/kiwano/utils/TaskScheduler.cpp | 25 +- src/kiwano/utils/TaskScheduler.h | 28 ++ src/kiwano/utils/Ticker.cpp | 14 +- src/kiwano/utils/Ticker.h | 2 +- src/kiwano/utils/Timer.cpp | 6 - src/kiwano/utils/Timer.h | 4 - 107 files changed, 944 insertions(+), 1597 deletions(-) create mode 100644 src/kiwano/base/RefPtr.h rename src/kiwano/core/{RefPtr.hpp => RefBasePtr.hpp} (64%) diff --git a/projects/kiwano/kiwano.vcxproj b/projects/kiwano/kiwano.vcxproj index ee563d2b..a8e1dbe0 100644 --- a/projects/kiwano/kiwano.vcxproj +++ b/projects/kiwano/kiwano.vcxproj @@ -18,6 +18,7 @@ + @@ -52,7 +53,7 @@ - + diff --git a/projects/kiwano/kiwano.vcxproj.filters b/projects/kiwano/kiwano.vcxproj.filters index 95c61c79..fd77980d 100644 --- a/projects/kiwano/kiwano.vcxproj.filters +++ b/projects/kiwano/kiwano.vcxproj.filters @@ -348,10 +348,13 @@ 2d\action - + + base + + core - + base diff --git a/src/kiwano-audio/Sound.cpp b/src/kiwano-audio/Sound.cpp index c8da50d7..f04421c4 100644 --- a/src/kiwano-audio/Sound.cpp +++ b/src/kiwano-audio/Sound.cpp @@ -27,26 +27,16 @@ namespace kiwano { namespace audio { -SoundPtr Sound::Create(const String& file_path) +Sound::Sound(const String& file_path) + : Sound() { - SoundPtr ptr = new (autogc) Sound; - if (ptr) - { - if (!ptr->Load(file_path)) - return nullptr; - } - return ptr; + Load(file_path); } -SoundPtr Sound::Create(const Resource& res) +Sound::Sound(const Resource& res) + : Sound() { - SoundPtr ptr = new (autogc) Sound; - if (ptr) - { - if (!ptr->Load(res)) - return nullptr; - } - return ptr; + Load(res); } Sound::Sound() diff --git a/src/kiwano-audio/Sound.h b/src/kiwano-audio/Sound.h index 299d9083..ebd2ce84 100644 --- a/src/kiwano-audio/Sound.h +++ b/src/kiwano-audio/Sound.h @@ -50,12 +50,12 @@ public: /// \~chinese /// @brief 创建音频对象 /// @param res 本地音频文件路径 - static SoundPtr Create(const String& file_path); + Sound(const String& file_path); /// \~chinese /// @brief 创建音频对象 /// @param res 音频资源 - static SoundPtr Create(const Resource& res); + Sound(const Resource& res); Sound(); diff --git a/src/kiwano-audio/SoundPlayer.cpp b/src/kiwano-audio/SoundPlayer.cpp index 31efc911..bfe7a562 100644 --- a/src/kiwano-audio/SoundPlayer.cpp +++ b/src/kiwano-audio/SoundPlayer.cpp @@ -25,12 +25,6 @@ namespace kiwano namespace audio { -SoundPlayerPtr SoundPlayer::Create() -{ - SoundPlayerPtr ptr = new (autogc) SoundPlayer; - return ptr; -} - SoundPlayer::SoundPlayer() : volume_(1.f) { @@ -47,7 +41,7 @@ size_t SoundPlayer::Load(const String& file_path) if (sound_cache_.end() != sound_cache_.find(hash)) return hash; - SoundPtr sound = new (autogc) Sound; + SoundPtr sound = MakePtr(); if (sound) { if (sound->Load(file_path)) @@ -66,7 +60,7 @@ size_t SoundPlayer::Load(const Resource& res) if (sound_cache_.end() != sound_cache_.find(hash_code)) return hash_code; - SoundPtr sound = new (autogc) Sound; + SoundPtr sound = MakePtr(); if (sound) { diff --git a/src/kiwano-audio/SoundPlayer.h b/src/kiwano-audio/SoundPlayer.h index 789c66b5..d9ae2719 100644 --- a/src/kiwano-audio/SoundPlayer.h +++ b/src/kiwano-audio/SoundPlayer.h @@ -40,10 +40,6 @@ KGE_DECLARE_SMART_PTR(SoundPlayer); class KGE_API SoundPlayer : public ObjectBase { public: - /// \~chinese - /// @brief 创建音频播放器 - static SoundPlayerPtr Create(); - SoundPlayer(); ~SoundPlayer(); diff --git a/src/kiwano-imgui/ImGuiLayer.cpp b/src/kiwano-imgui/ImGuiLayer.cpp index 68b2d161..128f28d2 100644 --- a/src/kiwano-imgui/ImGuiLayer.cpp +++ b/src/kiwano-imgui/ImGuiLayer.cpp @@ -25,27 +25,17 @@ namespace kiwano namespace imgui { -ImGuiLayerPtr ImGuiLayer::Create() -{ - ImGuiLayerPtr ptr = new (autogc) ImGuiLayer; - return ptr; -} - -ImGuiLayerPtr ImGuiLayer::Create(const String& name, const ImGuiPipeline& item) -{ - ImGuiLayerPtr ptr = new (autogc) ImGuiLayer; - if (ptr) - { - ptr->AddItem(name, item); - } - return ptr; -} - ImGuiLayer::ImGuiLayer() { SetSwallowEvents(true); } +ImGuiLayer::ImGuiLayer(const String& name, const ImGuiPipeline& item) + : ImGuiLayer() +{ + AddItem(name, item); +} + ImGuiLayer::~ImGuiLayer() {} void ImGuiLayer::OnRender(RenderContext& ctx) diff --git a/src/kiwano-imgui/ImGuiLayer.h b/src/kiwano-imgui/ImGuiLayer.h index 67417f42..1d52d5ae 100644 --- a/src/kiwano-imgui/ImGuiLayer.h +++ b/src/kiwano-imgui/ImGuiLayer.h @@ -38,17 +38,13 @@ using ImGuiPipeline = Function; class ImGuiLayer : public LayerActor { public: - /// \~chinese - /// @brief 创建ImGui图层 - static ImGuiLayerPtr Create(); + ImGuiLayer(); /// \~chinese /// @brief 创建ImGui图层 /// @param name 元素名称 /// @param item 管道 - static ImGuiLayerPtr Create(const String& name, const ImGuiPipeline& item); - - ImGuiLayer(); + ImGuiLayer(const String& name, const ImGuiPipeline& item); virtual ~ImGuiLayer(); diff --git a/src/kiwano-network/HttpModule.cpp b/src/kiwano-network/HttpModule.cpp index ab1d9f88..83f16e69 100644 --- a/src/kiwano-network/HttpModule.cpp +++ b/src/kiwano-network/HttpModule.cpp @@ -253,7 +253,7 @@ void HttpModule::NetworkThread() return; } - HttpResponsePtr response = new (autogc) HttpResponse(request); + HttpResponsePtr response = MakePtr(request); Perform(request, response); response_mutex_.lock(); diff --git a/src/kiwano-network/HttpRequest.cpp b/src/kiwano-network/HttpRequest.cpp index 18ef0abf..923a72d7 100644 --- a/src/kiwano-network/HttpRequest.cpp +++ b/src/kiwano-network/HttpRequest.cpp @@ -26,42 +26,23 @@ namespace kiwano namespace network { -HttpRequestPtr HttpRequest::Create(const String& url, HttpType type, const ResponseCallback& callback) +HttpRequest::HttpRequest(const String& url, HttpType type, const ResponseCallback& callback) + : type_(type) + , url_(url) + , response_cb_(callback) { - HttpRequestPtr ptr = new (autogc) HttpRequest; - if (ptr) - { - ptr->SetUrl(url); - ptr->SetType(type); - ptr->SetResponseCallback(callback); - } - return ptr; } -HttpRequestPtr HttpRequest::Create(const String& url, HttpType type, const String& data, const ResponseCallback& callback) +HttpRequest::HttpRequest(const String& url, HttpType type, const String& data, const ResponseCallback& callback) + : HttpRequest(url, type, callback) { - HttpRequestPtr ptr = new (autogc) HttpRequest; - if (ptr) - { - ptr->SetUrl(url); - ptr->SetType(type); - ptr->SetData(data); - ptr->SetResponseCallback(callback); - } - return ptr; + SetData(data); } -HttpRequestPtr HttpRequest::Create(const String& url, HttpType type, const Json& json, const ResponseCallback& callback) +HttpRequest::HttpRequest(const String& url, HttpType type, const Json& json, const ResponseCallback& callback) + : HttpRequest(url, type, callback) { - HttpRequestPtr ptr = new (autogc) HttpRequest; - if (ptr) - { - ptr->SetUrl(url); - ptr->SetType(type); - ptr->SetJsonData(json); - ptr->SetResponseCallback(callback); - } - return ptr; + SetJsonData(json); } void HttpRequest::SetJsonData(const Json& json) diff --git a/src/kiwano-network/HttpRequest.h b/src/kiwano-network/HttpRequest.h index 1760c3ea..51203b7a 100644 --- a/src/kiwano-network/HttpRequest.h +++ b/src/kiwano-network/HttpRequest.h @@ -63,7 +63,7 @@ public: /// @param url 请求地址 /// @param type 请求类型 /// @param callback 响应回调函数 - static HttpRequestPtr Create(const String& url, HttpType type, const ResponseCallback& callback); + HttpRequest(const String& url, HttpType type, const ResponseCallback& callback); /// \~chinese /// @brief 创建HTTP请求 @@ -71,7 +71,7 @@ public: /// @param type 请求类型 /// @param data 请求数据 /// @param callback 响应回调函数 - static HttpRequestPtr Create(const String& url, HttpType type, const String& data, const ResponseCallback& callback); + HttpRequest(const String& url, HttpType type, const String& data, const ResponseCallback& callback); /// \~chinese /// @brief 创建HTTP请求 @@ -79,12 +79,10 @@ public: /// @param type 请求类型 /// @param json 请求的JSON数据 /// @param callback 响应回调函数 - static HttpRequestPtr Create(const String& url, HttpType type, const Json& json, const ResponseCallback& callback); + HttpRequest(const String& url, HttpType type, const Json& json, const ResponseCallback& callback); HttpRequest(); - HttpRequest(HttpType type); - /// \~chinese /// @brief 设置请求地址 void SetUrl(const String& url); @@ -152,11 +150,6 @@ inline HttpRequest::HttpRequest() { } -inline HttpRequest::HttpRequest(HttpType type) - : type_(type) -{ -} - inline void HttpRequest::SetUrl(const String& url) { url_ = url; diff --git a/src/kiwano-physics/Fixture.cpp b/src/kiwano-physics/Fixture.cpp index d6e14cbd..ca217610 100644 --- a/src/kiwano-physics/Fixture.cpp +++ b/src/kiwano-physics/Fixture.cpp @@ -29,7 +29,7 @@ namespace physics FixturePtr Fixture::CreateCircle(const Param& param, float radius, const Point& offset) { - FixturePtr ptr = new (autogc) Fixture; + FixturePtr ptr = MakePtr(); if (ptr) { auto shape = std::make_unique(); @@ -44,7 +44,7 @@ FixturePtr Fixture::CreateCircle(const Param& param, float radius, const Point& FixturePtr Fixture::CreateRect(const Param& param, const Size& size, const Point& offset, float rotation) { - FixturePtr ptr = new (autogc) Fixture; + FixturePtr ptr = MakePtr(); if (ptr) { b2Vec2 b2size = global::LocalToWorld(size); @@ -61,7 +61,7 @@ FixturePtr Fixture::CreateRect(const Param& param, const Size& size, const Point FixturePtr Fixture::CreatePolygon(const Param& param, const Vector& vertexs) { - FixturePtr ptr = new (autogc) Fixture; + FixturePtr ptr = MakePtr(); if (ptr) { Vector b2vertexs; @@ -82,7 +82,7 @@ FixturePtr Fixture::CreatePolygon(const Param& param, const Vector& verte FixturePtr Fixture::CreateEdge(const Param& param, const Point& p1, const Point& p2) { - FixturePtr ptr = new (autogc) Fixture; + FixturePtr ptr = MakePtr(); if (ptr) { b2Vec2 start = global::LocalToWorld(p1); @@ -99,7 +99,7 @@ FixturePtr Fixture::CreateEdge(const Param& param, const Point& p1, const Point& FixturePtr Fixture::CreateChain(const Param& param, const Vector& vertices, bool loop) { - FixturePtr ptr = new (autogc) Fixture; + FixturePtr ptr = MakePtr(); if (ptr) { Vector b2vertices; diff --git a/src/kiwano-physics/Joint.cpp b/src/kiwano-physics/Joint.cpp index 220d3d29..6c3d06ae 100644 --- a/src/kiwano-physics/Joint.cpp +++ b/src/kiwano-physics/Joint.cpp @@ -107,18 +107,9 @@ void Joint::Destroy() // DistanceJoint // -DistanceJointPtr DistanceJoint::Create(const Param& param) -{ - DistanceJointPtr ptr = new (autogc) DistanceJoint; - if (ptr) - { - ptr->param_ = param; - } - return ptr; -} - -DistanceJoint::DistanceJoint() - : raw_joint_(nullptr) +DistanceJoint::DistanceJoint(const Param& param) + : param_(param) + , raw_joint_(nullptr) { } @@ -154,18 +145,9 @@ float DistanceJoint::GetLength() const // FrictionJoint // -FrictionJointPtr FrictionJoint::Create(const Param& param) -{ - FrictionJointPtr ptr = new (autogc) FrictionJoint; - if (ptr) - { - ptr->param_ = param; - } - return ptr; -} - -FrictionJoint::FrictionJoint() - : raw_joint_(nullptr) +FrictionJoint::FrictionJoint(const Param& param) + : param_(param) + , raw_joint_(nullptr) { } @@ -212,18 +194,9 @@ float FrictionJoint::GetMaxTorque() const // GearJoint // -GearJointPtr GearJoint::Create(const Param& param) -{ - GearJointPtr ptr = new (autogc) GearJoint; - if (ptr) - { - ptr->param_ = param; - } - return ptr; -} - -GearJoint::GearJoint() - : raw_joint_(nullptr) +GearJoint::GearJoint(const Param& param) + : param_(param) + , raw_joint_(nullptr) { } @@ -258,18 +231,9 @@ float GearJoint::GetRatio() const // MotorJoint // -MotorJointPtr MotorJoint::Create(const Param& param) -{ - MotorJointPtr ptr = new (autogc) MotorJoint; - if (ptr) - { - ptr->param_ = param; - } - return ptr; -} - -MotorJoint::MotorJoint() - : raw_joint_(nullptr) +MotorJoint::MotorJoint(const Param& param) + : param_(param) + , raw_joint_(nullptr) { } @@ -317,18 +281,9 @@ float MotorJoint::GetMaxTorque() const // PrismaticJoint // -PrismaticJointPtr PrismaticJoint::Create(const Param& param) -{ - PrismaticJointPtr ptr = new (autogc) PrismaticJoint; - if (ptr) - { - ptr->param_ = param; - } - return ptr; -} - -PrismaticJoint::PrismaticJoint() - : raw_joint_(nullptr) +PrismaticJoint::PrismaticJoint(const Param& param) + : param_(param) + , raw_joint_(nullptr) { } @@ -386,18 +341,9 @@ void PrismaticJoint::SetLimits(float lower, float upper) // PulleyJoint // -PulleyJointPtr PulleyJoint::Create(const Param& param) -{ - PulleyJointPtr ptr = new (autogc) PulleyJoint; - if (ptr) - { - ptr->param_ = param; - } - return ptr; -} - -PulleyJoint::PulleyJoint() - : raw_joint_(nullptr) +PulleyJoint::PulleyJoint(const Param& param) + : param_(param) + , raw_joint_(nullptr) { } @@ -462,18 +408,9 @@ float PulleyJoint::GetCurrentLengthB() const // RevoluteJoint // -RevoluteJointPtr RevoluteJoint::Create(const Param& param) -{ - RevoluteJointPtr ptr = new (autogc) RevoluteJoint; - if (ptr) - { - ptr->param_ = param; - } - return ptr; -} - -RevoluteJoint::RevoluteJoint() - : raw_joint_(nullptr) +RevoluteJoint::RevoluteJoint(const Param& param) + : param_(param) + , raw_joint_(nullptr) { } @@ -542,18 +479,9 @@ float RevoluteJoint::GetMaxMotorTorque() const // RopeJoint // -RopeJointPtr RopeJoint::Create(const Param& param) -{ - RopeJointPtr ptr = new (autogc) RopeJoint; - if (ptr) - { - ptr->param_ = param; - } - return ptr; -} - -RopeJoint::RopeJoint() - : raw_joint_(nullptr) +RopeJoint::RopeJoint(const Param& param) + : param_(param) + , raw_joint_(nullptr) { } @@ -590,18 +518,9 @@ float RopeJoint::GetMaxLength() const // WeldJoint // -WeldJointPtr WeldJoint::Create(const Param& param) -{ - WeldJointPtr ptr = new (autogc) WeldJoint; - if (ptr) - { - ptr->param_ = param; - } - return ptr; -} - -WeldJoint::WeldJoint() - : raw_joint_(nullptr) +WeldJoint::WeldJoint(const Param& param) + : param_(param) + , raw_joint_(nullptr) { } @@ -624,18 +543,9 @@ bool WeldJoint::Init(PhysicWorld* world) // WheelJoint // -WheelJointPtr WheelJoint::Create(const Param& param) -{ - WheelJointPtr ptr = new (autogc) WheelJoint; - if (ptr) - { - ptr->param_ = param; - } - return ptr; -} - -WheelJoint::WheelJoint() - : raw_joint_(nullptr) +WheelJoint::WheelJoint(const Param& param) + : param_(param) + , raw_joint_(nullptr) { } @@ -686,18 +596,9 @@ float WheelJoint::GetMaxMotorTorque() const // MouseJoint // -MouseJointPtr MouseJoint::Create(const Param& param) -{ - MouseJointPtr ptr = new (autogc) MouseJoint; - if (ptr) - { - ptr->param_ = param; - } - return ptr; -} - -MouseJoint::MouseJoint() - : raw_joint_(nullptr) +MouseJoint::MouseJoint(const Param& param) + : param_(param) + , raw_joint_(nullptr) { } diff --git a/src/kiwano-physics/Joint.h b/src/kiwano-physics/Joint.h index afd9c152..059f5812 100644 --- a/src/kiwano-physics/Joint.h +++ b/src/kiwano-physics/Joint.h @@ -161,9 +161,7 @@ public: /// \~chinese /// @brief 创建固定距离关节 /// @param param 关节参数 - static DistanceJointPtr Create(const Param& param); - - DistanceJoint(); + DistanceJoint(const Param& param); /// \~chinese /// @brief 初始化关节 @@ -226,9 +224,7 @@ public: /// \~chinese /// @brief 创建摩擦关节 /// @param param 关节参数 - static FrictionJointPtr Create(const Param& param); - - FrictionJoint(); + FrictionJoint(const Param& param); /// \~chinese /// @brief 初始化关节 @@ -285,9 +281,7 @@ public: /// \~chinese /// @brief 创建齿轮关节 /// @param param 关节参数 - static GearJointPtr Create(const Param& param); - - GearJoint(); + GearJoint(const Param& param); /// \~chinese /// @brief 初始化关节 @@ -336,9 +330,7 @@ public: /// \~chinese /// @brief 创建马达关节 /// @param param 关节参数 - static MotorJointPtr Create(const Param& param); - - MotorJoint(); + MotorJoint(const Param& param); /// \~chinese /// @brief 初始化关节 @@ -405,9 +397,7 @@ public: /// \~chinese /// @brief 创建平移关节 /// @param param 关节参数 - static PrismaticJointPtr Create(const Param& param); - - PrismaticJoint(); + PrismaticJoint(const Param& param); /// \~chinese /// @brief 初始化关节 @@ -509,9 +499,7 @@ public: /// \~chinese /// @brief 创建滑轮关节 /// @param param 关节参数 - static PulleyJointPtr Create(const Param& param); - - PulleyJoint(); + PulleyJoint(const Param& param); /// \~chinese /// @brief 初始化关节 @@ -588,9 +576,7 @@ public: /// \~chinese /// @brief 创建旋转关节 /// @param param 关节参数 - static RevoluteJointPtr Create(const Param& param); - - RevoluteJoint(); + RevoluteJoint(const Param& param); /// \~chinese /// @brief 初始化关节 @@ -687,9 +673,7 @@ public: /// \~chinese /// @brief 创建绳关节 /// @param param 关节参数 - static RopeJointPtr Create(const Param& param); - - RopeJoint(); + RopeJoint(const Param& param); /// \~chinese /// @brief 初始化关节 @@ -738,9 +722,7 @@ public: /// \~chinese /// @brief 创建焊接关节 /// @param param 关节参数 - static WeldJointPtr Create(const Param& param); - - WeldJoint(); + WeldJoint(const Param& param); /// \~chinese /// @brief 初始化关节 @@ -809,9 +791,7 @@ public: /// \~chinese /// @brief 创建轮关节 /// @param param 关节参数 - static WheelJointPtr Create(const Param& param); - - WheelJoint(); + WheelJoint(const Param& param); /// \~chinese /// @brief 初始化关节 @@ -911,9 +891,7 @@ public: /// \~chinese /// @brief 创建鼠标关节 /// @param param 关节参数 - static MouseJointPtr Create(const Param& param); - - MouseJoint(); + MouseJoint(const Param& param); /// \~chinese /// @brief 初始化关节 diff --git a/src/kiwano-physics/PhysicBody.cpp b/src/kiwano-physics/PhysicBody.cpp index 770b7101..b5da59d7 100644 --- a/src/kiwano-physics/PhysicBody.cpp +++ b/src/kiwano-physics/PhysicBody.cpp @@ -28,37 +28,20 @@ namespace kiwano namespace physics { -PhysicBodyPtr PhysicBody::Create(PhysicWorldPtr world, Type type) -{ - return PhysicBody::Create(world.Get(), type); -} - -PhysicBodyPtr PhysicBody::Create(PhysicWorld* world, Type type) -{ - KGE_ASSERT(world); - - PhysicBodyPtr ptr = new (autogc) PhysicBody; - if (ptr) - { - ptr->SetType(type); - if (ptr->Init(world)) - { - world->AddBody(ptr); - return ptr; - } - } - return nullptr; -} - -PhysicBody::PhysicBody() +PhysicBody::PhysicBody(PhysicWorld* world, Type type) : body_(nullptr) - , world_(nullptr) + , world_(world) , type_(Type::Static) , category_bits_(0x0001) , mask_bits_(0xFFFF) , group_index_(0) { SetName(KGE_PHYSIC_COMP_NAME); + + if (Init(world)) + { + world->AddBody(this); + } } PhysicBody::~PhysicBody() {} @@ -88,11 +71,6 @@ void PhysicBody::DestroyComponent() } } -bool PhysicBody::Init(PhysicWorldPtr world) -{ - return Init(world.Get()); -} - bool PhysicBody::Init(PhysicWorld* world) { KGE_ASSERT(body_ == nullptr); diff --git a/src/kiwano-physics/PhysicBody.h b/src/kiwano-physics/PhysicBody.h index 875e4a8a..296193b5 100644 --- a/src/kiwano-physics/PhysicBody.h +++ b/src/kiwano-physics/PhysicBody.h @@ -54,23 +54,10 @@ public: /// @brief 初始化物体 /// @param world 物理世界 /// @param type 物体类型 - static PhysicBodyPtr Create(PhysicWorldPtr world, Type type); - - /// \~chinese - /// @brief 初始化物体 - /// @param world 物理世界 - /// @param type 物体类型 - static PhysicBodyPtr Create(PhysicWorld* world, Type type); - - PhysicBody(); + PhysicBody(PhysicWorld* world, Type type); virtual ~PhysicBody(); - /// \~chinese - /// @brief 初始化物体 - /// @param[in] world 物理世界 - bool Init(PhysicWorldPtr world); - /// \~chinese /// @brief 初始化物体 /// @param[in] world 物理世界 diff --git a/src/kiwano-physics/PhysicWorld.cpp b/src/kiwano-physics/PhysicWorld.cpp index b712a343..acdd9ce4 100644 --- a/src/kiwano-physics/PhysicWorld.cpp +++ b/src/kiwano-physics/PhysicWorld.cpp @@ -31,11 +31,11 @@ class PhysicWorld::DebugDrawer : public b2Draw public: DebugDrawer(const Size& size) { - canvas_ = Canvas::Create(size); + canvas_ = MakePtr(size); ctx_ = canvas_->GetContext2D(); - fill_brush_ = Brush::Create(Color::White); - line_brush_ = Brush::Create(Color::White); + fill_brush_ = MakePtr(Color::White); + line_brush_ = MakePtr(Color::White); b2Draw::SetFlags(b2Draw::e_shapeBit | b2Draw::e_jointBit | b2Draw::e_jointBit | b2Draw::e_centerOfMassBit); } @@ -211,20 +211,10 @@ public: } }; -PhysicWorldPtr PhysicWorld::Create() +PhysicWorld::PhysicWorld(const Vec2& gravity) + : PhysicWorld() { - PhysicWorldPtr ptr = new (autogc) PhysicWorld; - return ptr; -} - -PhysicWorldPtr PhysicWorld::Create(const Vec2& gravity) -{ - PhysicWorldPtr ptr = new (autogc) PhysicWorld; - if (ptr) - { - ptr->SetGravity(gravity); - } - return ptr; + SetGravity(gravity); } PhysicWorld::PhysicWorld() diff --git a/src/kiwano-physics/PhysicWorld.h b/src/kiwano-physics/PhysicWorld.h index ed1af0d5..472959a2 100644 --- a/src/kiwano-physics/PhysicWorld.h +++ b/src/kiwano-physics/PhysicWorld.h @@ -47,16 +47,12 @@ class KGE_API PhysicWorld : public Component friend class Joint; public: - /// \~chinese - /// @brief 创建物理世界 - static PhysicWorldPtr Create(); + PhysicWorld(); /// \~chinese /// @brief 创建物理世界 /// @param gravity 重力 - static PhysicWorldPtr Create(const Vec2& gravity); - - PhysicWorld(); + PhysicWorld(const Vec2& gravity); virtual ~PhysicWorld(); diff --git a/src/kiwano/2d/Actor.cpp b/src/kiwano/2d/Actor.cpp index cc8265d4..faee8709 100644 --- a/src/kiwano/2d/Actor.cpp +++ b/src/kiwano/2d/Actor.cpp @@ -39,12 +39,6 @@ void Actor::SetDefaultAnchor(float anchor_x, float anchor_y) default_anchor_y = anchor_y; } -ActorPtr Actor::Create() -{ - ActorPtr ptr = new (autogc) Actor; - return ptr; -} - Actor::Actor() : ComponentManager(this) , visible_(true) @@ -264,18 +258,18 @@ bool Actor::HandleEvent(Event* evt) { hover_ = true; - MouseHoverEventPtr hover = new (autogc) MouseHoverEvent; - hover->pos = mouse_evt->pos; - HandleEvent(hover.Get()); + auto hover = new (autogc) MouseHoverEvent; + hover->pos = mouse_evt->pos; + HandleEvent(hover); } else if (hover_ && !contains) { hover_ = false; pressed_ = false; - MouseOutEventPtr out = new (autogc) MouseOutEvent; - out->pos = mouse_evt->pos; - HandleEvent(out.Get()); + auto out = new (autogc) MouseOutEvent; + out->pos = mouse_evt->pos; + HandleEvent(out); } } @@ -290,10 +284,10 @@ bool Actor::HandleEvent(Event* evt) auto mouse_up_evt = dynamic_cast(evt); - MouseClickEventPtr click = new (autogc) MouseClickEvent; - click->pos = mouse_up_evt->pos; - click->button = mouse_up_evt->button; - HandleEvent(click.Get()); + auto click = new (autogc) MouseClickEvent; + click->pos = mouse_up_evt->pos; + click->button = mouse_up_evt->button; + HandleEvent(click); } } return true; diff --git a/src/kiwano/2d/Actor.h b/src/kiwano/2d/Actor.h index b9193ad1..e99b12b6 100644 --- a/src/kiwano/2d/Actor.h +++ b/src/kiwano/2d/Actor.h @@ -77,10 +77,6 @@ public: /// @brief 角色更新回调函数 typedef Function UpdateCallback; - /// \~chinese - /// @brief 创建角色 - static ActorPtr Create(); - Actor(); virtual ~Actor(); diff --git a/src/kiwano/2d/Canvas.cpp b/src/kiwano/2d/Canvas.cpp index cc848a26..eb2179cc 100644 --- a/src/kiwano/2d/Canvas.cpp +++ b/src/kiwano/2d/Canvas.cpp @@ -24,25 +24,11 @@ namespace kiwano { -CanvasPtr Canvas::Create(const Size& size) +Canvas::Canvas(const Size& size) { - CanvasPtr ptr = new (autogc) Canvas; - if (ptr) - { - try - { - ptr->ResizeAndClear(size); - } - catch (std::exception) - { - return nullptr; - } - } - return ptr; + ResizeAndClear(size); } -Canvas::Canvas() {} - RenderContextPtr Canvas::GetContext2D() const { return render_ctx_; @@ -58,7 +44,7 @@ void Canvas::OnRender(RenderContext& ctx) void Canvas::ResizeAndClear(Size size) { - texture_cached_ = new (autogc) Texture; + texture_cached_ = MakePtr(); render_ctx_ = RenderContext::Create(*texture_cached_, size); SetSize(render_ctx_->GetSize()); diff --git a/src/kiwano/2d/Canvas.h b/src/kiwano/2d/Canvas.h index f0f147f0..e625ec04 100644 --- a/src/kiwano/2d/Canvas.h +++ b/src/kiwano/2d/Canvas.h @@ -45,7 +45,7 @@ public: /// \~chinese /// @brief 创建画布 /// @param size 画布大小 - static CanvasPtr Create(const Size& size); + Canvas(const Size& size); /// \~chinese /// @brief 获取2D绘图上下文 @@ -62,9 +62,6 @@ public: void OnRender(RenderContext& ctx) override; -private: - Canvas(); - private: TexturePtr texture_cached_; RenderContextPtr render_ctx_; diff --git a/src/kiwano/2d/DebugActor.cpp b/src/kiwano/2d/DebugActor.cpp index 048c29c1..2fbdba50 100644 --- a/src/kiwano/2d/DebugActor.cpp +++ b/src/kiwano/2d/DebugActor.cpp @@ -54,10 +54,10 @@ DebugActor::DebugActor() comma_locale_ = std::locale(std::locale(), new comma_numpunct); - background_brush_ = new (autogc) Brush; + background_brush_ = MakePtr(); background_brush_->SetColor(Color::Rgba(0x000000, 0.7f)); - BrushPtr fill_brush = new (autogc) Brush; + BrushPtr fill_brush = MakePtr(); fill_brush->SetColor(Color::White); debug_text_style_.font_family = "Arial"; diff --git a/src/kiwano/2d/GifSprite.cpp b/src/kiwano/2d/GifSprite.cpp index 68996b63..800d17a1 100644 --- a/src/kiwano/2d/GifSprite.cpp +++ b/src/kiwano/2d/GifSprite.cpp @@ -25,38 +25,6 @@ namespace kiwano { -GifSpritePtr GifSprite::Create(const String& file_path) -{ - GifSpritePtr ptr = new (autogc) GifSprite; - if (ptr) - { - if (!ptr->Load(file_path)) - return nullptr; - } - return ptr; -} - -GifSpritePtr GifSprite::Create(const Resource& res) -{ - GifSpritePtr ptr = new (autogc) GifSprite; - if (ptr) - { - if (!ptr->Load(res)) - return nullptr; - } - return ptr; -} - -GifSpritePtr GifSprite::Create(GifImagePtr gif) -{ - GifSpritePtr ptr = new (autogc) GifSprite; - if (ptr) - { - ptr->SetGifImage(gif); - } - return ptr; -} - GifSprite::GifSprite() : animating_(false) , next_index_(0) @@ -65,6 +33,24 @@ GifSprite::GifSprite() { } +GifSprite::GifSprite(const String& file_path) + : GifSprite() +{ + Load(file_path); +} + +GifSprite::GifSprite(const Resource& res) + : GifSprite() +{ + Load(res); +} + +GifSprite::GifSprite(GifImagePtr gif) + : GifSprite() +{ + SetGifImage(gif); +} + bool GifSprite::Load(const String& file_path) { GifImagePtr image = TextureCache::GetInstance().AddOrGetGifImage(file_path); @@ -92,7 +78,7 @@ bool GifSprite::Load(GifImagePtr gif) frame_rt_.Reset(); Size frame_size = Size(float(gif_->GetWidthInPixels()), float(gif_->GetHeightInPixels())); - frame_to_render_ = new (autogc) Texture; + frame_to_render_ = MakePtr(); frame_rt_ = RenderContext::Create(*frame_to_render_, frame_size); SetSize(frame_rt_->GetSize()); @@ -229,7 +215,7 @@ void GifSprite::SaveComposedFrame() if (!saved_frame_) { - saved_frame_ = new (autogc) Texture; + saved_frame_ = MakePtr(); frame_rt_->CreateTexture(*saved_frame_, frame_to_render_->GetSizeInPixels()); } saved_frame_->CopyFrom(frame_to_render_); diff --git a/src/kiwano/2d/GifSprite.h b/src/kiwano/2d/GifSprite.h index 12dd1a70..cc575b5e 100644 --- a/src/kiwano/2d/GifSprite.h +++ b/src/kiwano/2d/GifSprite.h @@ -48,22 +48,22 @@ public: /// @brief GIF播放结束回调 using DoneCallback = Function; + GifSprite(); + /// \~chinese /// @brief 创建GIF精灵 /// @param file_path GIF图片路径 - static GifSpritePtr Create(const String& file_path); + GifSprite(const String& file_path); /// \~chinese /// @brief 创建GIF精灵 /// @param res GIF图片资源 - static GifSpritePtr Create(const Resource& res); + GifSprite(const Resource& res); /// \~chinese /// @brief 创建GIF精灵 /// @param gif GIF图片 - static GifSpritePtr Create(GifImagePtr gif); - - GifSprite(); + GifSprite(GifImagePtr gif); /// \~chinese /// @brief 加载GIF图片 diff --git a/src/kiwano/2d/LayerActor.cpp b/src/kiwano/2d/LayerActor.cpp index f49ffe26..a45aec31 100644 --- a/src/kiwano/2d/LayerActor.cpp +++ b/src/kiwano/2d/LayerActor.cpp @@ -25,12 +25,6 @@ namespace kiwano { -LayerActorPtr LayerActor::Create() -{ - LayerActorPtr ptr = new (autogc) LayerActor; - return ptr; -} - LayerActor::LayerActor() : swallow_(false) { diff --git a/src/kiwano/2d/LayerActor.h b/src/kiwano/2d/LayerActor.h index 38cd2454..53d486b1 100644 --- a/src/kiwano/2d/LayerActor.h +++ b/src/kiwano/2d/LayerActor.h @@ -39,10 +39,6 @@ KGE_DECLARE_SMART_PTR(LayerActor); class KGE_API LayerActor : public Actor { public: - /// \~chinese - /// @brief 创建图层 - static LayerActorPtr Create(); - LayerActor(); virtual ~LayerActor(); diff --git a/src/kiwano/2d/ShapeActor.cpp b/src/kiwano/2d/ShapeActor.cpp index c837cf43..ba80091e 100644 --- a/src/kiwano/2d/ShapeActor.cpp +++ b/src/kiwano/2d/ShapeActor.cpp @@ -25,40 +25,25 @@ namespace kiwano { -ShapeActorPtr ShapeActor::Create(ShapePtr shape) +ShapeActor::ShapeActor() {} + +ShapeActor::ShapeActor(ShapePtr shape) { - ShapeActorPtr ptr = new (autogc) ShapeActor; - if (ptr) - { - ptr->SetShape(shape); - } - return ptr; + SetShape(shape); } -ShapeActorPtr ShapeActor::Create(ShapePtr shape, const Color& fill_color, const Color& stroke_color) +ShapeActor::ShapeActor(ShapePtr shape, const Color& fill_color, const Color& stroke_color) + : ShapeActor(shape) { - ShapeActorPtr ptr = ShapeActor::Create(shape); - if (ptr) - { - ptr->SetFillColor(fill_color); - ptr->SetStrokeColor(stroke_color); - } - return ptr; + SetFillColor(fill_color); + SetStrokeColor(stroke_color); } -ShapeActorPtr ShapeActor::Create(ShapePtr shape, BrushPtr fill_brush, BrushPtr stroke_brush) -{ - ShapeActorPtr ptr = ShapeActor::Create(shape); - if (ptr) - { - ptr->SetFillBrush(fill_brush); - ptr->SetStrokeBrush(stroke_brush); - } - return ptr; -} - -ShapeActor::ShapeActor() +ShapeActor::ShapeActor(ShapePtr shape, BrushPtr fill_brush, BrushPtr stroke_brush) + : ShapeActor(shape) { + SetFillBrush(fill_brush); + SetStrokeBrush(stroke_brush); } ShapeActor::~ShapeActor() {} @@ -127,18 +112,13 @@ bool ShapeActor::CheckVisibility(RenderContext& ctx) const // LineActor //------------------------------------------------------- -LineActorPtr LineActor::Create(const Point& begin, const Point& end) -{ - LineActorPtr ptr = new (autogc) LineActor; - if (ptr) - { - ptr->SetLine(begin, end); - } - return ptr; -} - LineActor::LineActor() {} +LineActor::LineActor(const Point& begin, const Point& end) +{ + SetLine(begin, end); +} + LineActor::~LineActor() {} void LineActor::SetLine(const Point& begin, const Point& end) @@ -155,18 +135,13 @@ void LineActor::SetLine(const Point& begin, const Point& end) // RectActor //------------------------------------------------------- -RectActorPtr RectActor::Create(const Size& size) -{ - RectActorPtr ptr = new (autogc) RectActor; - if (ptr) - { - ptr->SetRectSize(size); - } - return ptr; -} - RectActor::RectActor() {} +RectActor::RectActor(const Size& size) +{ + SetRectSize(size); +} + RectActor::~RectActor() {} void RectActor::SetRectSize(const Size& size) @@ -182,18 +157,13 @@ void RectActor::SetRectSize(const Size& size) // RoundedRectActor //------------------------------------------------------- -RoundedRectActorPtr RoundedRectActor::Create(const Size& size, const Vec2& radius) -{ - RoundedRectActorPtr ptr = new (autogc) RoundedRectActor; - if (ptr) - { - ptr->SetRoundedRect(size, radius); - } - return ptr; -} - RoundedRectActor::RoundedRectActor() {} +RoundedRectActor::RoundedRectActor(const Size& size, const Vec2& radius) +{ + SetRoundedRect(size, radius); +} + RoundedRectActor::~RoundedRectActor() {} void RoundedRectActor::SetRadius(const Vec2& radius) @@ -220,21 +190,17 @@ void RoundedRectActor::SetRoundedRect(const Size& size, const Vec2& radius) // CircleActor //------------------------------------------------------- -CircleActorPtr CircleActor::Create(float radius) -{ - CircleActorPtr ptr = new (autogc) CircleActor; - if (ptr) - { - ptr->SetRadius(radius); - } - return ptr; -} - CircleActor::CircleActor() : radius_(0.f) { } +CircleActor::CircleActor(float radius) + : radius_(0.f) +{ + SetRadius(radius); +} + CircleActor::~CircleActor() {} void CircleActor::SetRadius(float radius) @@ -250,18 +216,13 @@ void CircleActor::SetRadius(float radius) // EllipseActor //------------------------------------------------------- -EllipseActorPtr EllipseActor::Create(const Vec2& radius) -{ - EllipseActorPtr ptr = new (autogc) EllipseActor; - if (ptr) - { - ptr->SetRadius(radius); - } - return ptr; -} - EllipseActor::EllipseActor() {} +EllipseActor::EllipseActor(const Vec2& radius) +{ + SetRadius(radius); +} + EllipseActor::~EllipseActor() {} void EllipseActor::SetRadius(const Vec2& radius) @@ -277,30 +238,25 @@ void EllipseActor::SetRadius(const Vec2& radius) // PolygonActor //------------------------------------------------------- -PolygonActorPtr PolygonActor::Create(const Vector& points) -{ - PolygonActorPtr ptr = new (autogc) PolygonActor; - if (ptr) - { - ptr->SetVertices(points); - } - return ptr; -} - PolygonActor::PolygonActor() {} +PolygonActor::PolygonActor(const Vector& points) +{ + SetVertices(points); +} + PolygonActor::~PolygonActor() {} void PolygonActor::SetVertices(const Vector& points) { if (points.size() > 1) { - ShapeMakerPtr maker = ShapeMaker::Create(); - maker->BeginPath(points[0]); - maker->AddLines(&points[1], points.size() - 1); - maker->EndPath(true); + ShapeMaker maker; + maker.BeginPath(points[0]); + maker.AddLines(&points[1], points.size() - 1); + maker.EndPath(true); - SetShape(maker->GetShape()); + SetShape(maker.GetShape()); } } diff --git a/src/kiwano/2d/ShapeActor.h b/src/kiwano/2d/ShapeActor.h index ae4074f1..480bd056 100644 --- a/src/kiwano/2d/ShapeActor.h +++ b/src/kiwano/2d/ShapeActor.h @@ -48,28 +48,26 @@ KGE_DECLARE_SMART_PTR(PolygonActor); class KGE_API ShapeActor : public Actor { public: + ShapeActor(); + /// \~chinese /// @brief 创建形状角色 /// @param shape 形状 - static ShapeActorPtr Create(ShapePtr shape); + ShapeActor(ShapePtr shape); /// \~chinese /// @brief 创建形状角色 /// @param shape 形状 /// @param fill_color 填充颜色 /// @param stroke_color 轮廓颜色 - static ShapeActorPtr Create(ShapePtr shape, const Color& fill_color, const Color& stroke_color); + ShapeActor(ShapePtr shape, const Color& fill_color, const Color& stroke_color); /// \~chinese /// @brief 创建形状角色 /// @param shape 形状 /// @param fill_brush 填充画刷 /// @param stroke_brush 轮廓画刷 - static ShapeActorPtr Create(ShapePtr shape, BrushPtr fill_brush, BrushPtr stroke_brush); - - /// \~chinese - /// @brief 构造形状角色 - ShapeActor(); + ShapeActor(ShapePtr shape, BrushPtr fill_brush, BrushPtr stroke_brush); virtual ~ShapeActor(); @@ -147,13 +145,13 @@ private: class KGE_API LineActor : public ShapeActor { public: + LineActor(); + /// \~chinese /// @brief 创建线段角色 /// @param begin 线段起点 /// @param end 线段终点 - static LineActorPtr Create(const Point& begin, const Point& end); - - LineActor(); + LineActor(const Point& begin, const Point& end); virtual ~LineActor(); @@ -191,12 +189,12 @@ private: class KGE_API RectActor : public ShapeActor { public: + RectActor(); + /// \~chinese /// @brief 创建矩形角色 /// @param size 矩形大小 - static RectActorPtr Create(const Size& size); - - RectActor(); + RectActor(const Size& size); virtual ~RectActor(); @@ -218,13 +216,13 @@ private: class KGE_API RoundedRectActor : public ShapeActor { public: + RoundedRectActor(); + /// \~chinese /// @brief 创建圆角矩形角色 /// @param size 圆角矩形大小 /// @param radius 圆角半径 - static RoundedRectActorPtr Create(const Size& size, const Vec2& radius); - - RoundedRectActor(); + RoundedRectActor(const Size& size, const Vec2& radius); virtual ~RoundedRectActor(); @@ -262,12 +260,12 @@ private: class KGE_API CircleActor : public ShapeActor { public: + CircleActor(); + /// \~chinese /// @brief 创建圆形角色 /// @param radius 圆形半径 - static CircleActorPtr Create(float radius); - - CircleActor(); + CircleActor(float radius); virtual ~CircleActor(); @@ -289,12 +287,12 @@ private: class KGE_API EllipseActor : public ShapeActor { public: + EllipseActor(); + /// \~chinese /// @brief 创建椭圆角色 /// @param radius 椭圆半径 - static EllipseActorPtr Create(const Vec2& radius); - - EllipseActor(); + EllipseActor(const Vec2& radius); virtual ~EllipseActor(); @@ -316,12 +314,12 @@ private: class KGE_API PolygonActor : public ShapeActor { public: + PolygonActor(); + /// \~chinese /// @brief 创建多边形角色 /// @param points 多边形端点集合 - static PolygonActorPtr Create(const Vector& points); - - PolygonActor(); + PolygonActor(const Vector& points); virtual ~PolygonActor(); @@ -338,7 +336,7 @@ inline void ShapeActor::SetStrokeColor(const Color& color) { if (!stroke_brush_) { - stroke_brush_ = new (autogc) Brush; + stroke_brush_ = MakePtr(); } stroke_brush_->SetColor(color); } @@ -347,7 +345,7 @@ inline void ShapeActor::SetFillColor(const Color& color) { if (!fill_brush_) { - fill_brush_ = new (autogc) Brush; + fill_brush_ = MakePtr(); } fill_brush_->SetColor(color); } diff --git a/src/kiwano/2d/Sprite.cpp b/src/kiwano/2d/Sprite.cpp index 34be7de6..3518bee6 100644 --- a/src/kiwano/2d/Sprite.cpp +++ b/src/kiwano/2d/Sprite.cpp @@ -24,65 +24,40 @@ namespace kiwano { -SpritePtr Sprite::Create(const String& file_path) -{ - SpritePtr ptr = new (autogc) Sprite; - if (ptr) - { - if (!ptr->Load(file_path)) - return nullptr; - } - return ptr; -} - -SpritePtr Sprite::Create(const Resource& res) -{ - SpritePtr ptr = new (autogc) Sprite; - if (ptr) - { - if (!ptr->Load(res)) - return nullptr; - } - return ptr; -} - -SpritePtr Sprite::Create(FramePtr frame) -{ - SpritePtr ptr = new (autogc) Sprite; - if (ptr) - { - ptr->SetFrame(frame); - } - return ptr; -} - -SpritePtr Sprite::Create(const String& file_path, const Rect& crop_rect) -{ - SpritePtr ptr = Sprite::Create(file_path); - if (ptr) - { - ptr->SetCropRect(crop_rect); - } - return ptr; -} - -SpritePtr Sprite::Create(const Resource& res, const Rect& crop_rect) -{ - SpritePtr ptr = Sprite::Create(res); - if (ptr) - { - ptr->SetCropRect(crop_rect); - } - return ptr; -} - Sprite::Sprite() {} +Sprite::Sprite(const String& file_path) +{ + Load(file_path); +} + +Sprite::Sprite(const Resource& res) +{ + Load(res); +} + +Sprite::Sprite(FramePtr frame) +{ + SetFrame(frame); +} + +Sprite::Sprite(const String& file_path, const Rect& crop_rect) + : Sprite(file_path) +{ + SetCropRect(crop_rect); +} + +Sprite::Sprite(const Resource& res, const Rect& crop_rect) + : Sprite(res) +{ + SetCropRect(crop_rect); +} + Sprite::~Sprite() {} bool Sprite::Load(const String& file_path, bool autoresize) { - FramePtr frame = Frame::Create(file_path); + FramePtr frame = MakePtr(file_path); if (frame) { SetFrame(frame, autoresize); @@ -93,7 +68,7 @@ bool Sprite::Load(const String& file_path, bool autoresize) bool Sprite::Load(const Resource& res, bool autoresize) { - FramePtr frame = Frame::Create(res); + FramePtr frame = MakePtr(res); if (frame) { SetFrame(frame, autoresize); diff --git a/src/kiwano/2d/Sprite.h b/src/kiwano/2d/Sprite.h index 934abbac..f754d2f9 100644 --- a/src/kiwano/2d/Sprite.h +++ b/src/kiwano/2d/Sprite.h @@ -38,34 +38,34 @@ KGE_DECLARE_SMART_PTR(Sprite); class KGE_API Sprite : public Actor { public: + Sprite(); + /// \~chinese /// @brief 创建精灵 /// @param file_path 本地图片路径 - static SpritePtr Create(const String& file_path); + Sprite(const String& file_path); /// \~chinese /// @brief 创建精灵 /// @param res 图片资源 - static SpritePtr Create(const Resource& res); + Sprite(const Resource& res); /// \~chinese /// @brief 创建精灵 /// @param frame 图像帧 - static SpritePtr Create(FramePtr frame); + Sprite(FramePtr frame); /// \~chinese /// @brief 创建精灵 /// @param file_path 本地图片路径 /// @param crop_rect 裁剪矩形 - static SpritePtr Create(const String& file_path, const Rect& crop_rect); + Sprite(const String& file_path, const Rect& crop_rect); /// \~chinese /// @brief 创建精灵 /// @param res 图片资源 /// @param crop_rect 裁剪矩形 - static SpritePtr Create(const Resource& res, const Rect& crop_rect); - - Sprite(); + Sprite(const Resource& res, const Rect& crop_rect); virtual ~Sprite(); diff --git a/src/kiwano/2d/Stage.cpp b/src/kiwano/2d/Stage.cpp index 2a9ce6e3..d1df2af7 100644 --- a/src/kiwano/2d/Stage.cpp +++ b/src/kiwano/2d/Stage.cpp @@ -25,12 +25,6 @@ namespace kiwano { -StagePtr Stage::Create() -{ - StagePtr ptr = new (autogc) Stage; - return ptr; -} - Stage::Stage() { SetStage(this); @@ -57,13 +51,13 @@ void Stage::RenderBorder(RenderContext& ctx) if (!border_fill_brush_) { - border_fill_brush_ = new (autogc) Brush; + border_fill_brush_ = MakePtr(); border_fill_brush_->SetColor(Color(Color::Red, .4f)); } if (!border_stroke_brush_) { - border_stroke_brush_ = new (autogc) Brush; + border_stroke_brush_ = MakePtr(); border_stroke_brush_->SetColor(Color(Color::Red, .8f)); } diff --git a/src/kiwano/2d/Stage.h b/src/kiwano/2d/Stage.h index 4f92704e..e0a3dbef 100644 --- a/src/kiwano/2d/Stage.h +++ b/src/kiwano/2d/Stage.h @@ -43,10 +43,6 @@ class KGE_API Stage : public Actor friend class Director; public: - /// \~chinese - /// @brief 进入舞台时 - static StagePtr Create(); - Stage(); virtual ~Stage(); diff --git a/src/kiwano/2d/TextActor.cpp b/src/kiwano/2d/TextActor.cpp index 0acea3ec..1f7ad180 100644 --- a/src/kiwano/2d/TextActor.cpp +++ b/src/kiwano/2d/TextActor.cpp @@ -25,24 +25,17 @@ namespace kiwano { -TextActorPtr TextActor::Create(const String& text) +TextActor::TextActor() {} + +TextActor::TextActor(const String& text) + : TextActor(text, TextStyle()) { - return TextActor::Create(text, TextStyle()); } -TextActorPtr TextActor::Create(const String& text, const TextStyle& style) -{ - TextActorPtr ptr = new (autogc) TextActor; - if (ptr) - { - ptr->SetStyle(style); - ptr->SetText(text); - } - return ptr; -} - -TextActor::TextActor() +TextActor::TextActor(const String& text, const TextStyle& style) { + SetStyle(style); + SetText(text); } TextActor::~TextActor() {} @@ -65,7 +58,7 @@ void TextActor::SetText(const String& text) { if (!layout_) { - layout_ = TextLayout::Create(); + layout_ = MakePtr(); } layout_->Reset(text, style_); } @@ -215,7 +208,7 @@ void TextActor::SetFillColor(const Color& color) } else { - SetFillBrush(Brush::Create(color)); + SetFillBrush(MakePtr(color)); } } @@ -227,7 +220,7 @@ void TextActor::SetOutlineColor(const Color& outline_color) } else { - SetFillBrush(Brush::Create(outline_color)); + SetFillBrush(MakePtr(outline_color)); } } diff --git a/src/kiwano/2d/TextActor.h b/src/kiwano/2d/TextActor.h index f7939c1a..78ce1593 100644 --- a/src/kiwano/2d/TextActor.h +++ b/src/kiwano/2d/TextActor.h @@ -39,18 +39,18 @@ KGE_DECLARE_SMART_PTR(TextActor); class KGE_API TextActor : public Actor { public: + TextActor(); + /// \~chinese /// @brief 创建文本角色 /// @param text 文字内容 - static TextActorPtr Create(const String& text); + TextActor(const String& text); /// \~chinese /// @brief 创建文本角色 /// @param text 文字内容 /// @param style 文本样式 - static TextActorPtr Create(const String& text, const TextStyle& style); - - TextActor(); + TextActor(const String& text, const TextStyle& style); virtual ~TextActor(); diff --git a/src/kiwano/2d/Transition.cpp b/src/kiwano/2d/Transition.cpp index fcd9c7a7..b62167e3 100644 --- a/src/kiwano/2d/Transition.cpp +++ b/src/kiwano/2d/Transition.cpp @@ -51,7 +51,7 @@ bool Transition::IsDone() return done_; } -void Transition::Init(StagePtr prev, StagePtr next) +void Transition::Init(Stage* prev, Stage* next) { process_ = 0; delta_ = Duration{}; @@ -126,19 +126,14 @@ void Transition::Stop() // BoxTransition //------------------------------------------------------- -BoxTransitionPtr BoxTransition::Create(Duration duration) +BoxTransition::BoxTransition(Duration duration) { - BoxTransitionPtr ptr = new (autogc) BoxTransition; - if (ptr) - { - ptr->SetDuration(duration); - } - return ptr; + SetDuration(duration); } BoxTransition::BoxTransition() {} -void BoxTransition::Init(StagePtr prev, StagePtr next) +void BoxTransition::Init(Stage* prev, Stage* next) { Transition::Init(prev, next); @@ -167,19 +162,14 @@ void BoxTransition::Update(Duration dt) // EmergeTransition //------------------------------------------------------- -EmergeTransitionPtr EmergeTransition::Create(Duration duration) +EmergeTransition::EmergeTransition(Duration duration) { - EmergeTransitionPtr ptr = new (autogc) EmergeTransition; - if (ptr) - { - ptr->SetDuration(duration); - } - return ptr; + SetDuration(duration); } EmergeTransition::EmergeTransition() {} -void EmergeTransition::Init(StagePtr prev, StagePtr next) +void EmergeTransition::Init(Stage* prev, Stage* next) { Transition::Init(prev, next); @@ -199,19 +189,14 @@ void EmergeTransition::Update(Duration dt) // FadeTransition //------------------------------------------------------- -FadeTransitionPtr FadeTransition::Create(Duration duration) +FadeTransition::FadeTransition(Duration duration) { - FadeTransitionPtr ptr = new (autogc) FadeTransition; - if (ptr) - { - ptr->SetDuration(duration); - } - return ptr; + SetDuration(duration); } FadeTransition::FadeTransition() {} -void FadeTransition::Init(StagePtr prev, StagePtr next) +void FadeTransition::Init(Stage* prev, Stage* next) { Transition::Init(prev, next); @@ -239,15 +224,10 @@ void FadeTransition::Update(Duration dt) // MoveTransition //------------------------------------------------------- -MoveTransitionPtr MoveTransition::Create(Duration duration, Type type) +MoveTransition::MoveTransition(Duration duration, Type type) + : type_(type) { - MoveTransitionPtr ptr = new (autogc) MoveTransition; - if (ptr) - { - ptr->type_ = type; - ptr->SetDuration(duration); - } - return ptr; + SetDuration(duration); } MoveTransition::MoveTransition() @@ -255,7 +235,7 @@ MoveTransition::MoveTransition() { } -void MoveTransition::Init(StagePtr prev, StagePtr next) +void MoveTransition::Init(Stage* prev, Stage* next) { Transition::Init(prev, next); @@ -328,15 +308,10 @@ void MoveTransition::Reset() // RotationTransition //------------------------------------------------------- -RotationTransitionPtr RotationTransition::Create(Duration duration, float rotation) +RotationTransition::RotationTransition(Duration duration, float rotation) + : rotation_(rotation) { - RotationTransitionPtr ptr = new (autogc) RotationTransition; - if (ptr) - { - ptr->rotation_ = rotation; - ptr->SetDuration(duration); - } - return ptr; + SetDuration(duration); } RotationTransition::RotationTransition() @@ -344,7 +319,7 @@ RotationTransition::RotationTransition() { } -void RotationTransition::Init(StagePtr prev, StagePtr next) +void RotationTransition::Init(Stage* prev, Stage* next) { Transition::Init(prev, next); diff --git a/src/kiwano/2d/Transition.h b/src/kiwano/2d/Transition.h index 9444af79..86ced945 100644 --- a/src/kiwano/2d/Transition.h +++ b/src/kiwano/2d/Transition.h @@ -66,7 +66,7 @@ protected: * @param[in] prev 转出场景 * @param[in] next 转入场景 */ - virtual void Init(StagePtr prev, StagePtr next); + virtual void Init(Stage* prev, Stage* next); /** * \~chinese @@ -95,15 +95,15 @@ protected: virtual void Reset() {} protected: - bool done_; - float process_; - Duration duration_; - Duration delta_; - Size window_size_; - StagePtr out_stage_; - StagePtr in_stage_; - Layer out_layer_; - Layer in_layer_; + bool done_; + float process_; + Duration duration_; + Duration delta_; + Size window_size_; + StagePtr out_stage_; + StagePtr in_stage_; + Layer out_layer_; + Layer in_layer_; }; /** @@ -119,14 +119,14 @@ public: * @brief 创建淡入淡出过渡动画 * @param duration 动画时长 */ - static FadeTransitionPtr Create(Duration duration); + FadeTransition(Duration duration); FadeTransition(); protected: void Update(Duration dt) override; - virtual void Init(StagePtr prev, StagePtr next) override; + virtual void Init(Stage* prev, Stage* next) override; }; /** @@ -142,14 +142,14 @@ public: * @brief 创建渐变过渡动画 * @param duration 动画时长 */ - static EmergeTransitionPtr Create(Duration duration); + EmergeTransition(Duration duration); EmergeTransition(); protected: void Update(Duration dt) override; - virtual void Init(StagePtr prev, StagePtr next) override; + virtual void Init(Stage* prev, Stage* next) override; }; /** @@ -165,14 +165,14 @@ public: * @brief 创建盒状过渡动画 * @param duration 动画时长 */ - static BoxTransitionPtr Create(Duration duration); + BoxTransition(Duration duration); BoxTransition(); protected: void Update(Duration dt) override; - virtual void Init(StagePtr prev, StagePtr next) override; + virtual void Init(Stage* prev, Stage* next) override; }; /** @@ -201,14 +201,14 @@ public: * @param duration 动画时长 * @param type 位移方式 */ - static MoveTransitionPtr Create(Duration duration, Type type); + MoveTransition(Duration duration, Type type); MoveTransition(); protected: void Update(Duration dt) override; - virtual void Init(StagePtr prev, StagePtr next) override; + virtual void Init(Stage* prev, Stage* next) override; void Reset() override; @@ -232,14 +232,14 @@ public: * @param duration 动画时长 * @param rotation 旋转度数 */ - static RotationTransitionPtr Create(Duration duration, float rotation = 360.0f); + RotationTransition(Duration duration, float rotation = 360.0f); RotationTransition(); protected: void Update(Duration dt) override; - virtual void Init(StagePtr prev, StagePtr next) override; + virtual void Init(Stage* prev, Stage* next) override; void Reset() override; diff --git a/src/kiwano/2d/action/Action.cpp b/src/kiwano/2d/action/Action.cpp index 36654579..abdf00e5 100644 --- a/src/kiwano/2d/action/Action.cpp +++ b/src/kiwano/2d/action/Action.cpp @@ -106,7 +106,7 @@ void ActionEntity::Reset() loops_done_ = 0; } -ActionEntityPtr ActionEntity::DoClone(ActionEntityPtr to) const +void ActionEntity::DoClone(ActionEntity* to) const { if (to) { @@ -116,7 +116,6 @@ ActionEntityPtr ActionEntity::DoClone(ActionEntityPtr to) const to->SetLoops(this->GetLoops()); to->SetName(this->GetName()); } - return to; } } // namespace kiwano diff --git a/src/kiwano/2d/action/Action.h b/src/kiwano/2d/action/Action.h index 2a48709a..e3c6cf87 100644 --- a/src/kiwano/2d/action/Action.h +++ b/src/kiwano/2d/action/Action.h @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -103,7 +102,7 @@ public: /// \~chinese /// @brief 获取动画的倒转 - virtual ActionEntityPtr Reverse() const = 0; + virtual ActionEntity* Reverse() const = 0; /// \~chinese /// @brief 获取动画的运行状态 @@ -183,7 +182,7 @@ protected: /// \~chinese /// @brief 执行克隆 - ActionEntityPtr DoClone(ActionEntityPtr to) const; + void DoClone(ActionEntity* to) const; private: Status status_; @@ -300,6 +299,11 @@ public: return Get(); } + inline operator ActionEntity*() const + { + return Get(); + } + inline operator ActionEntityPtr() const { return ptr; diff --git a/src/kiwano/2d/action/ActionDelay.cpp b/src/kiwano/2d/action/ActionDelay.cpp index 1b7750d9..d4fefd27 100644 --- a/src/kiwano/2d/action/ActionDelay.cpp +++ b/src/kiwano/2d/action/ActionDelay.cpp @@ -25,27 +25,24 @@ namespace kiwano ActionDelay::ActionDelay(Duration delay) { - SetEntity(ActionDelayEntity::Create(delay)); + SetEntity(MakePtr(delay)); } -ActionDelayEntityPtr ActionDelayEntity::Create(Duration delay) +ActionDelayEntity::ActionDelayEntity(Duration delay) { - ActionDelayEntityPtr ptr = new (autogc) ActionDelayEntity; - if (ptr) - { - ptr->SetDelay(delay); - } + this->SetDelay(delay); +} + +ActionDelayEntity* ActionDelayEntity::Clone() const +{ + auto ptr = new (autogc) ActionDelayEntity(GetDelay()); + DoClone(ptr); return ptr; } -ActionEntityPtr ActionDelayEntity::Clone() const +ActionDelayEntity* ActionDelayEntity::Reverse() const { - return DoClone(ActionDelayEntity::Create(GetDelay())); -} - -ActionEntityPtr ActionDelayEntity::Reverse() const -{ - return DoClone(ActionDelayEntity::Create(GetDelay())); + return Clone(); } } // namespace kiwano diff --git a/src/kiwano/2d/action/ActionDelay.h b/src/kiwano/2d/action/ActionDelay.h index 2a702f53..51b39681 100644 --- a/src/kiwano/2d/action/ActionDelay.h +++ b/src/kiwano/2d/action/ActionDelay.h @@ -50,15 +50,15 @@ public: /// \~chinese /// @brief 创建延时动画 /// @param delay 延时时长 - static ActionDelayEntityPtr Create(Duration delay); + ActionDelayEntity(Duration delay); /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + ActionDelayEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - ActionEntityPtr Reverse() const override; + ActionDelayEntity* Reverse() const override; }; /** @} */ diff --git a/src/kiwano/2d/action/ActionGroup.cpp b/src/kiwano/2d/action/ActionGroup.cpp index 2128c3b2..e1841b03 100644 --- a/src/kiwano/2d/action/ActionGroup.cpp +++ b/src/kiwano/2d/action/ActionGroup.cpp @@ -27,18 +27,7 @@ namespace kiwano ActionGroup::ActionGroup(const Vector& actions, bool parallel) { - SetEntity(ActionGroupEntity::Create(actions, parallel)); -} - -ActionGroupEntityPtr ActionGroupEntity::Create(const Vector& actions, bool parallel) -{ - ActionGroupEntityPtr ptr = new (autogc) ActionGroupEntity; - if (ptr) - { - ptr->parallel_ = parallel; - ptr->AddActions(actions); - } - return ptr; + SetEntity(MakePtr(actions, parallel)); } ActionGroupEntity::ActionGroupEntity() @@ -46,9 +35,10 @@ ActionGroupEntity::ActionGroupEntity() { } -ActionGroupEntity::ActionGroupEntity(bool parallel) +ActionGroupEntity::ActionGroupEntity(const Vector& actions, bool parallel) : parallel_(parallel) { + AddActions(actions); } ActionGroupEntity::~ActionGroupEntity() {} @@ -123,7 +113,7 @@ void ActionGroupEntity::AddActions(const Vector& actions) AddAction(action); } -ActionEntityPtr ActionGroupEntity::Clone() const +ActionGroupEntity* ActionGroupEntity::Clone() const { Vector actions; if (!actions_.IsEmpty()) @@ -133,10 +123,12 @@ ActionEntityPtr ActionGroupEntity::Clone() const actions.push_back(action->Clone()); } } - return DoClone(ActionGroupEntity::Create(actions, parallel_)); + auto ptr = new (autogc) ActionGroupEntity(actions, parallel_); + DoClone(ptr); + return ptr; } -ActionEntityPtr ActionGroupEntity::Reverse() const +ActionGroupEntity* ActionGroupEntity::Reverse() const { Vector actions; if (!actions_.IsEmpty()) @@ -146,7 +138,9 @@ ActionEntityPtr ActionGroupEntity::Reverse() const actions.push_back(action->Reverse()); } } - return DoClone(ActionGroupEntity::Create(actions, parallel_)); + auto ptr = new (autogc) ActionGroupEntity(actions, parallel_); + DoClone(ptr); + return ptr; } } // namespace kiwano diff --git a/src/kiwano/2d/action/ActionGroup.h b/src/kiwano/2d/action/ActionGroup.h index 1afcb678..341f3904 100644 --- a/src/kiwano/2d/action/ActionGroup.h +++ b/src/kiwano/2d/action/ActionGroup.h @@ -47,15 +47,13 @@ public: class KGE_API ActionGroupEntity : public ActionEntity { public: + ActionGroupEntity(); + /// \~chinese /// @brief 创建动画组合 /// @param actions 动画集合 /// @param parallel 同步执行 - static ActionGroupEntityPtr Create(const Vector& actions, bool parallel = false); - - ActionGroupEntity(); - - ActionGroupEntity(bool parallel); + ActionGroupEntity(const Vector& actions, bool parallel = false); virtual ~ActionGroupEntity(); @@ -75,11 +73,11 @@ public: /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + ActionGroupEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - ActionEntityPtr Reverse() const override; + ActionGroupEntity* Reverse() const override; protected: void Init(Actor* target) override; diff --git a/src/kiwano/2d/action/ActionScheduler.cpp b/src/kiwano/2d/action/ActionScheduler.cpp index c34a8480..e1ff9f77 100644 --- a/src/kiwano/2d/action/ActionScheduler.cpp +++ b/src/kiwano/2d/action/ActionScheduler.cpp @@ -87,14 +87,14 @@ void ActionScheduler::StopAllActions() } } -ActionEntityPtr ActionScheduler::GetAction(const String& name) +ActionEntity* ActionScheduler::GetAction(const String& name) { if (actions_.IsEmpty()) return nullptr; for (auto& action : actions_) if (action->IsName(name)) - return action; + return action.Get(); return nullptr; } diff --git a/src/kiwano/2d/action/ActionScheduler.h b/src/kiwano/2d/action/ActionScheduler.h index ddb9027c..60a6dc20 100644 --- a/src/kiwano/2d/action/ActionScheduler.h +++ b/src/kiwano/2d/action/ActionScheduler.h @@ -55,7 +55,7 @@ public: /// \~chinese /// @brief 获取指定名称的动画 /// @param name 动画名称 - ActionEntityPtr GetAction(const String& name); + ActionEntity* GetAction(const String& name); /// \~chinese /// @brief 获取所有动画 diff --git a/src/kiwano/2d/action/ActionTween.cpp b/src/kiwano/2d/action/ActionTween.cpp index 4669db6c..14d40da5 100644 --- a/src/kiwano/2d/action/ActionTween.cpp +++ b/src/kiwano/2d/action/ActionTween.cpp @@ -95,9 +95,9 @@ ActionTweenEntity::ActionTweenEntity() { } -ActionTweenEntity::ActionTweenEntity(Duration duration, EaseFunc func) +ActionTweenEntity::ActionTweenEntity(Duration duration) : dur_(duration) - , ease_func_(func) + , ease_func_(nullptr) { } @@ -129,33 +129,26 @@ void ActionTweenEntity::Update(Actor* target, Duration dt) UpdateTween(target, percent); } -ActionEntityPtr ActionTweenEntity::DoClone(ActionTweenEntityPtr to) const +void ActionTweenEntity::DoClone(ActionTweenEntity* to) const { if (to) { + ActionEntity::DoClone(to); to->SetDuration(this->GetDuration()); to->SetEaseFunc(this->GetEaseFunc()); } - return ActionEntity::DoClone(to); } //------------------------------------------------------- // Move Action //------------------------------------------------------- -ActionMoveByEntityPtr ActionMoveByEntity::Create(Duration duration, const Vec2& displacement) +ActionMoveByEntity::ActionMoveByEntity(Duration duration, const Vec2& displacement) + : ActionTweenEntity(duration) + , displacement_(displacement) { - ActionMoveByEntityPtr ptr = new (autogc) ActionMoveByEntity; - if (ptr) - { - ptr->SetDuration(duration); - ptr->SetDisplacement(displacement); - } - return ptr; } -ActionMoveByEntity::ActionMoveByEntity() {} - void ActionMoveByEntity::Init(Actor* target) { if (target) @@ -175,32 +168,31 @@ void ActionMoveByEntity::UpdateTween(Actor* target, float percent) prev_pos_ = new_pos; } -ActionEntityPtr ActionMoveByEntity::Clone() const +ActionMoveByEntity* ActionMoveByEntity::Clone() const { - return DoClone(ActionMoveByEntity::Create(GetDuration(), displacement_)); -} - -ActionEntityPtr ActionMoveByEntity::Reverse() const -{ - return DoClone(ActionMoveByEntity::Create(GetDuration(), -displacement_)); -} - -ActionMoveToEntityPtr ActionMoveToEntity::Create(Duration duration, const Point& distination) -{ - ActionMoveToEntityPtr ptr = new (autogc) ActionMoveToEntity; - if (ptr) - { - ptr->SetDuration(duration); - ptr->SetDistination(distination); - } + auto ptr = new (autogc) ActionMoveByEntity(GetDuration(), displacement_); + DoClone(ptr); return ptr; } -ActionMoveToEntity::ActionMoveToEntity() {} - -ActionEntityPtr ActionMoveToEntity::Clone() const +ActionMoveByEntity* ActionMoveByEntity::Reverse() const { - return DoClone(ActionMoveToEntity::Create(GetDuration(), distination_)); + auto ptr = new (autogc) ActionMoveByEntity(GetDuration(), -displacement_); + DoClone(ptr); + return ptr; +} + +ActionMoveToEntity::ActionMoveToEntity(Duration duration, const Point& distination) + : ActionMoveByEntity(duration, Vec2()) + , distination_(distination) +{ +} + +ActionMoveToEntity* ActionMoveToEntity::Clone() const +{ + auto ptr = new (autogc) ActionMoveToEntity(GetDuration(), distination_); + DoClone(ptr); + return ptr; } void ActionMoveToEntity::Init(Actor* target) @@ -213,33 +205,26 @@ void ActionMoveToEntity::Init(Actor* target) // Jump Action //------------------------------------------------------- -ActionJumpByEntityPtr ActionJumpByEntity::Create(Duration duration, const Vec2& displacement, float height, int count) +ActionJumpByEntity::ActionJumpByEntity(Duration duration, const Vec2& displacement, float height, int count) + : ActionTweenEntity(duration) + , height_(height) + , jump_count_(count) + , displacement_(displacement) { - ActionJumpByEntityPtr ptr = new (autogc) ActionJumpByEntity; - if (ptr) - { - ptr->SetDuration(duration); - ptr->SetJumpHeight(height); - ptr->SetJumpCount(count); - ptr->SetDisplacement(displacement); - } +} + +ActionJumpByEntity* ActionJumpByEntity::Clone() const +{ + auto ptr = new (autogc) ActionJumpByEntity(GetDuration(), displacement_, height_, jump_count_); + DoClone(ptr); return ptr; } -ActionJumpByEntity::ActionJumpByEntity() - : height_(0.0f) - , jump_count_(0) +ActionJumpByEntity* ActionJumpByEntity::Reverse() const { -} - -ActionEntityPtr ActionJumpByEntity::Clone() const -{ - return DoClone(ActionJumpByEntity::Create(GetDuration(), displacement_, height_, jump_count_)); -} - -ActionEntityPtr ActionJumpByEntity::Reverse() const -{ - return DoClone(ActionJumpByEntity::Create(GetDuration(), -displacement_, height_, jump_count_)); + auto ptr = new (autogc) ActionJumpByEntity(GetDuration(), -displacement_, height_, jump_count_); + DoClone(ptr); + return ptr; } void ActionJumpByEntity::Init(Actor* target) @@ -266,24 +251,17 @@ void ActionJumpByEntity::UpdateTween(Actor* target, float percent) prev_pos_ = new_pos; } -ActionJumpToEntityPtr ActionJumpToEntity::Create(Duration duration, const Point& distination, float height, int count) +ActionJumpToEntity::ActionJumpToEntity(Duration duration, const Point& distination, float height, int count) + : ActionJumpByEntity(duration, Vec2(), height, count) + , distination_(distination) { - ActionJumpToEntityPtr ptr = new (autogc) ActionJumpToEntity; - if (ptr) - { - ptr->SetDuration(duration); - ptr->SetJumpHeight(height); - ptr->SetJumpCount(count); - ptr->SetDistination(distination); - } - return ptr; } -ActionJumpToEntity::ActionJumpToEntity() {} - -ActionEntityPtr ActionJumpToEntity::Clone() const +ActionJumpToEntity* ActionJumpToEntity::Clone() const { - return DoClone(ActionJumpToEntity::Create(GetDuration(), distination_, height_, jump_count_)); + auto ptr = new (autogc) ActionJumpToEntity(GetDuration(), distination_, height_, jump_count_); + DoClone(ptr); + return ptr; } void ActionJumpToEntity::Init(Actor* target) @@ -296,21 +274,10 @@ void ActionJumpToEntity::Init(Actor* target) // Scale Action //------------------------------------------------------- -ActionScaleByEntityPtr ActionScaleByEntity::Create(Duration duration, float scale_x, float scale_y) -{ - ActionScaleByEntityPtr ptr = new (autogc) ActionScaleByEntity; - if (ptr) - { - ptr->SetDuration(duration); - ptr->SetScaleX(scale_x); - ptr->SetScaleY(scale_y); - } - return ptr; -} - -ActionScaleByEntity::ActionScaleByEntity() - : delta_x_(0.0f) - , delta_y_(0.0f) +ActionScaleByEntity::ActionScaleByEntity(Duration duration, float scale_x, float scale_y) + : ActionTweenEntity(duration) + , delta_x_(scale_x) + , delta_y_(scale_y) , start_scale_x_(0.f) , start_scale_y_(0.f) { @@ -330,37 +297,32 @@ void ActionScaleByEntity::UpdateTween(Actor* target, float percent) target->SetScale(Vec2{ start_scale_x_ + delta_x_ * percent, start_scale_y_ + delta_y_ * percent }); } -ActionEntityPtr ActionScaleByEntity::Clone() const +ActionScaleByEntity* ActionScaleByEntity::Clone() const { - return DoClone(ActionScaleByEntity::Create(GetDuration(), delta_x_, delta_y_)); -} - -ActionEntityPtr ActionScaleByEntity::Reverse() const -{ - return DoClone(ActionScaleByEntity::Create(GetDuration(), -delta_x_, -delta_y_)); -} - -ActionScaleToEntityPtr ActionScaleToEntity::Create(Duration duration, float scale_x, float scale_y) -{ - ActionScaleToEntityPtr ptr = new (autogc) ActionScaleToEntity; - if (ptr) - { - ptr->SetDuration(duration); - ptr->SetTargetScaleX(scale_x); - ptr->SetTargetScaleY(scale_y); - } + auto ptr = new (autogc) ActionScaleByEntity(GetDuration(), delta_x_, delta_y_); + DoClone(ptr); return ptr; } -ActionScaleToEntity::ActionScaleToEntity() - : end_scale_x_(0.0f) - , end_scale_y_(0.0f) +ActionScaleByEntity* ActionScaleByEntity::Reverse() const +{ + auto ptr = new (autogc) ActionScaleByEntity(GetDuration(), -delta_x_, -delta_y_); + DoClone(ptr); + return ptr; +} + +ActionScaleToEntity::ActionScaleToEntity(Duration duration, float scale_x, float scale_y) + : ActionScaleByEntity(duration, 0, 0) + , end_scale_x_(scale_x) + , end_scale_y_(scale_y) { } -ActionEntityPtr ActionScaleToEntity::Clone() const +ActionScaleToEntity* ActionScaleToEntity::Clone() const { - return DoClone(ActionScaleToEntity::Create(GetDuration(), end_scale_x_, end_scale_y_)); + auto ptr = new (autogc) ActionScaleToEntity(GetDuration(), end_scale_x_, end_scale_y_); + DoClone(ptr); + return ptr; } void ActionScaleToEntity::Init(Actor* target) @@ -374,21 +336,11 @@ void ActionScaleToEntity::Init(Actor* target) // Opacity Action //------------------------------------------------------- -ActionFadeToEntityPtr ActionFadeToEntity::Create(Duration duration, float opacity) -{ - ActionFadeToEntityPtr ptr = new (autogc) ActionFadeToEntity; - if (ptr) - { - ptr->SetDuration(duration); - ptr->SetTargetOpacity(opacity); - } - return ptr; -} - -ActionFadeToEntity::ActionFadeToEntity() - : delta_val_(0.0f) +ActionFadeToEntity::ActionFadeToEntity(Duration duration, float opacity) + : ActionTweenEntity(duration) + , delta_val_(0.0f) , start_val_(0.f) - , end_val_(0.0f) + , end_val_(opacity) { } @@ -406,29 +358,21 @@ void ActionFadeToEntity::UpdateTween(Actor* target, float percent) target->SetOpacity(start_val_ + delta_val_ * percent); } -ActionEntityPtr ActionFadeToEntity::Clone() const +ActionFadeToEntity* ActionFadeToEntity::Clone() const { - return DoClone(ActionFadeToEntity::Create(GetDuration(), end_val_)); + auto ptr = new (autogc) ActionFadeToEntity(GetDuration(), end_val_); + DoClone(ptr); + return ptr; } //------------------------------------------------------- // Rotate Action //------------------------------------------------------- -ActionRotateByEntityPtr ActionRotateByEntity::Create(Duration duration, float rotation) -{ - ActionRotateByEntityPtr ptr = new (autogc) ActionRotateByEntity; - if (ptr) - { - ptr->SetDuration(duration); - ptr->SetRotation(rotation); - } - return ptr; -} - -ActionRotateByEntity::ActionRotateByEntity() - : start_val_(0.0f) - , delta_val_(0.0f) +ActionRotateByEntity::ActionRotateByEntity(Duration duration, float rotation) + : ActionTweenEntity(duration) + , delta_val_(rotation) + , start_val_(0.0f) { } @@ -449,35 +393,31 @@ void ActionRotateByEntity::UpdateTween(Actor* target, float percent) target->SetRotation(rotation); } -ActionEntityPtr ActionRotateByEntity::Clone() const +ActionRotateByEntity* ActionRotateByEntity::Clone() const { - return DoClone(ActionRotateByEntity::Create(GetDuration(), delta_val_)); -} - -ActionEntityPtr ActionRotateByEntity::Reverse() const -{ - return DoClone(ActionRotateByEntity::Create(GetDuration(), -delta_val_)); -} - -ActionRotateToEntityPtr ActionRotateToEntity::Create(Duration duration, float rotation) -{ - ActionRotateToEntityPtr ptr = new (autogc) ActionRotateToEntity; - if (ptr) - { - ptr->SetDuration(duration); - ptr->SetTargetRotation(rotation); - } + auto ptr = new (autogc) ActionRotateByEntity(GetDuration(), delta_val_); + DoClone(ptr); return ptr; } -ActionRotateToEntity::ActionRotateToEntity() - : end_val_(0.0f) +ActionRotateByEntity* ActionRotateByEntity::Reverse() const +{ + auto ptr = new (autogc) ActionRotateByEntity(GetDuration(), -delta_val_); + DoClone(ptr); + return ptr; +} + +ActionRotateToEntity::ActionRotateToEntity(Duration duration, float rotation) + : ActionRotateByEntity(duration, 0) + , end_val_(rotation) { } -ActionEntityPtr ActionRotateToEntity::Clone() const +ActionRotateToEntity* ActionRotateToEntity::Clone() const { - return DoClone(ActionRotateToEntity::Create(GetDuration(), end_val_)); + auto ptr = new (autogc) ActionRotateToEntity(GetDuration(), end_val_); + DoClone(ptr); + return ptr; } void ActionRotateToEntity::Init(Actor* target) @@ -490,22 +430,17 @@ void ActionRotateToEntity::Init(Actor* target) // ActionCustomEntity //------------------------------------------------------- -ActionCustomEntityPtr ActionCustomEntity::Create(Duration duration, ActionCustom::TweenFunc tween_func) +ActionCustomEntity::ActionCustomEntity(Duration duration, ActionCustom::TweenFunc tween_func) + : ActionTweenEntity(duration) + , tween_func_(tween_func) { - ActionCustomEntityPtr ptr = new (autogc) ActionCustomEntity; - if (ptr) - { - ptr->SetDuration(duration); - ptr->SetTweenFunc(tween_func); - } - return ptr; } -ActionCustomEntity::ActionCustomEntity() {} - -ActionEntityPtr ActionCustomEntity::Clone() const +ActionCustomEntity* ActionCustomEntity::Clone() const { - return DoClone(ActionCustomEntity::Create(GetDuration(), tween_func_)); + auto ptr = new (autogc) ActionCustomEntity(GetDuration(), tween_func_); + DoClone(ptr); + return ptr; } void ActionCustomEntity::Init(Actor* target) @@ -522,72 +457,72 @@ void ActionCustomEntity::UpdateTween(Actor* target, float percent) ActionMoveBy::ActionMoveBy(Duration duration, const Vec2& displacement) { - SetEntity(ActionMoveByEntity::Create(duration, displacement)); + SetEntity(MakePtr(duration, displacement)); } ActionMoveTo::ActionMoveTo(Duration duration, const Point& distination) { - SetEntity(ActionMoveToEntity::Create(duration, distination)); + SetEntity(MakePtr(duration, distination)); } ActionJumpBy::ActionJumpBy(Duration duration, const Vec2& displacement, float height, int count) { - SetEntity(ActionJumpByEntity::Create(duration, displacement, height, count)); + SetEntity(MakePtr(duration, displacement, height, count)); } ActionJumpTo::ActionJumpTo(Duration duration, const Point& distination, float height, int count) { - SetEntity(ActionJumpToEntity::Create(duration, distination, height, count)); + SetEntity(MakePtr(duration, distination, height, count)); } ActionScaleBy::ActionScaleBy(Duration duration, float scale_x, float scale_y) { - SetEntity(ActionScaleByEntity::Create(duration, scale_x, scale_y)); + SetEntity(MakePtr(duration, scale_x, scale_y)); } ActionScaleBy::ActionScaleBy(Duration duration, Vec2 scale) { - SetEntity(ActionScaleByEntity::Create(duration, scale.x, scale.y)); + SetEntity(MakePtr(duration, scale.x, scale.y)); } ActionScaleTo::ActionScaleTo(Duration duration, float scale_x, float scale_y) { - SetEntity(ActionScaleToEntity::Create(duration, scale_x, scale_y)); + SetEntity(MakePtr(duration, scale_x, scale_y)); } ActionScaleTo::ActionScaleTo(Duration duration, Vec2 scale) { - SetEntity(ActionScaleToEntity::Create(duration, scale.x, scale.y)); + SetEntity(MakePtr(duration, scale.x, scale.y)); } ActionFadeTo::ActionFadeTo(Duration duration, float opacity) { - SetEntity(ActionFadeToEntity::Create(duration, opacity)); + SetEntity(MakePtr(duration, opacity)); } ActionFadeIn::ActionFadeIn(Duration duration) { - SetEntity(ActionFadeToEntity::Create(duration, 1.0f)); + SetEntity(MakePtr(duration, 1.0f)); } ActionFadeOut::ActionFadeOut(Duration duration) { - SetEntity(ActionFadeToEntity::Create(duration, 0.0f)); + SetEntity(MakePtr(duration, 0.0f)); } ActionRotateBy::ActionRotateBy(Duration duration, float rotation) { - SetEntity(ActionRotateByEntity::Create(duration, rotation)); + SetEntity(MakePtr(duration, rotation)); } ActionRotateTo::ActionRotateTo(Duration duration, float rotation) { - SetEntity(ActionRotateToEntity::Create(duration, rotation)); + SetEntity(MakePtr(duration, rotation)); } ActionCustom::ActionCustom(Duration duration, TweenFunc tween_func) { - SetEntity(ActionCustomEntity::Create(duration, tween_func)); + SetEntity(MakePtr(duration, tween_func)); } } // namespace kiwano diff --git a/src/kiwano/2d/action/ActionTween.h b/src/kiwano/2d/action/ActionTween.h index 4f1c2085..53d1608a 100644 --- a/src/kiwano/2d/action/ActionTween.h +++ b/src/kiwano/2d/action/ActionTween.h @@ -88,14 +88,6 @@ KGE_DECLARE_SMART_PTR(ActionCustomEntity); class KGE_API ActionTweenEntity : public ActionEntity { public: - ActionTweenEntity(); - - /// \~chinese - /// @brief 补间动画 - /// @param duration 动画时长 - /// @param func 动画速度缓动函数 - ActionTweenEntity(Duration duration, EaseFunc ease); - /// \~chinese /// @brief 获取动画时长 Duration GetDuration() const; @@ -113,11 +105,19 @@ public: void SetEaseFunc(const EaseFunc& func); protected: + ActionTweenEntity(); + + /// \~chinese + /// @brief 补间动画 + /// @param duration 动画时长 + /// @param func 动画速度缓动函数 + ActionTweenEntity(Duration duration); + void Update(Actor* target, Duration dt) override; virtual void UpdateTween(Actor* target, float percent) = 0; - ActionEntityPtr DoClone(ActionTweenEntityPtr to) const; + void DoClone(ActionTweenEntity* to) const; private: Duration dur_; @@ -132,8 +132,8 @@ public: ActionTween() = default; inline ActionTween(ActionTweenEntityPtr ptr) - : Action(ptr.Get()) { + SetEntity(ptr); } /// \~chinese @@ -150,14 +150,14 @@ public: /// @brief 获取指针 inline ActionTweenEntity* Get() const { - return const_cast(static_cast(ptr.Get())); + return static_cast(Action::Get()); } /// \~chinese /// @brief 设置动画实体 inline void SetEntity(ActionTweenEntityPtr tween_ptr) { - this->ptr = static_cast(tween_ptr.Get()); + Action::SetEntity(tween_ptr.Get()); } inline ActionTweenEntity* operator->() const @@ -187,9 +187,7 @@ public: /// @brief 创建相对位移动画 /// @param duration 动画时长 /// @param displacement 位移向量 - static ActionMoveByEntityPtr Create(Duration duration, const Vec2& displacement); - - ActionMoveByEntity(); + ActionMoveByEntity(Duration duration, const Vec2& displacement); /// \~chinese /// @brief 获取位移向量 @@ -201,11 +199,11 @@ public: /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + ActionMoveByEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - ActionEntityPtr Reverse() const override; + ActionMoveByEntity* Reverse() const override; protected: void Init(Actor* target) override; @@ -239,9 +237,7 @@ public: /// @brief 创建位移动画 /// @param duration 动画时长 /// @param distination 目的坐标 - static ActionMoveToEntityPtr Create(Duration duration, const Point& distination); - - ActionMoveToEntity(); + ActionMoveToEntity(Duration duration, const Point& distination); /// \~chinese /// @brief 获取目的坐标 @@ -253,11 +249,11 @@ public: /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + ActionMoveToEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - virtual ActionEntityPtr Reverse() const override + ActionMoveToEntity* Reverse() const override { KGE_ERRORF("Reverse() not supported in ActionMoveToEntity"); return nullptr; @@ -295,9 +291,7 @@ public: /// @param displacement 跳跃位移向量 /// @param height 跳跃高度 /// @param count 跳跃次数 - static ActionJumpByEntityPtr Create(Duration duration, const Vec2& displacement, float height, int count = 1); - - ActionJumpByEntity(); + ActionJumpByEntity(Duration duration, const Vec2& displacement, float height, int count = 1); /// \~chinese /// @brief 获取跳跃位移 @@ -325,11 +319,11 @@ public: /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + ActionJumpByEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - ActionEntityPtr Reverse() const override; + ActionJumpByEntity* Reverse() const override; protected: void Init(Actor* target) override; @@ -369,9 +363,7 @@ public: /// @param distination 目的坐标 /// @param height 跳跃高度 /// @param count 跳跃次数 - static ActionJumpToEntityPtr Create(Duration duration, const Point& distination, float height, int count = 1); - - ActionJumpToEntity(); + ActionJumpToEntity(Duration duration, const Point& distination, float height, int count = 1); /// \~chinese /// @brief 获取目的坐标 @@ -383,11 +375,11 @@ public: /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + ActionJumpToEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - virtual ActionEntityPtr Reverse() const override + ActionJumpToEntity* Reverse() const override { KGE_ERRORF("Reverse() not supported in ActionJumpToEntity"); return nullptr; @@ -429,9 +421,7 @@ public: /// @param duration 动画时长 /// @param scale_x 横向缩放相对变化值 /// @param scale_y 纵向缩放相对变化值 - static ActionScaleByEntityPtr Create(Duration duration, float scale_x, float scale_y); - - ActionScaleByEntity(); + ActionScaleByEntity(Duration duration, float scale_x, float scale_y); /// \~chinese /// @brief 获取横向缩放相对变化值 @@ -451,11 +441,11 @@ public: /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + ActionScaleByEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - ActionEntityPtr Reverse() const override; + ActionScaleByEntity* Reverse() const override; protected: void Init(Actor* target) override; @@ -498,9 +488,7 @@ public: /// @param duration 动画时长 /// @param scale_x 横向缩放目标值 /// @param scale_y 纵向缩放目标值 - static ActionScaleToEntityPtr Create(Duration duration, float scale_x, float scale_y); - - ActionScaleToEntity(); + ActionScaleToEntity(Duration duration, float scale_x, float scale_y); /// \~chinese /// @brief 获取横向缩放目标值 @@ -520,11 +508,11 @@ public: /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + ActionScaleToEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - virtual ActionEntityPtr Reverse() const override + ActionScaleToEntity* Reverse() const override { KGE_ERRORF("Reverse() not supported in ActionScaleToEntity"); return nullptr; @@ -581,9 +569,7 @@ public: /// @brief 创建透明度渐变动画 /// @param duration 动画时长 /// @param opacity 目标透明度 - static ActionFadeToEntityPtr Create(Duration duration, float opacity); - - ActionFadeToEntity(); + ActionFadeToEntity(Duration duration, float opacity); /// \~chinese /// @brief 获取目标透明度 @@ -595,11 +581,11 @@ public: /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + ActionFadeToEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - virtual ActionEntityPtr Reverse() const override + ActionFadeToEntity* Reverse() const override { KGE_ERRORF("Reverse() not supported in ActionFadeToEntity"); return nullptr; @@ -637,9 +623,7 @@ public: /// @brief 创建相对旋转动画 /// @param duration 动画时长 /// @param rotation 角度相对变化值 - static ActionRotateByEntityPtr Create(Duration duration, float rotation); - - ActionRotateByEntity(); + ActionRotateByEntity(Duration duration, float rotation); /// \~chinese /// @brief 获取角度相对变化值 @@ -651,11 +635,11 @@ public: /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + ActionRotateByEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - ActionEntityPtr Reverse() const override; + ActionRotateByEntity* Reverse() const override; protected: void Init(Actor* target) override; @@ -688,9 +672,7 @@ public: /// @brief 创建旋转动画 /// @param duration 动画时长 /// @param rotation 目标角度 - static ActionRotateToEntityPtr Create(Duration duration, float rotation); - - ActionRotateToEntity(); + ActionRotateToEntity(Duration duration, float rotation); /// \~chinese /// @brief 获取目标角度 @@ -702,11 +684,11 @@ public: /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + ActionRotateToEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - virtual ActionEntityPtr Reverse() const override + ActionRotateToEntity* Reverse() const override { KGE_ERRORF("Reverse() not supported in ActionRotateToEntity"); return nullptr; @@ -745,9 +727,7 @@ public: /// @brief 创建自定义动画 /// @param duration 动画时长 /// @param tween_func 动画回调函数 - static ActionCustomEntityPtr Create(Duration duration, ActionCustom::TweenFunc tween_func); - - ActionCustomEntity(); + ActionCustomEntity(Duration duration, ActionCustom::TweenFunc tween_func); /// \~chinese /// @brief 获取动画回调函数 @@ -759,11 +739,11 @@ public: /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + ActionCustomEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - ActionEntityPtr Reverse() const override + ActionCustomEntity* Reverse() const override { KGE_ERRORF("Reverse() not supported in ActionCustomEntity"); return nullptr; diff --git a/src/kiwano/2d/action/ActionWalk.cpp b/src/kiwano/2d/action/ActionWalk.cpp index db197249..8b0be7cd 100644 --- a/src/kiwano/2d/action/ActionWalk.cpp +++ b/src/kiwano/2d/action/ActionWalk.cpp @@ -26,39 +26,31 @@ namespace kiwano ActionWalk::ActionWalk(Duration duration, ShapePtr path, bool rotating, float start, float end) { - SetEntity(ActionWalkEntity::Create(duration, path, rotating, start, end)); + SetEntity(MakePtr(duration, path, rotating, start, end)); } -ActionWalkEntityPtr ActionWalkEntity::Create(Duration duration, ShapePtr path, bool rotating, float start, float end) +ActionWalkEntity::ActionWalkEntity(Duration duration, ShapePtr path, bool rotating, float start, float end) + : ActionTweenEntity(duration) + , start_(start) + , end_(end) + , rotating_(rotating) + , length_(0.f) + , path_(path) { - ActionWalkEntityPtr ptr = new (autogc) ActionWalkEntity; - if (ptr) - { - ptr->SetDuration(duration); - ptr->SetPath(path); - ptr->SetRotating(rotating); - ptr->SetStartValue(start); - ptr->SetEndValue(end); - } +} + +ActionWalkEntity* ActionWalkEntity::Clone() const +{ + auto ptr = new (autogc) ActionWalkEntity(GetDuration(), path_, rotating_, start_, end_); + DoClone(ptr); return ptr; } -ActionWalkEntity::ActionWalkEntity() - : start_(0.0f) - , end_(1.0f) - , rotating_(false) - , length_(0.f) +ActionWalkEntity* ActionWalkEntity::Reverse() const { -} - -ActionEntityPtr ActionWalkEntity::Clone() const -{ - return DoClone(ActionWalkEntity::Create(GetDuration(), path_, rotating_, start_, end_)); -} - -ActionEntityPtr ActionWalkEntity::Reverse() const -{ - return DoClone(ActionWalkEntity::Create(GetDuration(), path_, rotating_, end_, start_)); + auto ptr = new (autogc) ActionWalkEntity(GetDuration(), path_, rotating_, end_, start_); + DoClone(ptr); + return ptr; } void ActionWalkEntity::Init(Actor* target) diff --git a/src/kiwano/2d/action/ActionWalk.h b/src/kiwano/2d/action/ActionWalk.h index 4f373f22..9a11873c 100644 --- a/src/kiwano/2d/action/ActionWalk.h +++ b/src/kiwano/2d/action/ActionWalk.h @@ -59,10 +59,7 @@ public: /// @param rotating 是否沿路径切线方向旋转 /// @param start 路径起点(百分比) /// @param end 路径终点(百分比) - static ActionWalkEntityPtr Create(Duration duration, ShapePtr path, bool rotating = false, float start = 0.f, - float end = 1.f); - - ActionWalkEntity(); + ActionWalkEntity(Duration duration, ShapePtr path, bool rotating = false, float start = 0.f, float end = 1.f); /// \~chinese /// @brief 获取路线 @@ -98,11 +95,11 @@ public: /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + ActionWalkEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - ActionEntityPtr Reverse() const override; + ActionWalkEntity* Reverse() const override; protected: void Init(Actor* target) override; diff --git a/src/kiwano/2d/action/Animation.cpp b/src/kiwano/2d/action/Animation.cpp index 4828db46..3afd55ff 100644 --- a/src/kiwano/2d/action/Animation.cpp +++ b/src/kiwano/2d/action/Animation.cpp @@ -27,18 +27,7 @@ namespace kiwano Animation::Animation(Duration dur, FrameSequencePtr frame_seq) { - SetEntity(AnimationEntity::Create(dur, frame_seq)); -} - -AnimationEntityPtr AnimationEntity::Create(Duration dur, FrameSequencePtr frame_seq) -{ - AnimationEntityPtr ptr = new (autogc) AnimationEntity; - if (ptr) - { - ptr->SetDuration(dur); - ptr->SetFrameSequence(frame_seq); - } - return ptr; + SetEntity(MakePtr(dur, frame_seq)); } AnimationEntity::AnimationEntity() @@ -46,6 +35,12 @@ AnimationEntity::AnimationEntity() { } +AnimationEntity::AnimationEntity(Duration dur, FrameSequencePtr frame_seq) + : ActionTweenEntity(dur) + , frame_seq_(frame_seq) +{ +} + AnimationEntity::~AnimationEntity() {} FrameSequencePtr AnimationEntity::GetFrameSequence() const @@ -90,26 +85,24 @@ void AnimationEntity::UpdateTween(Actor* target, float percent) } } -ActionEntityPtr AnimationEntity::Clone() const +AnimationEntity* AnimationEntity::Clone() const { - if (frame_seq_) - { - return DoClone(AnimationEntity::Create(GetDuration(), frame_seq_)); - } - return nullptr; + auto ptr = new (autogc) AnimationEntity(GetDuration(), frame_seq_); + DoClone(ptr); + return ptr; } -ActionEntityPtr AnimationEntity::Reverse() const +AnimationEntity* AnimationEntity::Reverse() const { + auto ptr = new (autogc) AnimationEntity(GetDuration(), nullptr); + DoClone(ptr); + if (frame_seq_) { FrameSequencePtr frames = frame_seq_->Reverse(); - if (frames) - { - return DoClone(AnimationEntity::Create(GetDuration(), frames)); - } + ptr->SetFrameSequence(frames); } - return nullptr; + return ptr; } } // namespace kiwano diff --git a/src/kiwano/2d/action/Animation.h b/src/kiwano/2d/action/Animation.h index a161a9a5..714529ad 100644 --- a/src/kiwano/2d/action/Animation.h +++ b/src/kiwano/2d/action/Animation.h @@ -49,13 +49,13 @@ public: class KGE_API AnimationEntity : public ActionTweenEntity { public: + AnimationEntity(); + /// \~chinese /// @brief 创建帧动画 /// @param dur 动画时长 /// @param frame_seq 序列帧 - static AnimationEntityPtr Create(Duration dur, FrameSequencePtr frame_seq); - - AnimationEntity(); + AnimationEntity(Duration dur, FrameSequencePtr frame_seq); virtual ~AnimationEntity(); @@ -70,11 +70,11 @@ public: /// \~chinese /// @brief 获取该动画的拷贝对象 - ActionEntityPtr Clone() const override; + AnimationEntity* Clone() const override; /// \~chinese /// @brief 获取该动画的倒转 - ActionEntityPtr Reverse() const override; + AnimationEntity* Reverse() const override; protected: void Init(Actor* target) override; @@ -86,4 +86,5 @@ private: }; /** @} */ + } // namespace kiwano diff --git a/src/kiwano/base/Director.cpp b/src/kiwano/base/Director.cpp index 263d1564..5000886c 100644 --- a/src/kiwano/base/Director.cpp +++ b/src/kiwano/base/Director.cpp @@ -26,6 +26,7 @@ namespace kiwano { + Director::Director() : render_border_enabled_(false) { @@ -49,7 +50,7 @@ void Director::EnterStage(StagePtr stage, TransitionPtr transition) transition_->Stop(); } transition_ = transition; - transition_->Init(current_stage_, next_stage_); + transition_->Init(current_stage_.Get(), next_stage_.Get()); } } @@ -80,7 +81,7 @@ void Director::PopStage(TransitionPtr transition) transition_->Stop(); } transition_ = transition; - transition_->Init(current_stage_, next_stage_); + transition_->Init(current_stage_.Get(), next_stage_.Get()); } } @@ -99,7 +100,7 @@ void Director::ShowDebugInfo(bool show) if (show) { if (!debug_actor_) - debug_actor_ = new (autogc) DebugActor; + debug_actor_ = MakePtr(); } else { diff --git a/src/kiwano/base/ObjectBase.h b/src/kiwano/base/ObjectBase.h index 7f7859bd..2ccb785a 100644 --- a/src/kiwano/base/ObjectBase.h +++ b/src/kiwano/base/ObjectBase.h @@ -22,8 +22,8 @@ #include #include #include -#include #include +#include namespace kiwano { diff --git a/src/kiwano/base/ObjectPool.cpp b/src/kiwano/base/ObjectPool.cpp index 189a43de..478cd48b 100644 --- a/src/kiwano/base/ObjectPool.cpp +++ b/src/kiwano/base/ObjectPool.cpp @@ -27,11 +27,6 @@ ObjectPool::ObjectPool() { } -ObjectPool::~ObjectPool() -{ - Clear(); -} - void ObjectPool::AddObject(RefObject* obj) { if (obj) diff --git a/src/kiwano/base/ObjectPool.h b/src/kiwano/base/ObjectPool.h index af17d68f..a45a3375 100644 --- a/src/kiwano/base/ObjectPool.h +++ b/src/kiwano/base/ObjectPool.h @@ -37,8 +37,6 @@ class KGE_API ObjectPool : public Singleton public: ObjectPool(); - virtual ~ObjectPool(); - /** * \~chinese * @brief 添加对象到内存池 diff --git a/src/kiwano/base/RefPtr.h b/src/kiwano/base/RefPtr.h new file mode 100644 index 00000000..4cfff9c2 --- /dev/null +++ b/src/kiwano/base/RefPtr.h @@ -0,0 +1,79 @@ +// Copyright (c) 2016-2018 Kiwano - 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. + +#pragma once +#include +#include + +namespace kiwano +{ + +/// \~chinese +/// @brief 默认的智能指针引用计数代理 +struct DefaultRefPtrRefProxy +{ + static inline void Retain(RefObject* ptr) + { + if (ptr) + ptr->Retain(); + } + + static inline void Release(RefObject* ptr) + { + if (ptr) + ptr->Release(); + } +}; + +/// \~chinese +/// @brief 引用计数对象智能指针 +template +using RefPtr = RefBasePtr<_Ty, DefaultRefPtrRefProxy>; + +/// \~chinese +/// @brief 构造引用计数对象智能指针 +template +inline RefPtr<_Ty> MakePtr(_Args&&... args) +{ + static_assert(std::is_base_of::value, "_Ty must be derived from RefObject"); + + RefPtr<_Ty> ptr; + + _Ty** pptr = ptr.GetAddressOfAndRelease(); + (*pptr) = new _Ty(std::forward<_Args>(args)...); + return ptr; +} + +/// \~chinese +/// @brief 构造引用计数对象智能指针 +template +inline RefPtr<_Ty> MakePtr(_Ty* ptr) +{ + static_assert(std::is_base_of::value, "_Ty must be derived from RefObject"); + return RefPtr<_Ty>(ptr); +} + +} // namespace kiwano + +#ifndef KGE_DECLARE_SMART_PTR +#define KGE_DECLARE_SMART_PTR(CLASS) \ + class CLASS; \ + typedef ::kiwano::RefPtr CLASS##Ptr; +#endif diff --git a/src/kiwano/base/component/Button.cpp b/src/kiwano/base/component/Button.cpp index 64388a98..6971549d 100644 --- a/src/kiwano/base/component/Button.cpp +++ b/src/kiwano/base/component/Button.cpp @@ -25,23 +25,19 @@ namespace kiwano { -ButtonPtr Button::Create(const Callback& click) +Button::Button(const Callback& click) + : Button(click, nullptr, nullptr, nullptr) { - return Button::Create(click, nullptr, nullptr, nullptr); } -ButtonPtr Button::Create(const Callback& click, const Callback& pressed, const Callback& mouse_over, - const Callback& mouse_out) +Button::Button(const Callback& click, const Callback& pressed, const Callback& mouse_over, const Callback& mouse_out) + : status_(Status::Normal) + , click_callback_(click) + , pressed_callback_(pressed) + , mouse_over_callback_(mouse_over) + , mouse_out_callback_(mouse_out) { - ButtonPtr ptr = new (autogc) Button; - if (ptr) - { - ptr->SetClickCallback(click); - ptr->SetPressedCallback(pressed); - ptr->SetMouseOverCallback(mouse_over); - ptr->SetMouseOutCallback(mouse_out); - } - return ptr; + SetName("__KGE_BUTTON__"); } Button::Button() diff --git a/src/kiwano/base/component/Button.h b/src/kiwano/base/component/Button.h index 197be05b..069a70e9 100644 --- a/src/kiwano/base/component/Button.h +++ b/src/kiwano/base/component/Button.h @@ -45,7 +45,7 @@ public: /// \~chinese /// @brief 创建按钮 /// @param click 按钮点击回调函数 - static ButtonPtr Create(const Callback& click); + Button(const Callback& click); /// \~chinese /// @brief 创建按钮 @@ -53,8 +53,7 @@ public: /// @param pressed 按钮按下回调函数 /// @param mouse_over 按钮移入回调函数 /// @param mouse_out 按钮移出回调函数 - static ButtonPtr Create(const Callback& click, const Callback& pressed, const Callback& mouse_over, - const Callback& mouse_out); + Button(const Callback& click, const Callback& pressed, const Callback& mouse_over, const Callback& mouse_out); Button(); diff --git a/src/kiwano/core/Cloneable.h b/src/kiwano/core/Cloneable.h index dc9c0c58..b2d792b1 100644 --- a/src/kiwano/core/Cloneable.h +++ b/src/kiwano/core/Cloneable.h @@ -19,7 +19,6 @@ // THE SOFTWARE. #pragma once -#include namespace kiwano { @@ -28,7 +27,7 @@ template class Cloneable { public: - virtual RefPtr<_Ty> Clone() const = 0; + virtual _Ty* Clone() const = 0; protected: Cloneable() = default; diff --git a/src/kiwano/core/RefPtr.hpp b/src/kiwano/core/RefBasePtr.hpp similarity index 64% rename from src/kiwano/core/RefPtr.hpp rename to src/kiwano/core/RefBasePtr.hpp index bcb22f26..2f8650df 100644 --- a/src/kiwano/core/RefPtr.hpp +++ b/src/kiwano/core/RefBasePtr.hpp @@ -22,36 +22,16 @@ #include #include #include -#include namespace kiwano { -/** - * \~chinese - * @brief 默认的智能指针引用计数代理 - */ -struct DefaultRefPtrRefProxy -{ - static inline void Retain(RefObject* ptr) - { - if (ptr) - ptr->Retain(); - } - - static inline void Release(RefObject* ptr) - { - if (ptr) - ptr->Release(); - } -}; - /** * \~chinese * @brief 引用计数智能指针 */ -template -class RefPtr +template +class RefBasePtr { public: using value_type = _Ty; @@ -60,41 +40,41 @@ public: using reference_type = _Ty&; using const_reference_type = const _Ty&; - RefPtr() noexcept + RefBasePtr() noexcept : ptr_(nullptr) { } - RefPtr(std::nullptr_t) noexcept + RefBasePtr(std::nullptr_t) noexcept : ptr_(nullptr) { } - RefPtr(pointer_type p) + RefBasePtr(pointer_type p) : ptr_(p) { _ProxyTy::Retain(ptr_); } - RefPtr(const RefPtr& other) + RefBasePtr(const RefBasePtr& other) : ptr_(other.ptr_) { _ProxyTy::Retain(ptr_); } - RefPtr(RefPtr&& other) noexcept + RefBasePtr(RefBasePtr&& other) noexcept : ptr_(nullptr) { Swap(other); } - ~RefPtr() + ~RefBasePtr() { Tidy(); } template ::value, int>::type = 0> - RefPtr(const RefPtr<_UTy, _ProxyTy>& other) + RefBasePtr(const RefBasePtr<_UTy, _ProxyTy>& other) { ptr_ = dynamic_cast(other.Get()); _ProxyTy::Retain(ptr_); @@ -114,12 +94,12 @@ public: inline void Reset(pointer_type ptr = nullptr) { if (ptr) - RefPtr(ptr).Swap(*this); + RefBasePtr(ptr).Swap(*this); else Tidy(); } - inline void Swap(RefPtr& other) noexcept + inline void Swap(RefBasePtr& other) noexcept { std::swap(ptr_, other.ptr_); } @@ -163,36 +143,36 @@ public: return ptr_ == 0; } - inline RefPtr& operator=(const RefPtr& other) + inline RefBasePtr& operator=(const RefBasePtr& other) { if (other.ptr_ != ptr_) - RefPtr(other).Swap(*this); + RefBasePtr(other).Swap(*this); return (*this); } - inline RefPtr& operator=(RefPtr&& other) noexcept + inline RefBasePtr& operator=(RefBasePtr&& other) noexcept { if (other.ptr_ != ptr_) other.Swap(*this); return (*this); } - inline RefPtr& operator=(pointer_type p) + inline RefBasePtr& operator=(pointer_type p) { if (p != ptr_) - RefPtr(p).Swap(*this); + RefBasePtr(p).Swap(*this); return (*this); } template ::value, int>::type = 0> - inline RefPtr& operator=(const RefPtr<_UTy, _ProxyTy>& other) + inline RefBasePtr& operator=(const RefBasePtr<_UTy, _ProxyTy>& other) { if (other.Get() != ptr_) - RefPtr(dynamic_cast(other.Get())).Swap(*this); + RefBasePtr(dynamic_cast(other.Get())).Swap(*this); return (*this); } - inline RefPtr& operator=(std::nullptr_t) + inline RefBasePtr& operator=(std::nullptr_t) { Tidy(); return *this; @@ -210,67 +190,67 @@ private: }; template -inline bool operator==(const RefPtr<_Ty, _ProxyTy>& lhs, const RefPtr<_UTy, _ProxyTy>& rhs) noexcept +inline bool operator==(const RefBasePtr<_Ty, _ProxyTy>& lhs, const RefBasePtr<_UTy, _ProxyTy>& rhs) noexcept { return lhs.Get() == rhs.Get(); } template -inline bool operator==(const RefPtr<_Ty, _ProxyTy>& lhs, _Ty* rhs) noexcept +inline bool operator==(const RefBasePtr<_Ty, _ProxyTy>& lhs, _Ty* rhs) noexcept { return lhs.Get() == rhs; } template -inline bool operator==(_Ty* lhs, const RefPtr<_Ty, _ProxyTy>& rhs) noexcept +inline bool operator==(_Ty* lhs, const RefBasePtr<_Ty, _ProxyTy>& rhs) noexcept { return lhs == rhs.Get(); } template -inline bool operator==(const RefPtr<_Ty, _ProxyTy>& lhs, std::nullptr_t) noexcept +inline bool operator==(const RefBasePtr<_Ty, _ProxyTy>& lhs, std::nullptr_t) noexcept { return !static_cast(lhs); } template -inline bool operator==(std::nullptr_t, const RefPtr<_Ty, _ProxyTy>& rhs) noexcept +inline bool operator==(std::nullptr_t, const RefBasePtr<_Ty, _ProxyTy>& rhs) noexcept { return !static_cast(rhs); } template -inline bool operator!=(const RefPtr<_Ty, _ProxyTy>& lhs, const RefPtr<_UTy, _ProxyTy>& rhs) noexcept +inline bool operator!=(const RefBasePtr<_Ty, _ProxyTy>& lhs, const RefBasePtr<_UTy, _ProxyTy>& rhs) noexcept { return !(lhs == rhs); } template -inline bool operator!=(const RefPtr<_Ty, _ProxyTy>& lhs, _Ty* rhs) noexcept +inline bool operator!=(const RefBasePtr<_Ty, _ProxyTy>& lhs, _Ty* rhs) noexcept { return lhs.Get() != rhs; } template -inline bool operator!=(_Ty* lhs, const RefPtr<_Ty, _ProxyTy>& rhs) noexcept +inline bool operator!=(_Ty* lhs, const RefBasePtr<_Ty, _ProxyTy>& rhs) noexcept { return lhs != rhs.Get(); } template -inline bool operator!=(const RefPtr<_Ty, _ProxyTy>& lhs, std::nullptr_t) noexcept +inline bool operator!=(const RefBasePtr<_Ty, _ProxyTy>& lhs, std::nullptr_t) noexcept { return static_cast(lhs); } template -inline bool operator!=(std::nullptr_t, const RefPtr<_Ty, _ProxyTy>& rhs) noexcept +inline bool operator!=(std::nullptr_t, const RefBasePtr<_Ty, _ProxyTy>& rhs) noexcept { return static_cast(rhs); } template -inline bool operator<(const RefPtr<_Ty, _ProxyTy>& lhs, const RefPtr<_UTy, _ProxyTy>& rhs) noexcept +inline bool operator<(const RefBasePtr<_Ty, _ProxyTy>& lhs, const RefBasePtr<_UTy, _ProxyTy>& rhs) noexcept { return lhs.Get() < rhs.Get(); } @@ -278,27 +258,9 @@ inline bool operator<(const RefPtr<_Ty, _ProxyTy>& lhs, const RefPtr<_UTy, _Prox // template class cannot specialize std::swap, // so implement a swap function in kiwano namespace template -inline void swap(RefPtr<_Ty, _ProxyTy>& lhs, RefPtr<_Ty, _ProxyTy>& rhs) noexcept +inline void swap(RefBasePtr<_Ty, _ProxyTy>& lhs, RefBasePtr<_Ty, _ProxyTy>& rhs) noexcept { lhs.Swap(rhs); } -template -inline RefPtr<_Ty> MakePtr(_Args&&... args) -{ - static_assert(std::is_base_of::value, "_Ty must be derived from RefObject"); - - RefPtr<_Ty> ptr; - - _Ty** pptr = ptr.GetAddressOfAndRelease(); - (*pptr) = new _Ty(std::forward<_Args>(args)...); - return ptr; -} - } // namespace kiwano - -#ifndef KGE_DECLARE_SMART_PTR -#define KGE_DECLARE_SMART_PTR(CLASS) \ - class CLASS; \ - typedef ::kiwano::RefPtr CLASS##Ptr; -#endif diff --git a/src/kiwano/event/Event.h b/src/kiwano/event/Event.h index d252da98..172bf8a9 100644 --- a/src/kiwano/event/Event.h +++ b/src/kiwano/event/Event.h @@ -19,7 +19,8 @@ // THE SOFTWARE. #pragma once -#include +#include +#include #include #include @@ -74,13 +75,6 @@ private: const EventType type_; }; -/// \~chinese -/// @brief 事件特性:判断是否是事件 -template -struct IsBaseOfEvent : public std::bool_constant::value || std::is_same::value> -{ -}; - /// \~chinese /// @brief 事件特性:判断事件类型是否相同 template @@ -88,7 +82,7 @@ struct IsSameEventType { inline bool operator()(const Event* evt) const { - static_assert(kiwano::IsBaseOfEvent<_Ty>::value, "_Ty is not an event type."); + static_assert(std::is_base_of::value, "_Ty is not an event type."); return evt->GetType() == KGE_EVENT(_Ty); } }; @@ -103,7 +97,7 @@ inline const EventType& Event::GetType() const template inline bool Event::IsType() const { - static_assert(kiwano::IsBaseOfEvent<_Ty>::value, "_Ty is not an event type."); + static_assert(std::is_base_of::value, "_Ty is not an event type."); return IsSameEventType<_Ty>()(this); } diff --git a/src/kiwano/event/EventDispatcher.cpp b/src/kiwano/event/EventDispatcher.cpp index fa97b779..71abe117 100644 --- a/src/kiwano/event/EventDispatcher.cpp +++ b/src/kiwano/event/EventDispatcher.cpp @@ -58,14 +58,14 @@ EventListener* EventDispatcher::AddListener(EventListenerPtr listener) EventListener* EventDispatcher::AddListener(const String& name, EventType type, EventListener::Callback callback) { - EventListenerPtr listener = EventListener::Create(name, type, callback); - return AddListener(listener); + auto lis = MakePtr(name, type, callback); + return AddListener(lis); } EventListener* EventDispatcher::AddListener(EventType type, EventListener::Callback callback) { - EventListenerPtr listener = EventListener::Create(type, callback); - return AddListener(listener); + auto lis = MakePtr(type, callback); + return AddListener(lis); } void EventDispatcher::StartListeners(const String& name) diff --git a/src/kiwano/event/EventDispatcher.h b/src/kiwano/event/EventDispatcher.h index 6e46a9a4..56be5fab 100644 --- a/src/kiwano/event/EventDispatcher.h +++ b/src/kiwano/event/EventDispatcher.h @@ -59,7 +59,7 @@ public: template EventListener* AddListener(EventListener::Callback callback) { - static_assert(kiwano::IsBaseOfEvent<_EventTy>::value, "_EventTy is not an event type."); + static_assert(std::is_base_of::value, "_EventTy is not an event type."); return AddListener(KGE_EVENT(_EventTy), callback); } @@ -71,7 +71,7 @@ public: template EventListener* AddListener(const String& name, EventListener::Callback callback) { - static_assert(kiwano::IsBaseOfEvent<_EventTy>::value, "_EventTy is not an event type."); + static_assert(std::is_base_of::value, "_EventTy is not an event type."); return AddListener(name, KGE_EVENT(_EventTy), callback); } diff --git a/src/kiwano/event/EventListener.cpp b/src/kiwano/event/EventListener.cpp index f134d2c6..31800fb9 100644 --- a/src/kiwano/event/EventListener.cpp +++ b/src/kiwano/event/EventListener.cpp @@ -24,29 +24,6 @@ namespace kiwano { -EventListenerPtr EventListener::Create(EventType type, const Callback& callback) -{ - EventListenerPtr ptr = new (autogc) EventListener; - if (ptr) - { - ptr->SetEventType(type); - ptr->SetCallback(callback); - } - return ptr; -} - -EventListenerPtr EventListener::Create(const String& name, EventType type, const Callback& callback) -{ - EventListenerPtr ptr = new (autogc) EventListener; - if (ptr) - { - ptr->SetName(name); - ptr->SetEventType(type); - ptr->SetCallback(callback); - } - return ptr; -} - EventListener::EventListener() : type_() , callback_() @@ -56,6 +33,19 @@ EventListener::EventListener() { } +EventListener::EventListener(EventType type, const Callback& callback) + : EventListener() +{ + this->SetEventType(type); + this->SetCallback(callback); +} + +EventListener::EventListener(const String& name, EventType type, const Callback& callback) + : EventListener(type, callback) +{ + this->SetName(name); +} + EventListener::~EventListener() {} void EventListener::Receive(Event* evt) diff --git a/src/kiwano/event/EventListener.h b/src/kiwano/event/EventListener.h index 24ac6016..d6383662 100644 --- a/src/kiwano/event/EventListener.h +++ b/src/kiwano/event/EventListener.h @@ -20,9 +20,8 @@ #pragma once #include -#include -#include #include +#include #include namespace kiwano @@ -47,44 +46,21 @@ public: /// @brief 监听器回调函数 using Callback = Function; - /// \~chinese - /// @brief 创建监听器 - /// @param type 监听的事件类型 - /// @param callback 回调函数 - static EventListenerPtr Create(EventType type, const Callback& callback); - - /// \~chinese - /// @brief 创建监听器 - /// @param name 监听器名称 - /// @param type 监听的事件类型 - /// @param callback 回调函数 - static EventListenerPtr Create(const String& name, EventType type, const Callback& callback); - - /// \~chinese - /// @brief 创建监听器 - /// @tparam _EventTy 事件类型 - /// @param callback 回调函数 - template - static inline EventListenerPtr Create(const Callback& callback) - { - static_assert(kiwano::IsBaseOfEvent<_EventTy>::value, "_EventTy is not an event type."); - return EventListener::Create(KGE_EVENT(_EventTy), callback); - } - - /// \~chinese - /// @brief 创建监听器 - /// @tparam _EventTy 事件类型 - /// @param name 监听器名称 - /// @param callback 回调函数 - template - static inline EventListenerPtr Create(const String& name, const Callback& callback) - { - static_assert(kiwano::IsBaseOfEvent<_EventTy>::value, "_EventTy is not an event type."); - return EventListener::Create(name, KGE_EVENT(_EventTy), callback); - } - EventListener(); + /// \~chinese + /// @brief 创建监听器 + /// @param type 监听的事件类型 + /// @param callback 回调函数 + EventListener(EventType type, const Callback& callback); + + /// \~chinese + /// @brief 创建监听器 + /// @param name 监听器名称 + /// @param type 监听的事件类型 + /// @param callback 回调函数 + EventListener(const String& name, EventType type, const Callback& callback); + virtual ~EventListener(); /// \~chinese @@ -142,7 +118,7 @@ public: template inline void SetEventType() { - static_assert(kiwano::IsBaseOfEvent<_EventTy>::value, "_EventTy is not an event type."); + static_assert(std::is_base_of::value, "_EventTy is not an event type."); SetEventType(KGE_EVENT(_EventTy)); } diff --git a/src/kiwano/kiwano.h b/src/kiwano/kiwano.h index 1d42d897..17a57ea7 100644 --- a/src/kiwano/kiwano.h +++ b/src/kiwano/kiwano.h @@ -46,7 +46,7 @@ #include #include #include -#include +#include #include // diff --git a/src/kiwano/platform/Application.cpp b/src/kiwano/platform/Application.cpp index cf0f3856..d0a64ca3 100644 --- a/src/kiwano/platform/Application.cpp +++ b/src/kiwano/platform/Application.cpp @@ -52,7 +52,7 @@ void Application::Run(RunnerPtr runner) running_ = true; is_paused_ = false; runner_ = runner; - timer_ = Timer::Create(); + timer_ = MakePtr(); // Initialize runner runner->InitSettings(); diff --git a/src/kiwano/platform/Runner.cpp b/src/kiwano/platform/Runner.cpp index 6193a6d9..869da1ac 100644 --- a/src/kiwano/platform/Runner.cpp +++ b/src/kiwano/platform/Runner.cpp @@ -28,45 +28,9 @@ namespace kiwano { -RunnerPtr Runner::Create(Settings settings) +Runner::Runner(Settings settings) + : settings_(settings) { - RunnerPtr ptr = new (autogc) Runner; - if (ptr) - { - ptr->SetSettings(settings); - } - return ptr; -} - -RunnerPtr Runner::Create(Settings settings, Function on_ready, Function on_destroy) -{ - class CallbackRunner : public Runner - { - public: - void OnReady() override - { - if (on_ready) - on_ready(); - } - - void OnDestroy() override - { - if (on_destroy) - on_destroy(); - } - - Function on_ready; - Function on_destroy; - }; - - RefPtr ptr = new (autogc) CallbackRunner; - if (ptr) - { - ptr->on_ready = on_ready; - ptr->on_destroy = on_destroy; - ptr->SetSettings(settings); - } - return ptr; } Runner::Runner() {} @@ -105,7 +69,7 @@ void Runner::InitSettings() // Create frame ticker if (!settings_.frame_interval.IsZero()) { - frame_ticker_ = Ticker::Create(settings_.frame_interval, -1); + frame_ticker_ = MakePtr(settings_.frame_interval, -1); } } diff --git a/src/kiwano/platform/Runner.h b/src/kiwano/platform/Runner.h index dfe1da94..f48ce828 100644 --- a/src/kiwano/platform/Runner.h +++ b/src/kiwano/platform/Runner.h @@ -62,20 +62,13 @@ struct Settings class KGE_API Runner : public ObjectBase { public: - /// \~chinese - /// @brief 创建程序运行器 - /// @param main_window 主窗口 - static RunnerPtr Create(Settings settings); - - /// \~chinese - /// @brief 创建程序运行器 - /// @param main_window 主窗口 - /// @param on_ready 应用程序初始化完成后执行的回调函数 - /// @param on_destroy 应用程序销毁时执行的回调函数 - static RunnerPtr Create(Settings settings, Function on_ready, Function on_destroy = nullptr); - Runner(); + /// \~chinese + /// @brief 创建程序运行器 + /// @param main_window 主窗口 + Runner(Settings settings); + virtual ~Runner(); /// \~chinese diff --git a/src/kiwano/platform/win32/ComPtr.hpp b/src/kiwano/platform/win32/ComPtr.hpp index 28e41bfd..c019b950 100644 --- a/src/kiwano/platform/win32/ComPtr.hpp +++ b/src/kiwano/platform/win32/ComPtr.hpp @@ -21,7 +21,7 @@ #pragma once #include #include -#include +#include #include namespace kiwano @@ -43,6 +43,6 @@ struct ComPtrProxy // ComPtr<> is a smart pointer for COM template ::value, int>::type> -using ComPtr = RefPtr<_Ty, ComPtrProxy>; +using ComPtr = RefBasePtr<_Ty, ComPtrProxy>; } // namespace kiwano diff --git a/src/kiwano/platform/win32/WindowImpl.cpp b/src/kiwano/platform/win32/WindowImpl.cpp index c044eba4..e069005f 100644 --- a/src/kiwano/platform/win32/WindowImpl.cpp +++ b/src/kiwano/platform/win32/WindowImpl.cpp @@ -90,7 +90,7 @@ private: WindowPtr Window::Create(const WindowConfig& config) { - WindowWin32ImplPtr ptr = new (autogc) WindowWin32Impl; + WindowWin32ImplPtr ptr = MakePtr(); if (ptr) { ptr->Init(config); diff --git a/src/kiwano/render/Brush.cpp b/src/kiwano/render/Brush.cpp index 8904bef4..a2cc08c6 100644 --- a/src/kiwano/render/Brush.cpp +++ b/src/kiwano/render/Brush.cpp @@ -59,34 +59,22 @@ RadialGradientStyle::RadialGradientStyle(const Point& center, const Vec2& offset { } -BrushPtr Brush::Create(const Color& color) +Brush::Brush(const Color& color) + : Brush() { - BrushPtr ptr = new (autogc) Brush; - if (ptr) - { - ptr->SetColor(color); - } - return ptr; + SetColor(color); } -BrushPtr Brush::Create(const LinearGradientStyle& style) +Brush::Brush(const LinearGradientStyle& style) + : Brush() { - BrushPtr ptr = new (autogc) Brush; - if (ptr) - { - ptr->SetStyle(style); - } - return ptr; + SetStyle(style); } -BrushPtr Brush::Create(const RadialGradientStyle& style) +Brush::Brush(const RadialGradientStyle& style) + : Brush() { - BrushPtr ptr = new (autogc) Brush; - if (ptr) - { - ptr->SetStyle(style); - } - return ptr; + SetStyle(style); } Brush::Brush() diff --git a/src/kiwano/render/Brush.h b/src/kiwano/render/Brush.h index f2e1648d..d840c85b 100644 --- a/src/kiwano/render/Brush.h +++ b/src/kiwano/render/Brush.h @@ -92,17 +92,17 @@ public: /// \~chinese /// @brief 创建纯色画刷 /// @param color 画刷颜色 - static BrushPtr Create(const Color& color); + Brush(const Color& color); /// \~chinese /// @brief 创建线性渐变样式 /// @param style 线性渐变样式 - static BrushPtr Create(const LinearGradientStyle& style); + Brush(const LinearGradientStyle& style); /// \~chinese /// @brief 创建径向渐变样式 /// @param style 径向渐变样式 - static BrushPtr Create(const RadialGradientStyle& style); + Brush(const RadialGradientStyle& style); Brush(); diff --git a/src/kiwano/render/DirectX/RendererImpl.cpp b/src/kiwano/render/DirectX/RendererImpl.cpp index d45f24d6..8666e5f8 100644 --- a/src/kiwano/render/DirectX/RendererImpl.cpp +++ b/src/kiwano/render/DirectX/RendererImpl.cpp @@ -97,7 +97,7 @@ void RendererImpl::MakeContextForWindow(WindowPtr window) // Initialize other device resources if (SUCCEEDED(hr)) { - RenderContextImplPtr ctx = new (autogc) RenderContextImpl; + RenderContextImplPtr ctx = MakePtr(); hr = ctx->CreateDeviceResources(d2d_res_->GetFactory(), d2d_res_->GetDeviceContext()); if (SUCCEEDED(hr)) @@ -403,7 +403,7 @@ void RendererImpl::CreateGifImageFrame(GifImage::Frame& frame, const GifImage& g if (SUCCEEDED(hr)) { - frame.texture = new (autogc) Texture; + frame.texture = MakePtr(); NativePtr::Set(frame.texture, bitmap); frame.texture->SetSize({ bitmap->GetSize().width, bitmap->GetSize().height }); @@ -769,7 +769,7 @@ void RendererImpl::CreateShapeSink(ShapeMaker& maker) if (SUCCEEDED(hr)) { - ShapePtr shape = new (autogc) Shape; + ShapePtr shape = MakePtr(); NativePtr::Set(shape, geometry); maker.SetShape(shape); @@ -945,7 +945,7 @@ void RendererImpl::CreateStrokeStyle(StrokeStyle& stroke_style) RenderContextPtr RendererImpl::CreateTextureRenderContext(Texture& texture, const Size* desired_size) { - RenderContextImplPtr ptr = new (autogc) RenderContextImpl; + RenderContextImplPtr ptr = MakePtr(); HRESULT hr = S_OK; if (!d2d_res_) diff --git a/src/kiwano/render/Font.cpp b/src/kiwano/render/Font.cpp index 5d9fc2ab..b148ef3f 100644 --- a/src/kiwano/render/Font.cpp +++ b/src/kiwano/render/Font.cpp @@ -24,26 +24,14 @@ namespace kiwano { -FontPtr Font::Create(const String& file) +Font::Font(const String& file) { - FontPtr ptr = new (autogc) Font; - if (ptr) - { - if (!ptr->Load(file)) - return nullptr; - } - return ptr; + Load(file); } -FontPtr Font::Create(const Resource& resource) +Font::Font(const Resource& resource) { - FontPtr ptr = new (autogc) Font; - if (ptr) - { - if (!ptr->Load(resource)) - return nullptr; - } - return ptr; + Load(resource); } Font::Font() {} diff --git a/src/kiwano/render/Font.h b/src/kiwano/render/Font.h index 29db02e4..ef9491c8 100644 --- a/src/kiwano/render/Font.h +++ b/src/kiwano/render/Font.h @@ -44,11 +44,11 @@ class Font : public NativeObject public: /// \~chinese /// @brief 创建字体 - static FontPtr Create(const String& file); + Font(const String& file); /// \~chinese /// @brief 创建字体 - static FontPtr Create(const Resource& resource); + Font(const Resource& resource); Font(); diff --git a/src/kiwano/render/Frame.cpp b/src/kiwano/render/Frame.cpp index 44fc14af..f0a66265 100644 --- a/src/kiwano/render/Frame.cpp +++ b/src/kiwano/render/Frame.cpp @@ -24,36 +24,19 @@ namespace kiwano { -FramePtr Frame::Create(const String& file_path) +Frame::Frame(const String& file_path) { - FramePtr ptr = new (autogc) Frame; - if (ptr) - { - if (!ptr->Load(file_path)) - return nullptr; - } - return ptr; + Load(file_path); } -FramePtr Frame::Create(const Resource& res) +Frame::Frame(const Resource& res) { - FramePtr ptr = new (autogc) Frame; - if (ptr) - { - if (!ptr->Load(res)) - return nullptr; - } - return ptr; + Load(res); } -FramePtr Frame::Create(TexturePtr texture) +Frame::Frame(TexturePtr texture) { - FramePtr ptr = new (autogc) Frame; - if (ptr) - { - ptr->SetTexture(texture); - } - return ptr; + SetTexture(texture); } Frame::Frame() {} diff --git a/src/kiwano/render/Frame.h b/src/kiwano/render/Frame.h index f0679724..96e45b60 100644 --- a/src/kiwano/render/Frame.h +++ b/src/kiwano/render/Frame.h @@ -36,17 +36,17 @@ public: /// \~chinese /// @brief 创建图像帧 /// @param file_path 图像路径 - static FramePtr Create(const String& file_path); + Frame(const String& file_path); /// \~chinese /// @brief 创建图像帧 /// @param res 图像资源 - static FramePtr Create(const Resource& res); + Frame(const Resource& res); /// \~chinese /// @brief 创建图像帧 /// @param texture 纹理 - static FramePtr Create(TexturePtr texture); + Frame(TexturePtr texture); /// \~chinese /// @brief 构建空图像帧 diff --git a/src/kiwano/render/FrameSequence.cpp b/src/kiwano/render/FrameSequence.cpp index cd9a7d47..ec895f02 100644 --- a/src/kiwano/render/FrameSequence.cpp +++ b/src/kiwano/render/FrameSequence.cpp @@ -24,31 +24,15 @@ namespace kiwano { -FrameSequencePtr FrameSequence::Create() +FrameSequence::FrameSequence(const Vector& frames) { - FrameSequencePtr ptr = new (autogc) FrameSequence; - return ptr; + AddFrames(frames); } -FrameSequencePtr FrameSequence::Create(const Vector& frames) -{ - FrameSequencePtr ptr = new (autogc) FrameSequence; - if (ptr) - { - ptr->AddFrames(frames); - } - return ptr; -} - -FrameSequencePtr FrameSequence::Create(FramePtr frame, int cols, int rows, int max_num, float padding_x, +FrameSequence::FrameSequence(FramePtr frame, int cols, int rows, int max_num, float padding_x, float padding_y) { - FrameSequencePtr ptr = new (autogc) FrameSequence; - if (ptr) - { - ptr->AddFrames(frame, cols, rows, max_num, padding_x, padding_y); - } - return ptr; + AddFrames(frame, cols, rows, max_num, padding_x, padding_y); } FrameSequence::FrameSequence() {} @@ -103,7 +87,7 @@ void FrameSequence::AddFrames(FramePtr frame, int cols, int rows, int max_num, f for (int j = 0; j < cols; j++) { - FramePtr ptr = new (autogc) Frame; + FramePtr ptr = MakePtr(); if (ptr) { ptr->SetTexture(frame->GetTexture()); @@ -140,7 +124,7 @@ size_t FrameSequence::GetFramesCount() const FrameSequencePtr FrameSequence::Clone() const { - auto frame_seq = new (autogc) FrameSequence; + auto frame_seq = MakePtr(); if (frame_seq) { frame_seq->AddFrames(frames_); @@ -150,7 +134,7 @@ FrameSequencePtr FrameSequence::Clone() const FrameSequencePtr FrameSequence::Reverse() const { - auto frame_seq = new (autogc) FrameSequence; + auto frame_seq = MakePtr(); if (!frames_.empty()) { for (auto iter = frames_.crbegin(), crend = frames_.crend(); iter != crend; ++iter) diff --git a/src/kiwano/render/FrameSequence.h b/src/kiwano/render/FrameSequence.h index 904eba8d..ab54b5a2 100644 --- a/src/kiwano/render/FrameSequence.h +++ b/src/kiwano/render/FrameSequence.h @@ -34,14 +34,10 @@ KGE_DECLARE_SMART_PTR(FrameSequence); class KGE_API FrameSequence : public ObjectBase { public: - /// \~chinese - /// @brief 创建序列帧 - static FrameSequencePtr Create(); - /// \~chinese /// @brief 创建序列帧 /// @param frames 图像帧集合 - static FrameSequencePtr Create(const Vector& frames); + FrameSequence(const Vector& frames); /// \~chinese /// @brief 按行列分割图像并创建序列帧 @@ -51,7 +47,7 @@ public: /// @param max_num 最大帧数量,设-1为将分割后的图像全部作为序列帧 /// @param padding_x X方向间隔 /// @param padding_y Y方向间隔 - static FrameSequencePtr Create(FramePtr frame, int cols, int rows = 1, int max_num = -1, float padding_x = 0, + FrameSequence(FramePtr frame, int cols, int rows = 1, int max_num = -1, float padding_x = 0, float padding_y = 0); /// \~chinese diff --git a/src/kiwano/render/GifImage.cpp b/src/kiwano/render/GifImage.cpp index 8e59c177..53eebf62 100644 --- a/src/kiwano/render/GifImage.cpp +++ b/src/kiwano/render/GifImage.cpp @@ -25,26 +25,16 @@ namespace kiwano { -GifImagePtr GifImage::Create(const String& file_path) +GifImage::GifImage(const String& file_path) + : GifImage() { - GifImagePtr ptr = new (autogc) GifImage; - if (ptr) - { - if (!ptr->Load(file_path)) - return nullptr; - } - return ptr; + Load(file_path); } -GifImagePtr GifImage::Create(const Resource& res) +GifImage::GifImage(const Resource& res) + : GifImage() { - GifImagePtr ptr = new (autogc) GifImage; - if (ptr) - { - if (!ptr->Load(res)) - return nullptr; - } - return ptr; + Load(res); } GifImage::GifImage() diff --git a/src/kiwano/render/GifImage.h b/src/kiwano/render/GifImage.h index 43f50ee8..23745993 100644 --- a/src/kiwano/render/GifImage.h +++ b/src/kiwano/render/GifImage.h @@ -40,11 +40,11 @@ class KGE_API GifImage : public NativeObject public: /// \~chinese /// @brief 创建GIF图片 - static GifImagePtr Create(const String& file_path); + GifImage(const String& file_path); /// \~chinese /// @brief 创建GIF图片 - static GifImagePtr Create(const Resource& res); + GifImage(const Resource& res); GifImage(); diff --git a/src/kiwano/render/Shape.cpp b/src/kiwano/render/Shape.cpp index b077ddcc..f95038d5 100644 --- a/src/kiwano/render/Shape.cpp +++ b/src/kiwano/render/Shape.cpp @@ -136,35 +136,35 @@ bool Shape::ContainsPoint(const Point& point, const Matrix3x2* transform) const ShapePtr Shape::CreateLine(const Point& begin, const Point& end) { - ShapePtr output = new (autogc) Shape; + ShapePtr output = MakePtr(); Renderer::GetInstance().CreateLineShape(*output, begin, end); return output; } ShapePtr Shape::CreateRect(const Rect& rect) { - ShapePtr output = new (autogc) Shape; + ShapePtr output = MakePtr(); Renderer::GetInstance().CreateRectShape(*output, rect); return output; } ShapePtr Shape::CreateRoundedRect(const Rect& rect, const Vec2& radius) { - ShapePtr output = new (autogc) Shape; + ShapePtr output = MakePtr(); Renderer::GetInstance().CreateRoundedRectShape(*output, rect, radius); return output; } ShapePtr Shape::CreateCircle(const Point& center, float radius) { - ShapePtr output = new (autogc) Shape; + ShapePtr output = MakePtr(); Renderer::GetInstance().CreateEllipseShape(*output, center, Vec2{ radius, radius }); return output; } ShapePtr Shape::CreateEllipse(const Point& center, const Vec2& radius) { - ShapePtr output = new (autogc) Shape; + ShapePtr output = MakePtr(); Renderer::GetInstance().CreateEllipseShape(*output, center, radius); return output; } diff --git a/src/kiwano/render/ShapeMaker.cpp b/src/kiwano/render/ShapeMaker.cpp index 0ea96b04..819e0564 100644 --- a/src/kiwano/render/ShapeMaker.cpp +++ b/src/kiwano/render/ShapeMaker.cpp @@ -28,12 +28,6 @@ namespace kiwano { -ShapeMakerPtr ShapeMaker::Create() -{ - ShapeMakerPtr ptr = new (autogc) ShapeMaker; - return ptr; -} - ShapeMaker::ShapeMaker() {} ShapeMaker::~ShapeMaker() @@ -151,8 +145,8 @@ void ShapeMaker::AddArc(const Point& point, const Size& radius, float rotation, ShapePtr ShapeMaker::Combine(ShapePtr shape_a, ShapePtr shape_b, CombineMode mode, const Matrix3x2* matrix) { - ShapeMakerPtr maker = ShapeMaker::Create(); - maker->OpenStream(); + ShapeMaker maker; + maker.OpenStream(); #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX if (shape_a && shape_b) @@ -170,8 +164,8 @@ ShapePtr ShapeMaker::Combine(ShapePtr shape_a, ShapePtr shape_b, CombineMode mod // not supported #endif - maker->CloseStream(); - return maker->GetShape(); + maker.CloseStream(); + return maker.GetShape(); } void ShapeMaker::OpenStream() diff --git a/src/kiwano/render/ShapeMaker.h b/src/kiwano/render/ShapeMaker.h index 8fd78593..08cd5dfa 100644 --- a/src/kiwano/render/ShapeMaker.h +++ b/src/kiwano/render/ShapeMaker.h @@ -46,10 +46,6 @@ enum class CombineMode class KGE_API ShapeMaker : public NativeObject { public: - /// \~chinese - /// @brief 创建形状生成器 - static ShapeMakerPtr Create(); - ShapeMaker(); ~ShapeMaker(); diff --git a/src/kiwano/render/StrokeStyle.cpp b/src/kiwano/render/StrokeStyle.cpp index fa700932..c4ae9a50 100644 --- a/src/kiwano/render/StrokeStyle.cpp +++ b/src/kiwano/render/StrokeStyle.cpp @@ -24,39 +24,30 @@ namespace kiwano { -StrokeStylePtr StrokeStyle::Create(float width, CapStyle cap, LineJoinStyle line_join) +StrokeStyle::StrokeStyle(float width, CapStyle cap, LineJoinStyle line_join) + : StrokeStyle(width, cap, line_join, DashStyle::Solid) { - return StrokeStyle::Create(width, cap, line_join, DashStyle::Solid); } -StrokeStylePtr StrokeStyle::Create(float width, CapStyle cap, LineJoinStyle line_join, DashStyle dash, - float dash_offset) +StrokeStyle::StrokeStyle(float width, CapStyle cap, LineJoinStyle line_join, DashStyle dash, float dash_offset) + : StrokeStyle() { - StrokeStylePtr ptr = new (autogc) StrokeStyle; - if (ptr) - { - ptr->SetStrokeWidth(width); - ptr->SetCapStyle(cap); - ptr->SetLineJoinStyle(line_join); - ptr->SetDashStyle(dash); - ptr->SetDashOffset(dash_offset); - } - return ptr; + SetStrokeWidth(width); + SetCapStyle(cap); + SetLineJoinStyle(line_join); + SetDashStyle(dash); + SetDashOffset(dash_offset); } -StrokeStylePtr StrokeStyle::Create(float width, CapStyle cap, LineJoinStyle line_join, const float* dash_array, - size_t dash_size, float dash_offset) +StrokeStyle::StrokeStyle(float width, CapStyle cap, LineJoinStyle line_join, const float* dash_array, size_t dash_size, + float dash_offset) + : StrokeStyle() { - StrokeStylePtr ptr = new (autogc) StrokeStyle; - if (ptr) - { - ptr->SetStrokeWidth(width); - ptr->SetCapStyle(cap); - ptr->SetLineJoinStyle(line_join); - ptr->SetDashStyle(dash_array, dash_size); - ptr->SetDashOffset(dash_offset); - } - return ptr; + SetStrokeWidth(width); + SetCapStyle(cap); + SetLineJoinStyle(line_join); + SetDashStyle(dash_array, dash_size); + SetDashOffset(dash_offset); } StrokeStyle::StrokeStyle() diff --git a/src/kiwano/render/StrokeStyle.h b/src/kiwano/render/StrokeStyle.h index 5831b0b6..d5ef65ae 100644 --- a/src/kiwano/render/StrokeStyle.h +++ b/src/kiwano/render/StrokeStyle.h @@ -74,8 +74,7 @@ public: /// @param width 线条宽度 /// @param cap 线条端点样式 /// @param line_join 线条交点样式 - static StrokeStylePtr Create(float width, CapStyle cap = CapStyle::Flat, - LineJoinStyle line_join = LineJoinStyle::Miter); + StrokeStyle(float width, CapStyle cap = CapStyle::Flat, LineJoinStyle line_join = LineJoinStyle::Miter); /// \~chinese /// @brief 创建线条样式 @@ -84,8 +83,7 @@ public: /// @param line_join 线条交点样式 /// @param dash 线条虚线样式 /// @param dash_offset 线条虚线偏移量 - static StrokeStylePtr Create(float width, CapStyle cap, LineJoinStyle line_join, DashStyle dash, - float dash_offset = 0.0f); + StrokeStyle(float width, CapStyle cap, LineJoinStyle line_join, DashStyle dash, float dash_offset = 0.0f); /// \~chinese /// @brief 创建线条样式 @@ -95,8 +93,8 @@ public: /// @param dash_array 线条虚线的长度与间隙数组 /// @param dash_size 线条虚线数组大小 /// @param dash_offset 线条虚线偏移量 - static StrokeStylePtr Create(float width, CapStyle cap, LineJoinStyle line_join, const float* dash_array, - size_t dash_size, float dash_offset = 0.0f); + StrokeStyle(float width, CapStyle cap, LineJoinStyle line_join, const float* dash_array, size_t dash_size, + float dash_offset = 0.0f); /// \~chinese /// @brief 创建线条样式 @@ -107,10 +105,10 @@ public: /// @param dash_array 线条虚线的长度与间隙数组 /// @param dash_offset 线条虚线偏移量 template - static inline StrokeStylePtr Create(float width, CapStyle cap, LineJoinStyle line_join, - float (&dash_array)[_DashSize], float dash_offset = 0.0f) + StrokeStyle(float width, CapStyle cap, LineJoinStyle line_join, float (&dash_array)[_DashSize], + float dash_offset = 0.0f) + : StrokeStyle(width, cap, line_join, dash_array, _DashSize, dash_offset) { - return StrokeStyle::Create(width, cap, line_join, dash_array, _DashSize, dash_offset); } StrokeStyle(); diff --git a/src/kiwano/render/TextLayout.cpp b/src/kiwano/render/TextLayout.cpp index c49aa212..10d45a5f 100644 --- a/src/kiwano/render/TextLayout.cpp +++ b/src/kiwano/render/TextLayout.cpp @@ -28,28 +28,18 @@ namespace kiwano { -TextLayoutPtr TextLayout::Create() -{ - TextLayoutPtr ptr = new (autogc) TextLayout; - return ptr; -} - -TextLayoutPtr TextLayout::Create(const String& content, const TextStyle& style) -{ - TextLayoutPtr ptr = new (autogc) TextLayout; - if (ptr) - { - ptr->Reset(content, style); - } - return ptr; -} - TextLayout::TextLayout() : dirty_flag_(DirtyFlag::Clean) , line_count_(0) { } +TextLayout::TextLayout(const String& content, const TextStyle& style) + : TextLayout() +{ + Reset(content, style); +} + void TextLayout::Reset(const String& content, const TextStyle& style) { content_ = content; diff --git a/src/kiwano/render/TextLayout.h b/src/kiwano/render/TextLayout.h index c28d6869..3dfe0444 100644 --- a/src/kiwano/render/TextLayout.h +++ b/src/kiwano/render/TextLayout.h @@ -38,19 +38,13 @@ KGE_DECLARE_SMART_PTR(TextLayout); class KGE_API TextLayout : public NativeObject { public: - /// \~chinese - /// @brief 创建文本布局 - static TextLayoutPtr Create(); + TextLayout(); /// \~chinese /// @brief 创建文本布局 /// @param content 文字内容 /// @param style 文本样式 - static TextLayoutPtr Create(const String& content, const TextStyle& style); - - /// \~chinese - /// @brief 构造空的文本布局 - TextLayout(); + TextLayout(const String& content, const TextStyle& style); /// \~chinese /// @brief 文本布局是否陈旧 diff --git a/src/kiwano/render/Texture.cpp b/src/kiwano/render/Texture.cpp index 8c745c20..aecc0dc3 100644 --- a/src/kiwano/render/Texture.cpp +++ b/src/kiwano/render/Texture.cpp @@ -30,26 +30,16 @@ namespace kiwano InterpolationMode Texture::default_interpolation_mode_ = InterpolationMode::Linear; -TexturePtr Texture::Create(const String& file_path) +Texture::Texture(const String& file_path) + : Texture() { - TexturePtr ptr = new (autogc) Texture; - if (ptr) - { - if (!ptr->Load(file_path)) - return nullptr; - } - return ptr; + Load(file_path); } -TexturePtr Texture::Create(const Resource& res) +Texture::Texture(const Resource& res) + : Texture() { - TexturePtr ptr = new (autogc) Texture; - if (ptr) - { - if (!ptr->Load(res)) - return nullptr; - } - return ptr; + Load(res); } Texture::Texture() diff --git a/src/kiwano/render/Texture.h b/src/kiwano/render/Texture.h index 8fac0207..f3c4bf51 100644 --- a/src/kiwano/render/Texture.h +++ b/src/kiwano/render/Texture.h @@ -56,11 +56,11 @@ class KGE_API Texture : public NativeObject public: /// \~chinese /// @brief 从本地文件创建纹理 - static TexturePtr Create(const String& file_path); + Texture(const String& file_path); /// \~chinese /// @brief 从资源创建纹理 - static TexturePtr Create(const Resource& res); + Texture(const Resource& res); Texture(); diff --git a/src/kiwano/render/TextureCache.cpp b/src/kiwano/render/TextureCache.cpp index 9ccc41c5..143d650c 100644 --- a/src/kiwano/render/TextureCache.cpp +++ b/src/kiwano/render/TextureCache.cpp @@ -33,7 +33,7 @@ RefPtr<_Ty> CreateOrGetCache(_CacheTy& cache, const _PathTy& path, size_t hash) return iter->second; } - RefPtr<_Ty> texture = new (autogc) _Ty; + RefPtr<_Ty> texture = MakePtr<_Ty>(); if (texture->Load(path)) { cache.insert(std::make_pair(hash, texture)); diff --git a/src/kiwano/utils/ConfigIni.cpp b/src/kiwano/utils/ConfigIni.cpp index 07081e4b..12503650 100644 --- a/src/kiwano/utils/ConfigIni.cpp +++ b/src/kiwano/utils/ConfigIni.cpp @@ -94,17 +94,11 @@ public: } }; -ConfigIniPtr ConfigIni::Create(const String& file_path) +ConfigIni::ConfigIni() {} + +ConfigIni::ConfigIni(const String& file_path) { - ConfigIniPtr ptr = new (autogc) ConfigIni; - if (ptr) - { - if (!ptr->Load(file_path)) - { - return nullptr; - } - } - return ptr; + Load(file_path); } bool ConfigIni::Load(const String& file_path) diff --git a/src/kiwano/utils/ConfigIni.h b/src/kiwano/utils/ConfigIni.h index 07cc18a0..b0a4f8b1 100644 --- a/src/kiwano/utils/ConfigIni.h +++ b/src/kiwano/utils/ConfigIni.h @@ -32,11 +32,6 @@ KGE_DECLARE_SMART_PTR(ConfigIni); class KGE_API ConfigIni : public ObjectBase { public: - /// \~chinese - /// @brief 加载 ini 文件 - /// @param file_path 文件路径 - static ConfigIniPtr Create(const String& file_path); - /// \~chinese /// @brief 键值字典 typedef UnorderedMap ValueMap; @@ -45,6 +40,13 @@ public: /// @brief Section字典 typedef UnorderedMap SectionMap; + ConfigIni(); + + /// \~chinese + /// @brief 加载 ini 文件 + /// @param file_path 文件路径 + ConfigIni(const String& file_path); + /// \~chinese /// @brief 加载 ini 文件 /// @param file_path 文件路径 diff --git a/src/kiwano/utils/EventTicker.cpp b/src/kiwano/utils/EventTicker.cpp index 3c48973f..c8bf1632 100644 --- a/src/kiwano/utils/EventTicker.cpp +++ b/src/kiwano/utils/EventTicker.cpp @@ -29,15 +29,11 @@ TickEvent::TickEvent() { } -EventTickerPtr EventTicker::Create(Duration interval, int times) +EventTicker::EventTicker() {} + +EventTicker::EventTicker(Duration interval, int tick_count) + : Ticker(interval, tick_count) { - EventTickerPtr ptr = new (autogc) EventTicker; - if (ptr) - { - ptr->SetInterval(interval); - ptr->SetTotalTickCount(times); - } - return ptr; } bool EventTicker::Tick(Duration dt) diff --git a/src/kiwano/utils/EventTicker.h b/src/kiwano/utils/EventTicker.h index c1fd82d3..c71b49d3 100644 --- a/src/kiwano/utils/EventTicker.h +++ b/src/kiwano/utils/EventTicker.h @@ -57,11 +57,13 @@ class KGE_API EventTicker , public EventDispatcher { public: + EventTicker(); + /// \~chinese /// @brief 创建事件报时器 /// @param interval 报时间隔 - /// @param times 报时次数(设 -1 为永久) - static EventTickerPtr Create(Duration interval, int times = -1); + /// @param tick_count 报时次数(设 -1 为永久) + EventTicker(Duration interval, int tick_count = -1); using Ticker::Tick; diff --git a/src/kiwano/utils/Logger.cpp b/src/kiwano/utils/Logger.cpp index aac1c82c..47503dcf 100644 --- a/src/kiwano/utils/Logger.cpp +++ b/src/kiwano/utils/Logger.cpp @@ -194,10 +194,8 @@ std::ostream& ConsoleColorBrush(std::ostream& os) return os; } -LogProviderPtr ConsoleLogProvider::Create() +ConsoleLogProvider::ConsoleLogProvider() { - LogProviderPtr ptr = new ConsoleLogProvider; - return ptr; } ConsoleLogProvider::~ConsoleLogProvider() @@ -242,14 +240,9 @@ ConsoleLogProvider::ConsoleColor ConsoleLogProvider::GetColor(LogLevel level) return ConsoleColorBrush<0>; } -LogProviderPtr FileLogProvider::Create(const String& filepath, std::ios_base::openmode mode) +FileLogProvider::FileLogProvider(const String& filepath, std::ios_base::openmode mode) { - RefPtr ptr = new FileLogProvider; - if (ptr) - { - ptr->ofs_.open(filepath, mode); - } - return ptr; + ofs_.open(filepath, mode); } FileLogProvider::~FileLogProvider() @@ -500,10 +493,10 @@ Logger::Logger() , buffer_(1024) , stream_(&buffer_) { - LogFormaterPtr formater = new TextFormater; + LogFormaterPtr formater = MakePtr(); SetFormater(formater); - LogProviderPtr provider = ConsoleLogProvider::Create(); + LogProviderPtr provider = MakePtr(); AddProvider(provider); } diff --git a/src/kiwano/utils/Logger.h b/src/kiwano/utils/Logger.h index 8193b0f4..4e666064 100644 --- a/src/kiwano/utils/Logger.h +++ b/src/kiwano/utils/Logger.h @@ -175,7 +175,7 @@ protected: class KGE_API ConsoleLogProvider : public LogProvider { public: - static LogProviderPtr Create(); + ConsoleLogProvider(); virtual ~ConsoleLogProvider(); @@ -199,7 +199,7 @@ private: class KGE_API FileLogProvider : public LogProvider { public: - static LogProviderPtr Create(const String& filepath, std::ios_base::openmode mode = std::ios_base::out); + FileLogProvider(const String& filepath, std::ios_base::openmode mode = std::ios_base::out); virtual ~FileLogProvider(); diff --git a/src/kiwano/utils/ResourceCache.cpp b/src/kiwano/utils/ResourceCache.cpp index 625e5c87..9dc5da6a 100644 --- a/src/kiwano/utils/ResourceCache.cpp +++ b/src/kiwano/utils/ResourceCache.cpp @@ -214,7 +214,7 @@ bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String if (type == "gif") { // GIF image - GifImagePtr gif = new (autogc) GifImage; + GifImagePtr gif = MakePtr(); if (gif && gif->Load(gdata->path + file)) { return loader->AddObject(id, gif); @@ -224,7 +224,7 @@ bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String if (!file.empty()) { // Simple image - FramePtr frame = new (autogc) Frame; + FramePtr frame = MakePtr(); if (frame && frame->Load(gdata->path + file)) { return loader->AddObject(id, frame); @@ -246,13 +246,13 @@ bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String frames.reserve(files.size()); for (const auto& file : files) { - FramePtr frame = new (autogc) Frame; + FramePtr frame = MakePtr(); if (frame->Load(gdata->path + file)) { frames.push_back(frame); } } - FrameSequencePtr frame_seq = FrameSequence::Create(frames); + FrameSequencePtr frame_seq = MakePtr(frames); if (frame_seq) { return !!loader->AddObject(id, frame_seq); @@ -271,10 +271,10 @@ bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String if (rows || cols) { // Frame slices - FramePtr frame = new (autogc) Frame; + FramePtr frame = MakePtr(); if (frame && frame->Load(gdata->path + file)) { - FrameSequencePtr frame_seq = new (autogc) FrameSequence; + FrameSequencePtr frame_seq = MakePtr(); if (frame_seq) { frame_seq->AddFrames(frame, cols, rows, max_num, padding_x, padding_y); @@ -285,7 +285,7 @@ bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String else { // Simple image - FramePtr frame = new (autogc) Frame; + FramePtr frame = MakePtr(); if (frame && frame->Load(gdata->path + file)) { return loader->AddObject(id, frame); @@ -300,7 +300,7 @@ bool LoadFontsFromData(ResourceCache* loader, GlobalData* gdata, const String& i if (!gdata) return false; - FontPtr font = new (autogc) Font; + FontPtr font = MakePtr(); if (font && font->Load(gdata->path + file)) { return loader->AddObject(id, font); diff --git a/src/kiwano/utils/Task.cpp b/src/kiwano/utils/Task.cpp index 28393c3d..69b8c73c 100644 --- a/src/kiwano/utils/Task.cpp +++ b/src/kiwano/utils/Task.cpp @@ -22,37 +22,32 @@ namespace kiwano { -TaskPtr Task::Create(const Callback& cb, TickerPtr ticker) +Task::Task(const Callback& cb, TickerPtr ticker) + : running_(true) + , removeable_(false) + , callback_(cb) + , ticker_(ticker) { - TaskPtr ptr = new (autogc) Task; - if (ptr) - { - ptr->SetCallback(cb); - ptr->SetTicker(ticker); - } - return ptr; } -TaskPtr Task::Create(const String& name, const Callback& cb, TickerPtr ticker) +Task::Task(const String& name, const Callback& cb, TickerPtr ticker) + : Task(cb, ticker) { - TaskPtr ptr = Task::Create(cb, ticker); - if (ptr) - { - ptr->SetName(name); - } - return ptr; + SetName(name); } -TaskPtr Task::Create(const Callback& cb, Duration interval, int times) +Task::Task(const Callback& cb, Duration interval, int times) + : running_(true) + , removeable_(false) + , callback_(cb) { - TickerPtr ticker = Ticker::Create(interval, times); - return Task::Create(cb, ticker); + ticker_ = MakePtr(interval, times); } -TaskPtr Task::Create(const String& name, const Callback& cb, Duration interval, int times) +Task::Task(const String& name, const Callback& cb, Duration interval, int times) + : Task(cb, interval, times) { - TickerPtr ticker = Ticker::Create(interval, times); - return Task::Create(name, cb, ticker); + SetName(name); } Task::Task() diff --git a/src/kiwano/utils/Task.h b/src/kiwano/utils/Task.h index d9c4ca66..8ca4fd88 100644 --- a/src/kiwano/utils/Task.h +++ b/src/kiwano/utils/Task.h @@ -49,21 +49,21 @@ public: /// @brief 创建任务 /// @param cb 回调函数 /// @param 报时器 - static TaskPtr Create(const Callback& cb, TickerPtr ticker); + Task(const Callback& cb, TickerPtr ticker); /// \~chinese /// @brief 创建任务 /// @param name 名称 /// @param cb 回调函数 /// @param 报时器 - static TaskPtr Create(const String& name, const Callback& cb, TickerPtr ticker); + Task(const String& name, const Callback& cb, TickerPtr ticker); /// \~chinese /// @brief 创建任务 /// @param cb 回调函数 /// @param interval 时间间隔 /// @param times 执行次数(设 -1 为永久执行) - static TaskPtr Create(const Callback& cb, Duration interval, int times = -1); + Task(const Callback& cb, Duration interval, int times = -1); /// \~chinese /// @brief 创建任务 @@ -71,7 +71,7 @@ public: /// @param cb 回调函数 /// @param interval 时间间隔 /// @param times 执行次数(设 -1 为永久执行) - static TaskPtr Create(const String& name, const Callback& cb, Duration interval, int times = -1); + Task(const String& name, const Callback& cb, Duration interval, int times = -1); /// \~chinese /// @brief 构造空任务 diff --git a/src/kiwano/utils/TaskScheduler.cpp b/src/kiwano/utils/TaskScheduler.cpp index bf8ab4e3..dd6cbcb3 100644 --- a/src/kiwano/utils/TaskScheduler.cpp +++ b/src/kiwano/utils/TaskScheduler.cpp @@ -50,10 +50,33 @@ Task* TaskScheduler::AddTask(TaskPtr task) task->Reset(); tasks_.PushBack(task); } - return task.Get(); } +Task* TaskScheduler::AddTask(const Task::Callback& cb, TickerPtr ticker) +{ + auto task = MakePtr(cb, ticker); + return AddTask(task); +} + +Task* TaskScheduler::AddTask(const String& name, const Task::Callback& cb, TickerPtr ticker) +{ + auto task = MakePtr(name, cb, ticker); + return AddTask(task); +} + +Task* TaskScheduler::AddTask(const Task::Callback& cb, Duration interval, int times) +{ + auto task = MakePtr(cb, interval, times); + return AddTask(task); +} + +Task* TaskScheduler::AddTask(const String& name, const Task::Callback& cb, Duration interval, int times) +{ + auto task = MakePtr(name, cb, interval, times); + return AddTask(task); +} + void TaskScheduler::StopTasks(const String& name) { if (tasks_.IsEmpty()) diff --git a/src/kiwano/utils/TaskScheduler.h b/src/kiwano/utils/TaskScheduler.h index 65ee6b5a..ac091491 100644 --- a/src/kiwano/utils/TaskScheduler.h +++ b/src/kiwano/utils/TaskScheduler.h @@ -39,6 +39,34 @@ public: /// @brief 添加任务 Task* AddTask(TaskPtr task); + /// \~chinese + /// @brief 添加任务 + /// @param cb 回调函数 + /// @param 报时器 + Task* AddTask(const Task::Callback& cb, TickerPtr ticker); + + /// \~chinese + /// @brief 添加任务 + /// @param name 名称 + /// @param cb 回调函数 + /// @param 报时器 + Task* AddTask(const String& name, const Task::Callback& cb, TickerPtr ticker); + + /// \~chinese + /// @brief 添加任务 + /// @param cb 回调函数 + /// @param interval 时间间隔 + /// @param times 执行次数(设 -1 为永久执行) + Task* AddTask(const Task::Callback& cb, Duration interval, int times = -1); + + /// \~chinese + /// @brief 添加任务 + /// @param name 名称 + /// @param cb 回调函数 + /// @param interval 时间间隔 + /// @param times 执行次数(设 -1 为永久执行) + Task* AddTask(const String& name, const Task::Callback& cb, Duration interval, int times = -1); + /// \~chinese /// @brief 启动任务 void StartTasks(const String& task_name); diff --git a/src/kiwano/utils/Ticker.cpp b/src/kiwano/utils/Ticker.cpp index 88296be9..a91b9bd2 100644 --- a/src/kiwano/utils/Ticker.cpp +++ b/src/kiwano/utils/Ticker.cpp @@ -23,15 +23,11 @@ namespace kiwano { -TickerPtr Ticker::Create(Duration interval, int times) +Ticker::Ticker(Duration interval, int times) + : Ticker() { - TickerPtr ptr = new (autogc) Ticker; - if (ptr) - { - ptr->SetInterval(interval); - ptr->SetTotalTickCount(times); - } - return ptr; + SetInterval(interval); + SetTotalTickCount(times); } Ticker::Ticker() @@ -47,7 +43,7 @@ bool Ticker::Tick() return false; if (!timer_) - timer_ = Timer::Create(); + timer_ = MakePtr(); if (timer_->IsPausing()) return false; diff --git a/src/kiwano/utils/Ticker.h b/src/kiwano/utils/Ticker.h index 09d9df2b..888fa6a8 100644 --- a/src/kiwano/utils/Ticker.h +++ b/src/kiwano/utils/Ticker.h @@ -36,7 +36,7 @@ public: /// @brief 创建报时器 /// @param interval 报时间隔 /// @param tick_count 报时次数(设 -1 为永久) - static TickerPtr Create(Duration interval, int tick_count = -1); + Ticker(Duration interval, int tick_count = -1); Ticker(); diff --git a/src/kiwano/utils/Timer.cpp b/src/kiwano/utils/Timer.cpp index 70574c3a..a495f1d1 100644 --- a/src/kiwano/utils/Timer.cpp +++ b/src/kiwano/utils/Timer.cpp @@ -24,12 +24,6 @@ namespace kiwano { -TimerPtr Timer::Create() -{ - TimerPtr ptr = new (autogc) Timer; - return ptr; -} - Timer::Timer() : is_paused_(false) { diff --git a/src/kiwano/utils/Timer.h b/src/kiwano/utils/Timer.h index ab20f4dc..2caac649 100644 --- a/src/kiwano/utils/Timer.h +++ b/src/kiwano/utils/Timer.h @@ -33,10 +33,6 @@ class KGE_API Timer : public ObjectBase { public: - /// \~chinese - /// @brief 创建计时器 - static TimerPtr Create(); - Timer(); virtual ~Timer();