GC模式重做

This commit is contained in:
Nomango 2018-07-06 00:47:50 +08:00
parent 402e8a7f22
commit fb389cce89
38 changed files with 139 additions and 275 deletions

View File

@ -27,7 +27,7 @@ void e2d::Animate::setAnimation(Animation * animation)
{ {
if (animation && animation != _animation) if (animation && animation != _animation)
{ {
GC::release(_animation); GC::safeRelease(_animation);
_animation = animation; _animation = animation;
_animation->retain(); _animation->retain();
} }
@ -91,14 +91,14 @@ void e2d::Animate::reset()
void e2d::Animate::onDestroy() void e2d::Animate::onDestroy()
{ {
Action::onDestroy(); Action::onDestroy();
GC::release(_animation); GC::safeRelease(_animation);
} }
e2d::Animate * e2d::Animate::clone() const e2d::Animate * e2d::Animate::clone() const
{ {
if (_animation) if (_animation)
{ {
return Create<Animate>(_animation); return new (std::nothrow) Animate(_animation);
} }
return nullptr; return nullptr;
} }
@ -110,7 +110,7 @@ e2d::Animate * e2d::Animate::reverse() const
auto animation = _animation->reverse(); auto animation = _animation->reverse();
if (animation) if (animation)
{ {
return Create<Animate>(animation); return new (std::nothrow) Animate(animation);
} }
} }
return nullptr; return nullptr;

View File

@ -35,7 +35,7 @@ void e2d::Animation::onDestroy()
{ {
for (auto frame : _frames) for (auto frame : _frames)
{ {
GC::release(frame); GC::safeRelease(frame);
} }
} }
@ -68,7 +68,7 @@ const std::vector<e2d::Image*>& e2d::Animation::getFrames() const
e2d::Animation * e2d::Animation::clone() const e2d::Animation * e2d::Animation::clone() const
{ {
auto animation = Create<Animation>(_interval); auto animation = new (std::nothrow) Animation(_interval);
if (animation) if (animation)
{ {
for (auto frame : _frames) for (auto frame : _frames)
@ -99,5 +99,5 @@ e2d::Animation * e2d::Animation::reverse() const
} }
} }
return Create<Animation>(this->getInterval(), frames); return new (std::nothrow) Animation(this->getInterval(), frames);
} }

View File

@ -7,12 +7,12 @@ e2d::CallFunc::CallFunc(const Function& func) :
e2d::CallFunc * e2d::CallFunc::clone() const e2d::CallFunc * e2d::CallFunc::clone() const
{ {
return Create<CallFunc>(_func); return new (std::nothrow) CallFunc(_func);
} }
e2d::CallFunc * e2d::CallFunc::reverse() const e2d::CallFunc * e2d::CallFunc::reverse() const
{ {
return Create<CallFunc>(_func); return new (std::nothrow) CallFunc(_func);
} }
void e2d::CallFunc::_init() void e2d::CallFunc::_init()

View File

@ -8,12 +8,12 @@ e2d::Delay::Delay(double duration)
e2d::Delay * e2d::Delay::clone() const e2d::Delay * e2d::Delay::clone() const
{ {
return Create<Delay>(_delay); return new (std::nothrow) Delay(_delay);
} }
e2d::Delay * e2d::Delay::reverse() const e2d::Delay * e2d::Delay::reverse() const
{ {
return Create<Delay>(_delay); return new (std::nothrow) Delay(_delay);
} }
void e2d::Delay::reset() void e2d::Delay::reset()

View File

@ -11,12 +11,12 @@ e2d::JumpBy::JumpBy(double duration, const Vector2 & vec, double height, int jum
e2d::JumpBy * e2d::JumpBy::clone() const e2d::JumpBy * e2d::JumpBy::clone() const
{ {
return Create<JumpBy>(_duration, _deltaPos, _height, _jumps); return new (std::nothrow) JumpBy(_duration, _deltaPos, _height, _jumps);
} }
e2d::JumpBy * e2d::JumpBy::reverse() const e2d::JumpBy * e2d::JumpBy::reverse() const
{ {
return Create<JumpBy>(_duration, -_deltaPos, _height, _jumps); return new (std::nothrow) JumpBy(_duration, -_deltaPos, _height, _jumps);
} }
void e2d::JumpBy::_init() void e2d::JumpBy::_init()

View File

@ -9,7 +9,7 @@ e2d::JumpTo::JumpTo(double duration, const Point & pos, double height, int jumps
e2d::JumpTo * e2d::JumpTo::clone() const e2d::JumpTo * e2d::JumpTo::clone() const
{ {
return Create<JumpTo>(_duration, _endPos, _height, _jumps); return new (std::nothrow) JumpTo(_duration, _endPos, _height, _jumps);
} }
void e2d::JumpTo::_init() void e2d::JumpTo::_init()

View File

@ -23,7 +23,7 @@ e2d::Loop * e2d::Loop::clone() const
{ {
if (_action) if (_action)
{ {
return Create<Loop>(_action->clone()); return new (std::nothrow) Loop(_action->clone());
} }
else else
{ {
@ -35,7 +35,7 @@ e2d::Loop * e2d::Loop::reverse() const
{ {
if (_action) if (_action)
{ {
return Create<Loop>(_action->clone()); return new (std::nothrow) Loop(_action->clone());
} }
else else
{ {
@ -93,7 +93,7 @@ void e2d::Loop::reset()
void e2d::Loop::onDestroy() void e2d::Loop::onDestroy()
{ {
Action::onDestroy(); Action::onDestroy();
GC::release(_action); GC::safeRelease(_action);
} }
void e2d::Loop::_resetTime() void e2d::Loop::_resetTime()

View File

@ -37,10 +37,10 @@ void e2d::MoveBy::_update()
e2d::MoveBy * e2d::MoveBy::clone() const e2d::MoveBy * e2d::MoveBy::clone() const
{ {
return Create<MoveBy>(_duration, _deltaPos); return new (std::nothrow) MoveBy(_duration, _deltaPos);
} }
e2d::MoveBy * e2d::MoveBy::reverse() const e2d::MoveBy * e2d::MoveBy::reverse() const
{ {
return Create<MoveBy>(_duration, -_deltaPos); return new (std::nothrow) MoveBy(_duration, -_deltaPos);
} }

View File

@ -9,7 +9,7 @@ e2d::MoveTo::MoveTo(double duration, Point pos)
e2d::MoveTo * e2d::MoveTo::clone() const e2d::MoveTo * e2d::MoveTo::clone() const
{ {
return Create<MoveTo>(_duration, _endPos); return new (std::nothrow) MoveTo(_duration, _endPos);
} }
void e2d::MoveTo::_init() void e2d::MoveTo::_init()

View File

@ -30,10 +30,10 @@ void e2d::OpacityBy::_update()
e2d::OpacityBy * e2d::OpacityBy::clone() const e2d::OpacityBy * e2d::OpacityBy::clone() const
{ {
return Create<OpacityBy>(_duration, _deltaVal); return new (std::nothrow) OpacityBy(_duration, _deltaVal);
} }
e2d::OpacityBy * e2d::OpacityBy::reverse() const e2d::OpacityBy * e2d::OpacityBy::reverse() const
{ {
return Create<OpacityBy>(_duration, -_deltaVal); return new (std::nothrow) OpacityBy(_duration, -_deltaVal);
} }

View File

@ -10,7 +10,7 @@ e2d::OpacityTo::OpacityTo(double duration, double opacity)
e2d::OpacityTo * e2d::OpacityTo::clone() const e2d::OpacityTo * e2d::OpacityTo::clone() const
{ {
return Create<OpacityTo>(_duration, _endVal); return new (std::nothrow) OpacityTo(_duration, _endVal);
} }
void e2d::OpacityTo::_init() void e2d::OpacityTo::_init()

View File

@ -30,10 +30,10 @@ void e2d::RotateBy::_update()
e2d::RotateBy * e2d::RotateBy::clone() const e2d::RotateBy * e2d::RotateBy::clone() const
{ {
return Create<RotateBy>(_duration, _deltaVal); return new (std::nothrow) RotateBy(_duration, _deltaVal);
} }
e2d::RotateBy * e2d::RotateBy::reverse() const e2d::RotateBy * e2d::RotateBy::reverse() const
{ {
return Create<RotateBy>(_duration, -_deltaVal); return new (std::nothrow) RotateBy(_duration, -_deltaVal);
} }

View File

@ -10,7 +10,7 @@ e2d::RotateTo::RotateTo(double duration, double rotation)
e2d::RotateTo * e2d::RotateTo::clone() const e2d::RotateTo * e2d::RotateTo::clone() const
{ {
return Create<RotateTo>(_duration, _endVal); return new (std::nothrow) RotateTo(_duration, _endVal);
} }
void e2d::RotateTo::_init() void e2d::RotateTo::_init()

View File

@ -39,10 +39,10 @@ void e2d::ScaleBy::_update()
e2d::ScaleBy * e2d::ScaleBy::clone() const e2d::ScaleBy * e2d::ScaleBy::clone() const
{ {
return Create<ScaleBy>(_duration, _deltaX, _deltaY); return new (std::nothrow) ScaleBy(_duration, _deltaX, _deltaY);
} }
e2d::ScaleBy * e2d::ScaleBy::reverse() const e2d::ScaleBy * e2d::ScaleBy::reverse() const
{ {
return Create<ScaleBy>(_duration, -_deltaX, -_deltaY); return new (std::nothrow) ScaleBy(_duration, -_deltaX, -_deltaY);
} }

View File

@ -17,7 +17,7 @@ e2d::ScaleTo::ScaleTo(double duration, double scaleX, double scaleY)
e2d::ScaleTo * e2d::ScaleTo::clone() const e2d::ScaleTo * e2d::ScaleTo::clone() const
{ {
return Create<ScaleTo>(_duration, _endScaleX, _endScaleY); return new (std::nothrow) ScaleTo(_duration, _endScaleX, _endScaleY);
} }
void e2d::ScaleTo::_init() void e2d::ScaleTo::_init()

View File

@ -35,7 +35,7 @@ void e2d::Sequence::onDestroy()
Action::onDestroy(); Action::onDestroy();
for (auto action : _actions) for (auto action : _actions)
{ {
GC::release(action); GC::safeRelease(action);
} }
} }
@ -97,7 +97,7 @@ void e2d::Sequence::add(const std::vector<Action*>& actions)
e2d::Sequence * e2d::Sequence::clone() const e2d::Sequence * e2d::Sequence::clone() const
{ {
auto sequence = Create<Sequence>(); auto sequence = new (std::nothrow) Sequence();
for (const auto& action : _actions) for (const auto& action : _actions)
{ {
if (action) if (action)
@ -110,7 +110,7 @@ e2d::Sequence * e2d::Sequence::clone() const
e2d::Sequence * e2d::Sequence::reverse() const e2d::Sequence * e2d::Sequence::reverse() const
{ {
auto sequence = Create<Sequence>(); auto sequence = new (std::nothrow) Sequence();
if (sequence && !_actions.empty()) if (sequence && !_actions.empty())
{ {
std::vector<Action*> newActions(_actions.size()); std::vector<Action*> newActions(_actions.size());

View File

@ -32,7 +32,7 @@ void e2d::Spawn::onDestroy()
Action::onDestroy(); Action::onDestroy();
for (auto action : _actions) for (auto action : _actions)
{ {
GC::release(action); GC::safeRelease(action);
} }
} }
@ -95,7 +95,7 @@ void e2d::Spawn::add(const std::vector<Action*>& actions)
e2d::Spawn * e2d::Spawn::clone() const e2d::Spawn * e2d::Spawn::clone() const
{ {
auto spawn = Create<Spawn>(); auto spawn = new (std::nothrow) Spawn();
for (const auto& action : _actions) for (const auto& action : _actions)
{ {
if (action) if (action)
@ -108,7 +108,7 @@ e2d::Spawn * e2d::Spawn::clone() const
e2d::Spawn * e2d::Spawn::reverse() const e2d::Spawn * e2d::Spawn::reverse() const
{ {
auto spawn = Create<Spawn>(); auto spawn = new (std::nothrow) Spawn();
if (spawn && !_actions.empty()) if (spawn && !_actions.empty())
{ {
std::vector<Action*> newActions(_actions.size()); std::vector<Action*> newActions(_actions.size());

View File

@ -5,6 +5,12 @@
// GC 机制,用于自动销毁单例 // GC 机制,用于自动销毁单例
e2d::GC e2d::GC::_instance; e2d::GC e2d::GC::_instance;
e2d::GC::GC()
: _notifyed(false)
, _pool()
{
}
e2d::GC::~GC() e2d::GC::~GC()
{ {
// 删除所有单例 // 删除所有单例
@ -27,7 +33,8 @@ e2d::GC::~GC()
// 调用 retain 函数保证该对象不被删除,并在不再使用时调用 release 函数 // 调用 retain 函数保证该对象不被删除,并在不再使用时调用 release 函数
void e2d::GC::update() void e2d::GC::update()
{ {
if (!_notifyed) return; if (!_notifyed)
return;
_notifyed = false; _notifyed = false;
for (auto iter = _pool.begin(); iter != _pool.end();) for (auto iter = _pool.begin(); iter != _pool.end();)

View File

@ -9,15 +9,13 @@ e2d::Game * e2d::Game::_instance = nullptr;
e2d::Game::Game() e2d::Game::Game()
: _ended(false) : _ended(false)
, _paused(false) , _paused(false)
, _config(nullptr) , _config()
{ {
CoInitialize(nullptr); CoInitialize(nullptr);
} }
e2d::Game::~Game() e2d::Game::~Game()
{ {
GC::release(_config);
CoUninitialize(); CoUninitialize();
} }
@ -69,9 +67,9 @@ void e2d::Game::start(bool cleanup)
if (Time::__isReady()) if (Time::__isReady())
{ {
// 更新配置 // 更新配置
if (_config && _config->_unconfigured) if (_config._unconfigured)
{ {
_config->_update(); _config._update();
} }
input->update(); // 获取用户输入 input->update(); // 获取用户输入
@ -79,13 +77,13 @@ void e2d::Game::start(bool cleanup)
actionManager->update(); // 更新动作管理器 actionManager->update(); // 更新动作管理器
sceneManager->update(); // 更新场景内容 sceneManager->update(); // 更新场景内容
renderer->render(); // 渲染游戏画面 renderer->render(); // 渲染游戏画面
gc->update(); // 刷新内存池
Time::__updateLast(); // 刷新时间信息 Time::__updateLast(); // 刷新时间信息
} }
else else
{ {
Time::__sleep(); // 挂起线程 Time::__sleep(); // 挂起线程
gc->update(); // ˢĐÂÄÚ´ćłŘ
} }
} }
@ -118,24 +116,14 @@ bool e2d::Game::isPaused()
return _paused; return _paused;
} }
void e2d::Game::setConfig(Config * config) void e2d::Game::setConfig(const Config& config)
{ {
if (_config != config && config)
{
GC::release(_config);
_config = config; _config = config;
_config->_unconfigured = true; _config._unconfigured = true;
GC::retain(_config);
}
} }
e2d::Config * e2d::Game::getConfig() e2d::Config e2d::Game::getConfig() const
{ {
if (!_config)
{
_config = Create<Config>();
GC::retain(_config);
}
return _config; return _config;
} }

View File

@ -236,8 +236,9 @@ double e2d::Input::getMouseDeltaZ()
e2d::Listener * e2d::Input::addListener(const Function& func, const String& name, bool paused) e2d::Listener * e2d::Input::addListener(const Function& func, const String& name, bool paused)
{ {
auto listener = Create<Listener>(func, name, paused); auto listener = new (std::nothrow) Listener(func, name, paused);
GC::retain(listener); listener->autorelease();
listener->retain();
s_vListeners.push_back(listener); s_vListeners.push_back(listener);
return listener; return listener;
} }
@ -249,7 +250,7 @@ void e2d::Input::addListener(Listener * listener)
auto iter = std::find(s_vListeners.begin(), s_vListeners.end(), listener); auto iter = std::find(s_vListeners.begin(), s_vListeners.end(), listener);
if (iter == s_vListeners.end()) if (iter == s_vListeners.end())
{ {
GC::retain(listener); listener->retain();
s_vListeners.push_back(listener); s_vListeners.push_back(listener);
} }
} }
@ -262,7 +263,7 @@ void e2d::Input::removeListener(Listener * listener)
auto iter = std::find(s_vListeners.begin(), s_vListeners.end(), listener); auto iter = std::find(s_vListeners.begin(), s_vListeners.end(), listener);
if (iter != s_vListeners.end()) if (iter != s_vListeners.end())
{ {
GC::release(listener); GC::safeRelease(listener);
s_vListeners.erase(iter); s_vListeners.erase(iter);
} }
} }
@ -354,7 +355,7 @@ void e2d::Input::__updateListeners()
// Çå³ýÒÑÍ£Ö¹µÄ¼àÌýÆ÷ // Çå³ýÒÑÍ£Ö¹µÄ¼àÌýÆ÷
if (listener->_stopped) if (listener->_stopped)
{ {
GC::release(listener); GC::safeRelease(listener);
s_vListeners.erase(s_vListeners.begin() + i); s_vListeners.erase(s_vListeners.begin() + i);
} }
else else

View File

@ -104,7 +104,7 @@ void e2d::Collision::__update(Node * active, Node * passive)
// Çå³ýÒÑÍ£Ö¹µÄ¼àÌýÆ÷ // Çå³ýÒÑÍ£Ö¹µÄ¼àÌýÆ÷
if (listener->_stopped) if (listener->_stopped)
{ {
GC::release(listener); GC::safeRelease(listener);
s_vListeners.erase(s_vListeners.begin() + i); s_vListeners.erase(s_vListeners.begin() + i);
} }
else else
@ -121,8 +121,9 @@ void e2d::Collision::__update(Node * active, Node * passive)
e2d::Listener * e2d::Collision::addListener(const Function& func, const String& name, bool paused) e2d::Listener * e2d::Collision::addListener(const Function& func, const String& name, bool paused)
{ {
auto listener = Create<Listener>(func, name, paused); auto listener = new (std::nothrow) Listener(func, name, paused);
GC::retain(listener); listener->autorelease();
listener->retain();
s_vListeners.push_back(listener); s_vListeners.push_back(listener);
return listener; return listener;
} }
@ -134,7 +135,7 @@ void e2d::Collision::addListener(Listener * listener)
auto iter = std::find(s_vListeners.begin(), s_vListeners.end(), listener); auto iter = std::find(s_vListeners.begin(), s_vListeners.end(), listener);
if (iter == s_vListeners.end()) if (iter == s_vListeners.end())
{ {
GC::retain(listener); listener->retain();
s_vListeners.push_back(listener); s_vListeners.push_back(listener);
} }
} }
@ -147,7 +148,7 @@ void e2d::Collision::removeListener(Listener * listener)
auto iter = std::find(s_vListeners.begin(), s_vListeners.end(), listener); auto iter = std::find(s_vListeners.begin(), s_vListeners.end(), listener);
if (iter != s_vListeners.end()) if (iter != s_vListeners.end())
{ {
GC::release(listener); GC::safeRelease(listener);
s_vListeners.erase(iter); s_vListeners.erase(iter);
} }
} }

View File

@ -7,6 +7,7 @@ e2d::Config::Config()
, _soundEnabled(true) , _soundEnabled(true)
, _collisionEnabled(false) , _collisionEnabled(false)
, _colliderVisiable(false) , _colliderVisiable(false)
, _objectsAutoRelease(false)
, _nodeDefColliderType(Collider::Type::None) , _nodeDefColliderType(Collider::Type::None)
, _unconfigured(true) , _unconfigured(true)
{ {
@ -30,6 +31,11 @@ void e2d::Config::setSoundEnabled(bool enabled)
} }
} }
void e2d::Config::setObjectsAutoReleaseEnabled(bool enabled)
{
_objectsAutoRelease = enabled;
}
void e2d::Config::setCollisionEnabled(bool enabled) void e2d::Config::setCollisionEnabled(bool enabled)
{ {
_collisionEnabled = enabled; _collisionEnabled = enabled;
@ -63,6 +69,11 @@ bool e2d::Config::isSoundEnabled() const
return _soundEnabled; return _soundEnabled;
} }
bool e2d::Config::isObjectsAutoReleaseEnabled() const
{
return _objectsAutoRelease;
}
bool e2d::Config::isCollisionEnabled() const bool e2d::Config::isCollisionEnabled() const
{ {
return _collisionEnabled; return _collisionEnabled;

View File

@ -5,6 +5,10 @@
e2d::Object::Object() e2d::Object::Object()
: _refCount(0) : _refCount(0)
{ {
if (Game::getInstance()->getConfig().isObjectsAutoReleaseEnabled())
{
this->autorelease();
}
} }
e2d::Object::~Object() e2d::Object::~Object()

View File

@ -6,17 +6,11 @@ e2d::Scene::Scene()
: _autoUpdate(true) : _autoUpdate(true)
, _root(nullptr) , _root(nullptr)
{ {
_root = Create<Node>(); _root = new (std::nothrow) Node();
if (_root) _root->autorelease();
{
_root->retain(); _root->retain();
_root->_setParentScene(this); _root->_setParentScene(this);
} }
else
{
throw Exception(L"场景根节点构造失败");
}
}
e2d::Scene::~Scene() e2d::Scene::~Scene()
{ {
@ -26,7 +20,7 @@ void e2d::Scene::_render()
{ {
_root->_render(); _root->_render();
if (Game::getInstance()->getConfig()->isColliderVisiable()) if (Game::getInstance()->getConfig().isColliderVisiable())
{ {
// 恢复矩阵转换 // 恢复矩阵转换
Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity()); Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
@ -91,5 +85,5 @@ e2d::Node * e2d::Scene::getRoot() const
void e2d::Scene::onDestroy() void e2d::Scene::onDestroy()
{ {
GC::release(_root); GC::safeRelease(_root);
} }

View File

@ -200,7 +200,7 @@ void e2d::ActionManager::clearAllBindedWith(Node * target)
auto a = _runningActions[i]; auto a = _runningActions[i];
if (a->getTarget() == target) if (a->getTarget() == target)
{ {
GC::release(a); GC::safeRelease(a);
_runningActions.erase(_runningActions.begin() + i); _runningActions.erase(_runningActions.begin() + i);
} }
else else

View File

@ -43,7 +43,7 @@ void e2d::ColliderManager::clearAll()
void e2d::ColliderManager::__updateCollider(e2d::Collider * pActiveCollider) void e2d::ColliderManager::__updateCollider(e2d::Collider * pActiveCollider)
{ {
// 判断碰撞监听是否打开 // 判断碰撞监听是否打开
if (!Game::getInstance()->getConfig()->isCollisionEnabled()) if (!Game::getInstance()->getConfig().isCollisionEnabled())
return; return;
Node* pActiveNode = pActiveCollider->_parentNode; Node* pActiveNode = pActiveCollider->_parentNode;
@ -100,7 +100,7 @@ void e2d::ColliderManager::__remove(Collider * pCollider)
{ {
if (_colliders[i] == pCollider) if (_colliders[i] == pCollider)
{ {
GC::release(pCollider); GC::safeRelease(pCollider);
_colliders.erase(_colliders.begin() + i); _colliders.erase(_colliders.begin() + i);
return; return;
} }

View File

@ -95,7 +95,7 @@ void e2d::SceneManager::clear()
while (_scenes.size()) while (_scenes.size())
{ {
auto temp = _scenes.top(); auto temp = _scenes.top();
GC::release(temp); GC::safeRelease(temp);
_scenes.pop(); _scenes.pop();
} }
} }

View File

@ -32,13 +32,13 @@ e2d::Node::Node()
, _positionFixed(false) , _positionFixed(false)
, _colliderType(Collider::Type::None) , _colliderType(Collider::Type::None)
{ {
auto config = Game::getInstance()->getConfig(); auto& config = Game::getInstance()->getConfig();
// 设置默认中心点位置 // 设置默认中心点位置
Point defPivot = config->getNodeDefaultPivot(); Point defPivot = config.getNodeDefaultPivot();
this->_pivotX = float(defPivot.x); this->_pivotX = float(defPivot.x);
this->_pivotY = float(defPivot.y); this->_pivotY = float(defPivot.y);
// 设置默认碰撞体类型 // 设置默认碰撞体类型
this->setColliderType(config->getDefaultColliderType()); this->setColliderType(config.getDefaultColliderType());
} }
e2d::Node::~Node() e2d::Node::~Node()
@ -573,11 +573,12 @@ void e2d::Node::setColliderType(Collider::Type type)
case Collider::Type::Circle: case Collider::Type::Circle:
case Collider::Type::Ellipse: case Collider::Type::Ellipse:
{ {
this->_collider = Create<Collider>(); _collider = new (std::nothrow) Collider();
this->_collider->_parentNode = this; _collider->autorelease();
this->_collider->_recreate(type); _collider->_parentNode = this;
_collider->_recreate(type);
// 添加新的碰撞体 // 添加新的碰撞体
ColliderManager::getInstance()->__add(this->_collider); ColliderManager::getInstance()->__add(_collider);
} }
break; break;
@ -914,7 +915,7 @@ void e2d::Node::onDestroy()
ColliderManager::getInstance()->__remove(_collider); ColliderManager::getInstance()->__remove(_collider);
for (auto child : _children) for (auto child : _children)
{ {
GC::release(child); GC::safeRelease(child);
} }
} }

View File

@ -46,7 +46,7 @@ bool e2d::Sprite::open(Image * image)
{ {
if (image) if (image)
{ {
GC::release(_image); GC::safeRelease(_image);
_image = image; _image = image;
_image->retain(); _image->retain();
@ -60,7 +60,8 @@ bool e2d::Sprite::open(const String& filePath)
{ {
if (!_image) if (!_image)
{ {
_image = Create<Image>(); _image = new (std::nothrow) Image();
_image->autorelease();
_image->retain(); _image->retain();
} }
@ -76,7 +77,8 @@ bool e2d::Sprite::open(int resNameId, const String& resType)
{ {
if (!_image) if (!_image)
{ {
_image = Create<Image>(); _image = new (std::nothrow) Image();
_image->autorelease();
_image->retain(); _image->retain();
} }
@ -128,5 +130,5 @@ void e2d::Sprite::onRender()
void e2d::Sprite::onDestroy() void e2d::Sprite::onDestroy()
{ {
Node::onDestroy(); Node::onDestroy();
GC::release(_image); GC::safeRelease(_image);
} }

View File

@ -33,7 +33,7 @@ e2d::String e2d::Path::getDataPath()
{ {
// 设置数据的保存路径 // 设置数据的保存路径
String localAppDataPath = Path::getLocalAppDataPath(); String localAppDataPath = Path::getLocalAppDataPath();
String gameName = Game::getInstance()->getConfig()->getGameName(); String gameName = Game::getInstance()->getConfig().getGameName();
if (!localAppDataPath.isEmpty() && !gameName.isEmpty()) if (!localAppDataPath.isEmpty() && !gameName.isEmpty())
{ {
_dataPath = localAppDataPath + L"\\Easy2DGameData\\" << gameName << L"\\"; _dataPath = localAppDataPath + L"\\Easy2DGameData\\" << gameName << L"\\";
@ -54,7 +54,7 @@ e2d::String e2d::Path::getTempPath()
{ {
// 设置临时文件保存路径 // 设置临时文件保存路径
wchar_t path[_MAX_PATH]; wchar_t path[_MAX_PATH];
String gameName = Game::getInstance()->getConfig()->getGameName(); String gameName = Game::getInstance()->getConfig().getGameName();
if (0 != ::GetTempPath(_MAX_PATH, path) && !gameName.isEmpty()) if (0 != ::GetTempPath(_MAX_PATH, path) && !gameName.isEmpty())
{ {

View File

@ -14,11 +14,11 @@ e2d::Player::Player()
e2d::Player::~Player() e2d::Player::~Player()
{ {
for (auto pair : _fileList) for (auto pair : _fileList)
delete pair.second; pair.second->release();
_fileList.clear(); _fileList.clear();
for (auto pair : _resList) for (auto pair : _resList)
delete pair.second; pair.second->release();
_resList.clear(); _resList.clear();
if (_masteringVoice) if (_masteringVoice)
@ -70,18 +70,15 @@ bool e2d::Player::preload(const String& filePath)
else else
{ {
Music * music = new (std::nothrow) Music(); Music * music = new (std::nothrow) Music();
music->autorelease();
if (music->open(filePath)) if (music->open(filePath))
{ {
music->retain();
music->setVolume(_volume); music->setVolume(_volume);
_fileList.insert(std::pair<UINT, Music *>(hash, music)); _fileList.insert(std::pair<UINT, Music *>(hash, music));
return true; return true;
} }
else
{
delete music;
music = nullptr;
}
} }
return false; return false;
} }
@ -95,18 +92,15 @@ bool e2d::Player::preload(int resNameId, const String& resType)
else else
{ {
Music * music = new (std::nothrow) Music(); Music * music = new (std::nothrow) Music();
music->autorelease();
if (music->open(resNameId, resType)) if (music->open(resNameId, resType))
{ {
music->retain();
music->setVolume(_volume); music->setVolume(_volume);
_resList.insert(std::pair<UINT, Music *>(resNameId, music)); _resList.insert(std::pair<UINT, Music *>(resNameId, music));
return true; return true;
} }
else
{
delete music;
music = nullptr;
}
} }
return false; return false;
} }
@ -271,13 +265,13 @@ void e2d::Player::clearCache()
{ {
for (auto pair : _fileList) for (auto pair : _fileList)
{ {
delete pair.second; pair.second->release();
} }
_fileList.clear(); _fileList.clear();
for (auto pair : _resList) for (auto pair : _resList)
{ {
delete pair.second; pair.second->release();
} }
_resList.clear(); _resList.clear();
} }

View File

@ -29,8 +29,8 @@ bool e2d::Transition::isDone()
void e2d::Transition::onDestroy() void e2d::Transition::onDestroy()
{ {
GC::release(_outScene); GC::safeRelease(_outScene);
GC::release(_inScene); GC::safeRelease(_inScene);
} }
void e2d::Transition::_init(Scene * prev, Scene * next) void e2d::Transition::_init(Scene * prev, Scene * next)

View File

@ -42,11 +42,11 @@ public:
// 修改游戏配置 // 修改游戏配置
void setConfig( void setConfig(
Config * config const Config& config
); );
// 获取游戏配置 // 获取游戏配置
Config * getConfig(); Config getConfig() const;
private: private:
Game(); Game();
@ -58,7 +58,7 @@ private:
private: private:
bool _ended; bool _ended;
bool _paused; bool _paused;
Config* _config; Config _config;
static Game * _instance; static Game * _instance;
}; };
@ -521,19 +521,9 @@ public:
// 获取 GC 实例 // 获取 GC 实例
static GC* getInstance(); static GC* getInstance();
// 保留对象
template <typename Type>
static inline void retain(Type*& p)
{
if (p != nullptr)
{
p->retain();
}
}
// 释放对象 // 释放对象
template <typename Type> template <typename Type>
static inline void release(Type*& p) static inline void safeRelease(Type*& p)
{ {
if (p != nullptr) if (p != nullptr)
{ {
@ -557,7 +547,7 @@ public:
void clear(); void clear();
private: private:
GC() {} GC();
~GC(); ~GC();

View File

@ -743,8 +743,7 @@ protected:
class Game; class Game;
// 游戏配置 // 游戏配置
class Config : class Config
public Object
{ {
friend class Game; friend class Game;
@ -765,6 +764,12 @@ public:
bool enabled bool enabled
); );
// 打开或关闭自动回收 Easy2D 对象功能
// 默认:关闭
void setObjectsAutoReleaseEnabled(
bool enabled
);
// 打开或关闭碰撞监听 // 打开或关闭碰撞监听
// 默认:关闭 // 默认:关闭
void setCollisionEnabled( void setCollisionEnabled(
@ -795,6 +800,9 @@ public:
// 获取声音打开状态 // 获取声音打开状态
bool isSoundEnabled() const; bool isSoundEnabled() const;
// 获取 Easy2D 对象自动释放状态
bool isObjectsAutoReleaseEnabled() const;
// 获取碰撞监听状态 // 获取碰撞监听状态
bool isCollisionEnabled() const; bool isCollisionEnabled() const;
@ -812,6 +820,7 @@ protected:
protected: protected:
bool _unconfigured; bool _unconfigured;
bool _objectsAutoRelease;
bool _soundEnabled; bool _soundEnabled;
bool _collisionEnabled; bool _collisionEnabled;
bool _colliderVisiable; bool _colliderVisiable;
@ -821,150 +830,4 @@ protected:
}; };
#if _MSC_VER > 1700
// 创建可自动回收内存的对象
template <typename Type, typename... Args>
inline Type * Create(Args&&... args)
{
auto newObj = new (std::nothrow) Type(std::forward<Args>(args)...);
if (newObj)
{
newObj->autorelease();
return newObj;
}
return nullptr;
}
#else
template <typename Type>
inline Type * Create()
{
auto newObj = new (std::nothrow) Type();
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
template <typename Type, typename Arg1>
inline Type * Create(Arg1&& arg1)
{
auto newObj = new (std::nothrow) Type(std::forward<Arg1>(arg1));
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
template <typename Type,
typename Arg1,
typename Arg2>
inline Type * Create(
Arg1&& arg1,
Arg2&& arg2
)
{
auto newObj = new (std::nothrow) Type(
std::forward<Arg1>(arg1),
std::forward<Arg2>(arg2)
);
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
template <typename Type,
typename Arg1,
typename Arg2,
typename Arg3>
inline Type * Create(
Arg1&& arg1,
Arg2&& arg2,
Arg3&& arg3
)
{
auto newObj = new (std::nothrow) Type(
std::forward<Arg1>(arg1),
std::forward<Arg2>(arg2),
std::forward<Arg3>(arg3)
);
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
template <typename Type,
typename Arg1,
typename Arg2,
typename Arg3,
typename Arg4>
inline Type * Create(
Arg1&& arg1,
Arg2&& arg2,
Arg3&& arg3,
Arg4&& arg4
)
{
auto newObj = new (std::nothrow) Type(
std::forward<Arg1>(arg1),
std::forward<Arg2>(arg2),
std::forward<Arg3>(arg3),
std::forward<Arg4>(arg4)
);
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
template <typename Type,
typename Arg1,
typename Arg2,
typename Arg3,
typename Arg4,
typename Arg5>
inline Type * Create(
Arg1&& arg1,
Arg2&& arg2,
Arg3&& arg3,
Arg4&& arg4,
Arg5&& arg5
)
{
auto newObj = new (std::nothrow) Type(
std::forward<Arg1>(arg1),
std::forward<Arg2>(arg2),
std::forward<Arg3>(arg3),
std::forward<Arg4>(arg4),
std::forward<Arg5>(arg5)
);
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
template <typename Type,
typename Arg1,
typename Arg2,
typename Arg3,
typename Arg4,
typename Arg5,
typename Arg6>
inline Type * Create(
Arg1&& arg1,
Arg2&& arg2,
Arg3&& arg3,
Arg4&& arg4,
Arg5&& arg5,
Arg6&& arg6
)
{
auto newObj = new (std::nothrow) Type(
std::forward<Arg1>(arg1),
std::forward<Arg2>(arg2),
std::forward<Arg3>(arg3),
std::forward<Arg4>(arg4),
std::forward<Arg5>(arg5),
std::forward<Arg6>(arg6)
);
if (newObj) { newObj->autorelease(); return newObj; }
return nullptr;
}
#endif
} }

View File

@ -95,6 +95,7 @@
<ClCompile Include="..\..\core\Tool\Path.cpp" /> <ClCompile Include="..\..\core\Tool\Path.cpp" />
<ClCompile Include="..\..\core\Tool\Player.cpp" /> <ClCompile Include="..\..\core\Tool\Player.cpp" />
<ClCompile Include="..\..\core\Tool\Random.cpp" /> <ClCompile Include="..\..\core\Tool\Random.cpp" />
<ClCompile Include="..\..\core\Tool\Task.cpp" />
<ClCompile Include="..\..\core\Tool\Timer.cpp" /> <ClCompile Include="..\..\core\Tool\Timer.cpp" />
<ClCompile Include="..\..\core\Transition\BoxTransition.cpp" /> <ClCompile Include="..\..\core\Transition\BoxTransition.cpp" />
<ClCompile Include="..\..\core\Transition\EmergeTransition.cpp" /> <ClCompile Include="..\..\core\Transition\EmergeTransition.cpp" />

View File

@ -236,6 +236,9 @@
<ClCompile Include="..\..\core\Tool\Timer.cpp"> <ClCompile Include="..\..\core\Tool\Timer.cpp">
<Filter>Tool</Filter> <Filter>Tool</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\core\Tool\Task.cpp">
<Filter>Tool</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Transition\BoxTransition.cpp"> <ClCompile Include="..\..\core\Transition\BoxTransition.cpp">
<Filter>Transition</Filter> <Filter>Transition</Filter>
</ClCompile> </ClCompile>

View File

@ -239,6 +239,7 @@
<ClCompile Include="..\..\core\Tool\Path.cpp" /> <ClCompile Include="..\..\core\Tool\Path.cpp" />
<ClCompile Include="..\..\core\Tool\Player.cpp" /> <ClCompile Include="..\..\core\Tool\Player.cpp" />
<ClCompile Include="..\..\core\Tool\Random.cpp" /> <ClCompile Include="..\..\core\Tool\Random.cpp" />
<ClCompile Include="..\..\core\Tool\Task.cpp" />
<ClCompile Include="..\..\core\Tool\Timer.cpp" /> <ClCompile Include="..\..\core\Tool\Timer.cpp" />
<ClCompile Include="..\..\core\Transition\BoxTransition.cpp" /> <ClCompile Include="..\..\core\Transition\BoxTransition.cpp" />
<ClCompile Include="..\..\core\Transition\EmergeTransition.cpp" /> <ClCompile Include="..\..\core\Transition\EmergeTransition.cpp" />

View File

@ -236,6 +236,9 @@
<ClCompile Include="..\..\core\Tool\Timer.cpp"> <ClCompile Include="..\..\core\Tool\Timer.cpp">
<Filter>Tool</Filter> <Filter>Tool</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\core\Tool\Task.cpp">
<Filter>Tool</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Transition\BoxTransition.cpp"> <ClCompile Include="..\..\core\Transition\BoxTransition.cpp">
<Filter>Transition</Filter> <Filter>Transition</Filter>
</ClCompile> </ClCompile>