From cb75465913393c52df0447b39f0db825470c2417 Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 17 Nov 2011 10:10:13 +0800 Subject: [PATCH] issue #854: add test case for CCTextureCache --- cocos2dx/textures/CCTextureCache.cpp | 2 +- tests/test.android/jni/tests/Android.mk | 1 + tests/test.win32/test.win32.vcproj | 12 ++ .../TextureCacheTest/TextureCacheTest.cpp | 144 ++++++++++++++++++ .../tests/TextureCacheTest/TextureCacheTest.h | 28 ++++ tests/tests/controller.cpp | 2 +- tests/tests/tests.h | 3 + 7 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 tests/tests/TextureCacheTest/TextureCacheTest.cpp create mode 100644 tests/tests/TextureCacheTest/TextureCacheTest.h diff --git a/cocos2dx/textures/CCTextureCache.cpp b/cocos2dx/textures/CCTextureCache.cpp index 08fd22950c..8931a7078f 100644 --- a/cocos2dx/textures/CCTextureCache.cpp +++ b/cocos2dx/textures/CCTextureCache.cpp @@ -69,7 +69,7 @@ static void* loadImage(void* data) s_pAsyncObject = (AsyncStruct*)data; const char *filename = s_pAsyncObject->filename.c_str(); - CCLOG("thread 0x%x is loading image %s", pthread_self(), (void*)filename); + CCLOG("thread 0x%x is loading image %s", pthread_self(), filename); s_pImageAsync = new CCImage(); s_pImageAsync->initWithImageFile(filename); diff --git a/tests/test.android/jni/tests/Android.mk b/tests/test.android/jni/tests/Android.mk index 9467aa74e7..a6d356ff83 100644 --- a/tests/test.android/jni/tests/Android.mk +++ b/tests/test.android/jni/tests/Android.mk @@ -76,6 +76,7 @@ LOCAL_SRC_FILES := main.cpp \ ../../../tests/SceneTest/SceneTest.cpp \ ../../../tests/SchedulerTest/SchedulerTest.cpp \ ../../../tests/SpriteTest/SpriteTest.cpp \ +../../../tests/TextureCacheTest/TextureCacheTest.cpp \ ../../../tests/Texture2dTest/Texture2dTest.cpp \ ../../../tests/TileMapTest/TileMapTest.cpp \ ../../../tests/TouchesTest/Ball.cpp \ diff --git a/tests/test.win32/test.win32.vcproj b/tests/test.win32/test.win32.vcproj index 4ca3dacf97..92a3a0a9ef 100644 --- a/tests/test.win32/test.win32.vcproj +++ b/tests/test.win32/test.win32.vcproj @@ -1107,6 +1107,18 @@ > + + + + + + diff --git a/tests/tests/TextureCacheTest/TextureCacheTest.cpp b/tests/tests/TextureCacheTest/TextureCacheTest.cpp new file mode 100644 index 0000000000..2584cab2ec --- /dev/null +++ b/tests/tests/TextureCacheTest/TextureCacheTest.cpp @@ -0,0 +1,144 @@ +// enable log +#define COCOS2D_DEBUG 1 + +#include "TextureCacheTest.h" + +TextureCacheTest::TextureCacheTest() +: m_nNumberOfSprites(24) +, m_nNumberOfLoadedSprites(1) +{ + CCSize size = CCDirector::sharedDirector()->getWinSize(); + + m_pLabelLoading = CCLabelTTF::labelWithString("loading...", "Arial", 15); + m_pLabelPercent = CCLabelTTF::labelWithString("%0", "Arial", 15); + + m_pLabelLoading->setPosition(CCPointMake(size.width / 2, size.height / 2 - 20)); + m_pLabelPercent->setPosition(CCPointMake(size.width / 2, size.height / 2 + 20)); + + this->addChild(m_pLabelLoading); + this->addChild(m_pLabelPercent); + + // load textrues + CCTextureCache::sharedTextureCache()->addImageAsync("Images/HelloWorld.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_01.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_02.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_03.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_04.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_05.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_06.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_07.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_08.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_09.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_10.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_11.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_12.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_13.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_14.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/background1.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/background2.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/background3.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/background1-hd.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/background2-hd.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/background3-hd.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/blocks.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); + CCTextureCache::sharedTextureCache()->addImageAsync("Images/blocks-hd.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); +} + +void TextureCacheTest::loadingCallBack(CCObject *obj) +{ + ++m_nNumberOfLoadedSprites; + char tmp[10]; + //sprintf(tmp,"%%d", ((float)m_nNumberOfLoadedSprites / m_nNumberOfSprites) * 100); + sprintf(tmp,"%d", m_nNumberOfLoadedSprites); + m_pLabelPercent->setString(tmp); + + if (m_nNumberOfLoadedSprites == m_nNumberOfSprites) + { + this->removeChild(m_pLabelLoading, true); + this->removeChild(m_pLabelPercent, true); + addSprite(); + } +} + +void TextureCacheTest::addSprite() +{ + CCSize size = CCDirector::sharedDirector()->getWinSize(); + + // create sprites + + CCSprite *bg = CCSprite::spriteWithFile("Images/HelloWorld.png"); + bg->setPosition(CCPointMake(size.width / 2, size.height / 2)); + + CCSprite *s1 = CCSprite::spriteWithFile("Images/grossini.png"); + CCSprite *s2 = CCSprite::spriteWithFile("Images/grossini_dance_01.png"); + CCSprite *s3 = CCSprite::spriteWithFile("Images/grossini_dance_02.png"); + CCSprite *s4 = CCSprite::spriteWithFile("Images/grossini_dance_03.png"); + CCSprite *s5 = CCSprite::spriteWithFile("Images/grossini_dance_04.png"); + CCSprite *s6 = CCSprite::spriteWithFile("Images/grossini_dance_05.png"); + CCSprite *s7 = CCSprite::spriteWithFile("Images/grossini_dance_06.png"); + CCSprite *s8 = CCSprite::spriteWithFile("Images/grossini_dance_07.png"); + CCSprite *s9 = CCSprite::spriteWithFile("Images/grossini_dance_08.png"); + CCSprite *s10 = CCSprite::spriteWithFile("Images/grossini_dance_09.png"); + CCSprite *s11 = CCSprite::spriteWithFile("Images/grossini_dance_10.png"); + CCSprite *s12 = CCSprite::spriteWithFile("Images/grossini_dance_11.png"); + CCSprite *s13 = CCSprite::spriteWithFile("Images/grossini_dance_12.png"); + CCSprite *s14 = CCSprite::spriteWithFile("Images/grossini_dance_13.png"); + CCSprite *s15 = CCSprite::spriteWithFile("Images/grossini_dance_14.png"); + + // just loading textures to slow down + CCSprite *s16 = CCSprite::spriteWithFile("Images/background1.png"); + CCSprite *s17 = CCSprite::spriteWithFile("Images/background2.png"); + CCSprite *s18 = CCSprite::spriteWithFile("Images/background3.png"); + CCSprite *s19 = CCSprite::spriteWithFile("Images/background1-hd.png"); + CCSprite *s20 = CCSprite::spriteWithFile("Images/background2-hd.png"); + CCSprite *s21 = CCSprite::spriteWithFile("Images/background3-hd.png"); + CCSprite *s22 = CCSprite::spriteWithFile("Images/blocks.png"); + CCSprite *s23 = CCSprite::spriteWithFile("Images/blocks-hd.png"); + + s1->setPosition(CCPointMake(50, 50)); + s2->setPosition(CCPointMake(60, 50)); + s3->setPosition(CCPointMake(70, 50)); + s4->setPosition(CCPointMake(80, 50)); + s5->setPosition(CCPointMake(90, 50)); + s6->setPosition(CCPointMake(100, 50)); + + s7->setPosition(CCPointMake(50, 180)); + s8->setPosition(CCPointMake(60, 180)); + s9->setPosition(CCPointMake(70, 180)); + s10->setPosition(CCPointMake(80, 180)); + s11->setPosition(CCPointMake(90, 180)); + s12->setPosition(CCPointMake(100, 180)); + + s13->setPosition(CCPointMake(50, 270)); + s14->setPosition(CCPointMake(60, 270)); + s15->setPosition(CCPointMake(70, 270)); + + this->addChild(bg); + + this->addChild(s1); + this->addChild(s2); + this->addChild(s3); + this->addChild(s4); + this->addChild(s5); + this->addChild(s6); + this->addChild(s7); + this->addChild(s8); + this->addChild(s9); + this->addChild(s10); + this->addChild(s11); + this->addChild(s12); + this->addChild(s13); + this->addChild(s14); + this->addChild(s15); +} + + +void TextureCacheTestScene::runThisTest() +{ + CCLayer* pLayer = new TextureCacheTest(); + addChild(pLayer); + + CCDirector::sharedDirector()->replaceScene(this); + pLayer->release(); +} diff --git a/tests/tests/TextureCacheTest/TextureCacheTest.h b/tests/tests/TextureCacheTest/TextureCacheTest.h new file mode 100644 index 0000000000..086fcce1f9 --- /dev/null +++ b/tests/tests/TextureCacheTest/TextureCacheTest.h @@ -0,0 +1,28 @@ +#ifndef _TEXTURECACHE_TEST_H_ +#define _TEXTURECACHE_TEST_H_ + +#include "cocos2d.h" +#include "../testBasic.h" + +class TextureCacheTest : public CCLayer +{ +public: + TextureCacheTest(); + void addSprite(); + void loadingCallBack(cocos2d::CCObject *obj); + +private: + cocos2d::CCLabelTTF *m_pLabelLoading; + cocos2d::CCLabelTTF *m_pLabelPercent; + int m_nNumberOfSprites; + int m_nNumberOfLoadedSprites; + +}; + +class TextureCacheTestScene : public TestScene +{ +public: + virtual void runThisTest(); +}; + +#endif // _TEXTURECACHE_TEST_H_ diff --git a/tests/tests/controller.cpp b/tests/tests/controller.cpp index eac8212fec..6c65d7bc0c 100644 --- a/tests/tests/controller.cpp +++ b/tests/tests/controller.cpp @@ -111,7 +111,7 @@ static TestScene* CreateTestScene(int nIdx) pScene = new FontTestScene(); break; case TEST_CURRENT_LANGUAGE: pScene = new CurrentLanguageTestScene(); break; - break; + case TEST_TEXTURECACHE: pScene = new TextureCacheTestScene(); break; #endif default: diff --git a/tests/tests/tests.h b/tests/tests/tests.h index c4ee2fa49a..c62f0e466c 100644 --- a/tests/tests/tests.h +++ b/tests/tests/tests.h @@ -44,6 +44,7 @@ #include "Texture2dTest/Texture2dTest.h" #include "FontTest/FontTest.h" #include "CurrentLanguageTest/CurrentLanguageTest.h" +#include "TextureCacheTest/TextureCacheTest.h" #if (CC_TARGET_PLATFORM != CC_PLATFORM_AIRPLAY) #include "ChipmunkTest/cocos2dChipmunkDemo.h" @@ -98,6 +99,7 @@ enum TEST_BUGS, TEST_FONTS, TEST_CURRENT_LANGUAGE, + TEST_TEXTURECACHE, TESTS_COUNT, }; @@ -144,6 +146,7 @@ const std::string g_aTestNames[TESTS_COUNT] = { "BugsTest", "FontTest", "CurrentLanguageTest", + "TextureCacheTest" }; #endif