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
	assert(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();
	}
}
Example #2
0
// loads info about all tiles,sets self.contentSize & screenLoadRectExtension
// creates & adds tiles for dynamic usage if batchNode
void CCBigImage::prepareTilesWithFileExtensionZ(string plistFile, string extension, int tilesZ)
{
	// load plist with image & tiles info
    CCDictionary *dict = CCDictionary::createWithContentsOfFile(plistFile.c_str());
    if ( !dict )
    {
        CCLOGERROR("CCBigImage#prepareTilesWithFile:extension:z: can not load dictionary from file: %s", plistFile.c_str());
        return;
    }
	
	// load image size
    CCDictionary *sourceDict = (CCDictionary*)dict->objectForKey(std::string("Source"));
    CCSize size = CCSizeFromString(valueForKey("Size", sourceDict));
    
    this->setContentSize(size);
	
	// load tiles
    CCArray* array = (CCArray*)dict->objectForKey(std::string("Tiles"));
	
	_dynamicChildren = CCArray::createWithCapacity(array->count());
    _dynamicChildren->retain();
    
	// set screenLoadRectExtension = size of first tile
	if (array->count())
	{
        CCDictionary *dict_ = (CCDictionary*)array->objectAtIndex(0);
        _screenLoadRectExtension = CCRectFromString(valueForKey("Rect", dict_)).size;
	}
	
	//read data and create nodes and add them
    for (int i=0; i<array->count(); i++)
    {
        CCDictionary* tileDict = (CCDictionary*)array->objectAtIndex(i);

        // All properties of Dictionary
        const char *spriteName = valueForKey("Name", tileDict);
		
		CCRect tileRect = CCRectFromString(valueForKey("Rect", tileDict));
        
		// convert rect origin from top-left to bottom-left
		tileRect.origin.y = this->getContentSize().height - tileRect.origin.y - tileRect.size.height;
		
		// Use forced tile extension or original if tilesExtension isn't set
		if (!extension.empty())
		{
			// Change extension
            string filename = string(spriteName);
            int index = filename.find('.');
            string name = filename.substr(0, index);
			spriteName = (name+"."+extension).c_str();
		}

		// Create & Add Tile (Dynamic Sprite Mode)
		UnloadableSpriteNode* tile = UnloadableSpriteNode::nodeWithImageForRect(spriteName, tileRect);
		this->addChild(tile, tilesZ);
		_dynamicChildren->addObject(tile);
		
    } //< for dict in arr
    
    dict->release();
	
}
Example #3
0
CCTexture2D* TextureHelper::addImageFromPlist(const char* plist)
{
	std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(plist);
	CCDictionary *dict = CCDictionary::createWithContentsOfFileThreadSafe(fullPath.c_str());
	CCDictionary *metadataDict = (CCDictionary*)dict->objectForKey("metadata");
	std::string pngPath = metadataDict->valueForKey("realTextureFileName")->getCString();
	CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage(pngPath.c_str());
	CCDictionary *framesDict = (CCDictionary*)dict->objectForKey("frames");
	int format = 0;
	CCSize textureSize = texture->getContentSize();

	if(metadataDict != NULL) 
	{
		format = metadataDict->valueForKey("format")->intValue();
	}

	CCAssert(format >=0 && format <= 3, "format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:textureFilename:");

	CCDictElement* pElement = NULL;
	CCDICT_FOREACH(framesDict, pElement)
	{
		CCRect rect;
		CCDictionary* frameDict = (CCDictionary*)pElement->getObject();
		std::string spriteFrameName = pElement->getStrKey();
	
		if(format == 0) 
		{
			float x = frameDict->valueForKey("x")->floatValue();
			float y = frameDict->valueForKey("y")->floatValue();
			float w = frameDict->valueForKey("width")->floatValue();
			float h = frameDict->valueForKey("height")->floatValue();
			float ox = frameDict->valueForKey("offsetX")->floatValue();
			float oy = frameDict->valueForKey("offsetY")->floatValue();
			int ow = frameDict->valueForKey("originalWidth")->intValue();
			int oh = frameDict->valueForKey("originalHeight")->intValue();
			if(!ow || !oh)
			{
				CCLOGWARN("cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenrate the .plist");
			}
			ow = abs(ow);
			oh = abs(oh);
			rect = CCRectMake(x + ox,textureSize.height - y + oy,ow,oh);
		 } 
		 else if(format == 1 || format == 2) 
		 {
			  CCRect frame = CCRectFromString(frameDict->valueForKey("frame")->getCString());
			  frame.origin.y = textureSize.height - frame.origin.y;
			  bool rotated = false;

			  if (format == 2)
			  {
				  rotated = frameDict->valueForKey("rotated")->boolValue();
			  }

			  CCPoint offset = CCPointFromString(frameDict->valueForKey("offset")->getCString());
			  CCSize sourceSize = CCSizeFromString(frameDict->valueForKey("sourceSize")->getCString());
			  rect = frame;
		  } 
		  else if (format == 3)
		  {
			  CCSize spriteSize = CCSizeFromString(frameDict->valueForKey("spriteSize")->getCString());
			  CCPoint spriteOffset = CCPointFromString(frameDict->valueForKey("spriteOffset")->getCString());
			  CCSize spriteSourceSize = CCSizeFromString(frameDict->valueForKey("spriteSourceSize")->getCString());
			  CCRect textureRect = CCRectFromString(frameDict->valueForKey("textureRect")->getCString());
			  bool textureRotated = frameDict->valueForKey("textureRotated")->boolValue();
			  rect = textureRect;
		  }
		 rectMap.insert(std::pair<std::string,CCRect>(spriteFrameName,rect));
	}