From 627f04d7cec72727be248f0120e09e8c06487113 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Sat, 3 Mar 2018 11:46:57 +0800 Subject: [PATCH] rename some protected functions --- core/Base/Game.cpp | 4 ++-- core/Common/Object.cpp | 12 ++++++------ core/Common/Scene.cpp | 2 +- core/Manager/ObjectManager.cpp | 10 +++++----- core/Manager/SceneManager.cpp | 8 ++++---- core/Manager/TimerManager.cpp | 19 ++++++++++++++---- core/eactions.h | 2 +- core/ecommon.h | 23 +++++++++++++--------- core/emanagers.h | 36 ++++++++++++++++++++-------------- core/enodes.h | 2 +- core/eshape.h | 2 +- core/etools.h | 4 ++-- core/etransitions.h | 2 +- 13 files changed, 74 insertions(+), 52 deletions(-) diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index 2089836a..0a1c5262 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -115,7 +115,7 @@ int e2d::Game::run() } else { - ObjectManager::__flush(); // 刷新内存池 + ObjectManager::__clearObjects(); // 刷新内存池 Time::__sleep(); // 挂起线程 } } @@ -164,7 +164,7 @@ void e2d::Game::uninit() // 清空图片缓存 Image::clearCache(); // 刷新内存池 - ObjectManager::__flush(); + ObjectManager::__clearObjects(); // 删除渲染相关资源 Renderer::__discardResources(); // 销毁窗口 diff --git a/core/Common/Object.cpp b/core/Common/Object.cpp index 8d7eeed7..5f36f1cb 100644 --- a/core/Common/Object.cpp +++ b/core/Common/Object.cpp @@ -1,27 +1,27 @@ #include "..\ebase.h" #include "..\emanagers.h" -e2d::Obj::Obj() +e2d::Object::Object() : m_nRefCount(0) , m_bManaged(false) { - ObjectManager::preload(this); // 将该对象放入释放池中 + ObjectManager::add(this); // 将该对象放入释放池中 } -e2d::Obj::~Obj() +e2d::Object::~Object() { } // 引用计数加一 -void e2d::Obj::retain() +void e2d::Object::retain() { m_nRefCount++; } // 引用计数减一 -void e2d::Obj::release() +void e2d::Object::release() { m_nRefCount--; // 通知对象管理池刷新 - ObjectManager::notifyFlush(); + ObjectManager::clear(); } diff --git a/core/Common/Scene.cpp b/core/Common/Scene.cpp index c99d4111..aea79382 100644 --- a/core/Common/Scene.cpp +++ b/core/Common/Scene.cpp @@ -51,7 +51,7 @@ void e2d::Scene::setAutoUpdate(bool bAutoUpdate) m_bAutoUpdate = bAutoUpdate; } -void e2d::Scene::preload(Node * child, int order /* = 0 */) +void e2d::Scene::add(Node * child, int order /* = 0 */) { m_pRoot->addChild(child, order); } diff --git a/core/Manager/ObjectManager.cpp b/core/Manager/ObjectManager.cpp index 1f70cee9..e38f2d96 100644 --- a/core/Manager/ObjectManager.cpp +++ b/core/Manager/ObjectManager.cpp @@ -9,17 +9,17 @@ // 让其自动释放 // 释放池容器 -static std::vector s_vPool; +static std::vector s_vPool; // 标志释放池执行状态 static bool s_bNotifyed = false; -void e2d::ObjectManager::__flush() +void e2d::ObjectManager::__clearObjects() { if (!s_bNotifyed) return; s_bNotifyed = false; // 创建迭代器 - static std::vector::iterator iter; + static std::vector::iterator iter; // 循环遍历容器中的所有对象 for (iter = s_vPool.begin(); iter != s_vPool.end();) { @@ -37,7 +37,7 @@ void e2d::ObjectManager::__flush() } } -void e2d::ObjectManager::preload(e2d::Obj * nptr) +void e2d::ObjectManager::add(e2d::Object * nptr) { if (!nptr->m_bManaged) { @@ -46,7 +46,7 @@ void e2d::ObjectManager::preload(e2d::Obj * nptr) } } -void e2d::ObjectManager::notifyFlush() +void e2d::ObjectManager::clear() { s_bNotifyed = true; } diff --git a/core/Manager/SceneManager.cpp b/core/Manager/SceneManager.cpp index 4185e16b..0cf09a17 100644 --- a/core/Manager/SceneManager.cpp +++ b/core/Manager/SceneManager.cpp @@ -8,7 +8,7 @@ static e2d::Scene * s_pNextScene = nullptr; static e2d::Transition * s_pTransition = nullptr; static std::stack s_SceneStack; -void e2d::SceneManager::enterScene(Scene * scene, Transition * transition /* = nullptr */, bool saveCurrentScene /* = true */) +void e2d::SceneManager::enter(Scene * scene, Transition * transition /* = nullptr */, bool saveCurrentScene /* = true */) { ASSERT(scene != nullptr, "Next scene NULL pointer exception!"); scene->retain(); @@ -33,7 +33,7 @@ void e2d::SceneManager::enterScene(Scene * scene, Transition * transition /* = n } } -void e2d::SceneManager::backScene(Transition * transition /* = nullptr */) +void e2d::SceneManager::back(Transition * transition /* = nullptr */) { // 栈为空时,调用返回场景函数失败 WARN_IF(s_SceneStack.size() == 0, "Scene stack now is empty!"); @@ -61,7 +61,7 @@ void e2d::SceneManager::backScene(Transition * transition /* = nullptr */) } } -void e2d::SceneManager::clearScene() +void e2d::SceneManager::clear() { // 清空场景栈 while (s_SceneStack.size()) @@ -168,5 +168,5 @@ void e2d::SceneManager::__uninit() SafeRelease(&s_pCurrentScene); SafeRelease(&s_pNextScene); SafeRelease(&s_pTransition); - SceneManager::clearScene(); + SceneManager::clear(); } diff --git a/core/Manager/TimerManager.cpp b/core/Manager/TimerManager.cpp index 1a8eb724..0372d202 100644 --- a/core/Manager/TimerManager.cpp +++ b/core/Manager/TimerManager.cpp @@ -19,12 +19,23 @@ void e2d::TimerManager::__update() } } -void e2d::TimerManager::preload(Timer * pTimer, Scene * pParentScene) +void e2d::TimerManager::add(double timeOut, TimerCallback callback) { - TimerManager::preload(pTimer, pParentScene->getRoot()); + auto pTimer = new Timer(callback, timeOut, 0, false); + TimerManager::add(pTimer, SceneManager::getCurrentScene()); } -void e2d::TimerManager::preload(Timer * pTimer, Node * pParentNode) +void e2d::TimerManager::add(Timer * pTimer, Scene * pParentScene) +{ + WARN_IF(pParentScene == nullptr, "Bind Timer with a NULL Scene pointer!"); + + if (pParentScene) + { + TimerManager::add(pTimer, pParentScene->getRoot()); + } +} + +void e2d::TimerManager::add(Timer * pTimer, Node * pParentNode) { WARN_IF(pTimer == nullptr, "Timer NULL pointer exception!"); WARN_IF(pParentNode == nullptr, "Bind Timer with a NULL Node pointer!"); @@ -32,7 +43,7 @@ void e2d::TimerManager::preload(Timer * pTimer, Node * pParentNode) if (pTimer && pParentNode) { ASSERT( - !pTimer->m_pParentNode, + pTimer->m_pParentNode != nullptr, "The timer is already binded, cannot be binded again!" ); diff --git a/core/eactions.h b/core/eactions.h index 05a37ba4..92f56321 100644 --- a/core/eactions.h +++ b/core/eactions.h @@ -13,7 +13,7 @@ class EActionTwoAtSameTime; class TransitionFade; class Action : - public Obj + public Object { friend ActionManager; friend ActionTwo; diff --git a/core/ecommon.h b/core/ecommon.h index 44bc738c..b84b1a93 100644 --- a/core/ecommon.h +++ b/core/ecommon.h @@ -388,14 +388,14 @@ public: class ObjectManager; // 基础对象 -class Obj +class Object { friend ObjectManager; public: - Obj(); + Object(); - virtual ~Obj(); + virtual ~Object(); // 引用计数加一 void retain(); @@ -412,7 +412,7 @@ private: class Text; class Font : - public Obj + public Object { friend Text; @@ -486,7 +486,7 @@ protected: // 图片 class Image : - public Obj + public Object { public: // 创建一个空的图片 @@ -583,7 +583,7 @@ class Action; // 场景 class Scene : - public Obj + public Object { friend SceneManager; @@ -619,7 +619,7 @@ public: ); // 添加节点到场景 - void preload( + void add( Node * child, int zOrder = 0 ); @@ -665,11 +665,16 @@ typedef VoidFunction TimerCallback; // 按钮点击回调函数 typedef VoidFunction ButtonCallback; +#ifndef CreateCallback + #define CreateCallback(Func) std::bind(&Func, this) +#endif + + template inline void SafeDelete(T** p) { if (*p) { delete *p; *p = nullptr; } } -template -inline void SafeRelease(Obj** p) { if (*p) { (*p)->release(); *p = nullptr; } } +template +inline void SafeRelease(Object** p) { if (*p) { (*p)->release(); *p = nullptr; } } template inline void SafeReleaseInterface(Interface **pp) { if (*pp != nullptr) { (*pp)->Release(); (*pp) = nullptr; } } diff --git a/core/emanagers.h b/core/emanagers.h index df5ebd79..ad02ca83 100644 --- a/core/emanagers.h +++ b/core/emanagers.h @@ -7,7 +7,7 @@ namespace e2d class Game; class Renderer; -class Obj; +class Object; class Scene; class Node; class Timer; @@ -22,17 +22,17 @@ class ObjectManager friend Game; public: - // 将一个节点放入内存池 - static void preload( - e2d::Obj * nptr + // 将一个对象放入内存池 + static void add( + e2d::Object * nptr ); - // 通知内存池刷新 - static void notifyFlush(); + // 释放垃圾对象的内存空间 + static void clear(); private: - // 刷新内存池 - static void __flush(); + // 释放引用计数为 0 的对象 + static void __clearObjects(); }; @@ -44,19 +44,19 @@ class SceneManager public: // 切换场景 - static void enterScene( + static void enter( Scene * scene, /* 下一个场景的指针 */ Transition * transition = nullptr, /* 场景切换动画 */ bool saveCurrentScene = true /* 是否保存当前场景 */ ); // 返回上一场景 - static void backScene( + static void back( Transition * transition = nullptr /* 场景切换动画 */ ); // 清空保存的所有场景 - static void clearScene(); + static void clear(); // 获取当前场景 static Scene * getCurrentScene(); @@ -86,18 +86,24 @@ class TimerManager friend Node; public: - // 绑定定时器到场景 - static void preload( + // 添加一个定时器,并将它绑定到场景 + static void add( Timer * pTimer, Scene * pParentScene ); - // 绑定定时器到节点 - static void preload( + // 添加一个定时器,并将它绑定到节点 + static void add( Timer * pTimer, Node * pParentNode ); + // 等待一段时间后执行指定函数 + static void add( + double timeOut, /* 等待的时长(秒) */ + TimerCallback callback /* 执行的函数 */ + ); + // 启动具有相同名称的定时器 static void startTimers( const String &name diff --git a/core/enodes.h b/core/enodes.h index de605740..66964c73 100644 --- a/core/enodes.h +++ b/core/enodes.h @@ -10,7 +10,7 @@ class Shape; class Transition; class Node : - public Obj + public Object { friend Scene; friend Shape; diff --git a/core/eshape.h b/core/eshape.h index 743768b3..03378239 100644 --- a/core/eshape.h +++ b/core/eshape.h @@ -10,7 +10,7 @@ class Node; class Shape : - public Obj + public Object { friend ShapeManager; friend Node; diff --git a/core/etools.h b/core/etools.h index 4623f718..fd7c8c57 100644 --- a/core/etools.h +++ b/core/etools.h @@ -42,7 +42,7 @@ public: // 定时器 class Timer : - public Obj + public Object { friend TimerManager; @@ -210,7 +210,7 @@ public: // 音乐播放器 class Music : - public Obj + public Object { friend MusicManager; diff --git a/core/etransitions.h b/core/etransitions.h index c27a26fc..082f9e67 100644 --- a/core/etransitions.h +++ b/core/etransitions.h @@ -8,7 +8,7 @@ namespace e2d class SceneManager; class Transition : - public Obj + public Object { friend SceneManager;