PuzzleSDK
TextureCacheTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "TextureCacheTest.h"
26
27// enable log
28#define COCOS2D_DEBUG 1
29
31
32TextureCacheTests::TextureCacheTests()
33{
36}
37
39: _numberOfSprites(20)
40, _numberOfLoadedSprites(0)
41{
42 auto size = Director::getInstance()->getWinSize();
43
44 _labelLoading = Label::createWithTTF("loading...", "fonts/arial.ttf", 15);
45 _labelPercent = Label::createWithTTF("%0", "fonts/arial.ttf", 15);
46
47 _labelLoading->setPosition(Vec2(size.width / 2, size.height / 2 - 20));
48 _labelPercent->setPosition(Vec2(size.width / 2, size.height / 2 + 20));
49
50 this->addChild(_labelLoading);
51 this->addChild(_labelPercent);
52
53 // load textures
54 Director::getInstance()->getTextureCache()->addImageAsync("Images/HelloWorld.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
55 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
56 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_01.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
57 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_02.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
58 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_03.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
59 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_04.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
60 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_05.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
61 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_06.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
62 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_07.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
63 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_08.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
64 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_09.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
65 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_10.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
66 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_11.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
67 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_12.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
68 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_13.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
69 Director::getInstance()->getTextureCache()->addImageAsync("Images/grossini_dance_14.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
70 Director::getInstance()->getTextureCache()->addImageAsync("Images/background1.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
71 Director::getInstance()->getTextureCache()->addImageAsync("Images/background2.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
72 Director::getInstance()->getTextureCache()->addImageAsync("Images/background3.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
73 Director::getInstance()->getTextureCache()->addImageAsync("Images/blocks.png", CC_CALLBACK_1(TextureCacheTest::loadingCallBack, this));
74}
75
76void TextureCacheTest::loadingCallBack(cocos2d::Texture2D *texture)
77{
79 char tmp[10];
80 sprintf(tmp,"%%%d", (int)(((float)_numberOfLoadedSprites / _numberOfSprites) * 100));
81 _labelPercent->setString(tmp);
82
84 {
85 this->removeChild(_labelLoading, true);
86 this->removeChild(_labelPercent, true);
87 addSprite();
88 }
89}
90
92{
93 auto size = Director::getInstance()->getWinSize();
94
95 // create sprites
96
97 auto bg = Sprite::create("Images/HelloWorld.png");
98 bg->setPosition(Vec2(size.width / 2, size.height / 2));
99
100 auto s1 = Sprite::create("Images/grossini.png");
101 auto s2 = Sprite::create("Images/grossini_dance_01.png");
102 auto s3 = Sprite::create("Images/grossini_dance_02.png");
103 auto s4 = Sprite::create("Images/grossini_dance_03.png");
104 auto s5 = Sprite::create("Images/grossini_dance_04.png");
105 auto s6 = Sprite::create("Images/grossini_dance_05.png");
106 auto s7 = Sprite::create("Images/grossini_dance_06.png");
107 auto s8 = Sprite::create("Images/grossini_dance_07.png");
108 auto s9 = Sprite::create("Images/grossini_dance_08.png");
109 auto s10 = Sprite::create("Images/grossini_dance_09.png");
110 auto s11 = Sprite::create("Images/grossini_dance_10.png");
111 auto s12 = Sprite::create("Images/grossini_dance_11.png");
112 auto s13 = Sprite::create("Images/grossini_dance_12.png");
113 auto s14 = Sprite::create("Images/grossini_dance_13.png");
114 auto s15 = Sprite::create("Images/grossini_dance_14.png");
115
116 // just loading textures to slow down
117 Sprite::create("Images/background1.png");
118 Sprite::create("Images/background2.png");
119 Sprite::create("Images/background3.png");
120 Sprite::create("Images/blocks.png");
121
122 s1->setPosition(Vec2(50, 50));
123 s2->setPosition(Vec2(60, 50));
124 s3->setPosition(Vec2(70, 50));
125 s4->setPosition(Vec2(80, 50));
126 s5->setPosition(Vec2(90, 50));
127 s6->setPosition(Vec2(100, 50));
128
129 s7->setPosition(Vec2(50, 180));
130 s8->setPosition(Vec2(60, 180));
131 s9->setPosition(Vec2(70, 180));
132 s10->setPosition(Vec2(80, 180));
133 s11->setPosition(Vec2(90, 180));
134 s12->setPosition(Vec2(100, 180));
135
136 s13->setPosition(Vec2(50, 270));
137 s14->setPosition(Vec2(60, 270));
138 s15->setPosition(Vec2(70, 270));
139
140 this->addChild(bg);
141
142 this->addChild(s1);
143 this->addChild(s2);
144 this->addChild(s3);
145 this->addChild(s4);
146 this->addChild(s5);
147 this->addChild(s6);
148 this->addChild(s7);
149 this->addChild(s8);
150 this->addChild(s9);
151 this->addChild(s10);
152 this->addChild(s11);
153 this->addChild(s12);
154 this->addChild(s13);
155 this->addChild(s14);
156 this->addChild(s15);
157}
158
160{
161 auto size = Director::getInstance()->getWinSize();
162
163 Label* nothing =
164 Label::createWithTTF
165 ("There should be\nnothing below", "fonts/arial.ttf", 15);
166 nothing->setPosition(Vec2(size.width / 4, 5 * size.height / 6));
167 this->addChild(nothing);
168
169 Label* something =
170 Label::createWithTTF
171 ("There should be\na white square below", "fonts/arial.ttf", 15);
172 something->setPosition(Vec2(3 * size.width / 4, 5 * size.height / 6));
173 this->addChild(something);
174
175 auto cache = Director::getInstance()->getTextureCache();
176
177 cache->removeTextureForKey("Images/texture2048x2048.png");
178
179 cache->addImageAsync
180 ("Images/texture2048x2048.png",
181 CC_CALLBACK_1(TextureCacheUnbindTest::textureLoadedA, this),
182 "A");
183 cache->addImageAsync
184 ("Images/texture2048x2048.png",
185 CC_CALLBACK_1(TextureCacheUnbindTest::textureLoadedB, this),
186 "B");
187 cache->unbindImageAsync("A");
188}
189
191{
192 auto size = Director::getInstance()->getWinSize();
193 auto s = Sprite::create("Images/texture2048x2048.png");
194 s->setScale(0.15);
195 s->setPosition(size.width / 4, size.height / 2);
196 this->addChild(s);
197}
198
200{
201 auto size = Director::getInstance()->getWinSize();
202 auto s = Sprite::create("Images/texture2048x2048.png");
203 s->setScale(0.15);
204 s->setPosition(3 * size.width / 4, size.height / 2);
205 this->addChild(s);
206}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC
void loadingCallBack(cocos2d::Texture2D *texture)
cocos2d::Label * _labelPercent
cocos2d::Label * _labelLoading
void textureLoadedB(cocos2d::Texture2D *texture)
void textureLoadedA(cocos2d::Texture2D *texture)