代码优化

This commit is contained in:
Nomango 2018-08-12 14:30:28 +08:00
parent 6382adb1b1
commit fe52ebf451
15 changed files with 77 additions and 71 deletions

View File

@ -24,7 +24,7 @@ e2d::Animation::Animation(float interval, const std::vector<Image*>& frames)
e2d::Animation::~Animation()
{
for (auto frame : _frames)
for (const auto& frame : _frames)
{
GC::getInstance()->safeRelease(frame);
}
@ -67,7 +67,7 @@ e2d::Animation * e2d::Animation::clone() const
auto animation = new (e2d::autorelease) Animation(_interval);
if (animation)
{
for (auto frame : _frames)
for (const auto& frame : _frames)
{
animation->add(frame);
}

View File

@ -13,7 +13,7 @@ e2d::Sequence::Sequence(const std::vector<Action*>& actions)
e2d::Sequence::~Sequence()
{
for (auto action : _actions)
for (const auto& action : _actions)
{
GC::getInstance()->safeRelease(action);
}
@ -25,7 +25,7 @@ void e2d::Sequence::_init()
// 将所有动作与目标绑定
if (_target)
{
for (auto action : _actions)
for (const auto& action : _actions)
{
action->_target = _target;
}
@ -58,7 +58,7 @@ void e2d::Sequence::_update()
void e2d::Sequence::reset()
{
Action::reset();
for (auto action : _actions)
for (const auto& action : _actions)
{
action->reset();
}
@ -67,7 +67,7 @@ void e2d::Sequence::reset()
void e2d::Sequence::_resetTime()
{
for (auto action : _actions)
for (const auto& action : _actions)
{
action->_resetTime();
}

View File

@ -11,7 +11,7 @@ e2d::Spawn::Spawn(const std::vector<Action*>& actions)
e2d::Spawn::~Spawn()
{
for (auto action : _actions)
for (const auto& action : _actions)
{
GC::getInstance()->safeRelease(action);
}
@ -23,7 +23,7 @@ void e2d::Spawn::_init()
if (_target)
{
for (auto action : _actions)
for (const auto& action : _actions)
{
action->_target = _target;
action->_init();
@ -36,7 +36,7 @@ void e2d::Spawn::_update()
Action::_update();
size_t doneNum = 0;
for (auto action : _actions)
for (const auto& action : _actions)
{
if (action->_isDone())
{
@ -57,7 +57,7 @@ void e2d::Spawn::_update()
void e2d::Spawn::reset()
{
Action::reset();
for (auto action : _actions)
for (const auto& action : _actions)
{
action->reset();
}
@ -65,7 +65,7 @@ void e2d::Spawn::reset()
void e2d::Spawn::_resetTime()
{
for (auto action : _actions)
for (const auto& action : _actions)
{
action->_resetTime();
}

View File

@ -80,7 +80,7 @@ void e2d::GC::clear()
Timer::getInstance()->clearAllTasks();
ActionManager::getInstance()->clearAll();
for (auto ref : _pool)
for (const auto& ref : _pool)
{
delete ref;
}

View File

@ -299,7 +299,7 @@ void e2d::Image::clearCache()
if (_bitmapCache.empty())
return;
for (auto bitmap : _bitmapCache)
for (const auto& bitmap : _bitmapCache)
{
bitmap.second->Release();
}

View File

@ -72,7 +72,7 @@ void e2d::Scene::add(Node * child, int order /* = 0 */)
void e2d::Scene::add(const std::vector<Node*>& nodes, int order)
{
for (auto node : nodes)
for (const auto& node : nodes)
{
this->add(node, order);
}

View File

@ -195,21 +195,21 @@ wchar_t &e2d::String::operator[](int index)
return _str[size_t(index)];
}
e2d::String e2d::String::operator+(const wchar_t *str)
e2d::String e2d::String::operator+(const wchar_t *str) const
{
String temp;
temp._str = _str + str;
return std::move(temp);
}
e2d::String e2d::String::operator+(const char *str)
e2d::String e2d::String::operator+(const char *str) const
{
String temp;
temp._str = _str + static_cast<wchar_t*>(_bstr_t(str));
return std::move(temp);
}
e2d::String e2d::String::operator+(const e2d::String &str)
e2d::String e2d::String::operator+(const e2d::String &str) const
{
String temp;
temp._str = _str + str._str;

View File

@ -87,7 +87,7 @@ void e2d::ActionManager::resumeAllBindedWith(Node * target)
if (_runningActions.empty() || target == nullptr)
return;
for (auto action : _runningActions)
for (const auto& action : _runningActions)
{
if (action->getTarget() == target)
{
@ -101,7 +101,7 @@ void e2d::ActionManager::pauseAllBindedWith(Node * target)
if (_runningActions.empty() || target == nullptr)
return;
for (auto action : _runningActions)
for (const auto& action : _runningActions)
{
if (action->getTarget() == target)
{
@ -115,7 +115,7 @@ void e2d::ActionManager::stopAllBindedWith(Node * target)
if (_runningActions.empty() || target == nullptr)
return;
for (auto action : _runningActions)
for (const auto& action : _runningActions)
{
if (action->getTarget() == target)
{
@ -154,7 +154,7 @@ void e2d::ActionManager::resume(const String& name)
if (_runningActions.empty() || name.isEmpty())
return;
for (auto action : _runningActions)
for (const auto& action : _runningActions)
{
if (action->getName() == name)
{
@ -168,7 +168,7 @@ void e2d::ActionManager::pause(const String& name)
if (_runningActions.empty() || name.isEmpty())
return;
for (auto action : _runningActions)
for (const auto& action : _runningActions)
{
if (action->getName() == name)
{
@ -182,7 +182,7 @@ void e2d::ActionManager::stop(const String& name)
if (_runningActions.empty() || name.isEmpty())
return;
for (auto action : _runningActions)
for (const auto& action : _runningActions)
{
if (action->getName() == name)
{
@ -215,7 +215,7 @@ void e2d::ActionManager::clearAll()
{
if (!_runningActions.empty())
{
for (auto action : _runningActions)
for (const auto& action : _runningActions)
{
action->release();
}
@ -228,7 +228,7 @@ void e2d::ActionManager::clearAll()
std::vector<e2d::Action*> e2d::ActionManager::get(const String& name)
{
std::vector<Action*> actions;
for (auto action : _actions)
for (const auto& action : _actions)
{
if (action->getName() == name)
{
@ -245,7 +245,7 @@ const std::vector<e2d::Action*>& e2d::ActionManager::getAll()
void e2d::ActionManager::updateTime()
{
for (auto action : _runningActions)
for (const auto& action : _runningActions)
{
action->_resetTime();
}

View File

@ -92,7 +92,7 @@ void e2d::CollisionManager::addName(const String & name1, const String & name2)
void e2d::CollisionManager::addName(const std::vector<std::pair<String, String> >& names)
{
for (auto& name : names)
for (const auto& name : names)
{
if (!name.first.isEmpty() && !name.second.isEmpty())
{
@ -112,7 +112,7 @@ bool e2d::CollisionManager::isCollidable(const String & name1, const String & na
hashName2 = name2.getHashCode();
auto pair1 = std::make_pair(hashName1, hashName2),
pair2 = std::make_pair(hashName2, hashName1);
for (auto& pair : _collisionList)
for (const auto& pair : _collisionList)
{
if (pair == pair1 || pair == pair2)
{

View File

@ -8,7 +8,7 @@ e2d::Menu::Menu()
e2d::Menu::Menu(const std::vector<Button*>& buttons)
: _enabled(true)
{
for (auto button : buttons)
for (const auto& button : buttons)
{
this->addButton(button);
}
@ -30,7 +30,7 @@ void e2d::Menu::setEnabled(bool enabled)
{
_enabled = enabled;
for (auto button : _buttons)
for (const auto& button : _buttons)
{
button->setEnabled(enabled);
}

View File

@ -73,7 +73,7 @@ e2d::Node::Node()
e2d::Node::~Node()
{
ActionManager::getInstance()->clearAllBindedWith(this);
for (auto child : _children)
for (const auto& child : _children)
{
GC::getInstance()->safeRelease(child);
}
@ -209,7 +209,7 @@ void e2d::Node::_renderOutline()
);
// 渲染所有子节点的轮廓
for (auto child : _children)
for (const auto& child : _children)
{
child->_renderOutline();
}
@ -223,7 +223,7 @@ void e2d::Node::_renderCollider()
_collider.render();
// 绘制所有子节点的几何碰撞体
for (auto child : _children)
for (const auto& child : _children)
{
child->_renderCollider();
}
@ -268,13 +268,15 @@ void e2d::Node::updateTransform()
_collider.recreate();
// 通知子节点进行转换
for (auto& child : _children)
for (const auto& child : _children)
{
child->_needTransform = true;
}
}
bool e2d::Node::dispatch(const MouseEvent & e)
{
if (_visible)
{
if (onMouseEvent(e))
return true;
@ -282,11 +284,14 @@ bool e2d::Node::dispatch(const MouseEvent & e)
for (auto iter = _children.rbegin(); iter != _children.rend(); ++iter)
if ((*iter)->dispatch(e))
return true;
}
return false;
}
bool e2d::Node::dispatch(const KeyEvent & e)
{
if (_visible)
{
if (onKeyEvent(e))
return true;
@ -294,6 +299,7 @@ bool e2d::Node::dispatch(const KeyEvent & e)
for (auto iter = _children.rbegin(); iter != _children.rend(); ++iter)
if ((*iter)->dispatch(e))
return true;
}
return false;
}
@ -318,7 +324,7 @@ void e2d::Node::_updateOpacity()
{
_displayOpacity = _realOpacity * _parent->_displayOpacity;
}
for (auto child : _children)
for (const auto& child : _children)
{
child->_updateOpacity();
}
@ -685,7 +691,7 @@ void e2d::Node::addChild(Node * child, int order /* = 0 */)
void e2d::Node::addChild(const std::vector<Node*>& nodes, int order)
{
for (auto node : nodes)
for (const auto& node : nodes)
{
this->addChild(node, order);
}
@ -706,7 +712,7 @@ std::vector<e2d::Node*> e2d::Node::getChildren(const String& name) const
std::vector<Node*> vChildren;
size_t hash = name.getHashCode();
for (auto child : _children)
for (const auto& child : _children)
{
// 不同的名称可能会有相同的 Hash 值,但是先比较 Hash 可以提升搜索速度
if (child->_hashName == hash && child->_name == name)
@ -721,7 +727,7 @@ e2d::Node * e2d::Node::getChild(const String& name) const
{
size_t hash = name.getHashCode();
for (auto child : _children)
for (const auto& child : _children)
{
// 不同的名称可能会有相同的 Hash 值,但是先比较 Hash 可以提升搜索速度
if (child->_hashName == hash && child->_name == name)
@ -811,7 +817,7 @@ void e2d::Node::removeChildren(const String& childName)
void e2d::Node::removeAllChildren()
{
// 所有节点的引用计数减一
for (auto child : _children)
for (const auto& child : _children)
{
child->release();
}
@ -827,7 +833,7 @@ void e2d::Node::runAction(Action * action)
void e2d::Node::resumeAction(const String& name)
{
auto& actions = ActionManager::getInstance()->get(name);
for (auto action : actions)
for (const auto& action : actions)
{
if (action->getTarget() == this)
{
@ -839,7 +845,7 @@ void e2d::Node::resumeAction(const String& name)
void e2d::Node::pauseAction(const String& name)
{
auto& actions = ActionManager::getInstance()->get(name);
for (auto action : actions)
for (const auto& action : actions)
{
if (action->getTarget() == this)
{
@ -851,7 +857,7 @@ void e2d::Node::pauseAction(const String& name)
void e2d::Node::stopAction(const String& name)
{
auto& actions = ActionManager::getInstance()->get(name);
for (auto action : actions)
for (const auto& action : actions)
{
if (action->getTarget() == this)
{
@ -991,7 +997,7 @@ void e2d::Node::setName(const String& name)
void e2d::Node::_setParentScene(Scene * scene)
{
_parentScene = scene;
for (auto child : _children)
for (const auto& child : _children)
{
child->_setParentScene(scene);
}

View File

@ -39,7 +39,7 @@ bool e2d::File::open(const String & fileName)
}
else
{
for (auto& resPath : _searchPaths)
for (const auto& resPath : _searchPaths)
{
if (FindFile(resPath + fileName))
{

View File

@ -24,7 +24,7 @@ e2d::Player::~Player()
{
if (!_musicList.empty())
{
for (auto pair : _musicList)
for (const auto& pair : _musicList)
{
delete pair.second;
}
@ -113,7 +113,7 @@ float e2d::Player::getVolume()
void e2d::Player::setVolume(float volume)
{
_volume = std::min(std::max(volume, -224.f), 224.f);
for (auto pair : _musicList)
for (const auto& pair : _musicList)
{
pair.second->setVolume(_volume);
}
@ -121,7 +121,7 @@ void e2d::Player::setVolume(float volume)
void e2d::Player::pauseAll()
{
for (auto pair : _musicList)
for (const auto& pair : _musicList)
{
pair.second->pause();
}
@ -129,7 +129,7 @@ void e2d::Player::pauseAll()
void e2d::Player::resumeAll()
{
for (auto pair : _musicList)
for (const auto& pair : _musicList)
{
pair.second->resume();
}
@ -137,7 +137,7 @@ void e2d::Player::resumeAll()
void e2d::Player::stopAll()
{
for (auto pair : _musicList)
for (const auto& pair : _musicList)
{
pair.second->stop();
}
@ -154,7 +154,7 @@ void e2d::Player::setEnabled(bool enabled)
void e2d::Player::clearCache()
{
for (auto pair : _musicList)
for (const auto& pair : _musicList)
{
delete pair.second;
}

View File

@ -43,7 +43,7 @@ void e2d::Timer::addTask(Task * task)
void e2d::Timer::stopTasks(const String& name)
{
for (auto task : _tasks)
for (const auto& task : _tasks)
{
if (task->getName() == name)
{
@ -54,7 +54,7 @@ void e2d::Timer::stopTasks(const String& name)
void e2d::Timer::startTasks(const String& name)
{
for (auto task : _tasks)
for (const auto& task : _tasks)
{
if (task->getName() == name)
{
@ -65,7 +65,7 @@ void e2d::Timer::startTasks(const String& name)
void e2d::Timer::removeTasks(const String& name)
{
for (auto task : _tasks)
for (const auto& task : _tasks)
{
if (task->getName() == name)
{
@ -76,7 +76,7 @@ void e2d::Timer::removeTasks(const String& name)
void e2d::Timer::stopAllTasks()
{
for (auto task : _tasks)
for (const auto& task : _tasks)
{
task->stop();
}
@ -84,7 +84,7 @@ void e2d::Timer::stopAllTasks()
void e2d::Timer::startAllTasks()
{
for (auto task : _tasks)
for (const auto& task : _tasks)
{
task->start();
}
@ -92,7 +92,7 @@ void e2d::Timer::startAllTasks()
void e2d::Timer::removeAllTasks()
{
for (auto task : _tasks)
for (const auto& task : _tasks)
{
task->_stopped = true;
}
@ -103,7 +103,7 @@ void e2d::Timer::clearAllTasks()
if (_tasks.empty())
return;
for (auto task : _tasks)
for (const auto& task : _tasks)
{
task->release();
}
@ -139,7 +139,7 @@ void e2d::Timer::update()
void e2d::Timer::updateTime()
{
for (auto task : _tasks)
for (const auto& task : _tasks)
{
task->_lastTime = Time::now();
}

View File

@ -233,9 +233,9 @@ public:
String& operator+= (const String &);
String& operator+= (const char *);
String& operator+= (const wchar_t *);
String operator+ (const String &);
String operator+ (const char *);
String operator+ (const wchar_t *);
String operator+ (const String &) const;
String operator+ (const char *) const;
String operator+ (const wchar_t *) const;
// ÓÑÔªÔËËã·û
friend String operator+ (const char *, const String &);