PuzzleSDK
SpriteFrameCacheTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2012 cocos2d-x.org
3 Copyright (c) 2013-2016 Chukong Technologies Inc.
4 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
5
6 http://www.cocos2d-x.org
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 ****************************************************************************/
26
28
29#include <cassert>
30
31// enable log
32#define COCOS2D_DEBUG 1
33
35
36SpriteFrameCacheTests::SpriteFrameCacheTests()
37{
41}
42
44{
45 const Size screenSize = Director::getInstance()->getWinSize();
46
47 infoLabel = Label::create();
48 infoLabel->setAnchorPoint(Point(0.5f, 1.0f));
49 infoLabel->setAlignment(cocos2d::TextHAlignment::CENTER);
50 infoLabel->setPosition(screenSize.width * 0.5f, screenSize.height * 0.7f);
51 addChild(infoLabel);
52
53 // load atlas definition with specified PixelFormat and check that it matches to expected format
54 loadSpriteFrames("Images/sprite_frames_test/test_A8.plist", backend::PixelFormat::A8);
55 loadSpriteFrames("Images/sprite_frames_test/test_RGBA8888.plist", backend::PixelFormat::RGBA8888);
56 loadSpriteFrames("Images/sprite_frames_test/test_AI88.plist", backend::PixelFormat::AI88);
57 loadSpriteFrames("Images/sprite_frames_test/test_RGBA8888.plist", backend::PixelFormat::RGBA8888);
58 loadSpriteFrames("Images/sprite_frames_test/test_RGB565.plist", backend::PixelFormat::RGB565);
59 loadSpriteFrames("Images/sprite_frames_test/test_RGB888.plist", backend::PixelFormat::RGB888);
60 loadSpriteFrames("Images/sprite_frames_test/test_RGBA4444.plist", backend::PixelFormat::RGBA4444);
61 loadSpriteFrames("Images/sprite_frames_test/test_RGBA5551.plist", backend::PixelFormat::RGB5A1);
62
63 if (Configuration::getInstance()->supportsPVRTC()) {
64 loadSpriteFrames("Images/sprite_frames_test/test_PVRTC2.plist", backend::PixelFormat::PVRTC2A);
65 loadSpriteFrames("Images/sprite_frames_test/test_PVRTC4.plist", backend::PixelFormat::PVRTC4A);
66 loadSpriteFrames("Images/sprite_frames_test/test_PVRTC2_NOALPHA.plist", backend::PixelFormat::PVRTC2);
67 }
68
69 // test loading atlases without PixelFormat specified
70 Texture2D::setDefaultAlphaPixelFormat(backend::PixelFormat::RGB5A1);
71 loadSpriteFrames("Images/sprite_frames_test/test_NoFormat.plist", backend::PixelFormat::RGB5A1);
72
73 // restore default alpha pixel format
74 Texture2D::setDefaultAlphaPixelFormat(backend::PixelFormat::RGBA8888);
75}
76
77void SpriteFrameCachePixelFormatTest::loadSpriteFrames(const std::string &file, cocos2d::backend::PixelFormat expectedFormat)
78{
79 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(file);
80 SpriteFrame *spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName("grossini.png");
81 Texture2D *texture = spriteFrame->getTexture();
82 const ssize_t bitsPerKB = 8 * 1024;
83 const double memorySize = 1.0 * texture->getBitsPerPixelForFormat() * texture->getContentSizeInPixels().width * texture->getContentSizeInPixels().height / bitsPerKB;
84#ifndef CC_USE_METAL
85 CC_ASSERT(texture->getPixelFormat() == expectedFormat);
86#endif
87 const std::string textureInfo = StringUtils::format("%s: %.2f KB\r\n", texture->getStringForFormat(), memorySize);
88 infoLabel->setString(infoLabel->getString() + textureInfo);
89
90 SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(file);
91 Director::getInstance()->getTextureCache()->removeTexture(texture);
92}
93
94
96{
97 const Size screenSize = Director::getInstance()->getWinSize();
98
99 // load atlas definition with specified PixelFormat and check that it matches to expected format
100 loadSpriteFrames("Images/sprite_frames_test/test_RGBA8888.plist", backend::PixelFormat::RGBA8888);
101 loadSpriteFrames("Images/sprite_frames_test/test_RGBA8888.plist", backend::PixelFormat::RGBA8888);
102 loadSpriteFrames("Images/sprite_frames_test/test_RGBA8888.plist", backend::PixelFormat::RGBA8888);
103 loadSpriteFrames("Images/sprite_frames_test/test_RGBA8888.plist", backend::PixelFormat::RGBA8888);
104
105}
106
107void SpriteFrameCacheLoadMultipleTimes::loadSpriteFrames(const std::string &file, cocos2d::backend::PixelFormat expectedFormat)
108{
109 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(file);
110 SpriteFrame *spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName("grossini.png");
111 Texture2D *texture = spriteFrame->getTexture();
112 CC_ASSERT(texture->getPixelFormat() == expectedFormat);
113
114 SpriteFrameCache::getInstance()->removeSpriteFrameByName("grossini.png");
115 Director::getInstance()->getTextureCache()->removeTexture(texture);
116}
117
118
120{
121 const Size screenSize = Director::getInstance()->getWinSize();
122 // load atlas definition with specified PixelFormat and check that it matches to expected format
123 loadSpriteFrames("Images/test_polygon.plist", backend::PixelFormat::RGBA8888);
124}
125
126void SpriteFrameCacheFullCheck::loadSpriteFrames(const std::string &file, cocos2d::backend::PixelFormat expectedFormat)
127{
128 auto cache = SpriteFrameCache::getInstance();
129
130 CCASSERT(cache->isSpriteFramesWithFileLoaded("plist which not exists") == false, "Plist not exists");
131
132 cache->addSpriteFramesWithFile(file);
133 CCASSERT(cache->isSpriteFramesWithFileLoaded(file) == true, "Plist should be full after loaded");
134
135 cache->removeSpriteFrameByName("not_exists_grossinis_sister.png");
136 CCASSERT(cache->isSpriteFramesWithFileLoaded(file) == true, "Plist should not be still full");
137
138 cache->removeSpriteFrameByName("grossinis_sister1.png");
139 CCASSERT(cache->isSpriteFramesWithFileLoaded(file) == false, "Plist should not be full after remove any sprite");
140
141 cache->addSpriteFramesWithFile(file);
142 CCASSERT(cache->isSpriteFramesWithFileLoaded(file) == true, "Plist should be full after reloaded");
143}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
void loadSpriteFrames(const std::string &file, cocos2d::backend::PixelFormat expectedFormat)
void loadSpriteFrames(const std::string &file, cocos2d::backend::PixelFormat expectedFormat)
void loadSpriteFrames(const std::string &file, cocos2d::backend::PixelFormat expectedFormat)