From c95cbb1774c1af55336780bb5ffca95da985d0b4 Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 11 Jun 2012 10:59:57 +0800 Subject: [PATCH] issue #1310: Used CCLabelAtlas instead of CCLabelBMFont to display FPS,SPF status. Updated CCFileUtils, added some method, such as sharedFileUtils/purgeFileUtils/purgeCachedEntries. Fixed a bug in CCDirector, made retina mode work correctly. --- cocos2dx/CCDirector.cpp | 78 ++++++++++++++----- cocos2dx/CCDirector.h | 21 +++-- cocos2dx/platform/CCFileUtils.h | 2 + cocos2dx/platform/android/CCFileUtils.cpp | 30 ++++++- cocos2dx/platform/ios/CCFileUtils.mm | 26 +++++++ cocos2dx/platform/win32/CCFileUtils.cpp | 10 +++ .../DrawPrimitivesTest/DrawPrimitivesTest.cpp | 3 + tests/tests/ParticleTest/ParticleTest.cpp | 2 +- .../PerformanceParticleTest.cpp | 2 +- 9 files changed, 145 insertions(+), 29 deletions(-) diff --git a/cocos2dx/CCDirector.cpp b/cocos2dx/CCDirector.cpp index 5c840fded0..1c7c589d1f 100644 --- a/cocos2dx/CCDirector.cpp +++ b/cocos2dx/CCDirector.cpp @@ -120,7 +120,7 @@ bool CCDirector::init(void) // paused ? m_bPaused = false; - + // purge ? m_bPurgeDirecotorInNextLoop = false; @@ -267,7 +267,7 @@ void CCDirector::calculateDeltaTime(void) return; } - // new delta time + // new delta time. Re-fixed issue #1277 if (m_bNextDeltaTimeZero) { m_fDeltaTime = 0; @@ -417,11 +417,12 @@ void CCDirector::purgeCachedData(void) { CCLabelBMFont::purgeCachedData(); CCTextureCache::sharedTextureCache()->removeUnusedTextures(); + CCFileUtils::sharedFileUtils()->purgeCachedEntries(); } float CCDirector::getZEye(void) { - return (m_obWinSizeInPixels.height / 1.1566f); + return (m_obWinSizeInPixels.height / 1.1566f / CC_CONTENT_SCALE_FACTOR()); } void CCDirector::setAlphaBlending(bool bOn) @@ -546,6 +547,35 @@ void CCDirector::popScene(void) } } +void CCDirector::popToRootScene(void) +{ + CCAssert(m_pRunningScene != NULL, "A running Scene is needed"); + unsigned int c = m_pobScenesStack->count(); + + if (c == 1) + { + m_pobScenesStack->removeLastObject(); + this->end(); + } + else + { + while (c > 1) + { + CCScene *current = (CCScene*)m_pobScenesStack->lastObject(); + if( current->getIsRunning() ) + { + current->onExit(); + } + current->cleanup(); + + m_pobScenesStack->removeLastObject(); + c--; + } + m_pNextScene = (CCScene*)m_pobScenesStack->lastObject(); + m_bSendCleanupToScene = false; + } +} + void CCDirector::end() { m_bPurgeDirecotorInNextLoop = true; @@ -588,6 +618,7 @@ void CCDirector::purgeDirector() CCSpriteFrameCache::purgeSharedSpriteFrameCache(); CCTextureCache::purgeSharedTextureCache(); CCShaderCache::purgeSharedShaderCache(); + CCFileUtils::purgeFileUtils(); CCConfiguration::purgeConfiguration(); // cocos2d-x specific data structures @@ -719,21 +750,32 @@ void CCDirector::calculateMPF() void CCDirector::createStatsLabel() { - CC_SAFE_RELEASE_NULL(m_pFPSLabel); - CC_SAFE_RELEASE_NULL(m_pSPFLabel); - CC_SAFE_RELEASE_NULL(m_pDrawsLabel); - - m_pFPSLabel = CCLabelBMFont::labelWithString("00.0", "fps_images.fnt"); - m_pSPFLabel = CCLabelBMFont::labelWithString("0.000", "fps_images.fnt"); - m_pDrawsLabel = CCLabelBMFont::labelWithString("000", "fps_images.fnt"); - - m_pFPSLabel->retain(); - m_pSPFLabel->retain(); - m_pDrawsLabel->retain(); - - m_pDrawsLabel->setPosition(ccp(20, 50)); - m_pSPFLabel->setPosition(ccp(25, 30)); - m_pFPSLabel->setPosition(ccp(20, 10)); + if( m_pFPSLabel && m_pSPFLabel ) + { + CCTexture2D *texture = m_pFPSLabel->getTexture(); + + CC_SAFE_RELEASE_NULL(m_pFPSLabel); + CC_SAFE_RELEASE_NULL(m_pSPFLabel); + CC_SAFE_RELEASE_NULL(m_pDrawsLabel); + CCTextureCache::sharedTextureCache()->removeTexture(texture); + + CCFileUtils::sharedFileUtils()->purgeCachedEntries(); + } + + CCTexture2DPixelFormat currentFormat = CCTexture2D::defaultAlphaPixelFormat(); + CCTexture2D::setDefaultAlphaPixelFormat(kCCTexture2DPixelFormat_RGBA4444); + m_pFPSLabel = new CCLabelAtlas(); + m_pFPSLabel->initWithString("00.0", "fps_images.png", 12, 32, '.'); + m_pSPFLabel = new CCLabelAtlas(); + m_pSPFLabel->initWithString("0.000", "fps_images.png", 12, 32, '.'); + m_pDrawsLabel = new CCLabelAtlas(); + m_pDrawsLabel->initWithString("000", "fps_images.png", 12, 32, '.'); + + CCTexture2D::setDefaultAlphaPixelFormat(currentFormat); + + m_pDrawsLabel->setPosition( ccpAdd( ccp(0,34), CC_DIRECTOR_STATS_POSITION ) ); + m_pSPFLabel->setPosition( ccpAdd( ccp(0,17), CC_DIRECTOR_STATS_POSITION ) ); + m_pFPSLabel->setPosition( CC_DIRECTOR_STATS_POSITION ); } diff --git a/cocos2dx/CCDirector.h b/cocos2dx/CCDirector.h index 9b63a63aea..ff53a42da1 100644 --- a/cocos2dx/CCDirector.h +++ b/cocos2dx/CCDirector.h @@ -55,7 +55,7 @@ typedef enum { } ccDirectorProjection; /* Forward declarations. */ -class CCLabelBMFont; +class CCLabelAtlas; class CCScene; class CCEGLView; class CCDirectorDelegate; @@ -120,7 +120,7 @@ public: /** Whether or not the Director is paused */ inline bool isPaused(void) { return m_bPaused; } - + /** How many frames were called since the director started */ inline unsigned int getFrames(void) { return m_uFrames; } @@ -200,6 +200,13 @@ public: */ void popScene(void); + /**Pops out all scenes from the queue until the root scene in the queue. + * This scene will replace the running one. + * The running scene will be deleted. If there are no more scenes in the stack the execution is terminated. + * ONLY call it if there is a running scene. + */ + void popToRootScene(void); + /** Replaces the running scene with a new one. The running scene is terminated. * ONLY call it if there is a running scene. */ @@ -332,13 +339,13 @@ protected: float m_fAccumDt; float m_fFrameRate; - CCLabelBMFont *m_pFPSLabel; - CCLabelBMFont *m_pSPFLabel; - CCLabelBMFont *m_pDrawsLabel; + CCLabelAtlas *m_pFPSLabel; + CCLabelAtlas *m_pSPFLabel; + CCLabelAtlas *m_pDrawsLabel; - /* is the running scene paused */ + /** Whether or not the Director is paused */ bool m_bPaused; - + /* How many frames were called since the director started */ unsigned int m_uTotalFrames; unsigned int m_uFrames; diff --git a/cocos2dx/platform/CCFileUtils.h b/cocos2dx/platform/CCFileUtils.h index b4e83fde1d..9bf5931574 100644 --- a/cocos2dx/platform/CCFileUtils.h +++ b/cocos2dx/platform/CCFileUtils.h @@ -36,6 +36,8 @@ class CC_DLL CCFileUtils public: static CCFileUtils* sharedFileUtils(); static void purgeFileUtils(); + + void purgeCachedEntries(); /** @brief Get resource file data @param[in] pszFileName The resource file name which contain the path diff --git a/cocos2dx/platform/android/CCFileUtils.cpp b/cocos2dx/platform/android/CCFileUtils.cpp index 4d2d437e9b..60c6df25b2 100644 --- a/cocos2dx/platform/android/CCFileUtils.cpp +++ b/cocos2dx/platform/android/CCFileUtils.cpp @@ -25,16 +25,42 @@ THE SOFTWARE. #define __CC_PLATFORM_FILEUTILS_CPP__ #include "CCFileUtilsCommon_cpp.h" +using namespace std; + NS_CC_BEGIN #include "CCCommon.h" #include "jni/SystemInfoJni.h" -using namespace std; - // record the resource path static string s_strResourcePath = ""; +static CCFileUtils* s_pFileUtils = NULL; + +CCFileUtils* CCFileUtils::sharedFileUtils() +{ + if (s_pFileUtils == NULL) + { + s_pFileUtils = new CCFileUtils(); + } + return s_pFileUtils; +} + +void CCFileUtils::purgeFileUtils() +{ + if (s_pFileUtils != NULL) + { + s_pFileUtils->purgeCachedEntries(); + } + + CC_SAFE_DELETE(s_pFileUtils); +} + +void CCFileUtils::purgeCachedEntries() +{ + +} + /* * This function is implemented for jni to set apk path. */ diff --git a/cocos2dx/platform/ios/CCFileUtils.mm b/cocos2dx/platform/ios/CCFileUtils.mm index 2cc27f59af..a738994dbf 100644 --- a/cocos2dx/platform/ios/CCFileUtils.mm +++ b/cocos2dx/platform/ios/CCFileUtils.mm @@ -212,6 +212,32 @@ static void static_addValueToCCDict(id key, id value, CCDictionary* pDict) NS_CC_BEGIN +static CCFileUtils* s_pFileUtils = NULL; + +CCFileUtils* CCFileUtils::sharedFileUtils() +{ + if (s_pFileUtils == NULL) + { + s_pFileUtils = new CCFileUtils(); + } + return s_pFileUtils; +} + +void CCFileUtils::purgeFileUtils() +{ + if (s_pFileUtils != NULL) + { + s_pFileUtils->purgeCachedEntries(); + } + + CC_SAFE_DELETE(s_pFileUtils); +} + +void CCFileUtils::purgeCachedEntries() +{ + +} + void CCFileUtils::setResourcePath(const char *pszResourcePath) { assert(0); diff --git a/cocos2dx/platform/win32/CCFileUtils.cpp b/cocos2dx/platform/win32/CCFileUtils.cpp index 33887eb046..73105bc103 100644 --- a/cocos2dx/platform/win32/CCFileUtils.cpp +++ b/cocos2dx/platform/win32/CCFileUtils.cpp @@ -62,9 +62,19 @@ CCFileUtils* CCFileUtils::sharedFileUtils() void CCFileUtils::purgeFileUtils() { + if (s_pFileUtils != NULL) + { + s_pFileUtils->purgeCachedEntries(); + } + CC_SAFE_DELETE(s_pFileUtils); } +void CCFileUtils::purgeCachedEntries() +{ + +} + void CCFileUtils::setResourcePath(const char *pszResourcePath) { CCAssert(pszResourcePath != NULL, "[FileUtils setResourcePath] -- wrong resource path"); diff --git a/tests/tests/DrawPrimitivesTest/DrawPrimitivesTest.cpp b/tests/tests/DrawPrimitivesTest/DrawPrimitivesTest.cpp index 33bbedb155..67d30e156f 100644 --- a/tests/tests/DrawPrimitivesTest/DrawPrimitivesTest.cpp +++ b/tests/tests/DrawPrimitivesTest/DrawPrimitivesTest.cpp @@ -90,6 +90,9 @@ void DrawPrimitivesTest::draw() ccDrawCubicBezier(ccp(s.width/2, s.height/2), ccp(s.width/2+30,s.height/2+50), ccp(s.width/2+60,s.height/2-50),ccp(s.width, s.height/2),100); CHECK_GL_ERROR_DEBUG(); + //draw a solid polygon + CCPoint vertices3[] = {ccp(60,160), ccp(70,190), ccp(100,190), ccp(90,160)}; + ccDrawSolidPoly( vertices3, 4, ccc4f(1,1,0,1) ); // restore original values glLineWidth(1); diff --git a/tests/tests/ParticleTest/ParticleTest.cpp b/tests/tests/ParticleTest/ParticleTest.cpp index cc2d44a15b..aa63850ac8 100644 --- a/tests/tests/ParticleTest/ParticleTest.cpp +++ b/tests/tests/ParticleTest/ParticleTest.cpp @@ -1104,7 +1104,7 @@ void ParticleDemo::onEnter(void) addChild( menu, 100 ); - CCLabelAtlas* labelAtlas = CCLabelAtlas::labelWithString("0000", "fonts/fps_images.png", 16, 24, '.'); + CCLabelAtlas* labelAtlas = CCLabelAtlas::labelWithString("0000", "fps_images.png", 12, 32, '.'); addChild(labelAtlas, 100, kTagParticleCount); labelAtlas->setPosition(CCPointMake(s.width-66,50)); diff --git a/tests/tests/PerformanceTest/PerformanceParticleTest.cpp b/tests/tests/PerformanceTest/PerformanceParticleTest.cpp index 0701cb82a5..fec426fcf6 100644 --- a/tests/tests/PerformanceTest/PerformanceParticleTest.cpp +++ b/tests/tests/PerformanceTest/PerformanceParticleTest.cpp @@ -95,7 +95,7 @@ void ParticleMainScene::initWithSubTest(int asubtest, int particles) addChild(infoLabel, 1, kTagInfoLayer); // particles on stage - CCLabelAtlas *labelAtlas = CCLabelAtlas::labelWithString("0000", "fonts/fps_images.png", 16, 24, '.'); + CCLabelAtlas *labelAtlas = CCLabelAtlas::labelWithString("0000", "fps_images.png", 12, 32, '.'); addChild(labelAtlas, 0, kTagLabelAtlas); labelAtlas->setPosition(ccp(s.width-66,50));