Skip to content

Commit 6e0c487

Browse files
authored
refactor CCScene (#19767) (#19944)
* refactor-CCScene prefer in-class initialization * in-class initialization of pointer to nullptr
1 parent 7cea351 commit 6e0c487

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

cocos/2d/CCScene.cpp

+3-15
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,16 @@ THE SOFTWARE.
5050
NS_CC_BEGIN
5151

5252
Scene::Scene()
53+
: _defaultCamera(Camera::create())
54+
, _event(Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_PROJECTION_CHANGED, std::bind(&Scene::onProjectionChanged, this, std::placeholders::_1)))
5355
{
54-
#if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
55-
_physics3DWorld = nullptr;
56-
_physics3dDebugCamera = nullptr;
57-
#endif
58-
#if CC_USE_NAVMESH
59-
_navMesh = nullptr;
60-
_navMeshDebugCamera = nullptr;
61-
#endif
62-
#if CC_USE_PHYSICS
63-
_physicsWorld = nullptr;
64-
#endif
6556
_ignoreAnchorPointForPosition = true;
6657
setAnchorPoint(Vec2(0.5f, 0.5f));
6758

68-
_cameraOrderDirty = true;
69-
7059
//create default camera
71-
_defaultCamera = Camera::create();
60+
7261
addChild(_defaultCamera);
7362

74-
_event = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_PROJECTION_CHANGED, std::bind(&Scene::onProjectionChanged, this, std::placeholders::_1));
7563
_event->retain();
7664

7765
Camera::_visitingCamera = nullptr;

cocos/2d/CCScene.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ class CC_DLL Scene : public Node
137137
friend class Renderer;
138138

139139
std::vector<Camera*> _cameras; //weak ref to Camera
140-
Camera* _defaultCamera; //weak ref, default camera created by scene, _cameras[0], Caution that the default camera can not be added to _cameras before onEnter is called
141-
bool _cameraOrderDirty; // order is dirty, need sort
142-
EventListenerCustom* _event;
140+
Camera* _defaultCamera = nullptr; //weak ref, default camera created by scene, _cameras[0], Caution that the default camera can not be added to _cameras before onEnter is called
141+
bool _cameraOrderDirty = true; // order is dirty, need sort
142+
EventListenerCustom* _event = nullptr;
143143

144144
std::vector<BaseLight *> _lights;
145145

@@ -183,12 +183,12 @@ class CC_DLL Scene : public Node
183183
void addChildToPhysicsWorld(Node* child);
184184

185185
#if CC_USE_PHYSICS
186-
PhysicsWorld* _physicsWorld;
186+
PhysicsWorld* _physicsWorld = nullptr;
187187
#endif
188188

189189
#if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
190-
Physics3DWorld* _physics3DWorld;
191-
Camera* _physics3dDebugCamera; //
190+
Physics3DWorld* _physics3DWorld = nullptr;
191+
Camera* _physics3dDebugCamera = nullptr;
192192
#endif
193193
#endif // (CC_USE_PHYSICS || CC_USE_3D_PHYSICS)
194194

@@ -204,8 +204,8 @@ class CC_DLL Scene : public Node
204204
void setNavMeshDebugCamera(Camera *camera);
205205

206206
protected:
207-
NavMesh* _navMesh;
208-
Camera * _navMeshDebugCamera;
207+
NavMesh* _navMesh = nullptr;
208+
Camera * _navMeshDebugCamera = nullptr;
209209
#endif
210210

211211
#if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION) || CC_USE_NAVMESH)

0 commit comments

Comments
 (0)