diff --git a/cocos2dx/include/CCSpriteFrameCache.h b/cocos2dx/include/CCSpriteFrameCache.h index d54d423c4c..d7a82e8978 100644 --- a/cocos2dx/include/CCSpriteFrameCache.h +++ b/cocos2dx/include/CCSpriteFrameCache.h @@ -52,8 +52,8 @@ public: /*Adds multiple Sprite Frames with a dictionary. The texture will be associated with the created sprite frames. the pobDictionary look like: - "metadata" -> NSMutableDictionary - "frames" -> NSMutableDictionary*> + "metadata" -> NSMutableDictionary + "frames" -> NSMutableDictionary*> | |__"x" -> "123" "y" -> "12" diff --git a/cocos2dx/sprite_nodes/CCSpriteFrameCache.cpp b/cocos2dx/sprite_nodes/CCSpriteFrameCache.cpp index 1a967d2512..78c9a33cc6 100644 --- a/cocos2dx/sprite_nodes/CCSpriteFrameCache.cpp +++ b/cocos2dx/sprite_nodes/CCSpriteFrameCache.cpp @@ -28,6 +28,7 @@ THE SOFTWARE. #include "CCSpriteFrame.h" #include "CCSprite.h" #include "support/TransformUtils.h" +#include "platform/platform.h" using namespace std; @@ -73,10 +74,10 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(map *pobDi */ map::iterator metadataIter = pobDictionary->find("metadata"); - map *pMetadataMap = NULL; + map *pMetadataMap = NULL; if (metadataIter != pobDictionary->end()) { - pMetadataMap = static_cast*>(metadataIter->second); + pMetadataMap = static_cast*>(metadataIter->second); } map::iterator framesIter = pobDictionary->find("frames"); @@ -90,10 +91,10 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(map *pobDi // get the format if (pMetadataMap) { - map::iterator formatIter = pMetadataMap->find("format"); + map::iterator formatIter = pMetadataMap->find("format"); if (formatIter != pMetadataMap->end()) { - format = atoi(formatIter->second.c_str()); + format = atoi(formatIter->second->c_str()); } } @@ -107,7 +108,7 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(map *pobDi map::iterator frameIter; - map *pFrame; + map *pFrame; string key; for (frameIter = pFramesMap->begin(); frameIter != pFramesMap->end(); ++frameIter) { @@ -120,7 +121,7 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(map *pobDi } CCSpriteFrame *pSpriteFrame; - pFrame = static_cast*>(frameIter->second); + pFrame = static_cast*>(frameIter->second); if (format == 0) { /* @@ -134,14 +135,14 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(map *pobDi int oh = (int)atof(pFrameDict->objectForKey("originalHeight").c_str()); */ - float x = (float)atof(pFrame->find("x")->second.c_str()); - float y = (float)atof(pFrame->find("y")->second.c_str()); - float w = (float)atof(pFrame->find("width")->second.c_str()); - float h = (float)atof(pFrame->find("height")->second.c_str()); - float ox = (float)atof(pFrame->find("offsetX")->second.c_str()); - float oy = (float)atof(pFrame->find("offsetY")->second.c_str()); - int ow = (int)atof(pFrame->find("originalWidth")->second.c_str()); - int oh = (int)atof(pFrame->find("originalHeight")->second.c_str()); + float x = (float)atof(pFrame->find("x")->second->c_str()); + float y = (float)atof(pFrame->find("y")->second->c_str()); + float w = (float)atof(pFrame->find("width")->second->c_str()); + float h = (float)atof(pFrame->find("height")->second->c_str()); + float ox = (float)atof(pFrame->find("offsetX")->second->c_str()); + float oy = (float)atof(pFrame->find("offsetY")->second->c_str()); + int ow = (int)atof(pFrame->find("originalWidth")->second->c_str()); + int oh = (int)atof(pFrame->find("originalHeight")->second->c_str()); @@ -183,17 +184,32 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(map *pobDi } ///@todo implement later -/* void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist, CCTexture2D *pobTexture) { char *pszPath = CCFileUtils::fullPathFromRelativePath(pszPlist); - + map *dict = CCFileUtils::dictionaryWithContentsOfFile(pszPath); + + return addSpriteFramesWithDictionary(dict, pobTexture); } -*/ ///@todo implement later void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist) { + char *pszPath = CCFileUtils::fullPathFromRelativePath(pszPlist); + map *dict = CCFileUtils::dictionaryWithContentsOfFile(pszPath); + + string texturePath = string(pszPlist); + + // remove .xxx + size_t startPos = texturePath.find_last_of("."); + texturePath = texturePath.erase(startPos); + + // append .png + texturePath = texturePath.append(".png"); + + CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(texturePath.c_str()); + + return addSpriteFramesWithDictionary(dict, pTexture); } void CCSpriteFrameCache::addSpriteFrame(CCSpriteFrame *pobFrame, const char *pszFrameName) @@ -248,3 +264,11 @@ CCSpriteFrame* CCSpriteFrameCache::spriteFrameByName(const char *pszName) return pFrame; } + +CCSprite* CCSpriteFrameCache::createSpriteWithFrameName(const char *pszName) +{ + map::iterator iter = m_pSpriteFramesMap->find(pszName); + CCSpriteFrame *pFrame = iter->second; + + return CCSprite::spriteWithSpriteFrame(pFrame); +}