CCAnimationCache.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  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. #include "2d/CCAnimationCache.h"
  25. #include "2d/CCSpriteFrameCache.h"
  26. #include "platform/CCFileUtils.h"
  27. using namespace std;
  28. NS_CC_BEGIN
  29. AnimationCache* AnimationCache::s_sharedAnimationCache = nullptr;
  30. AnimationCache* AnimationCache::getInstance()
  31. {
  32. if (! s_sharedAnimationCache)
  33. {
  34. s_sharedAnimationCache = new (std::nothrow) AnimationCache();
  35. s_sharedAnimationCache->init();
  36. }
  37. return s_sharedAnimationCache;
  38. }
  39. void AnimationCache::destroyInstance()
  40. {
  41. CC_SAFE_RELEASE_NULL(s_sharedAnimationCache);
  42. }
  43. bool AnimationCache::init()
  44. {
  45. return true;
  46. }
  47. AnimationCache::AnimationCache()
  48. {
  49. }
  50. AnimationCache::~AnimationCache()
  51. {
  52. CCLOGINFO("deallocing AnimationCache: %p", this);
  53. }
  54. void AnimationCache::addAnimation(Animation *animation, const std::string& name)
  55. {
  56. _animations.insert(name, animation);
  57. }
  58. void AnimationCache::removeAnimation(const std::string& name)
  59. {
  60. if (name.empty())
  61. return;
  62. _animations.erase(name);
  63. }
  64. Animation* AnimationCache::getAnimation(const std::string& name)
  65. {
  66. return _animations.at(name);
  67. }
  68. void AnimationCache::parseVersion1(const ValueMap& animations)
  69. {
  70. SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
  71. for (const auto& anim : animations)
  72. {
  73. const ValueMap& animationDict = anim.second.asValueMap();
  74. const ValueVector& frameNames = animationDict.at("frames").asValueVector();
  75. float delay = animationDict.at("delay").asFloat();
  76. Animation* animation = nullptr;
  77. if ( frameNames.empty() )
  78. {
  79. CCLOG("cocos2d: AnimationCache: Animation '%s' found in dictionary without any frames - cannot add to animation cache.", anim.first.c_str());
  80. continue;
  81. }
  82. ssize_t frameNameSize = frameNames.size();
  83. Vector<AnimationFrame*> frames(frameNameSize);
  84. for (auto& frameName : frameNames)
  85. {
  86. SpriteFrame* spriteFrame = frameCache->getSpriteFrameByName(frameName.asString());
  87. if ( ! spriteFrame ) {
  88. CCLOG("cocos2d: AnimationCache: Animation '%s' refers to frame '%s' which is not currently in the SpriteFrameCache. This frame will not be added to the animation.", anim.first.c_str(), frameName.asString().c_str());
  89. continue;
  90. }
  91. AnimationFrame* animFrame = AnimationFrame::create(spriteFrame, 1, ValueMap());
  92. frames.pushBack(animFrame);
  93. }
  94. if ( frames.empty() )
  95. {
  96. CCLOG("cocos2d: AnimationCache: None of the frames for animation '%s' were found in the SpriteFrameCache. Animation is not being added to the Animation Cache.", anim.first.c_str());
  97. continue;
  98. }
  99. else if ( frames.size() != frameNameSize )
  100. {
  101. CCLOG("cocos2d: AnimationCache: An animation in your dictionary refers to a frame which is not in the SpriteFrameCache. Some or all of the frames for the animation '%s' may be missing.", anim.first.c_str());
  102. }
  103. animation = Animation::create(frames, delay, 1);
  104. AnimationCache::getInstance()->addAnimation(animation, anim.first);
  105. }
  106. }
  107. void AnimationCache::parseVersion2(const ValueMap& animations)
  108. {
  109. SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
  110. for (const auto& anim : animations)
  111. {
  112. std::string name = anim.first;
  113. ValueMap& animationDict = const_cast<ValueMap&>(anim.second.asValueMap());
  114. const Value& loops = animationDict["loops"];
  115. bool restoreOriginalFrame = animationDict["restoreOriginalFrame"].asBool();
  116. ValueVector& frameArray = animationDict["frames"].asValueVector();
  117. if ( frameArray.empty() )
  118. {
  119. CCLOG("cocos2d: AnimationCache: Animation '%s' found in dictionary without any frames - cannot add to animation cache.", name.c_str());
  120. continue;
  121. }
  122. // Array of AnimationFrames
  123. Vector<AnimationFrame*> array(static_cast<int>(frameArray.size()));
  124. for (auto& obj : frameArray)
  125. {
  126. ValueMap& entry = obj.asValueMap();
  127. std::string spriteFrameName = entry["spriteframe"].asString();
  128. SpriteFrame *spriteFrame = frameCache->getSpriteFrameByName(spriteFrameName);
  129. if( ! spriteFrame ) {
  130. CCLOG("cocos2d: AnimationCache: Animation '%s' refers to frame '%s' which is not currently in the SpriteFrameCache. This frame will not be added to the animation.", name.c_str(), spriteFrameName.c_str());
  131. continue;
  132. }
  133. float delayUnits = entry["delayUnits"].asFloat();
  134. Value& userInfo = entry["notification"];
  135. AnimationFrame *animFrame = AnimationFrame::create(spriteFrame, delayUnits, userInfo.getType() == Value::Type::MAP ? userInfo.asValueMap() : ValueMapNull);
  136. array.pushBack(animFrame);
  137. }
  138. float delayPerUnit = animationDict["delayPerUnit"].asFloat();
  139. Animation *animation = Animation::create(array, delayPerUnit, loops.getType() != Value::Type::NONE ? loops.asInt() : 1);
  140. animation->setRestoreOriginalFrame(restoreOriginalFrame);
  141. AnimationCache::getInstance()->addAnimation(animation, name);
  142. }
  143. }
  144. void AnimationCache::addAnimationsWithDictionary(const ValueMap& dictionary,const std::string& plist)
  145. {
  146. if ( dictionary.find("animations") == dictionary.end() )
  147. {
  148. CCLOG("cocos2d: AnimationCache: No animations were found in provided dictionary.");
  149. return;
  150. }
  151. const Value& animations = dictionary.at("animations");
  152. unsigned int version = 1;
  153. if( dictionary.find("properties") != dictionary.end() )
  154. {
  155. const ValueMap& properties = dictionary.at("properties").asValueMap();
  156. version = properties.at("format").asInt();
  157. const ValueVector& spritesheets = properties.at("spritesheets").asValueVector();
  158. for(const auto &value : spritesheets) {
  159. std::string path = FileUtils::getInstance()->fullPathFromRelativeFile(value.asString(),plist);
  160. SpriteFrameCache::getInstance()->addSpriteFramesWithFile(path);
  161. }
  162. }
  163. switch (version) {
  164. case 1:
  165. parseVersion1(animations.asValueMap());
  166. break;
  167. case 2:
  168. parseVersion2(animations.asValueMap());
  169. break;
  170. default:
  171. CCASSERT(false, "Invalid animation format");
  172. }
  173. }
  174. /** Read an NSDictionary from a plist file and parse it automatically for animations */
  175. void AnimationCache::addAnimationsWithFile(const std::string& plist)
  176. {
  177. CCASSERT(!plist.empty(), "Invalid texture file name");
  178. if (plist.empty()) {
  179. log("%s error:file name is empty!", __FUNCTION__);
  180. return;
  181. }
  182. ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(plist);
  183. CCASSERT( !dict.empty(), "CCAnimationCache: File could not be found");
  184. if (dict.empty()) {
  185. log("AnimationCache::addAnimationsWithFile error:%s not exist!", plist.c_str());
  186. }
  187. addAnimationsWithDictionary(dict,plist);
  188. }
  189. NS_CC_END