CCAnimationCache.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "2d/CCAnimationCache.h"
  24. #include "2d/CCSpriteFrameCache.h"
  25. #include "platform/CCFileUtils.h"
  26. using namespace std;
  27. NS_CC_BEGIN
  28. AnimationCache* AnimationCache::s_sharedAnimationCache = nullptr;
  29. AnimationCache* AnimationCache::getInstance()
  30. {
  31. if (! s_sharedAnimationCache)
  32. {
  33. s_sharedAnimationCache = new (std::nothrow) AnimationCache();
  34. s_sharedAnimationCache->init();
  35. }
  36. return s_sharedAnimationCache;
  37. }
  38. void AnimationCache::destroyInstance()
  39. {
  40. CC_SAFE_RELEASE_NULL(s_sharedAnimationCache);
  41. }
  42. bool AnimationCache::init()
  43. {
  44. return true;
  45. }
  46. AnimationCache::AnimationCache()
  47. {
  48. }
  49. AnimationCache::~AnimationCache()
  50. {
  51. CCLOGINFO("deallocing AnimationCache: %p", this);
  52. }
  53. void AnimationCache::addAnimation(Animation *animation, const std::string& name)
  54. {
  55. _animations.insert(name, animation);
  56. }
  57. void AnimationCache::removeAnimation(const std::string& name)
  58. {
  59. if (name.empty())
  60. return;
  61. _animations.erase(name);
  62. }
  63. Animation* AnimationCache::getAnimation(const std::string& name)
  64. {
  65. return _animations.at(name);
  66. }
  67. void AnimationCache::parseVersion1(const ValueMap& animations)
  68. {
  69. SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
  70. for (const auto& anim : animations)
  71. {
  72. const ValueMap& animationDict = anim.second.asValueMap();
  73. const ValueVector& frameNames = animationDict.at("frames").asValueVector();
  74. float delay = animationDict.at("delay").asFloat();
  75. Animation* animation = nullptr;
  76. if ( frameNames.empty() )
  77. {
  78. CCLOG("cocos2d: AnimationCache: Animation '%s' found in dictionary without any frames - cannot add to animation cache.", anim.first.c_str());
  79. continue;
  80. }
  81. ssize_t frameNameSize = frameNames.size();
  82. Vector<AnimationFrame*> frames(frameNameSize);
  83. for (auto& frameName : frameNames)
  84. {
  85. SpriteFrame* spriteFrame = frameCache->getSpriteFrameByName(frameName.asString());
  86. if ( ! spriteFrame ) {
  87. 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());
  88. continue;
  89. }
  90. AnimationFrame* animFrame = AnimationFrame::create(spriteFrame, 1, ValueMap());
  91. frames.pushBack(animFrame);
  92. }
  93. if ( frames.empty() )
  94. {
  95. 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());
  96. continue;
  97. }
  98. else if ( frames.size() != frameNameSize )
  99. {
  100. 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());
  101. }
  102. animation = Animation::create(frames, delay, 1);
  103. AnimationCache::getInstance()->addAnimation(animation, anim.first);
  104. }
  105. }
  106. void AnimationCache::parseVersion2(const ValueMap& animations)
  107. {
  108. SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
  109. for (const auto& anim : animations)
  110. {
  111. std::string name = anim.first;
  112. ValueMap& animationDict = const_cast<ValueMap&>(anim.second.asValueMap());
  113. const Value& loops = animationDict["loops"];
  114. bool restoreOriginalFrame = animationDict["restoreOriginalFrame"].asBool();
  115. ValueVector& frameArray = animationDict["frames"].asValueVector();
  116. if ( frameArray.empty() )
  117. {
  118. CCLOG("cocos2d: AnimationCache: Animation '%s' found in dictionary without any frames - cannot add to animation cache.", name.c_str());
  119. continue;
  120. }
  121. // Array of AnimationFrames
  122. Vector<AnimationFrame*> array(static_cast<int>(frameArray.size()));
  123. for (auto& obj : frameArray)
  124. {
  125. ValueMap& entry = obj.asValueMap();
  126. std::string spriteFrameName = entry["spriteframe"].asString();
  127. SpriteFrame *spriteFrame = frameCache->getSpriteFrameByName(spriteFrameName);
  128. if( ! spriteFrame ) {
  129. 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());
  130. continue;
  131. }
  132. float delayUnits = entry["delayUnits"].asFloat();
  133. Value& userInfo = entry["notification"];
  134. AnimationFrame *animFrame = AnimationFrame::create(spriteFrame, delayUnits, userInfo.getType() == Value::Type::MAP ? userInfo.asValueMap() : ValueMapNull);
  135. array.pushBack(animFrame);
  136. }
  137. float delayPerUnit = animationDict["delayPerUnit"].asFloat();
  138. Animation *animation = Animation::create(array, delayPerUnit, loops.getType() != Value::Type::NONE ? loops.asInt() : 1);
  139. animation->setRestoreOriginalFrame(restoreOriginalFrame);
  140. AnimationCache::getInstance()->addAnimation(animation, name);
  141. }
  142. }
  143. void AnimationCache::addAnimationsWithDictionary(const ValueMap& dictionary,const std::string& plist)
  144. {
  145. if ( dictionary.find("animations") == dictionary.end() )
  146. {
  147. CCLOG("cocos2d: AnimationCache: No animations were found in provided dictionary.");
  148. return;
  149. }
  150. const Value& animations = dictionary.at("animations");
  151. unsigned int version = 1;
  152. if( dictionary.find("properties") != dictionary.end() )
  153. {
  154. const ValueMap& properties = dictionary.at("properties").asValueMap();
  155. version = properties.at("format").asInt();
  156. const ValueVector& spritesheets = properties.at("spritesheets").asValueVector();
  157. for(const auto &value : spritesheets) {
  158. std::string path = FileUtils::getInstance()->fullPathFromRelativeFile(value.asString(),plist);
  159. SpriteFrameCache::getInstance()->addSpriteFramesWithFile(path);
  160. }
  161. }
  162. switch (version) {
  163. case 1:
  164. parseVersion1(animations.asValueMap());
  165. break;
  166. case 2:
  167. parseVersion2(animations.asValueMap());
  168. break;
  169. default:
  170. CCASSERT(false, "Invalid animation format");
  171. }
  172. }
  173. /** Read an NSDictionary from a plist file and parse it automatically for animations */
  174. void AnimationCache::addAnimationsWithFile(const std::string& plist)
  175. {
  176. CCASSERT(!plist.empty(), "Invalid texture file name");
  177. if (plist.empty()) {
  178. log("%s error:file name is empty!", __FUNCTION__);
  179. return;
  180. }
  181. ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(plist);
  182. CCASSERT( !dict.empty(), "CCAnimationCache: File could not be found");
  183. if (dict.empty()) {
  184. log("AnimationCache::addAnimationsWithFile error:%s not exist!", plist.c_str());
  185. }
  186. addAnimationsWithDictionary(dict,plist);
  187. }
  188. NS_CC_END