axmol/core/2d/CCAnimationCache.cpp

269 lines
8.8 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-08-08 18:02:17 +08:00
https://axys1.github.io/
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.
****************************************************************************/
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCAnimationCache.h"
#include "2d/CCSpriteFrameCache.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCFileUtils.h"
using namespace std;
NS_AX_BEGIN
AnimationCache* AnimationCache::s_sharedAnimationCache = nullptr;
AnimationCache* AnimationCache::getInstance()
{
2021-12-25 10:04:45 +08:00
if (!s_sharedAnimationCache)
{
2021-12-08 00:11:53 +08:00
s_sharedAnimationCache = new AnimationCache();
s_sharedAnimationCache->init();
}
return s_sharedAnimationCache;
}
void AnimationCache::destroyInstance()
{
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE_NULL(s_sharedAnimationCache);
}
bool AnimationCache::init()
{
return true;
}
2021-12-25 10:04:45 +08:00
AnimationCache::AnimationCache() {}
AnimationCache::~AnimationCache()
{
2022-07-16 10:43:05 +08:00
AXLOGINFO("deallocing AnimationCache: %p", this);
}
2021-12-26 23:26:34 +08:00
void AnimationCache::addAnimation(Animation* animation, std::string_view name)
{
2021-12-26 23:26:34 +08:00
_animations.insert(std::string{name}, animation);
}
2021-12-26 23:26:34 +08:00
void AnimationCache::removeAnimation(std::string_view name)
{
if (name.empty())
return;
_animations.erase(name);
}
2021-12-26 23:26:34 +08:00
Animation* AnimationCache::getAnimation(std::string_view name)
{
return _animations.at(name);
}
void AnimationCache::parseVersion1(const ValueMap& animations)
{
2021-12-25 10:04:45 +08:00
SpriteFrameCache* frameCache = SpriteFrameCache::getInstance();
for (const auto& anim : animations)
{
const ValueMap& animationDict = anim.second.asValueMap();
const ValueVector& frameNames = animationDict.at("frames").asValueVector();
2021-12-25 10:04:45 +08:00
float delay = animationDict.at("delay").asFloat();
Animation* animation = nullptr;
2021-12-25 10:04:45 +08:00
if (frameNames.empty())
{
2022-07-16 10:43:05 +08:00
AXLOG(
2021-12-25 10:04:45 +08:00
"cocos2d: AnimationCache: Animation '%s' found in dictionary without any frames - cannot add to "
"animation cache.",
anim.first.c_str());
continue;
}
2013-12-27 15:06:16 +08:00
ssize_t frameNameSize = frameNames.size();
Vector<AnimationFrame*> frames(frameNameSize);
for (auto&& frameName : frameNames)
{
SpriteFrame* spriteFrame = frameCache->getSpriteFrameByName(frameName.asString());
2021-12-25 10:04:45 +08:00
if (!spriteFrame)
{
2022-07-16 10:43:05 +08:00
AXLOG(
2021-12-25 10:04:45 +08:00
"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());
continue;
}
AnimationFrame* animFrame = AnimationFrame::create(spriteFrame, 1, ValueMap());
frames.pushBack(animFrame);
}
2021-12-25 10:04:45 +08:00
if (frames.empty())
{
2022-07-16 10:43:05 +08:00
AXLOG(
2021-12-25 10:04:45 +08:00
"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());
continue;
}
2021-12-25 10:04:45 +08:00
else if (frames.size() != frameNameSize)
{
2022-07-16 10:43:05 +08:00
AXLOG(
2021-12-25 10:04:45 +08:00
"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());
}
animation = Animation::create(frames, delay, 1);
AnimationCache::getInstance()->addAnimation(animation, anim.first);
}
}
void AnimationCache::parseVersion2(const ValueMap& animations)
{
2021-12-25 10:04:45 +08:00
SpriteFrameCache* frameCache = SpriteFrameCache::getInstance();
for (const auto& anim : animations)
{
2021-12-25 10:04:45 +08:00
std::string name = anim.first;
ValueMap& animationDict = const_cast<ValueMap&>(anim.second.asValueMap());
2021-12-25 10:04:45 +08:00
const Value& loops = animationDict["loops"];
2020-08-18 11:33:18 +08:00
bool restoreOriginalFrame = animationDict["restoreOriginalFrame"].asBool();
ValueVector& frameArray = animationDict["frames"].asValueVector();
2021-12-25 10:04:45 +08:00
if (frameArray.empty())
{
2022-07-16 10:43:05 +08:00
AXLOG(
2021-12-25 10:04:45 +08:00
"cocos2d: AnimationCache: Animation '%s' found in dictionary without any frames - cannot add to "
"animation cache.",
name.c_str());
continue;
}
// Array of AnimationFrames
2013-12-06 16:32:06 +08:00
Vector<AnimationFrame*> array(static_cast<int>(frameArray.size()));
for (auto&& obj : frameArray)
{
2021-12-25 10:04:45 +08:00
ValueMap& entry = obj.asValueMap();
std::string spriteFrameName = entry["spriteframe"].asString();
2021-12-25 10:04:45 +08:00
SpriteFrame* spriteFrame = frameCache->getSpriteFrameByName(spriteFrameName);
2021-12-25 10:04:45 +08:00
if (!spriteFrame)
{
2022-07-16 10:43:05 +08:00
AXLOG(
2021-12-25 10:04:45 +08:00
"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());
continue;
}
2020-08-18 11:33:18 +08:00
float delayUnits = entry["delayUnits"].asFloat();
2021-12-25 10:04:45 +08:00
Value& userInfo = entry["notification"];
2021-12-25 10:04:45 +08:00
AnimationFrame* animFrame = AnimationFrame::create(
spriteFrame, delayUnits, userInfo.getType() == Value::Type::MAP ? userInfo.asValueMap() : ValueMapNull);
array.pushBack(animFrame);
}
2020-08-18 11:33:18 +08:00
float delayPerUnit = animationDict["delayPerUnit"].asFloat();
2021-12-25 10:04:45 +08:00
Animation* animation =
Animation::create(array, delayPerUnit, loops.getType() != Value::Type::NONE ? loops.asInt() : 1);
animation->setRestoreOriginalFrame(restoreOriginalFrame);
AnimationCache::getInstance()->addAnimation(animation, name);
}
}
2021-12-26 23:26:34 +08:00
void AnimationCache::addAnimationsWithDictionary(const ValueMap& dictionary, std::string_view plist)
{
auto anisItr = dictionary.find("animations");
2021-12-25 10:04:45 +08:00
if (anisItr == dictionary.end())
{
2022-07-16 10:43:05 +08:00
AXLOG("cocos2d: AnimationCache: No animations were found in provided dictionary.");
return;
}
2021-12-25 10:04:45 +08:00
const Value& animations = anisItr->second;
2021-12-25 10:04:45 +08:00
unsigned int version = 1;
auto propsItr = dictionary.find("properties");
2021-12-25 10:04:45 +08:00
if (propsItr != dictionary.end())
{
2021-12-25 10:04:45 +08:00
const ValueMap& properties = propsItr->second.asValueMap();
version = properties.at("format").asInt();
const ValueVector& spritesheets = properties.at("spritesheets").asValueVector();
2021-12-25 10:04:45 +08:00
for (const auto& value : spritesheets)
{
std::string path = FileUtils::getInstance()->fullPathFromRelativeFile(value.asString(), plist);
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(path);
}
}
2021-12-25 10:04:45 +08:00
switch (version)
{
case 1:
parseVersion1(animations.asValueMap());
break;
case 2:
parseVersion2(animations.asValueMap());
break;
default:
2022-07-16 10:43:05 +08:00
AXASSERT(false, "Invalid animation format");
}
}
/** Read an NSDictionary from a plist file and parse it automatically for animations */
2021-12-26 23:26:34 +08:00
void AnimationCache::addAnimationsWithFile(std::string_view plist)
{
2022-07-16 10:43:05 +08:00
AXASSERT(!plist.empty(), "Invalid texture file name");
2021-12-25 10:04:45 +08:00
if (plist.empty())
{
log("%s error:file name is empty!", __FUNCTION__);
return;
}
2021-12-25 10:04:45 +08:00
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(plist);
2022-07-16 10:43:05 +08:00
AXASSERT(!dict.empty(), "CCAnimationCache: File could not be found");
2021-12-25 10:04:45 +08:00
if (dict.empty())
{
2021-12-26 23:26:34 +08:00
log("AnimationCache::addAnimationsWithFile error:%s not exist!", plist.data());
}
2021-12-25 10:04:45 +08:00
addAnimationsWithDictionary(dict, plist);
}
NS_AX_END