void CCSpriteFrameCache::removeSpriteFramesFromDictionary(CCDictionary<std::string, CCSpriteFrame*> *dictionary) { CCDictionary<std::string, CCObject*>* framesDict = (CCDictionary<std::string, CCObject*>*)dictionary->objectForKey(string("frames")); vector<string> keysToRemove; framesDict->begin(); std::string key = ""; CCDictionary<std::string, CCObject*> *frameDict = NULL; while( (frameDict = (CCDictionary<std::string, CCObject*>*)framesDict->next(&key)) ) { if (m_pSpriteFrames->objectForKey(key)) { keysToRemove.push_back(key); } } framesDict->end(); vector<string>::iterator iter; for (iter = keysToRemove.begin(); iter != keysToRemove.end(); ++iter) { m_pSpriteFrames->removeObjectForKey(*iter); } }
void CCSpriteFrameCache::addSpriteFramesWithDictionary(CCDictionary<std::string, CCObject*> *dictionary, CCTexture2D *pobTexture) { /* Supported Zwoptex Formats: ZWTCoordinatesFormatOptionXMLLegacy = 0, // Flash Version ZWTCoordinatesFormatOptionXML1_0 = 1, // Desktop Version 0.0 - 0.4b ZWTCoordinatesFormatOptionXML1_1 = 2, // Desktop Version 1.0.0 - 1.0.1 ZWTCoordinatesFormatOptionXML1_2 = 3, // Desktop Version 1.0.2+ */ CCDictionary<std::string, CCObject*> *metadataDict = (CCDictionary<std::string, CCObject*>*)dictionary->objectForKey(std::string("metadata")); CCDictionary<std::string, CCObject*> *framesDict = (CCDictionary<std::string, CCObject*>*)dictionary->objectForKey(std::string("frames")); int format = 0; // get the format if(metadataDict != NULL) { format = atoi(valueForKey("format", metadataDict)); } // check the format CCAssert(format >=0 && format <= 3, ""); framesDict->begin(); std::string key = ""; CCDictionary<std::string, CCObject*> *frameDict = NULL; while( (frameDict = (CCDictionary<std::string, CCObject*>*)framesDict->next(&key)) ) { CCSpriteFrame *spriteFrame = m_pSpriteFrames->objectForKey(key); if (spriteFrame) { continue; } if(format == 0) { float x = (float)atof(valueForKey("x", frameDict)); float y = (float)atof(valueForKey("y", frameDict)); float w = (float)atof(valueForKey("width", frameDict)); float h = (float)atof(valueForKey("height", frameDict)); float ox = (float)atof(valueForKey("offsetX", frameDict)); float oy = (float)atof(valueForKey("offsetY", frameDict)); int ow = atoi(valueForKey("originalWidth", frameDict)); int oh = atoi(valueForKey("originalHeight", frameDict)); // check ow/oh if(!ow || !oh) { CCLOG("cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenrate the .plist"); } // abs ow/oh ow = abs(ow); oh = abs(oh); // create frame spriteFrame = new CCSpriteFrame(); spriteFrame->initWithTexture(pobTexture, CCRectMake(x, y, w, h), false, CCPointMake(ox, oy), CCSizeMake((float)ow, (float)oh) ); } else if(format == 1 || format == 2) { CCRect frame = CCRectFromString(valueForKey("frame", frameDict)); bool rotated = false; // rotation if (format == 2) { rotated = atoi(valueForKey("rotated", frameDict)) == 0 ? false : true; } CCPoint offset = CCPointFromString(valueForKey("offset", frameDict)); CCSize sourceSize = CCSizeFromString(valueForKey("sourceSize", frameDict)); // create frame spriteFrame = new CCSpriteFrame(); spriteFrame->initWithTexture(pobTexture, frame, rotated, offset, sourceSize ); } else if (format == 3) { // get values CCSize spriteSize = CCSizeFromString(valueForKey("spriteSize", frameDict)); CCPoint spriteOffset = CCPointFromString(valueForKey("spriteOffset", frameDict)); CCSize spriteSourceSize = CCSizeFromString(valueForKey("spriteSourceSize", frameDict)); CCRect textureRect = CCRectFromString(valueForKey("textureRect", frameDict)); bool textureRotated = atoi(valueForKey("textureRotated", frameDict)) == 0 ? false : true; // get aliases CCMutableArray<CCString*> *aliases = (CCMutableArray<CCString*> *) (frameDict->objectForKey(std::string("aliases"))); CCMutableArray<CCString*>::CCMutableArrayIterator iter; CCString * frameKey = new CCString(key.c_str()); for (iter = aliases->begin(); iter != aliases->end(); ++iter) { std::string oneAlias = ((CCString*) (*iter))->m_sString; if (m_pSpriteFramesAliases->objectForKey(oneAlias)) { CCLOG("cocos2d: WARNING: an alias with name %s already exists", oneAlias.c_str()); } m_pSpriteFramesAliases->setObject(frameKey, oneAlias); } frameKey->release(); // create frame spriteFrame = new CCSpriteFrame(); spriteFrame->initWithTexture(pobTexture, CCRectMake(textureRect.origin.x, textureRect.origin.y, spriteSize.width, spriteSize.height), textureRotated, spriteOffset, spriteSourceSize); } // add sprite frame m_pSpriteFrames->setObject(spriteFrame, key); spriteFrame->release(); } }
//----------------------------------------------------------------- // // void CCXMLLayer::LoadPlist( const char *pList ) { string strPath = GetGameLevelPath(); strPath = strPath + pList; std::string fullpath(CCFileUtils::fullPathFromRelativePath(strPath.c_str())); CCDictionary<std::string, CCObject*> *dict = CCFileUtils::dictionaryWithContentsOfFile(fullpath.c_str()); CCDictionary<std::string, CCObject*> *imagesDict = (CCDictionary<std::string, CCObject*>*)dict->objectForKey(std::string("images")); m_vNodeObject.clear(); imagesDict->begin(); std::string key = ""; CCDictionary<std::string, CCObject*> *imageDict = NULL; while( (imageDict = (CCDictionary<std::string, CCObject*>*)imagesDict->next(&key)) ) { float x = (float)atof(valueForKey("x", imageDict)); float y = (float)atof(valueForKey("y", imageDict)); float w = (float)atof(valueForKey("width", imageDict)); float h = (float)atof(valueForKey("height", imageDict)); // #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) // if( CCDirector::sharedDirector()->getWinSize().width == 1024 || 2 == CC_CONTENT_SCALE_FACTOR() ) // { // // } // else // { // x = x / 2; // y = y / 2; // w = w / 2; // h = h / 2; // } // #endif float layer = (float)atof(valueForKey("layer", imageDict)); bool flipX = (bool)atoi(valueForKey("FlipX", imageDict)); bool flipY = (bool)atoi(valueForKey("FlipY", imageDict)); TurnitorqueWorldToCoco2d( x, y, layer ); if( strstr( key.c_str(), "t2dSceneObject_") ) { CCNode *pNode = CCNode::node(); //pNode->setPositionInPixels( ccp( x, y ) ); pNode->setPosition( ccp( x / CC_CONTENT_SCALE_FACTOR(), y / CC_CONTENT_SCALE_FACTOR() ) ); CCSize size( w / CC_CONTENT_SCALE_FACTOR(), h / CC_CONTENT_SCALE_FACTOR() ); pNode->setContentSize( size ); string *pNameKey = new string( key ); pNode->setUserData( pNameKey ); addChild( pNode, (int)layer ); m_vNodeObject.push_back( pNode ); } else if( strstr( key.c_str(), "t2dAnimatedSprite_") ) { AdvanceSprite *m_pAnimation = new AdvanceSprite(); string plist = (valueForKey("plist", imageDict)); m_pAnimation->addFramesFromiT2D( plist.c_str() ); m_pAnimation->autorelease(); //m_pAnimation->setPositionInPixels( ccp( x, y ) ); m_pAnimation->setPosition( ccp( x / CC_CONTENT_SCALE_FACTOR(), y / CC_CONTENT_SCALE_FACTOR() ) ); m_pAnimation->setScaleX( ( w / CC_CONTENT_SCALE_FACTOR() ) / m_pAnimation->getContentSize().width ); m_pAnimation->setScaleY( ( h / CC_CONTENT_SCALE_FACTOR() ) / m_pAnimation->getContentSize().height ); m_pAnimation->setFlipX( flipX ); m_pAnimation->setFlipY( flipY ); string *pNameKey = new string( key ); m_pAnimation->setUserData( pNameKey ); int startFrameIndex = (int)atoi(valueForKey("startframe", imageDict)); int endFrameIndex = (int)atoi(valueForKey("endframe", imageDict)); float time = (float)atof(valueForKey("animationtime", imageDict)); m_pAnimation->startAnimation( startFrameIndex, endFrameIndex, -1, NULL, this, (float)( ( endFrameIndex - startFrameIndex + 1 ) / time ), false, false ); addChild( m_pAnimation, (int)layer ); m_vNodeObject.push_back( m_pAnimation ); } else { const char *image = valueForKey("image", imageDict); string ImagePath = GetGameImagesPath(); ImagePath = ImagePath + image; CCSprite *pSprite = CCSprite::spriteWithFile( ImagePath.c_str() ); //pSprite->setPositionInPixels( ccp( x, y ) ); pSprite->setPosition( ccp( x / CC_CONTENT_SCALE_FACTOR(), y / CC_CONTENT_SCALE_FACTOR() ) ); pSprite->setScaleX( ( w / CC_CONTENT_SCALE_FACTOR() ) / pSprite->getTextureRect().size.width ); pSprite->setScaleY( ( h / CC_CONTENT_SCALE_FACTOR() ) / pSprite->getTextureRect().size.height ); pSprite->setFlipX( flipX ); pSprite->setFlipY( flipY ); string *pNameKey = new string( key ); pSprite->setUserData( pNameKey ); addChild( pSprite, (int)layer ); m_vNodeObject.push_back( pSprite ); } } }