Skip to content

Commit 05a8449

Browse files
author
minggo
committed
fixed cocos2d#1185: reload image data and recreate buffer of object when backing to foreground
1 parent 7c8a9cc commit 05a8449

File tree

21 files changed

+265
-167
lines changed

21 files changed

+265
-167
lines changed

HelloWorld/proj.android/jni/helloworld/main.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi
3838
{
3939
ccDrawInit();
4040
ccGLInvalidateStateCache();
41-
cocos2d::CCDirector::sharedDirector()->setGLDefaultValues();
41+
4242
CCShaderCache::sharedShaderCache()->reloadDefaultShaders();
4343
cocos2d::CCTextureCache::reloadAllTextures();
44+
cocos2d::CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL);
45+
cocos2d::CCDirector::sharedDirector()->setGLDefaultValues();
4446
}
4547
}
4648

cocos2dx/CCDirector.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ void CCDirector::purgeDirector()
567567

568568
// cocos2d-x specific data structures
569569
CCUserDefault::purgeSharedUserDefault();
570-
CCNotificationCenter::purgeNotifCenter();
570+
CCNotificationCenter::purgeNotificationCenter();
571571

572572
ccGLInvalidateStateCache();
573573

cocos2dx/extensions/CCNotificationCenter/CCNotificationCenter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ CCNotificationCenter::~CCNotificationCenter()
4343
m_observers->release();
4444
}
4545

46-
CCNotificationCenter *CCNotificationCenter::sharedNotifCenter(void)
46+
CCNotificationCenter *CCNotificationCenter::sharedNotificationCenter(void)
4747
{
4848
if (!s_sharedNotifCenter)
4949
{
@@ -52,7 +52,7 @@ CCNotificationCenter *CCNotificationCenter::sharedNotifCenter(void)
5252
return s_sharedNotifCenter;
5353
}
5454

55-
void CCNotificationCenter::purgeNotifCenter(void)
55+
void CCNotificationCenter::purgeNotificationCenter(void)
5656
{
5757
CC_SAFE_RELEASE_NULL(s_sharedNotifCenter);
5858
}

cocos2dx/extensions/CCNotificationCenter/CCNotificationCenter.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class CC_DLL CCNotificationCenter : public CCObject
3737
CCNotificationCenter();
3838
~CCNotificationCenter();
3939

40-
static CCNotificationCenter *sharedNotifCenter(void);
41-
static void purgeNotifCenter(void);
40+
static CCNotificationCenter *sharedNotificationCenter(void);
41+
static void purgeNotificationCenter(void);
4242

4343
void addObserver(CCObject *target,
4444
SEL_CallFuncO selector,

cocos2dx/include/CCEventType.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#ifndef __CCEVENT_TYPE_H__
3+
#define __CCEVENT_TYPE_H__
4+
5+
/**
6+
* This header is used for defining event types using in CCNotificationCenter
7+
*/
8+
9+
#define EVNET_COME_TO_FOREGROUND "event_come_to_foreground"
10+
11+
#endif // __CCEVENT_TYPE_H__

cocos2dx/include/CCParticleSystemQuad.h

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ class CC_DLL CCParticleSystemQuad : public CCParticleSystem
9292
virtual void draw();
9393
virtual void setBatchNode(CCParticleBatchNode* batchNode);
9494
virtual void setTotalParticles(unsigned int tp);
95+
96+
/** listen the event that coming to foreground on Android
97+
*/
98+
void listenBackToForeground(CCObject *obj);
9599
private:
96100
#if CC_TEXTURE_ATLAS_USE_VAO
97101
void setupVBOandVAO();

cocos2dx/include/CCTextureAtlas.h

+3
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ class CC_DLL CCTextureAtlas : public CCObject
193193
/** draws all the Atlas's Quads
194194
*/
195195
void drawQuads();
196+
/** listen the event that coming to foreground on Android
197+
*/
198+
void listenBackToForeground(CCObject *obj);
196199
private:
197200
void setupIndices();
198201
void mapBuffers();

cocos2dx/include/CCTextureCache.h

+9
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ typedef enum {
175175
kImageFile,
176176
kImageData,
177177
kString,
178+
kImage,
178179
}ccCachedImageType;
179180

180181
public:
@@ -184,16 +185,24 @@ typedef enum {
184185
static void addImageTexture(CCTexture2D *tt, const char* imageFileName, CCImage::EImageFormat format);
185186
static void addStringTexture(CCTexture2D *tt, const char* text, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize);
186187
static void addDataTexture(CCTexture2D *tt, void* data, CCTexture2DPixelFormat pixelFormat, const CCSize& contentSize);
188+
static void addCCImage(CCTexture2D *tt, CCImage *image);
187189

188190
static void removeTexture(CCTexture2D *t);
189191
static void reloadAllTextures();
190192

191193
public:
192194
static std::list<VolatileTexture*> textures;
193195
static bool isReloading;
196+
197+
private:
198+
// find VolatileTexture by CCTexture2D*
199+
// if not found, create a new one
200+
static VolatileTexture* findVolotileTexture(CCTexture2D *tt);
194201

195202
protected:
196203
CCTexture2D *texture;
204+
205+
CCImage *uiImage;
197206

198207
ccCachedImageType m_eCashedImageType;
199208

cocos2dx/include/ccMacros.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ It should work same as apples CFSwapInt32LittleToHost(..)
225225
do { \
226226
GLenum __error = glGetError(); \
227227
if(__error) { \
228-
CCLog("OpenGL error 0x%04X in %s %d\n", __error, __FUNCTION__, __LINE__); \
228+
CCLog("OpenGL error 0x%04X in %s %s %d\n", __error, __FILE__, __FUNCTION__, __LINE__); \
229229
} \
230230
} while (false)
231231
#endif

cocos2dx/particle_nodes/CCParticleSystem.cpp

+62-54
Original file line numberDiff line numberDiff line change
@@ -269,62 +269,61 @@ bool CCParticleSystem::initWithDictionary(CCDictionary *dictionary)
269269
//don't get the internal texture if a batchNode is used
270270
if (!m_pBatchNode)
271271
{
272-
// texture
273-
// Try to get the texture from the cache
274-
const char* textureName = dictionary->valueForKey("textureFileName")->getCString();
275-
std::string fullpath = CCFileUtils::fullPathFromRelativeFile(textureName, m_sPlistFile.c_str());
276-
277-
CCTexture2D *tex = NULL;
278-
279-
if (strlen(textureName) > 0)
280-
{
281-
// set not pop-up message box when load image failed
282-
bool bNotify = CCFileUtils::getIsPopupNotify();
283-
CCFileUtils::setIsPopupNotify(false);
284-
tex = CCTextureCache::sharedTextureCache()->addImage(fullpath.c_str());
285-
286-
// reset the value of UIImage notify
287-
CCFileUtils::setIsPopupNotify(bNotify);
288-
}
289-
290-
if (tex)
291-
{
292-
setTexture(tex);
293-
}
294-
else
295-
{
296-
const char *textureData = dictionary->valueForKey("textureImageData")->getCString();
297-
CCAssert(textureData, "");
298-
299-
int dataLen = strlen(textureData);
300-
if(dataLen != 0)
272+
// texture
273+
// Try to get the texture from the cache
274+
const char* textureName = dictionary->valueForKey("textureFileName")->getCString();
275+
std::string fullpath = CCFileUtils::fullPathFromRelativeFile(textureName, m_sPlistFile.c_str());
276+
277+
CCTexture2D *tex = NULL;
278+
279+
if (strlen(textureName) > 0)
301280
{
302-
// if it fails, try to get it from the base64-gzipped data
303-
int decodeLen = base64Decode((unsigned char*)textureData, (unsigned int)dataLen, &buffer);
304-
CCAssert( buffer != NULL, "CCParticleSystem: error decoding textureImageData");
305-
CC_BREAK_IF(!buffer);
306-
281+
// set not pop-up message box when load image failed
282+
bool bNotify = CCFileUtils::getIsPopupNotify();
283+
CCFileUtils::setIsPopupNotify(false);
284+
tex = CCTextureCache::sharedTextureCache()->addImage(fullpath.c_str());
285+
286+
// reset the value of UIImage notify
287+
CCFileUtils::setIsPopupNotify(bNotify);
288+
}
289+
290+
if (tex)
291+
{
292+
setTexture(tex);
293+
}
294+
else
295+
{
296+
const char *textureData = dictionary->valueForKey("textureImageData")->getCString();
297+
CCAssert(textureData, "");
298+
299+
int dataLen = strlen(textureData);
300+
if(dataLen != 0)
301+
{
302+
// if it fails, try to get it from the base64-gzipped data
303+
int decodeLen = base64Decode((unsigned char*)textureData, (unsigned int)dataLen, &buffer);
304+
CCAssert( buffer != NULL, "CCParticleSystem: error decoding textureImageData");
305+
CC_BREAK_IF(!buffer);
306+
307307
int deflatedLen = ZipUtils::ccInflateMemory(buffer, decodeLen, &deflated);
308308
CCAssert( deflated != NULL, "CCParticleSystem: error ungzipping textureImageData");
309309
CC_BREAK_IF(!deflated);
310310

311+
// don't delete image, VolatileTexture use it in CCTextureCache::sharedTextureCache()->addUIImage()
311312
image = new CCImage();
312313
bool isOK = image->initWithImageData(deflated, deflatedLen);
313314
CCAssert(isOK, "CCParticleSystem: error init image with Data");
314315
CC_BREAK_IF(!isOK);
315316

316317
setTexture(CCTextureCache::sharedTextureCache()->addUIImage(image, fullpath.c_str()));
318+
}
317319
}
318-
}
319-
CCAssert( this->m_pTexture != NULL, "CCParticleSystem: error loading the texture");
320-
320+
CCAssert( this->m_pTexture != NULL, "CCParticleSystem: error loading the texture");
321321
}
322322
bRet = true;
323323
}
324324
} while (0);
325325
CC_SAFE_DELETE_ARRAY(buffer);
326326
CC_SAFE_DELETE_ARRAY(deflated);
327-
CC_SAFE_DELETE(image);
328327
return bRet;
329328
}
330329

@@ -470,7 +469,7 @@ void CCParticleSystem::initParticle(tCCParticle* particle)
470469
float a = CC_DEGREES_TO_RADIANS( m_fAngle + m_fAngleVar * CCRANDOM_MINUS1_1() );
471470

472471
// Mode Gravity: A
473-
if( m_nEmitterMode == kCCParticleModeGravity )
472+
if (m_nEmitterMode == kCCParticleModeGravity)
474473
{
475474
CCPoint v(cosf( a ), sinf( a ));
476475
float s = modeA.speed + modeA.speedVar * CCRANDOM_MINUS1_1();
@@ -488,17 +487,22 @@ void CCParticleSystem::initParticle(tCCParticle* particle)
488487
}
489488

490489
// Mode Radius: B
491-
else {
490+
else
491+
{
492492
// Set the default diameter of the particle from the source position
493493
float startRadius = modeB.startRadius + modeB.startRadiusVar * CCRANDOM_MINUS1_1();
494494
float endRadius = modeB.endRadius + modeB.endRadiusVar * CCRANDOM_MINUS1_1();
495495

496496
particle->modeB.radius = startRadius;
497497

498-
if( modeB.endRadius == kCCParticleStartRadiusEqualToEndRadius )
498+
if(modeB.endRadius == kCCParticleStartRadiusEqualToEndRadius)
499+
{
499500
particle->modeB.deltaRadius = 0;
501+
}
500502
else
503+
{
501504
particle->modeB.deltaRadius = (endRadius - startRadius) / particle->timeToLive;
505+
}
502506

503507
particle->modeB.angle = a;
504508
particle->modeB.degreesPerSecond = CC_DEGREES_TO_RADIANS(modeB.rotatePerSecond + modeB.rotatePerSecondVar * CCRANDOM_MINUS1_1());
@@ -532,7 +536,7 @@ void CCParticleSystem::update(ccTime dt)
532536
{
533537
CC_PROFILER_START_CATEGORY(kCCProfilerCategoryParticles , "CCParticleSystem - update");
534538

535-
if( m_bIsActive && m_fEmissionRate )
539+
if (m_bIsActive && m_fEmissionRate)
536540
{
537541
float rate = 1.0f / m_fEmissionRate;
538542
//issue #1201, prevent bursts of particles, due to too high emitCounter
@@ -541,14 +545,14 @@ void CCParticleSystem::update(ccTime dt)
541545
m_fEmitCounter += dt;
542546
}
543547

544-
while( m_uParticleCount < m_uTotalParticles && m_fEmitCounter > rate )
548+
while (m_uParticleCount < m_uTotalParticles && m_fEmitCounter > rate)
545549
{
546550
this->addParticle();
547551
m_fEmitCounter -= rate;
548552
}
549553

550554
m_fElapsed += dt;
551-
if(m_fDuration != -1 && m_fDuration < m_fElapsed)
555+
if (m_fDuration != -1 && m_fDuration < m_fElapsed)
552556
{
553557
this->stopSystem();
554558
}
@@ -557,35 +561,37 @@ void CCParticleSystem::update(ccTime dt)
557561
m_uParticleIdx = 0;
558562

559563
CCPoint currentPosition = CCPointZero;
560-
if( m_ePositionType == kCCPositionTypeFree )
564+
if (m_ePositionType == kCCPositionTypeFree)
561565
{
562566
currentPosition = this->convertToWorldSpace(CCPointZero);
563567
}
564-
else if ( m_ePositionType == kCCPositionTypeRelative )
568+
else if (m_ePositionType == kCCPositionTypeRelative)
565569
{
566570
currentPosition = m_tPosition;
567571
}
568572

569573
if (m_bIsVisible)
570574
{
571-
while( m_uParticleIdx < m_uParticleCount )
575+
while (m_uParticleIdx < m_uParticleCount)
572576
{
573577
tCCParticle *p = &m_pParticles[m_uParticleIdx];
574578

575579
// life
576580
p->timeToLive -= dt;
577581

578-
if( p->timeToLive > 0 )
582+
if (p->timeToLive > 0)
579583
{
580584
// Mode A: gravity, direction, tangential accel & radial accel
581-
if( m_nEmitterMode == kCCParticleModeGravity )
585+
if (m_nEmitterMode == kCCParticleModeGravity)
582586
{
583587
CCPoint tmp, radial, tangential;
584588

585589
radial = CCPointZero;
586590
// radial acceleration
587-
if(p->pos.x || p->pos.y)
591+
if (p->pos.x || p->pos.y)
592+
{
588593
radial = ccpNormalize(p->pos);
594+
}
589595
tangential = radial;
590596
radial = ccpMult(radial, p->modeA.radialAccel);
591597

@@ -604,7 +610,8 @@ void CCParticleSystem::update(ccTime dt)
604610
}
605611

606612
// Mode B: radius movement
607-
else {
613+
else
614+
{
608615
// Update the angle and radius of the particle.
609616
p->modeB.angle += p->modeB.degreesPerSecond * dt;
610617
p->modeB.radius += p->modeB.deltaRadius * dt;
@@ -632,7 +639,7 @@ void CCParticleSystem::update(ccTime dt)
632639

633640
CCPoint newPos;
634641

635-
if( m_ePositionType == kCCPositionTypeFree || m_ePositionType == kCCPositionTypeRelative )
642+
if (m_ePositionType == kCCPositionTypeFree || m_ePositionType == kCCPositionTypeRelative)
636643
{
637644
CCPoint diff = ccpSub( currentPosition, p->startPos );
638645
newPos = ccpSub(p->pos, diff);
@@ -655,7 +662,6 @@ void CCParticleSystem::update(ccTime dt)
655662

656663
// update particle counter
657664
++m_uParticleIdx;
658-
659665
}
660666
else
661667
{
@@ -687,8 +693,10 @@ void CCParticleSystem::update(ccTime dt)
687693
} //while
688694
m_bTransformSystemDirty = false;
689695
}
690-
if (!m_pBatchNode)
696+
if (! m_pBatchNode)
697+
{
691698
postStep();
699+
}
692700

693701
CC_PROFILER_STOP_CATEGORY(kCCProfilerCategoryParticles , "CCParticleSystem - update");
694702
}

0 commit comments

Comments
 (0)