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"
|
2015-06-09 15:32:12 +08:00
|
|
|
#include "renderer/CCFrameBuffer.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
|
2015-09-18 11:48:43 +08:00
|
|
|
#include "physics/CCPhysicsWorld.h"
|
2014-08-27 13:39:50 +08:00
|
|
|
#endif
|
|
|
|
|
2015-05-11 15:34:37 +08:00
|
|
|
#if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
|
2015-05-08 15:49:33 +08:00
|
|
|
#include "physics3d/CCPhysics3DWorld.h"
|
|
|
|
#include "physics3d/CCPhysics3DComponent.h"
|
|
|
|
#endif
|
|
|
|
|
2015-05-29 16:45:33 +08:00
|
|
|
#if CC_USE_NAVMESH
|
|
|
|
#include "navmesh/CCNavMesh.h"
|
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Scene::Scene()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-05-11 15:34:37 +08:00
|
|
|
#if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
|
2015-05-08 15:49:33 +08:00
|
|
|
_physics3DWorld = nullptr;
|
|
|
|
_physics3dDebugCamera = nullptr;
|
2015-05-29 16:45:33 +08:00
|
|
|
#endif
|
|
|
|
#if CC_USE_NAVMESH
|
|
|
|
_navMesh = nullptr;
|
|
|
|
_navMeshDebugCamera = nullptr;
|
2015-09-08 09:54:01 +08:00
|
|
|
#endif
|
|
|
|
#if CC_USE_PHYSICS
|
2015-09-18 11:48:43 +08:00
|
|
|
_physicsWorld = nullptr;
|
2015-05-08 15:49:33 +08:00
|
|
|
#endif
|
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();
|
2015-08-21 17:38:56 +08:00
|
|
|
|
|
|
|
Camera::_visitingCamera = nullptr;
|
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
|
|
|
{
|
2015-05-11 15:34:37 +08:00
|
|
|
#if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
|
2015-05-08 15:49:33 +08:00
|
|
|
CC_SAFE_RELEASE(_physics3DWorld);
|
|
|
|
CC_SAFE_RELEASE(_physics3dDebugCamera);
|
2015-05-29 16:45:33 +08:00
|
|
|
#endif
|
|
|
|
#if CC_USE_NAVMESH
|
|
|
|
CC_SAFE_RELEASE(_navMesh);
|
2014-05-02 07:42:35 +08:00
|
|
|
#endif
|
2014-08-12 14:42:08 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_event);
|
|
|
|
CC_SAFE_RELEASE(_event);
|
2015-09-08 09:54:01 +08:00
|
|
|
|
|
|
|
#if CC_USE_PHYSICS
|
2015-09-18 11:48:43 +08:00
|
|
|
delete _physicsWorld;
|
2015-09-08 09:54:01 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2015-05-29 16:45:33 +08:00
|
|
|
#if CC_USE_NAVMESH
|
|
|
|
void Scene::setNavMesh(NavMesh* navMesh)
|
|
|
|
{
|
|
|
|
if (_navMesh != navMesh)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(navMesh);
|
|
|
|
CC_SAFE_RELEASE(_navMesh);
|
|
|
|
_navMesh = navMesh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
{
|
2015-05-12 16:43:17 +08:00
|
|
|
return a->getRenderOrder() < b->getRenderOrder();
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
|
2015-06-18 10:23:52 +08:00
|
|
|
const std::vector<Camera*>& Scene::getCameras()
|
2014-09-26 09:36:43 +08:00
|
|
|
{
|
2015-02-11 18:14:22 +08:00
|
|
|
if (_cameraOrderDirty)
|
|
|
|
{
|
|
|
|
stable_sort(_cameras.begin(), _cameras.end(), camera_cmp);
|
|
|
|
_cameraOrderDirty = false;
|
|
|
|
}
|
2015-06-18 10:23:52 +08:00
|
|
|
return _cameras;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::render(Renderer* renderer)
|
|
|
|
{
|
|
|
|
auto director = Director::getInstance();
|
|
|
|
Camera* defaultCamera = nullptr;
|
|
|
|
const auto& transform = getNodeToParentTransform();
|
|
|
|
|
|
|
|
for (const auto& camera : getCameras())
|
2014-09-26 09:36:43 +08:00
|
|
|
{
|
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());
|
2015-05-29 14:08:20 +08:00
|
|
|
camera->apply();
|
2015-05-07 15:09:06 +08:00
|
|
|
//clear background with max depth
|
2015-08-20 16:19:57 +08:00
|
|
|
camera->clearBackground();
|
2014-09-26 09:36:43 +08:00
|
|
|
//visit the scene
|
2014-11-10 12:48:42 +08:00
|
|
|
visit(renderer, transform, 0);
|
2015-05-29 16:45:33 +08:00
|
|
|
#if CC_USE_NAVMESH
|
|
|
|
if (_navMesh && _navMeshDebugCamera == camera)
|
|
|
|
{
|
|
|
|
_navMesh->debugDraw(renderer);
|
|
|
|
}
|
|
|
|
#endif
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2014-09-26 09:36:43 +08:00
|
|
|
renderer->render();
|
|
|
|
|
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
}
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2015-05-11 15:34:37 +08:00
|
|
|
#if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
|
2015-05-08 15:49:33 +08:00
|
|
|
if (_physics3DWorld && _physics3DWorld->isDebugDrawEnabled())
|
|
|
|
{
|
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, _physics3dDebugCamera != nullptr ? _physics3dDebugCamera->getViewProjectionMatrix() : defaultCamera->getViewProjectionMatrix());
|
|
|
|
_physics3DWorld->debugDraw(renderer);
|
|
|
|
renderer->render();
|
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
}
|
|
|
|
#endif
|
2015-02-12 16:58:34 +08:00
|
|
|
|
2014-09-26 09:36:43 +08:00
|
|
|
Camera::_visitingCamera = nullptr;
|
2015-06-09 10:59:43 +08:00
|
|
|
experimental::FrameBuffer::applyDefaultFBO();
|
2014-09-26 09:36:43 +08:00
|
|
|
}
|
|
|
|
|
2015-03-12 10:39:21 +08:00
|
|
|
void Scene::removeAllChildren()
|
|
|
|
{
|
|
|
|
if (_defaultCamera)
|
|
|
|
_defaultCamera->retain();
|
|
|
|
|
|
|
|
Node::removeAllChildren();
|
|
|
|
|
|
|
|
if (_defaultCamera)
|
|
|
|
{
|
|
|
|
addChild(_defaultCamera);
|
|
|
|
_defaultCamera->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 15:34:37 +08:00
|
|
|
#if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
|
2015-05-08 15:49:33 +08:00
|
|
|
void Scene::setPhysics3DDebugCamera(Camera* camera)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(camera);
|
|
|
|
CC_SAFE_RELEASE(_physics3dDebugCamera);
|
|
|
|
_physics3dDebugCamera = camera;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-29 16:45:33 +08:00
|
|
|
#if CC_USE_NAVMESH
|
|
|
|
void Scene::setNavMeshDebugCamera(Camera *camera)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(camera);
|
|
|
|
CC_SAFE_RELEASE(_navMeshDebugCamera);
|
|
|
|
_navMeshDebugCamera = camera;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-05-11 15:34:37 +08:00
|
|
|
#if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION))
|
2014-06-25 11:27:48 +08:00
|
|
|
|
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()
|
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
#if CC_USE_PHYSICS
|
|
|
|
_physicsWorld = PhysicsWorld::construct(this);
|
|
|
|
#endif
|
2015-09-08 09:54:01 +08:00
|
|
|
|
2014-05-02 07:42:35 +08:00
|
|
|
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());
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2015-05-11 15:34:37 +08:00
|
|
|
#if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
|
2015-05-08 15:49:33 +08:00
|
|
|
Physics3DWorldDes info;
|
|
|
|
CC_BREAK_IF(! (_physics3DWorld = Physics3DWorld::create(&info)));
|
|
|
|
_physics3DWorld->retain();
|
|
|
|
#endif
|
2014-05-02 07:42:35 +08:00
|
|
|
|
|
|
|
// success
|
|
|
|
ret = true;
|
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-06-09 13:49:07 +08:00
|
|
|
#if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION) || CC_USE_NAVMESH)
|
|
|
|
void Scene::stepPhysicsAndNavigation(float deltaTime)
|
|
|
|
{
|
|
|
|
#if CC_USE_PHYSICS
|
2015-09-18 11:48:43 +08:00
|
|
|
if (_physicsWorld && _physicsWorld->isAutoStep())
|
|
|
|
_physicsWorld->update(deltaTime);
|
2015-06-09 13:49:07 +08:00
|
|
|
#endif
|
2015-09-08 09:54:01 +08:00
|
|
|
|
2015-06-09 13:49:07 +08:00
|
|
|
#if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
|
|
|
|
if (_physics3DWorld)
|
|
|
|
{
|
|
|
|
_physics3DWorld->stepSimulate(deltaTime);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if CC_USE_NAVMESH
|
|
|
|
if (_navMesh)
|
|
|
|
{
|
|
|
|
_navMesh->update(deltaTime);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|