bool CCActivityIndicator::init() { auto spritecache = SpriteFrameCache::getInstance(); spritecache->addSpriteFramesWithFile("ccactivityindicator.plist"); CCSpriteBatchNode::initWithFile("ccactivityindicator.png", 1); Size winSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); indicator = CCSprite::createWithSpriteFrameName("ccactivityindicator_1.gif"); indicator->setPosition(Vec2(winSize.width/2,winSize.height/2)); addChild(indicator); animating = false; hidesWhenStopped = true; // beginCallback = NULL; // endCallback = //load all sprite frames into array for (int i=1; i<=kActivityIndicatorFramesCount; i++) { SpriteFrame * frame = spritecache->getSpriteFrameByName(CCString::createWithFormat("ccactivityindicator_%d.gif",i)->getCString()); frame->retain(); spriteFrames.pushBack(frame); } return true; }
CacheGif::~CacheGif() { if(m_frameData.size() > 0) for(std::vector<GifSprieFrame*>::iterator iter = m_frameData.begin(); iter != m_frameData.end(); ) { GifSprieFrame* sprite = *iter; ++iter; uint32_t index = sprite->Index(); std::string gifFrameName = getGifFrameName(index); do { SpriteFrame* spriteFrame = sprite->getSpriteFrame(); CC_BREAK_IF(spriteFrame == NULL); bool spriteFrameInCache = SpriteFrameCache::getInstance()->getSpriteFrameByName(gifFrameName.c_str()) == spriteFrame; //1. just GifSprieFrame retain //2. CCSpriteFrameCache and GifSprieFrame retain //more. other gif CacheGif retain if(spriteFrame->getReferenceCount() == 1 || (spriteFrame->getReferenceCount() ==2 && spriteFrameInCache)) { Texture2D* texture = sprite->getSpriteFrame()->getTexture(); Director::getInstance()->getTextureCache()->removeTexture(texture); SpriteFrameCache::getInstance()->removeSpriteFramesFromTexture(texture); } } while (0); delete sprite; } }
//创建战斗动画模板 Animation* AnimationManager::createFightAnimation() { //定义每帧的序号 int fightAnim[] = { 4,6,8,10,13,15,17,19,20,22 }; Vector<SpriteFrame*> animFrames; Texture2D *texture = Director::getInstance()->getTextureCache()->addImage("sword.png"); //Texture2D *texture = TextureCache::getInstance()->addImage("sword.png"); SpriteFrame *frame; int x, y; for (int i = 0; i < 10; i++) { //计算每帧在整个纹理中的偏移量 x = fightAnim[i] % 5 - 1; y = fightAnim[i] / 5; frame = SpriteFrame::createWithTexture(texture, Rect(192*x, 192*y, 192, 192)); //第17和19帧在y方向上有-8的偏移 if (fightAnim[i] == 17 || fightAnim[i] == 19) { frame->setOffsetInPixels( Vec2(0, -8) ); } animFrames.pushBack(frame); } Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.1f); //animation->createWithSpriteFrames(animFrames, 0.1f); return animation; }
void SpriteFrameCache::removeUnusedSpriteFrames() { bool removed = false; std::vector<std::string> toRemoveFrames; for (auto iter = _spriteFrames.begin(); iter != _spriteFrames.end(); ++iter) { SpriteFrame* spriteFrame = iter->second; if( spriteFrame->getReferenceCount() == 1 ) { toRemoveFrames.push_back(iter->first); spriteFrame->getTexture()->removeSpriteFrameCapInset(spriteFrame); CCLOG("cocos2d: SpriteFrameCache: removing unused frame: %s", iter->first.c_str()); removed = true; } } _spriteFrames.erase(toRemoveFrames); // FIXME:. Since we don't know the .plist file that originated the frame, we must remove all .plist from the cache if( removed ) { _loadedFileNames->clear(); } }
SpriteFrame* SpriteFrame::createWithTexture(Texture2D* pobTexture, const Rect& rect, bool rotated, const Point& offset, const Size& originalSize) { SpriteFrame *pSpriteFrame = new SpriteFrame();; pSpriteFrame->initWithTexture(pobTexture, rect, rotated, offset, originalSize); pSpriteFrame->autorelease(); return pSpriteFrame; }
SpriteFrame* SpriteFrame::clone() const { // no copy constructor SpriteFrame *copy = new SpriteFrame(); copy->initWithTextureFilename(_textureFilename.c_str(), _rectInPixels, _rotated, _offsetInPixels, _originalSizeInPixels); copy->setTexture(_texture); return copy; }
SpriteFrame* SpriteFrame::create(const char* filename, const Rect& rect, bool rotated, const Point& offset, const Size& originalSize) { SpriteFrame *pSpriteFrame = new SpriteFrame();; pSpriteFrame->initWithTextureFilename(filename, rect, rotated, offset, originalSize); pSpriteFrame->autorelease(); return pSpriteFrame; }
SpriteFrame* SpriteFrame::createWithTexture(Texture2D* texture, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize) { SpriteFrame *spriteFrame = new SpriteFrame(); spriteFrame->initWithTexture(texture, rect, rotated, offset, originalSize); spriteFrame->autorelease(); return spriteFrame; }
SpriteFrame* SpriteFrame::create(const std::string& filename, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize) { SpriteFrame *spriteFrame = new SpriteFrame(); spriteFrame->initWithTextureFilename(filename, rect, rotated, offset, originalSize); spriteFrame->autorelease(); return spriteFrame; }
SpriteFrame* SpriteFrame::createWithTexture(Texture2D *texture, const Rect& rect) { SpriteFrame *spriteFrame = new (std::nothrow) SpriteFrame(); spriteFrame->initWithTexture(texture, rect); spriteFrame->autorelease(); return spriteFrame; }
SpriteFrame* SpriteFrame::createWithTexture(Texture2D *pobTexture, const Rect& rect) { SpriteFrame *pSpriteFrame = new SpriteFrame();; pSpriteFrame->initWithTexture(pobTexture, rect); pSpriteFrame->autorelease(); return pSpriteFrame; }
SpriteFrame* SpriteFrame::clone() const { // no copy constructor SpriteFrame *copy = new (std::nothrow) SpriteFrame(); copy->initWithTexture(_texture, _rectInPixels, _rotated, _offsetInPixels, _originalSizeInPixels); copy->setPolygonInfo(_polygonInfo); copy->autorelease(); return copy; }
SpriteFrame* SpriteFrame::create(const std::string& filename, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize) { SpriteFrame *spriteFrame = new (std::nothrow) SpriteFrame(); if (spriteFrame && spriteFrame->initWithTextureFilename(filename, rect, rotated, offset, originalSize)) { spriteFrame->autorelease(); return spriteFrame; } delete spriteFrame; return nullptr; }
bool StartLayer::init() { if (!Layer::init()) { return false; } Size screenSize = Director::getInstance()->getVisibleSize(); SCALE_DENSITY = screenSize.width / 640; this->setScale(SCALE_DENSITY); Sprite *bgSprite = Sprite::create("loading.png"); bgSprite->setPosition(Point(screenSize.width/2, screenSize.height/2)); this->addChild(bgSprite); Sprite* card = Sprite::create(); Animation* animation = Animation::create(); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("card.plist"); int cardheight; for(int i=0;i<13;i++){ char framename[64]; sprintf(framename, "card%d.png",i+1); SpriteFrame *sf = SpriteFrameCache::getInstance()->getSpriteFrameByName(framename); Rect rect = sf->getRect(); cardheight = rect.size.height; animation->addSpriteFrame(sf); } animation->setDelayPerUnit(0.1f); Animate* cardAni = Animate::create(animation); RepeatForever* rf = RepeatForever::create(cardAni); card->setPosition(Point(screenSize.width/2,screenSize.height/2 + cardheight)); this->addChild(card,1); card->runAction(rf); // リクエスト生成 HttpRequest* request = new HttpRequest(); request->setUrl("http://httpbin.org/post"); request->setRequestType(HttpRequest::Type::POST); request->setResponseCallback(this, httpresponse_selector(StartLayer::onHttpRequestCompleted)); request->setTag("POST test1"); // POSTデータ生成 string postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest"; request->setRequestData(postData.c_str(), postData.length()); // リクエスト送信 HttpClient::getInstance()->send(request); request->release(); return true; }
Sprite* Sprite::createByKey(const char* key_1, ...) { Sprite* ret = Sprite::create(); va_list ap; va_start(ap, key_1); SpriteFrame* sf = SpriteFrameManager::getInstance()->getFrameByVMap(ap, key_1); ret->setFrame(sf); va_end(ap); OutputDebugStringA(sf->getKey().c_str()); return ret; }
Sprite* TuiManager::createAnim(float tag,const char* name,const char* plist,float x,float y,float rotation){ SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist); Animation* pAnim = TuiUtil::createAnimWithName(name,0.05f,-1); SpriteFrame *pTmpFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(string(name) + "1.png"); Sprite* pSprite = Sprite::create(); pSprite->runAction(Animate::create(pAnim)); pSprite->setPosition(Vec2(x,-y)); pSprite->setRotation(rotation); pSprite->setContentSize(pTmpFrame->getOriginalSize()); pSprite->setTag(tag); return pSprite; }
NS_CC_BEGIN // implementation of SpriteFrame SpriteFrame* SpriteFrame::create(const char* filename, const Rect& rect) { SpriteFrame *pSpriteFrame = new SpriteFrame();; pSpriteFrame->initWithTextureFilename(filename, rect); pSpriteFrame->autorelease(); return pSpriteFrame; }
NS_CC_BEGIN // implementation of SpriteFrame SpriteFrame* SpriteFrame::create(const std::string& filename, const Rect& rect) { SpriteFrame *spriteFrame = new (std::nothrow) SpriteFrame(); spriteFrame->initWithTextureFilename(filename, rect); spriteFrame->autorelease(); return spriteFrame; }
void LightEffect::prepareForRender(Sprite *sprite, Texture2D *normalmap) { auto gl = getGLProgramState(); gl->setUniformVec2("u_contentSize", sprite->getContentSize()); Point posRelToSprite = PointApplyAffineTransform(Point(_lightPos.x, _lightPos.y), sprite->getWorldToNodeAffineTransform()); gl->setUniformVec3("u_lightPos", Vec3(posRelToSprite.x, posRelToSprite.y, _lightPos.z)); gl->setUniformTexture("u_normals", normalmap); int opacity = sprite->getOpacity(); SpriteFrame *frame = sprite->getSpriteFrame(); Size untrimmedSize = frame->getOriginalSize(); Size trimmedSize = frame->getRect().size; Vec2 framePos = frame->getRect().origin; Size texSize = frame->getTexture()->getContentSize(); // set sprite position in sheet gl->setUniformVec2("u_spritePosInSheet", Vec2(framePos.x / texSize.width, framePos.y / texSize.height)); gl->setUniformVec2("u_spriteSizeRelToSheet", Vec2(untrimmedSize.width / texSize.width, untrimmedSize.height / texSize.height)); gl->setUniformInt("u_spriteRotated", frame->isRotated()); gl->setUniformFloat("u_spriteOpacity", (float)opacity / 255.0f); // set offset of trimmed sprite Vec2 bottomLeft = frame->getOffset() + (untrimmedSize - trimmedSize) / 2; Vec2 cornerOffset = frame->isRotated() ? Vec2(bottomLeft.y, bottomLeft.x) : Vec2(bottomLeft.x, untrimmedSize.height - trimmedSize.height - bottomLeft.y); gl->setUniformVec2("u_spriteOffset", cornerOffset); }
bool SGSHero::initAttackActions() { std::string hero_attack_res_file_full_path = getHeroResFile(SG_SKIRMISH_SCENE_HERO_ATTACK_RES_PATH); Texture2D* texture = Director::getInstance()->getTextureCache()->addImage(hero_attack_res_file_full_path); for (int i = 0; i < 12; i++) { SpriteFrame* frame = SpriteFrame::createWithTexture( texture, Rect(0, SG_SKIRMISH_SCENE_HERO_ATTACK_RES_HEIGHT * i, SG_SKIRMISH_SCENE_HERO_ATTACK_RES_WIDTH, SG_SKIRMISH_SCENE_HERO_ATTACK_RES_HEIGHT)); frame->retain(); __attack_sprite_frames.pushBack(frame); } Vector<SpriteFrame*>* animFrames_south = new Vector<SpriteFrame*>; animFrames_south->pushBack(__attack_sprite_frames.at(0)); animFrames_south->pushBack(__attack_sprite_frames.at(1)); animFrames_south->pushBack(__attack_sprite_frames.at(2)); animFrames_south->pushBack(__attack_sprite_frames.at(3)); Animation* animation_south = Animation::createWithSpriteFrames(*animFrames_south, SGS_HERO_ATTACK_ACTION_INTERVAL); Animate* animate_south = Animate::create(animation_south); animate_south->retain(); std::string animate_name = "attack_south"; __animate_map[animate_name] = animate_south; Vector<SpriteFrame*>* animFrames_north = new Vector<SpriteFrame*>; animFrames_north->pushBack(__attack_sprite_frames.at(4)); animFrames_north->pushBack(__attack_sprite_frames.at(5)); animFrames_north->pushBack(__attack_sprite_frames.at(6)); animFrames_north->pushBack(__attack_sprite_frames.at(7)); Animation* animation_north = Animation::createWithSpriteFrames(*animFrames_north, SGS_HERO_ATTACK_ACTION_INTERVAL); Animate* animate_north = Animate::create(animation_north); animate_north->retain(); animate_name = "attack_north"; __animate_map[animate_name] = animate_north; Vector<SpriteFrame*>* animFrames_west = new Vector<SpriteFrame*>; animFrames_west->pushBack(__attack_sprite_frames.at(8)); animFrames_west->pushBack(__attack_sprite_frames.at(9)); animFrames_west->pushBack(__attack_sprite_frames.at(10)); animFrames_west->pushBack(__attack_sprite_frames.at(11)); Animation* animation_west = Animation::createWithSpriteFrames(*animFrames_west, SGS_HERO_ATTACK_ACTION_INTERVAL); Animate* animate_west = Animate::create(animation_west); animate_west->retain(); animate_name = "attack_west"; __animate_map[animate_name] = animate_west; return true; }
void SpriteFrameCache::removeSpriteFramesFromTexture(Texture2D* texture) { std::vector<std::string> keysToRemove; for (auto iter = _spriteFrames.cbegin(); iter != _spriteFrames.cend(); ++iter) { std::string key = iter->first; SpriteFrame* frame = _spriteFrames.at(key); if (frame && (frame->getTexture() == texture)) { keysToRemove.push_back(key); } } _spriteFrames.erase(keysToRemove); }
void SpriteFrameCachePixelFormatTest::loadSpriteFrames(const std::string &file, cocos2d::Texture2D::PixelFormat expectedFormat) { SpriteFrameCache::getInstance()->addSpriteFramesWithFile(file); SpriteFrame *spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName("grossini.png"); Texture2D *texture = spriteFrame->getTexture(); const ssize_t bitsPerKB = 8 * 1024; const double memorySize = 1.0 * texture->getBitsPerPixelForFormat() * texture->getContentSizeInPixels().width * texture->getContentSizeInPixels().height / bitsPerKB; //FIXME: texture->getPixelFormat() != expectedFormat all the time, such as Texture2D::PixelFormat::PVRTC2A // CC_ASSERT(texture->getPixelFormat() == expectedFormat); const std::string textureInfo = StringUtils::format("%s: %.2f KB\r\n", texture->getStringForFormat(), memorySize); infoLabel->setString(infoLabel->getString() + textureInfo); SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(file); Director::getInstance()->getTextureCache()->removeTexture(texture); }
void SpriteSequence::openFrameSequence() { if (!isSequenceOpen()) { FrameSequence::openFrameSequence(); if (isSequenceOpen()) { uint32 numFrames = getNumFrames(); for (uint32 i = 0; i < numFrames; ++i) { SpriteFrame *frame = new SpriteFrame(); frame->initFromPICTResource(_resFork, i + 0x80, _transparent); _sprite.addFrame(frame, 0, 0); } _sprite.setBounds(_bounds); } } }
bool SGSHero::initSpecActions() { std::string hero_spec_res_file_full_path = getHeroResFile(SG_SKIRMISH_SCENE_HERO_SPEC_RES_PATH); Texture2D* texture = Director::getInstance()->getTextureCache()->addImage(hero_spec_res_file_full_path); for (int i = 0; i < 5; i++) { SpriteFrame* frame = SpriteFrame::createWithTexture( texture, Rect(0, SG_SKIRMISH_SCENE_HERO_SPEC_RES_WIDTH * i, SG_SKIRMISH_SCENE_HERO_SPEC_RES_WIDTH, SG_SKIRMISH_SCENE_HERO_SPEC_RES_HEIGHT)); frame->retain(); __spec_sprite_frames.pushBack(frame); } Vector<SpriteFrame*>* block_south = new Vector<SpriteFrame*>; block_south->pushBack(__spec_sprite_frames.at(0)); Animation* animation_south = Animation::createWithSpriteFrames(*block_south, 0.5f); Animate* animate_south = Animate::create(animation_south); animate_south->retain(); std::string animate_name = "block_south"; __animate_map[animate_name] = animate_south; Vector<SpriteFrame*>* block_north = new Vector<SpriteFrame*>; block_north->pushBack(__spec_sprite_frames.at(1)); Animation* animation_north = Animation::createWithSpriteFrames(*block_north, 0.5f); Animate* animate_north = Animate::create(animation_north); animate_north->retain(); animate_name = "block_north"; __animate_map[animate_name] = animate_north; Vector<SpriteFrame*>* block_west = new Vector<SpriteFrame*>; block_west->pushBack(__spec_sprite_frames.at(2)); Animation* animation_west = Animation::createWithSpriteFrames(*block_west, 0.5f); Animate* animate_west = Animate::create(animation_west); animate_west->retain(); animate_name = "block_west"; __animate_map[animate_name] = animate_west; Vector<SpriteFrame*>* attacked = new Vector<SpriteFrame*>; attacked->pushBack(__spec_sprite_frames.at(3)); Animation* animation_attacked = Animation::createWithSpriteFrames(*attacked, 1.0f); Animate* animate_attack = Animate::create(animation_attacked); animate_attack->retain(); animate_name = "attacked"; __animate_map[animate_name] = animate_attack; return true; }
void NoradElevator::openInteraction() { SpriteFrame *frame = new SpriteFrame(); frame->initFromPICTResource(((PegasusEngine *)g_engine)->_resFork, kElevatorLabelID, true); _elevatorControls.addFrame(frame, 0, 0); frame = new SpriteFrame(); frame->initFromPICTResource(((PegasusEngine *)g_engine)->_resFork, kElevatorButtonsID, true); _elevatorControls.addFrame(frame, 0, 0); frame = new SpriteFrame(); frame->initFromPICTResource(((PegasusEngine *)g_engine)->_resFork, kElevatorDownOnID, true); _elevatorControls.addFrame(frame, 0, 0); frame = new SpriteFrame(); frame->initFromPICTResource(((PegasusEngine *)g_engine)->_resFork, kElevatorUpOnID, true); _elevatorControls.addFrame(frame, 0, 0); _elevatorControls.setCurrentFrameIndex(0); _elevatorControls.setDisplayOrder(kElevatorControlsOrder); Common::Rect r; frame->getSurfaceBounds(r); r.moveTo(kNoradAlphaElevatorControlsLeft, kNoradAlphaElevatorControlsTop); _elevatorControls.setBounds(r); _elevatorControls.startDisplaying(); _elevatorControls.show(); }
EnergyMonitor::EnergyMonitor() : IdlerAnimation(kEnergyBarID), _energyLight(kWarningLightID) { PegasusEngine *vm = (PegasusEngine *)g_engine; _stage = kStageNoStage; _calibrating = false; _dontFlash = false; setBounds(338, 48, 434, 54); setDisplayOrder(kEnergyBarOrder); startDisplaying(); SpriteFrame *frame = new SpriteFrame(); frame->initFromPICTResource(vm->_resFork, kLightOffID); _energyLight.addFrame(frame, 0, 0); frame = new SpriteFrame(); frame->initFromPICTResource(vm->_resFork, kLightYellowID); _energyLight.addFrame(frame, 0, 0); frame = new SpriteFrame(); frame->initFromPICTResource(vm->_resFork, kLightOrangeID); _energyLight.addFrame(frame, 0, 0); frame = new SpriteFrame(); frame->initFromPICTResource(vm->_resFork, kLightRedID); _energyLight.addFrame(frame, 0, 0); _energyLight.setBounds(540, 35, 600, 59); _energyLight.setDisplayOrder(kEnergyLightOrder); _energyLight.startDisplaying(); setScale(1); setSegment(0, kMaxJMPEnergy); setEnergyValue(kCasualEnergy); g_energyMonitor = this; }
void LightsHolder::prepareForRender(Sprite *_sprite, Texture2D *normalmap) { int idx=0; for(auto &ef : _effectsArray) { if (ef != nullptr) { Point posRelToSprite = PointApplyAffineTransform(Point(ef->getLightPos().x, ef->getLightPos().y),_sprite->getWorldToNodeAffineTransform()); _lightPos[idx]=Vec3(posRelToSprite.x, posRelToSprite.y, ef->getLightPos().z); brightness[idx]=ef->getBrightness(); } idx++; } auto gl = getGLProgramState(); gl->setUniformVec2("u_contentSize", _sprite->getContentSize()); gl->setUniformVec3v("u_lightPos",_lightsNumber,_lightPos); gl->setUniformTexture("u_normals", normalmap); SpriteFrame *frame = _sprite->getSpriteFrame(); Size untrimmedSize = frame->getOriginalSize(); Size trimmedSize = frame->getRect().size; Vec2 framePos = frame->getRect().origin; Size texSize = frame->getTexture()->getContentSize(); // set sprite position in sheet gl->setUniformVec2("u_spritePosInSheet", Vec2(framePos.x / texSize.width, framePos.y / texSize.height)); gl->setUniformVec2("u_spriteSizeRelToSheet", Vec2(untrimmedSize.width / texSize.width, untrimmedSize.height / texSize.height)); gl->setUniformFloatv("u_brightness",_lightsNumber, brightness); gl->setUniformInt("u_spriteRotated", frame->isRotated()); gl->setUniformInt("u_lightsNumber", _lightsNumber); // set offset of trimmed sprite Vec2 bottomLeft = frame->getOffset() + (untrimmedSize - trimmedSize) / 2; Vec2 cornerOffset = frame->isRotated() ? Vec2(bottomLeft.y, bottomLeft.x) : Vec2(bottomLeft.x, untrimmedSize.height - trimmedSize.height - bottomLeft.y); gl->setUniformVec2("u_spriteOffset", cornerOffset); }
Texture2D * ParticleSystemQuadLoader::parsePropTypeTexture(Node * pNode, Node * pParent, CCBReader * ccbReader) { std::string spriteFile = ccbReader->readCachedString(); memset(&_textureRect, 0, sizeof(_textureRect)); if (spriteFile.length() > 0) { Texture2D* texture = Director::getInstance()->getTextureCache()->addImage(ccbReader->getCCBRootPath() + spriteFile.c_str()); if (texture == NULL) { SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFile); texture = spriteFrame->getTexture(); _textureRect.origin = spriteFrame->getOffsetInPixels(); _textureRect.size = spriteFrame->getOriginalSizeInPixels(); } return texture; } else { return NULL; } }
void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Texture2D* texture) { /* 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+ */ ValueMap& framesDict = dictionary["frames"].asValueMap(); int format = 0; // get the format if (dictionary.find("metadata") != dictionary.end()) { ValueMap& metadataDict = dictionary["metadata"].asValueMap(); format = metadataDict["format"].asInt(); } // check the format CCASSERT(format >=0 && format <= 3, "format is not supported for SpriteFrameCache addSpriteFramesWithDictionary:textureFilename:"); for (auto iter = framesDict.begin(); iter != framesDict.end(); ++iter) { ValueMap& frameDict = iter->second.asValueMap(); std::string spriteFrameName = iter->first; SpriteFrame* spriteFrame = _spriteFrames.at(spriteFrameName); if (spriteFrame) { continue; } if(format == 0) { float x = frameDict["x"].asFloat(); float y = frameDict["y"].asFloat(); float w = frameDict["width"].asFloat(); float h = frameDict["height"].asFloat(); float ox = frameDict["offsetX"].asFloat(); float oy = frameDict["offsetY"].asFloat(); int ow = frameDict["originalWidth"].asInt(); int oh = frameDict["originalHeight"].asInt(); // check ow/oh if(!ow || !oh) { CCLOGWARN("cocos2d: WARNING: originalWidth/Height not found on the SpriteFrame. AnchorPoint won't work as expected. Regenrate the .plist"); } // abs ow/oh ow = abs(ow); oh = abs(oh); // create frame spriteFrame = new SpriteFrame(); spriteFrame->initWithTexture(texture, Rect(x, y, w, h), false, Vec2(ox, oy), Size((float)ow, (float)oh) ); } else if(format == 1 || format == 2) { Rect frame = RectFromString(frameDict["frame"].asString()); bool rotated = false; // rotation if (format == 2) { rotated = frameDict["rotated"].asBool(); } Vec2 offset = PointFromString(frameDict["offset"].asString()); Size sourceSize = SizeFromString(frameDict["sourceSize"].asString()); // create frame spriteFrame = new SpriteFrame(); spriteFrame->initWithTexture(texture, frame, rotated, offset, sourceSize ); } else if (format == 3) { // get values Size spriteSize = SizeFromString(frameDict["spriteSize"].asString()); Vec2 spriteOffset = PointFromString(frameDict["spriteOffset"].asString()); Size spriteSourceSize = SizeFromString(frameDict["spriteSourceSize"].asString()); Rect textureRect = RectFromString(frameDict["textureRect"].asString()); bool textureRotated = frameDict["textureRotated"].asBool(); // get aliases ValueVector& aliases = frameDict["aliases"].asValueVector(); for(const auto &value : aliases) { std::string oneAlias = value.asString(); if (_spriteFramesAliases.find(oneAlias) != _spriteFramesAliases.end()) { CCLOGWARN("cocos2d: WARNING: an alias with name %s already exists", oneAlias.c_str()); } _spriteFramesAliases[oneAlias] = Value(spriteFrameName); } // create frame spriteFrame = new SpriteFrame(); spriteFrame->initWithTexture(texture, Rect(textureRect.origin.x, textureRect.origin.y, spriteSize.width, spriteSize.height), textureRotated, spriteOffset, spriteSourceSize); } // add sprite frame _spriteFrames.insert(spriteFrameName, spriteFrame); spriteFrame->release(); } }
bool SGSHero::initActions() { std::string hero_res_file_full_path = getHeroResFile(SG_SKIRMISH_SCENE_HERO_RES_PATH); // Init hero start picture if (!Sprite::initWithFile(hero_res_file_full_path, Rect(0, 0, SG_SKIRMISH_SCENE_HERO_WALK_RES_WIDTH, SG_SKIRMISH_SCENE_HERO_WALK_RES_HEIGHT))) { return false; } Texture2D* texture = Director::getInstance()->getTextureCache()->addImage(hero_res_file_full_path); for (int i = 0; i < 11; i++) { SpriteFrame* frame = SpriteFrame::createWithTexture( texture, Rect(0, SG_SKIRMISH_SCENE_HERO_WALK_RES_HEIGHT * i, SG_SKIRMISH_SCENE_HERO_WALK_RES_WIDTH, SG_SKIRMISH_SCENE_HERO_WALK_RES_HEIGHT)); frame->retain(); __sprite_frames.pushBack(frame); } Vector<SpriteFrame*>* animFrames = new Vector<SpriteFrame*>; animFrames->pushBack(__sprite_frames.at(0)); animFrames->pushBack(__sprite_frames.at(1)); Animation* animation = Animation::createWithSpriteFrames(*animFrames, 0.2f); Animate* animate = Animate::create(animation); animate->retain(); std::string animate_name = "face_south"; __animate_map[animate_name] = animate; Vector<SpriteFrame*>* animFrames_north = new Vector<SpriteFrame*>; animFrames_north->pushBack(__sprite_frames.at(2)); animFrames_north->pushBack(__sprite_frames.at(3)); Animation* animation_north = Animation::createWithSpriteFrames(*animFrames_north, 0.2f); Animate* animate_north = Animate::create(animation_north); animate_north->retain(); animate_name = "face_north"; __animate_map[animate_name] = animate_north; Vector<SpriteFrame*>* animFrames_west = new Vector<SpriteFrame*>; animFrames_west->pushBack(__sprite_frames.at(4)); animFrames_west->pushBack(__sprite_frames.at(5)); Animation* animation_west = Animation::createWithSpriteFrames(*animFrames_west, 0.2f); Animate* animate_west = Animate::create(animation_west); animate_west->retain(); animate_name = "face_west"; __animate_map[animate_name] = animate_west; Vector<SpriteFrame*>* fragile = new Vector<SpriteFrame*>; fragile->pushBack(__sprite_frames.at(9)); Animation* animation_fragile = Animation::createWithSpriteFrames(*fragile, 1.0f); Animate* animate_fragile = Animate::create(animation_fragile); animate_fragile->retain(); animate_name = "fragile"; __animate_map[animate_name] = animate_fragile; Vector<SpriteFrame*>* wheeze = new Vector<SpriteFrame*>; wheeze->pushBack(__sprite_frames.at(9)); wheeze->pushBack(__sprite_frames.at(10)); Animation* animation_wheeze = Animation::createWithSpriteFrames(*wheeze, 0.2f); Animate* animate_wheeze = Animate::create(animation_wheeze); animate_wheeze->retain(); animate_name = "wheeze"; __animate_map[animate_name] = animate_wheeze; return true; }