2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCScene.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-10-20 16:25:24 +08:00
|
|
|
#include "2d/CCCamera.h"
|
2014-08-12 14:42:08 +08:00
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
2014-09-26 09:36:43 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2014-04-09 22:30:49 +08:00
|
|
|
#include "deprecated/CCString.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-08-27 13:39:50 +08:00
|
|
|
#if CC_USE_PHYSICS
|
|
|
|
#include "physics/CCPhysicsWorld.h"
|
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Scene::Scene()
|
2014-05-02 07:42:35 +08:00
|
|
|
#if CC_USE_PHYSICS
|
|
|
|
: _physicsWorld(nullptr)
|
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_ignoreAnchorPointForPosition = true;
|
2014-05-15 01:07:09 +08:00
|
|
|
setAnchorPoint(Vec2(0.5f, 0.5f));
|
2014-08-12 23:34:06 +08:00
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
_cameraOrderDirty = true;
|
|
|
|
|
2014-08-12 23:34:06 +08:00
|
|
|
//create default camera
|
|
|
|
_defaultCamera = Camera::create();
|
|
|
|
addChild(_defaultCamera);
|
|
|
|
|
|
|
|
_event = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_PROJECTION_CHANGED, std::bind(&Scene::onProjectionChanged, this, std::placeholders::_1));
|
|
|
|
_event->retain();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Scene::~Scene()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-02 07:42:35 +08:00
|
|
|
#if CC_USE_PHYSICS
|
|
|
|
CC_SAFE_DELETE(_physicsWorld);
|
|
|
|
#endif
|
2014-08-12 14:42:08 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_event);
|
|
|
|
CC_SAFE_RELEASE(_event);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Scene::init()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-31 07:42:05 +08:00
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
return initWithSize(size);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
bool Scene::initWithSize(const Size& size)
|
|
|
|
{
|
|
|
|
setContentSize(size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scene* Scene::create()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Scene *ret = new (std::nothrow) Scene();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret && ret->init())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
Scene* Scene::createWithSize(const Size& size)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Scene *ret = new (std::nothrow) Scene();
|
2014-05-31 07:42:05 +08:00
|
|
|
if (ret && ret->initWithSize(size))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
std::string Scene::getDescription() const
|
|
|
|
{
|
|
|
|
return StringUtils::format("<Scene | tag = %d>", _tag);
|
|
|
|
}
|
|
|
|
|
2014-08-27 13:39:50 +08:00
|
|
|
void Scene::onProjectionChanged(EventCustom* event)
|
|
|
|
{
|
|
|
|
if (_defaultCamera)
|
|
|
|
{
|
|
|
|
_defaultCamera->initDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 16:58:34 +08:00
|
|
|
static bool camera_cmp(const Camera* a, const Camera* b)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return a->getDepth() < b->getDepth();
|
|
|
|
}
|
|
|
|
|
2014-09-26 09:36:43 +08:00
|
|
|
void Scene::render(Renderer* renderer)
|
|
|
|
{
|
|
|
|
auto director = Director::getInstance();
|
|
|
|
Camera* defaultCamera = nullptr;
|
2014-11-10 12:48:42 +08:00
|
|
|
const auto& transform = getNodeToParentTransform();
|
2015-02-11 18:14:22 +08:00
|
|
|
if (_cameraOrderDirty)
|
|
|
|
{
|
|
|
|
stable_sort(_cameras.begin(), _cameras.end(), camera_cmp);
|
|
|
|
_cameraOrderDirty = false;
|
|
|
|
}
|
|
|
|
|
2014-09-26 09:36:43 +08:00
|
|
|
for (const auto& camera : _cameras)
|
|
|
|
{
|
2015-01-12 10:08:55 +08:00
|
|
|
if (!camera->isVisible())
|
|
|
|
continue;
|
|
|
|
|
2014-09-26 09:36:43 +08:00
|
|
|
Camera::_visitingCamera = camera;
|
|
|
|
if (Camera::_visitingCamera->getCameraFlag() == CameraFlag::DEFAULT)
|
|
|
|
{
|
|
|
|
defaultCamera = Camera::_visitingCamera;
|
|
|
|
}
|
|
|
|
|
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, Camera::_visitingCamera->getViewProjectionMatrix());
|
|
|
|
|
|
|
|
//visit the scene
|
2014-11-10 12:48:42 +08:00
|
|
|
visit(renderer, transform, 0);
|
2014-09-26 09:36:43 +08:00
|
|
|
renderer->render();
|
|
|
|
|
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
}
|
2015-02-12 16:58:34 +08:00
|
|
|
|
2014-09-26 09:36:43 +08:00
|
|
|
Camera::_visitingCamera = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-12 10:39:21 +08:00
|
|
|
void Scene::removeAllChildren()
|
|
|
|
{
|
|
|
|
if (_defaultCamera)
|
|
|
|
_defaultCamera->retain();
|
|
|
|
|
|
|
|
Node::removeAllChildren();
|
|
|
|
|
|
|
|
if (_defaultCamera)
|
|
|
|
{
|
|
|
|
addChild(_defaultCamera);
|
|
|
|
_defaultCamera->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 07:42:35 +08:00
|
|
|
#if CC_USE_PHYSICS
|
|
|
|
void Scene::addChild(Node* child, int zOrder, int tag)
|
|
|
|
{
|
|
|
|
Node::addChild(child, zOrder, tag);
|
|
|
|
addChildToPhysicsWorld(child);
|
|
|
|
}
|
|
|
|
|
2014-06-25 11:27:48 +08:00
|
|
|
void Scene::addChild(Node* child, int zOrder, const std::string &name)
|
|
|
|
{
|
|
|
|
Node::addChild(child, zOrder, name);
|
|
|
|
addChildToPhysicsWorld(child);
|
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
Scene* Scene::createWithPhysics()
|
2014-05-02 07:42:35 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Scene *ret = new (std::nothrow) Scene();
|
2014-05-02 07:42:35 +08:00
|
|
|
if (ret && ret->initWithPhysics())
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scene::initWithPhysics()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Director * director;
|
|
|
|
CC_BREAK_IF( ! (director = Director::getInstance()) );
|
2014-08-09 17:37:18 +08:00
|
|
|
|
2014-05-02 07:42:35 +08:00
|
|
|
this->setContentSize(director->getWinSize());
|
|
|
|
CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::construct(*this)));
|
|
|
|
|
|
|
|
// success
|
|
|
|
ret = true;
|
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::addChildToPhysicsWorld(Node* child)
|
|
|
|
{
|
|
|
|
if (_physicsWorld)
|
|
|
|
{
|
|
|
|
std::function<void(Node*)> addToPhysicsWorldFunc = nullptr;
|
|
|
|
addToPhysicsWorldFunc = [this, &addToPhysicsWorldFunc](Node* node) -> void
|
|
|
|
{
|
2015-04-10 15:39:39 +08:00
|
|
|
node->_physicsWorld = _physicsWorld;
|
|
|
|
|
2014-05-02 07:42:35 +08:00
|
|
|
if (node->getPhysicsBody())
|
|
|
|
{
|
|
|
|
_physicsWorld->addBody(node->getPhysicsBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& children = node->getChildren();
|
|
|
|
for( const auto &n : children) {
|
|
|
|
addToPhysicsWorldFunc(n);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
addToPhysicsWorldFunc(child);
|
|
|
|
}
|
|
|
|
}
|
2014-08-07 15:23:31 +08:00
|
|
|
|
2014-05-02 07:42:35 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|