2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-11-17 10:10:13 +08:00
|
|
|
#include "TextureCacheTest.h"
|
|
|
|
|
2012-06-14 05:19:13 +08:00
|
|
|
USING_NS_CC;
|
2011-11-21 12:04:21 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
TextureCacheTests::TextureCacheTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(TextureCacheTest);
|
2017-02-04 09:38:54 +08:00
|
|
|
ADD_TEST_CASE(TextureCacheUnbindTest);
|
2015-04-03 14:31:03 +08:00
|
|
|
}
|
|
|
|
|
2011-11-17 10:10:13 +08:00
|
|
|
TextureCacheTest::TextureCacheTest()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _numberOfSprites(20)
|
|
|
|
, _numberOfLoadedSprites(0)
|
2011-11-17 10:10:13 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto size = Director::getInstance()->getWinSize();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
_labelLoading = Label::createWithTTF("loading...", "fonts/arial.ttf", 15);
|
|
|
|
_labelPercent = Label::createWithTTF("%0", "fonts/arial.ttf", 15);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
_labelLoading->setPosition(Vec2(size.width / 2, size.height / 2 - 20));
|
|
|
|
_labelPercent->setPosition(Vec2(size.width / 2, size.height / 2 + 20));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
this->addChild(_labelLoading);
|
|
|
|
this->addChild(_labelPercent);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2017-02-10 10:35:52 +08:00
|
|
|
// load textures
|
2014-01-02 17:23:00 +08:00
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/HelloWorld.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_01.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_02.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_03.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_04.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_05.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_06.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_07.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_08.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_09.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_10.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_11.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_12.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_13.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_14.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/background1.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/background2.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/background3.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
|
|
|
Director::getInstance()->getTextureCache()->addImageAsync("Images/blocks.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
|
2011-11-17 10:10:13 +08:00
|
|
|
}
|
|
|
|
|
2014-01-02 17:23:00 +08:00
|
|
|
void TextureCacheTest::loadingCallBack(cocos2d::Texture2D *texture)
|
2011-11-17 10:10:13 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
++_numberOfLoadedSprites;
|
2012-04-19 14:35:52 +08:00
|
|
|
char tmp[10];
|
2013-06-15 14:03:30 +08:00
|
|
|
sprintf(tmp,"%%%d", (int)(((float)_numberOfLoadedSprites / _numberOfSprites) * 100));
|
|
|
|
_labelPercent->setString(tmp);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_numberOfLoadedSprites == _numberOfSprites)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
this->removeChild(_labelLoading, true);
|
|
|
|
this->removeChild(_labelPercent, true);
|
2012-04-19 14:35:52 +08:00
|
|
|
addSprite();
|
|
|
|
}
|
2011-11-17 10:10:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCacheTest::addSprite()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto size = Director::getInstance()->getWinSize();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// create sprites
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto bg = Sprite::create("Images/HelloWorld.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
bg->setPosition(Vec2(size.width / 2, size.height / 2));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s1 = Sprite::create("Images/grossini.png");
|
|
|
|
auto s2 = Sprite::create("Images/grossini_dance_01.png");
|
|
|
|
auto s3 = Sprite::create("Images/grossini_dance_02.png");
|
|
|
|
auto s4 = Sprite::create("Images/grossini_dance_03.png");
|
|
|
|
auto s5 = Sprite::create("Images/grossini_dance_04.png");
|
|
|
|
auto s6 = Sprite::create("Images/grossini_dance_05.png");
|
|
|
|
auto s7 = Sprite::create("Images/grossini_dance_06.png");
|
|
|
|
auto s8 = Sprite::create("Images/grossini_dance_07.png");
|
|
|
|
auto s9 = Sprite::create("Images/grossini_dance_08.png");
|
|
|
|
auto s10 = Sprite::create("Images/grossini_dance_09.png");
|
|
|
|
auto s11 = Sprite::create("Images/grossini_dance_10.png");
|
|
|
|
auto s12 = Sprite::create("Images/grossini_dance_11.png");
|
|
|
|
auto s13 = Sprite::create("Images/grossini_dance_12.png");
|
|
|
|
auto s14 = Sprite::create("Images/grossini_dance_13.png");
|
|
|
|
auto s15 = Sprite::create("Images/grossini_dance_14.png");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// just loading textures to slow down
|
2013-06-20 14:17:10 +08:00
|
|
|
Sprite::create("Images/background1.png");
|
|
|
|
Sprite::create("Images/background2.png");
|
|
|
|
Sprite::create("Images/background3.png");
|
|
|
|
Sprite::create("Images/blocks.png");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
s1->setPosition(Vec2(50, 50));
|
|
|
|
s2->setPosition(Vec2(60, 50));
|
|
|
|
s3->setPosition(Vec2(70, 50));
|
|
|
|
s4->setPosition(Vec2(80, 50));
|
|
|
|
s5->setPosition(Vec2(90, 50));
|
|
|
|
s6->setPosition(Vec2(100, 50));
|
|
|
|
|
|
|
|
s7->setPosition(Vec2(50, 180));
|
|
|
|
s8->setPosition(Vec2(60, 180));
|
|
|
|
s9->setPosition(Vec2(70, 180));
|
|
|
|
s10->setPosition(Vec2(80, 180));
|
|
|
|
s11->setPosition(Vec2(90, 180));
|
|
|
|
s12->setPosition(Vec2(100, 180));
|
|
|
|
|
|
|
|
s13->setPosition(Vec2(50, 270));
|
|
|
|
s14->setPosition(Vec2(60, 270));
|
|
|
|
s15->setPosition(Vec2(70, 270));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
this->addChild(bg);
|
2011-11-17 10:10:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->addChild(s1);
|
2011-11-17 10:10:13 +08:00
|
|
|
this->addChild(s2);
|
2012-04-19 14:35:52 +08:00
|
|
|
this->addChild(s3);
|
|
|
|
this->addChild(s4);
|
|
|
|
this->addChild(s5);
|
|
|
|
this->addChild(s6);
|
|
|
|
this->addChild(s7);
|
|
|
|
this->addChild(s8);
|
|
|
|
this->addChild(s9);
|
2011-11-17 10:10:13 +08:00
|
|
|
this->addChild(s10);
|
2012-04-19 14:35:52 +08:00
|
|
|
this->addChild(s11);
|
|
|
|
this->addChild(s12);
|
|
|
|
this->addChild(s13);
|
|
|
|
this->addChild(s14);
|
|
|
|
this->addChild(s15);
|
2011-11-17 10:10:13 +08:00
|
|
|
}
|
2017-02-04 09:38:54 +08:00
|
|
|
|
|
|
|
TextureCacheUnbindTest::TextureCacheUnbindTest()
|
|
|
|
{
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
Label* nothing =
|
|
|
|
Label::createWithTTF
|
|
|
|
("There should be\nnothing below", "fonts/arial.ttf", 15);
|
|
|
|
nothing->setPosition(Vec2(size.width / 4, 5 * size.height / 6));
|
|
|
|
this->addChild(nothing);
|
|
|
|
|
|
|
|
Label* something =
|
|
|
|
Label::createWithTTF
|
|
|
|
("There should be\na white square below", "fonts/arial.ttf", 15);
|
|
|
|
something->setPosition(Vec2(3 * size.width / 4, 5 * size.height / 6));
|
|
|
|
this->addChild(something);
|
|
|
|
|
|
|
|
auto cache = Director::getInstance()->getTextureCache();
|
|
|
|
|
|
|
|
cache->removeTextureForKey("Images/texture2048x2048.png");
|
|
|
|
|
|
|
|
cache->addImageAsync
|
|
|
|
("Images/texture2048x2048.png",
|
|
|
|
CC_CALLBACK_1(TextureCacheUnbindTest::textureLoadedA, this),
|
|
|
|
"A");
|
|
|
|
cache->addImageAsync
|
|
|
|
("Images/texture2048x2048.png",
|
|
|
|
CC_CALLBACK_1(TextureCacheUnbindTest::textureLoadedB, this),
|
|
|
|
"B");
|
|
|
|
cache->unbindImageAsync("A");
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCacheUnbindTest::textureLoadedA(Texture2D* texture)
|
|
|
|
{
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
auto s = Sprite::create("Images/texture2048x2048.png");
|
|
|
|
s->setScale(0.15);
|
|
|
|
s->setPosition(size.width / 4, size.height / 2);
|
|
|
|
this->addChild(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCacheUnbindTest::textureLoadedB(Texture2D* texture)
|
|
|
|
{
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
auto s = Sprite::create("Images/texture2048x2048.png");
|
|
|
|
s->setScale(0.15);
|
|
|
|
s->setPosition(3 * size.width / 4, size.height / 2);
|
|
|
|
this->addChild(s);
|
|
|
|
}
|