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
|
|
|
|
|
|
|
//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-06-17 11:43:03 +08:00
|
|
|
Scene* Scene::getScene() const
|
2013-12-22 04:28:49 +08:00
|
|
|
{
|
2014-06-17 11:43:03 +08:00
|
|
|
// FIX ME: should use const_case<> to fix compiling error
|
|
|
|
return const_cast<Scene*>(this);
|
2013-12-22 04:28:49 +08:00
|
|
|
}
|
|
|
|
|
2014-08-27 13:39:50 +08:00
|
|
|
void Scene::onProjectionChanged(EventCustom* event)
|
|
|
|
{
|
|
|
|
if (_defaultCamera)
|
|
|
|
{
|
|
|
|
_defaultCamera->initDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 09:36:43 +08:00
|
|
|
void Scene::render(Renderer* renderer)
|
|
|
|
{
|
|
|
|
auto director = Director::getInstance();
|
|
|
|
Camera* defaultCamera = nullptr;
|
|
|
|
for (const auto& camera : _cameras)
|
|
|
|
{
|
|
|
|
Camera::_visitingCamera = camera;
|
|
|
|
if (Camera::_visitingCamera->getCameraFlag() == CameraFlag::DEFAULT)
|
|
|
|
{
|
|
|
|
defaultCamera = Camera::_visitingCamera;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, Camera::_visitingCamera->getViewProjectionMatrix());
|
|
|
|
|
|
|
|
//visit the scene
|
|
|
|
visit(renderer, Mat4::IDENTITY, 0);
|
|
|
|
renderer->render();
|
|
|
|
|
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
}
|
|
|
|
//draw with default camera
|
|
|
|
if (defaultCamera)
|
|
|
|
{
|
|
|
|
Camera::_visitingCamera = defaultCamera;
|
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, Camera::_visitingCamera->getViewProjectionMatrix());
|
|
|
|
|
|
|
|
//visit the scene
|
|
|
|
visit(renderer, Mat4::IDENTITY, 0);
|
|
|
|
renderer->render();
|
|
|
|
|
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
}
|
|
|
|
Camera::_visitingCamera = nullptr;
|
|
|
|
}
|
|
|
|
|
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-02 07:42:35 +08:00
|
|
|
void Scene::update(float delta)
|
|
|
|
{
|
|
|
|
Node::update(delta);
|
2014-06-12 12:45:21 +08:00
|
|
|
if (nullptr != _physicsWorld && _physicsWorld->isAutoStep())
|
2014-05-02 07:42:35 +08:00
|
|
|
{
|
|
|
|
_physicsWorld->update(delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)));
|
|
|
|
|
|
|
|
this->scheduleUpdate();
|
|
|
|
// 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
|
|
|
|
{
|
|
|
|
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
|