Blame view

ios/cocos2d/cocos/2d/CCScene.cpp 9.43 KB
520389e3   xiaoyu   接入cocos源码,编译未通过,继续修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  /****************************************************************************
  Copyright (c) 2008-2010 Ricardo Quesada
  Copyright (c) 2010-2012 cocos2d-x.org
  Copyright (c) 2011      Zynga Inc.
  Copyright (c) 2013-2016 Chukong Technologies Inc.
  Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  
  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.
  ****************************************************************************/
  
  #include "2d/CCScene.h"
  #include "base/CCDirector.h"
  #include "2d/CCCamera.h"
  #include "base/CCEventDispatcher.h"
  #include "base/CCEventListenerCustom.h"
  #include "base/ccUTF8.h"
  #include "renderer/CCRenderer.h"
  
  #if CC_USE_PHYSICS
  #include "physics/CCPhysicsWorld.h"
  #endif
  
  #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
  #include "physics3d/CCPhysics3DWorld.h"
  #include "physics3d/CCPhysics3DComponent.h"
  #endif
  
  #if CC_USE_NAVMESH
  #include "navmesh/CCNavMesh.h"
  #endif
  
  NS_CC_BEGIN
  
  Scene::Scene()
  : _defaultCamera(Camera::create())
  , _event(Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_PROJECTION_CHANGED, std::bind(&Scene::onProjectionChanged, this, std::placeholders::_1)))
  {
      _ignoreAnchorPointForPosition = true;
      setAnchorPoint(Vec2(0.5f, 0.5f));
      
      //create default camera
  
      addChild(_defaultCamera);
      
      _event->retain();
      
      Camera::_visitingCamera = nullptr;
  }
  
  Scene::~Scene()
  {
  #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
      CC_SAFE_RELEASE(_physics3DWorld);
      CC_SAFE_RELEASE(_physics3dDebugCamera);
  #endif
  #if CC_USE_NAVMESH
      CC_SAFE_RELEASE(_navMesh);
  #endif
      Director::getInstance()->getEventDispatcher()->removeEventListener(_event);
      CC_SAFE_RELEASE(_event);
      
  #if CC_USE_PHYSICS
      delete _physicsWorld;
  #endif
      
  #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
      auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
      if (sEngine)
      {
          sEngine->releaseAllChildrenRecursive(this);
      }
  #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  }
  
  #if CC_USE_NAVMESH
  void Scene::setNavMesh(NavMesh* navMesh)
  {
      if (_navMesh != navMesh)
      {
          CC_SAFE_RETAIN(navMesh);
          CC_SAFE_RELEASE(_navMesh);
          _navMesh = navMesh;
      }
  }
  #endif
  
  bool Scene::init()
  {
      auto size = Director::getInstance()->getWinSize();
      return initWithSize(size);
  }
  
  bool Scene::initWithSize(const Size& size)
  {
      setContentSize(size);
      return true;
  }
  
  Scene* Scene::create()
  {
      Scene *ret = new (std::nothrow) Scene();
      if (ret && ret->init())
      {
          ret->autorelease();
          return ret;
      }
      else
      {
          CC_SAFE_DELETE(ret);
          return nullptr;
      }
  }
  
  Scene* Scene::createWithSize(const Size& size)
  {
      Scene *ret = new (std::nothrow) Scene();
      if (ret && ret->initWithSize(size))
      {
          ret->autorelease();
          return ret;
      }
      else
      {
          CC_SAFE_DELETE(ret);
          return nullptr;
      }
  }
  
  std::string Scene::getDescription() const
  {
      return StringUtils::format("<Scene | tag = %d>", _tag);
  }
  
  void Scene::onProjectionChanged(EventCustom* /*event*/)
  {
      if (_defaultCamera)
      {
          _defaultCamera->initDefault();
      }
  }
  
  static bool camera_cmp(const Camera* a, const Camera* b)
  {
      return a->getRenderOrder() < b->getRenderOrder();
  }
  
  const std::vector<Camera*>& Scene::getCameras()
  {
      if (_cameraOrderDirty)
      {
          stable_sort(_cameras.begin(), _cameras.end(), camera_cmp);
          _cameraOrderDirty = false;
      }
      return _cameras;
  }
  
  void Scene::render(Renderer* renderer, const Mat4& eyeTransform, const Mat4* eyeProjection)
  {
      auto director = Director::getInstance();
      Camera* defaultCamera = nullptr;
      const auto& transform = getNodeToParentTransform();
  
      for (const auto& camera : getCameras())
      {
          if (!camera->isVisible())
              continue;
  
          Camera::_visitingCamera = camera;
          if (Camera::_visitingCamera->getCameraFlag() == CameraFlag::DEFAULT)
          {
              defaultCamera = Camera::_visitingCamera;
          }
  
          // There are two ways to modify the "default camera" with the eye Transform:
          // a) modify the "nodeToParentTransform" matrix
          // b) modify the "additional transform" matrix
          // both alternatives are correct, if the user manually modifies the camera with a camera->setPosition()
          // then the "nodeToParent transform" will be lost.
          // And it is important that the change is "permanent", because the matrix might be used for calculate
          // culling and other stuff.
          if (eyeProjection)
              camera->setAdditionalProjection(*eyeProjection * camera->getProjectionMatrix().getInversed());
  
          camera->setAdditionalTransform(eyeTransform.getInversed());
          director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
          director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, Camera::_visitingCamera->getViewProjectionMatrix());
  
          camera->apply();
          //clear background with max depth
          camera->clearBackground();
          //visit the scene
          visit(renderer, transform, 0);
  #if CC_USE_NAVMESH
          if (_navMesh && _navMeshDebugCamera == camera)
          {
              _navMesh->debugDraw(renderer);
          }
  #endif
  
          renderer->render();
  
          director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
  
          // we shouldn't restore the transform matrix since it could be used
          // from "update" or other parts of the game to calculate culling or something else.
          //        camera->setNodeToParentTransform(eyeCopy);
      }
  
  #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
      if (_physics3DWorld && _physics3DWorld->isDebugDrawEnabled())
      {
          Camera *physics3dDebugCamera = _physics3dDebugCamera != nullptr ? _physics3dDebugCamera: defaultCamera;
  
          if (eyeProjection)
              physics3dDebugCamera->setAdditionalProjection(*eyeProjection * physics3dDebugCamera->getProjectionMatrix().getInversed());
  
          physics3dDebugCamera->setAdditionalTransform(eyeTransform.getInversed());
          director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
          director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, physics3dDebugCamera->getViewProjectionMatrix());
  
          physics3dDebugCamera->apply();
          physics3dDebugCamera->clearBackground();
  
          _physics3DWorld->debugDraw(renderer);
          renderer->render();
  
          director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
      }
  #endif
  
      Camera::_visitingCamera = nullptr;
  }
  
  void Scene::removeAllChildren()
  {
      if (_defaultCamera)
          _defaultCamera->retain();
  
      Node::removeAllChildren();
  
      if (_defaultCamera)
      {
          addChild(_defaultCamera);
          _defaultCamera->release();
      }
  }
  
  #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
  void Scene::setPhysics3DDebugCamera(Camera* camera)
  {
      CC_SAFE_RETAIN(camera);
      CC_SAFE_RELEASE(_physics3dDebugCamera);
      _physics3dDebugCamera = camera;
  }
  #endif
  
  #if CC_USE_NAVMESH
  void Scene::setNavMeshDebugCamera(Camera *camera)
  {
      CC_SAFE_RETAIN(camera);
      CC_SAFE_RELEASE(_navMeshDebugCamera);
      _navMeshDebugCamera = camera;
  }
  
  #endif
  
  #if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION))
  
  Scene* Scene::createWithPhysics()
  {
      Scene *ret = new (std::nothrow) Scene();
      if (ret && ret->initWithPhysics())
      {
          ret->autorelease();
          return ret;
      }
      else
      {
          CC_SAFE_DELETE(ret);
          return nullptr;
      }
  }
  
  bool Scene::initWithPhysics()
  {
  #if CC_USE_PHYSICS
      _physicsWorld = PhysicsWorld::construct(this);
  #endif
  
      bool ret = false;
      do
      {
          Director * director;
          CC_BREAK_IF( ! (director = Director::getInstance()) );
  
          this->setContentSize(director->getWinSize());
  
  #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
          Physics3DWorldDes info;
          CC_BREAK_IF(! (_physics3DWorld = Physics3DWorld::create(&info)));
          _physics3DWorld->retain();
  #endif
  
          // success
          ret = true;
      } while (0);
      return ret;
  }
  
  #endif
  
  #if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION) || CC_USE_NAVMESH)
  void Scene::stepPhysicsAndNavigation(float deltaTime)
  {
  #if CC_USE_PHYSICS
      if (_physicsWorld && _physicsWorld->isAutoStep())
          _physicsWorld->update(deltaTime);
  #endif
  
  #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
  
  NS_CC_END