Skip to content

Commit d68e3bc

Browse files
James Chenstevetranby
authored andcommitted
The optimization for huawei. (cocos2d#17832)
* The optimization for huawei. * setAnimationInterval fix for all platforms. * disable debug * include fix.
1 parent a97ebab commit d68e3bc

38 files changed

+2368
-41
lines changed

cocos/2d/CCActionManager.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,17 @@ size_t ActionManager::getNumberOfRunningActionsInTargetByTag(const Node *target,
423423
return count;
424424
}
425425

426+
ssize_t ActionManager::getNumberOfRunningActions() const
427+
{
428+
ssize_t count = 0;
429+
struct _hashElement* element = nullptr;
430+
struct _hashElement* tmp = nullptr;
431+
HASH_ITER(hh, _targets, element, tmp)
432+
{
433+
count += (element->actions ? element->actions->num : 0);
434+
}
435+
return count;
436+
}
426437

427438
// main loop
428439
void ActionManager::update(float dt)

cocos/2d/CCActionManager.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,17 @@ class CC_DLL ActionManager : public Ref
139139
* @return The numbers of actions that are running in a certain target.
140140
* @js NA
141141
*/
142-
virtual ssize_t getNumberOfRunningActionsInTarget(const Node *target) const;
142+
ssize_t getNumberOfRunningActionsInTarget(const Node *target) const;
143+
144+
/** Returns the numbers of actions that are running in all targets.
145+
* Composable actions are counted as 1 action. Example:
146+
* - If you are running 1 Sequence of 7 actions, it will return 1.
147+
* - If you are running 7 Sequences of 2 actions, it will return 7.
148+
*
149+
* @return The numbers of actions that are running in a certain target.
150+
* @js NA
151+
*/
152+
ssize_t getNumberOfRunningActions() const;
143153

144154
/** @deprecated Use getNumberOfRunningActionsInTarget() instead.
145155
*/

cocos/2d/CCNode.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ NS_CC_BEGIN
5757

5858
// FIXME:: Yes, nodes might have a sort problem once every 30 days if the game runs at 60 FPS and each frame sprites are reordered.
5959
unsigned int Node::s_globalOrderOfArrival = 0;
60+
int Node::__attachedNodeCount = 0;
6061

6162
// MARK: Constructor, Destructor, Init
6263

@@ -1353,6 +1354,10 @@ Mat4 Node::transform(const Mat4& parentTransform)
13531354

13541355
void Node::onEnter()
13551356
{
1357+
if (!_running)
1358+
{
1359+
++__attachedNodeCount;
1360+
}
13561361
#if CC_ENABLE_SCRIPT_BINDING
13571362
if (_scriptType == kScriptTypeJavascript)
13581363
{
@@ -1437,6 +1442,10 @@ void Node::onExitTransitionDidStart()
14371442

14381443
void Node::onExit()
14391444
{
1445+
if (_running)
1446+
{
1447+
--__attachedNodeCount;
1448+
}
14401449
#if CC_ENABLE_SCRIPT_BINDING
14411450
if (_scriptType == kScriptTypeJavascript)
14421451
{
@@ -2262,6 +2271,11 @@ void Node::setCameraMask(unsigned short mask, bool applyChildren)
22622271
}
22632272
}
22642273

2274+
int Node::getAttachedNodeCount()
2275+
{
2276+
return __attachedNodeCount;
2277+
}
2278+
22652279
// MARK: Deprecated
22662280

22672281
__NodeRGBA::__NodeRGBA()

cocos/2d/CCNode.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ class CC_DLL Node : public Ref
127127
*/
128128
static Node * create();
129129

130+
/**
131+
* Gets count of nodes those are attached to scene graph.
132+
*/
133+
static int getAttachedNodeCount();
134+
public:
135+
130136
/**
131137
* Gets the description string. It makes debugging easier.
132138
* @return A string
@@ -2027,6 +2033,8 @@ class CC_DLL Node : public Ref
20272033
friend class PhysicsBody;
20282034
#endif
20292035

2036+
static int __attachedNodeCount;
2037+
20302038
private:
20312039
CC_DISALLOW_COPY_AND_ASSIGN(Node);
20322040
};

cocos/2d/CCParticleSystem.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ void ParticleData::release()
185185
CC_SAFE_FREE(modeB.radius);
186186
}
187187

188+
Vector<ParticleSystem*> ParticleSystem::__allInstances;
189+
float ParticleSystem::__totalParticleCountFactor = 1.0f;
190+
188191
ParticleSystem::ParticleSystem()
189192
: _isBlendAdditive(false)
190193
, _isAutoRemoveOnFinish(false)
@@ -262,6 +265,17 @@ ParticleSystem* ParticleSystem::createWithTotalParticles(int numberOfParticles)
262265
return ret;
263266
}
264267

268+
// static
269+
Vector<ParticleSystem*>& ParticleSystem::getAllParticleSystems()
270+
{
271+
return __allInstances;
272+
}
273+
274+
void ParticleSystem::setTotalParticleCountFactor(float factor)
275+
{
276+
__totalParticleCountFactor = factor;
277+
}
278+
265279
bool ParticleSystem::init()
266280
{
267281
return initWithTotalParticles(150);
@@ -793,6 +807,8 @@ void ParticleSystem::onEnter()
793807

794808
// update after action in run!
795809
this->scheduleUpdateWithPriority(1);
810+
811+
__allInstances.pushBack(this);
796812
}
797813

798814
void ParticleSystem::onExit()
@@ -807,6 +823,12 @@ void ParticleSystem::onExit()
807823

808824
this->unscheduleUpdate();
809825
Node::onExit();
826+
827+
auto iter = std::find(std::begin(__allInstances), std::end(__allInstances), this);
828+
if (iter != std::end(__allInstances))
829+
{
830+
__allInstances.erase(iter);
831+
}
810832
}
811833

812834
void ParticleSystem::stopSystem()
@@ -839,15 +861,17 @@ void ParticleSystem::update(float dt)
839861
if (_isActive && _emissionRate)
840862
{
841863
float rate = 1.0f / _emissionRate;
864+
int totalParticles = static_cast<int>(_totalParticles * __totalParticleCountFactor);
865+
842866
//issue #1201, prevent bursts of particles, due to too high emitCounter
843-
if (_particleCount < _totalParticles)
867+
if (_particleCount < totalParticles)
844868
{
845869
_emitCounter += dt;
846870
if (_emitCounter < 0.f)
847871
_emitCounter = 0.f;
848872
}
849873

850-
int emitCount = MIN(_totalParticles - _particleCount, _emitCounter / rate);
874+
int emitCount = MIN(totalParticles - _particleCount, _emitCounter / rate);
851875
addParticles(emitCount);
852876
_emitCounter -= rate * emitCount;
853877

cocos/2d/CCParticleSystem.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,11 @@ class CC_DLL ParticleSystem : public Node, public TextureProtocol, public Playab
250250
* @js NA
251251
*/
252252
static ParticleSystem* createWithTotalParticles(int numberOfParticles);
253-
253+
254+
/** Gets all ParticleSystem references
255+
*/
256+
static Vector<ParticleSystem*>& getAllParticleSystems();
257+
public:
254258
void addParticles(int count);
255259

256260
void stopSystem();
@@ -815,6 +819,13 @@ class CC_DLL ParticleSystem : public Node, public TextureProtocol, public Playab
815819

816820
protected:
817821
virtual void updateBlendFunc();
822+
823+
private:
824+
friend class EngineDataManager;
825+
/** Internal use only, it's used by EngineDataManager class for Android platform */
826+
static void setTotalParticleCountFactor(float factor);
827+
828+
protected:
818829

819830
/** whether or not the particles are using blend additive.
820831
If enabled, the following blending function will be used.
@@ -904,6 +915,9 @@ class CC_DLL ParticleSystem : public Node, public TextureProtocol, public Playab
904915

905916
/** Quantity of particles that are being simulated at the moment */
906917
int _particleCount;
918+
/** The factor affects the total particle count, its value should be 0.0f ~ 1.0f, default 1.0f*/
919+
static float __totalParticleCountFactor;
920+
907921
/** How many seconds the emitter will run. -1 means 'forever' */
908922
float _duration;
909923
/** sourcePosition of the emitter */
@@ -971,6 +985,8 @@ class CC_DLL ParticleSystem : public Node, public TextureProtocol, public Playab
971985
/** is the emitter paused */
972986
bool _paused;
973987

988+
static Vector<ParticleSystem*> __allInstances;
989+
974990
private:
975991
CC_DISALLOW_COPY_AND_ASSIGN(ParticleSystem);
976992
};

cocos/audio/AudioEngine.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ std::unordered_map<int, AudioEngine::AudioInfo> AudioEngine::_audioIDInfoMap;
6666
AudioEngineImpl* AudioEngine::_audioEngineImpl = nullptr;
6767

6868
AudioEngine::AudioEngineThreadPool* AudioEngine::s_threadPool = nullptr;
69+
bool AudioEngine::_isEnabled = true;
6970

7071
AudioEngine::AudioInfo::AudioInfo()
7172
: filePath(nullptr)
@@ -190,6 +191,11 @@ int AudioEngine::play2d(const std::string& filePath, bool loop, float volume, co
190191
int ret = AudioEngine::INVALID_AUDIO_ID;
191192

192193
do {
194+
if (!isEnabled())
195+
{
196+
break;
197+
}
198+
193199
if ( !lazyInit() ){
194200
break;
195201
}
@@ -525,6 +531,12 @@ AudioProfile* AudioEngine::getProfile(const std::string &name)
525531

526532
void AudioEngine::preload(const std::string& filePath, std::function<void(bool isSuccess)> callback)
527533
{
534+
if (!isEnabled())
535+
{
536+
callback(false);
537+
return;
538+
}
539+
528540
lazyInit();
529541

530542
if (_audioEngineImpl)
@@ -550,3 +562,27 @@ void AudioEngine::addTask(const std::function<void()>& task)
550562
s_threadPool->addTask(task);
551563
}
552564
}
565+
566+
int AudioEngine::getPlayingAudioCount()
567+
{
568+
return static_cast<int>(_audioIDInfoMap.size());
569+
}
570+
571+
void AudioEngine::setEnabled(bool isEnabled)
572+
{
573+
if (_isEnabled != isEnabled)
574+
{
575+
_isEnabled = isEnabled;
576+
577+
if (!_isEnabled)
578+
{
579+
stopAll();
580+
}
581+
}
582+
}
583+
584+
bool AudioEngine::isEnabled()
585+
{
586+
return _isEnabled;
587+
}
588+

cocos/audio/include/AudioEngine.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,21 @@ class EXPORT_DLL AudioEngine
292292
*/
293293
static void preload(const std::string& filePath, std::function<void(bool isSuccess)> callback);
294294

295+
/**
296+
* Gets playing audio count.
297+
*/
298+
static int getPlayingAudioCount();
299+
300+
/**
301+
* Whether to enable playing audios
302+
* @note If it's disabled, current playing audios will be stopped and the later 'preload', 'play2d' methods will take no effects.
303+
*/
304+
static void setEnabled(bool isEnabled);
305+
/**
306+
* Check whether AudioEngine is enabled.
307+
*/
308+
static bool isEnabled();
309+
295310
protected:
296311
static void addTask(const std::function<void()>& task);
297312
static void remove(int audioID);
@@ -348,6 +363,8 @@ class EXPORT_DLL AudioEngine
348363
class AudioEngineThreadPool;
349364
static AudioEngineThreadPool* s_threadPool;
350365

366+
static bool _isEnabled;
367+
351368
friend class AudioEngineImpl;
352369
};
353370

0 commit comments

Comments
 (0)