CCActionInterval* CCAnimate::reverse(void) { CCMutableArray<CCSpriteFrame*> *pOldArray = m_pAnimation->getFrames(); CCMutableArray<CCSpriteFrame*> *pNewArray = new CCMutableArray<CCSpriteFrame*>(pOldArray->count()); if (pOldArray->count() > 0) { CCSpriteFrame *pElement; CCMutableArray<CCSpriteFrame*>::CCMutableArrayRevIterator iter; for (iter = pOldArray->rbegin(); iter != pOldArray->rend(); iter++) { pElement = *iter; if (! pElement) { break; } pNewArray->addObject((CCSpriteFrame*)(pElement->copy()->autorelease())); } } CCAnimation *pNewAnim = CCAnimation::animationWithFrames(pNewArray, m_pAnimation->getDelay()); pNewArray->release(); return CCAnimate::actionWithDuration(m_fDuration, pNewAnim, m_bRestoreOriginalFrame); }
unsigned int CCSpriteBatchNode::rebuildIndexInOrder(CCSprite *pobParent, unsigned int uIndex) { CCMutableArray<CCNode*> *pChildren = pobParent->getChildren(); if (pChildren && pChildren->count() > 0) { CCSprite *pSprite; CCMutableArray<CCNode*>::CCMutableArrayIterator iter; for (iter = pChildren->begin(); iter != pChildren->end(); ++iter) { pSprite = (CCSprite*)(*iter); if (! pSprite) { break; } if (pSprite->getZOrder() < 0) { uIndex = rebuildIndexInOrder(pSprite, uIndex); } } } // ignore self (batch node) if (! pobParent->isEqual(this)) { pobParent->setAtlasIndex(uIndex); uIndex++; } if (pChildren && pChildren->count() > 0) { CCSprite *pSprite; CCMutableArray<CCNode*>::CCMutableArrayIterator iter; for (iter = pChildren->begin(); iter != pChildren->end(); ++iter) { pSprite = (CCSprite*)(*iter); if (! pSprite) { break; } if (pSprite->getZOrder() >= 0) { uIndex = rebuildIndexInOrder(pSprite, uIndex); } } } return uIndex; }
void CCSpriteBatchNode::insertChild(CCSprite *pobSprite, unsigned int uIndex) { pobSprite->useBatchNode(this); pobSprite->setAtlasIndex(uIndex); pobSprite->setDirty(true); if (m_pobTextureAtlas->getTotalQuads() == m_pobTextureAtlas->getCapacity()) { increaseAtlasCapacity(); } ccV3F_C4B_T2F_Quad quad = pobSprite->getQuad(); m_pobTextureAtlas->insertQuad(&quad, uIndex); m_pobDescendants->insertObjectAtIndex(pobSprite, uIndex); // update indices unsigned int i = 0; if (m_pobDescendants && m_pobDescendants->count() > 0) { CCMutableArray<CCSprite*>::CCMutableArrayIterator iter; for (iter = m_pobDescendants->begin(); iter != m_pobDescendants->end(); ++iter) { if (! *iter) { break; } if (i > uIndex) { (*iter)->setAtlasIndex((*iter)->getAtlasIndex() + 1); } ++i; } } // add children recursively CCMutableArray<CCNode*> *pChildren = pobSprite->getChildren(); if (pChildren && pChildren->count() > 0) { CCMutableArray<CCNode*>::CCMutableArrayIterator iterNode; CCSprite *pSprite; for (iterNode = pChildren->begin(); iterNode != pChildren->end(); ++iterNode) { pSprite = (CCSprite*)(*iterNode); if (! pSprite) { break; } unsigned int uIndex = atlasIndexForChild(pSprite, pSprite->getZOrder()); insertChild(pSprite, uIndex); } } }
unsigned int CCSpriteBatchNode::lowestAtlasIndexInChild(CCSprite *pSprite) { CCMutableArray<CCNode*> *pChildren = pSprite->getChildren(); if (! pChildren || pChildren->count() == 0) { return pSprite->getAtlasIndex(); } else { return lowestAtlasIndexInChild((CCSprite*)(pChildren->getObjectAtIndex(0))); } }
CCTMXTilesetInfo * CCTMXTiledMap::tilesetForLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo) { CCTMXTilesetInfo *tileset = NULL; //CFByteOrder o = CFByteOrderGetCurrent(); CCSize size = layerInfo->m_tLayerSize; CCMutableArray<CCTMXTilesetInfo*>* tilesets = mapInfo->getTilesets(); if (tilesets && tilesets->count()>0) { CCTMXTilesetInfo *tileset = NULL; CCMutableArray<CCTMXTilesetInfo*>::CCMutableArrayRevIterator rit; for (rit = tilesets->rbegin(); rit != tilesets->rend(); ++rit) { tileset = *rit; if (tileset) { for( unsigned int y=0; y < size.height; y++ ) { for( unsigned int x=0; x < size.width; x++ ) { unsigned int pos = (unsigned int)(x + size.width * y); unsigned int gid = layerInfo->m_pTiles[ pos ]; // gid are stored in little endian. // if host is big endian, then swap //if( o == CFByteOrderBigEndian ) // gid = CFSwapInt32( gid ); /* We support little endian.*/ // XXX: gid == 0 --> empty tile if( gid != 0 ) { // Optimization: quick return // if the layer is invalid (more than 1 tileset per layer) an assert will be thrown later if( gid >= tileset->m_uFirstGid ) return tileset; } } } } } } // If all the tiles are 0, return empty tileset CCLOG("cocos2d: Warning: TMX Layer '%@' has no tiles", layerInfo->m_sName.c_str()); return tileset; }
void CCAnimate::update(ccTime time) { CCMutableArray<CCSpriteFrame*> *pFrames = m_pAnimation->getFrames(); unsigned int numberOfFrames = pFrames->count(); unsigned int idx = (unsigned int)(time * numberOfFrames); if (idx >= numberOfFrames) { idx = numberOfFrames - 1; } CCSprite *pSprite = (CCSprite*)(m_pTarget); if (! pSprite->isFrameDisplayed(pFrames->getObjectAtIndex(idx))) { pSprite->setDisplayFrame(pFrames->getObjectAtIndex(idx)); } }
void CCSpriteBatchNode::removeSpriteFromAtlas(CCSprite *pobSprite) { // remove from TextureAtlas m_pobTextureAtlas->removeQuadAtIndex(pobSprite->getAtlasIndex()); // Cleanup sprite. It might be reused (issue #569) pobSprite->useSelfRender(); unsigned int uIndex = m_pobDescendants->getIndexOfObject(pobSprite); if (uIndex != -1) { m_pobDescendants->removeObjectAtIndex(uIndex); // update all sprites beyond this one unsigned int count = m_pobDescendants->count(); for(; uIndex < count; ++uIndex) { CCSprite* s = (CCSprite*)(m_pobDescendants->getObjectAtIndex(uIndex)); s->setAtlasIndex( s->getAtlasIndex() - 1 ); } } // remove children recursively CCMutableArray<CCNode*> *pChildren = pobSprite->getChildren(); if (pChildren && pChildren->count() > 0) { CCSprite *pSprite; CCMutableArray<CCNode*>::CCMutableArrayIterator iter; for (iter = pChildren->begin(); iter != pChildren->end(); ++iter) { pSprite = (CCSprite*)(*iter); if (! pSprite) { break; } removeSpriteFromAtlas(pSprite); } } }
CCMutableArray<StringLabel*>* TagString::parse( bool useTag ) { string cut = ""; int start_idx = 0; float zen_width = 0; // 全角幅を取得 { StringLabel* label_zen = StringLabel::init( "1", ccc3(0,0,0), baseFontSize ); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) zen_width = (int)(label_zen->getContentSize().width); #else zen_width = label_zen->getContentSize().width; #endif } ///////////////////////////////////////////// // 変数タグの置換 start_idx = 0; for( ;; ) { int tag_start1 = tagStr.find( "<param=", start_idx ); if( tag_start1 == string::npos ) break; int tag_start2 = tagStr.find( ">", tag_start1 ); string param_str = tagStr.substr( tag_start1+7, tag_start2-(tag_start1+7) ); // 変数の値取得 string value = ParamList::shared()->getValue( param_str ); if( !value.empty() ) { // 文字列の置換 tagStr.replace( tag_start1, tag_start2-tag_start1+1, value ); start_idx = 0; continue; } start_idx = tag_start2 + 1; } //tagStr->getStringWidth() ///////////////////////////////////////////// // 自動改行チェック if( autoKaigyouFlg ) { vector< string > list = StringComparator::comparate( tagStr ); bool tag_start = false; float total_size = 0; string conv_str = ""; for( int i = 0; i < list.size(); i++ ) { if (useTag) { if( list[i] == "<" ) { tag_start = true; } if( list[i] == ">" ) { tag_start = false; conv_str.append( list[i] ); continue; } if( tag_start ) { // 改行チェック if( list[i] == "b" ) { if( list[i+1] == "r" ) { total_size = 0; } } conv_str.append( list[i] ); continue; } } float strw = 0; // 半角 if( list[i].length() == 1 ) { StringLabel* label = StringLabel::init( list[i].c_str(), ccc3(0,0,0), baseFontSize ); strw = label->getContentSize().width; } // 全角 else { strw = zen_width; } total_size += strw; // 改行を挿入 if( width > 0 && total_size > width ) { //CCLog( "kaigyou2 total_size=%d", total_size ); // 改行コードを入れる conv_str.append( "<br>" ); total_size = strw; } else { if (list[i] == "\n") total_size = 0; } conv_str.append( list[i] ); } tagStr = conv_str; } ///////////////////////////////////////////// // 改行、カラー、フォントサイズ、アンカー解析 int line = 0; start_idx = 0; int active_anchor = 0; int active_font_size = baseFontSize; ccColor3B active_color = baseColor; //CCLog( "tag_str=%s", tagStr.c_str() ); bool brflg = false; for( ;; ) { // 開始タグ判定 int tag_start = tagStr.find( "<", start_idx ); if( tag_start != string::npos && useTag ) { // 表示文字列を取得 cut = tagStr.substr( start_idx, tag_start - (start_idx) ); //CCLog( "cut=%s actuve=%d", cut.c_str(), active_anchor ); if( !cut.empty() || brflg ) { parseList->addObject( new ColorString( cut, active_color, active_font_size, line, active_anchor ) ); } int tag_close = tagStr.find( ">", tag_start ); // タグ文字列取得 string param_str = tagStr.substr( tag_start+1, tag_close-(tag_start+1) ); // アンカー if( param_str.find( "left" ) != string::npos ) { active_anchor = ALIGN_LEFT; } if( param_str.find( "center" ) != string::npos ) { active_anchor = ALIGN_CENTER; } if( param_str.find( "right" ) != string::npos ) { active_anchor = ALIGN_RIGHT; } // 改行 if( param_str.find( "br" ) != string::npos ) { line += 1; brflg = true; } // カラー設定 if( param_str.find( "color=" ) != string::npos ) { // 色情報の切り出し string color_str = param_str.substr( 6, param_str.length()-6 ); // 色成分に分解 char *csv = const_cast<char*>( color_str.c_str() ); int red = CommonUtils::StrToInt( strtok( csv, ":" ) ); int green = CommonUtils::StrToInt( strtok( NULL, ":" ) ); int blue = CommonUtils::StrToInt( strtok( NULL, ":" ) ); active_color = ccc3( red, green, blue ); } // フォントサイズ if( param_str.find( "size=" ) != string::npos ) { string size_str = param_str.substr( 5, param_str.length()-5 ); active_font_size = CommonUtils::StrToInt( size_str ); } // カラー 終了タグ if( param_str.find( "/color" ) != string::npos ) { active_color = baseColor; } // アンカー 終了タグ if( param_str.find( "/center" ) != string::npos ) { active_anchor = ALIGN_LEFT; } if( param_str.find( "/right" ) != string::npos ) { active_anchor = ALIGN_LEFT; } // フォントサイズ 終了タグ if( param_str.find( "/size" ) != string::npos ) { active_font_size = baseFontSize; } start_idx = tag_close + 1; } else { /* for( ;; ) { // 改行コードがある場合 tag_start = tagStr.find( "\n", start_idx ); if (tag_start != string::npos) { // 表示文字列を取得 cut = tagStr.substr( start_idx, tag_start - (start_idx) ); if( !cut.empty() ) { parseList->addObject( new ColorString( cut, active_color, active_font_size, line, active_anchor ) ); line += 1; } start_idx = tag_start + 1; } else { break; } } */ cut = tagStr.substr( start_idx, tagStr.length() - (start_idx) ); if( !cut.empty() ) { //CCLog( "cut=%s", cut.c_str() ); parseList->addObject( new ColorString( cut, active_color, active_font_size, line, active_anchor ) ); } break; } } int linecnt = getLineCount(); float basex = 0; float basey = 0; // アライン設定 if( baseAlign == TEXT_ALIGN_CENTER_TOP ) { basex = ( ( width - getStringWidth() )/2 ); } if( baseAlign == TEXT_ALIGN_RIGHT_TOP ) { basex = width - getStringWidth(); } if( baseAlign == TEXT_ALIGN_LEFT_MIDDLE ) { basey = ( ( height - getStringHeight() )/2 ); } if( baseAlign == TEXT_ALIGN_CENTER_MIDDLE ) { basex = ( ( width - getStringWidth() )/2 ); basey = ( ( height - getStringHeight() )/2 ); } if( baseAlign == TEXT_ALIGN_RIGHT_MIDDLE ) { basex = width - getStringWidth(); basey = ( ( height - getStringHeight() )/2 ); } if( baseAlign == TEXT_ALIGN_CENTER_BOTTOM ) { basex = ( ( width - getStringWidth() )/2 ); basey = height - getStringHeight(); } if( baseAlign == TEXT_ALIGN_RIGHT_BOTTOM ) { basex = width - getStringWidth(); basey = height - getStringHeight(); } if( baseAlign == TEXT_ALIGN_LEFT_BOTTOM ) { basey = height - getStringHeight(); } float dispx = basex; float dispy = basey; // 返却用のリスト CCMutableArray<StringLabel*>* ret = new CCMutableArray<StringLabel*>; ret->autorelease(); // ラベルを生成 for( int i = 0; i < linecnt; i++ ) { CCMutableArray<ColorString*> *list = getStringList( i ); float lh = getStringLineHeight( i ); float lw = getStringLineWidth( i ); // 下合わせ dispy += lh; for( int j = 0; j < list->count(); j++ ) { ColorString* cstr = list->getObjectAtIndex( j ); // アライン if( j == 0 ) { if( cstr->getAlign() == ALIGN_LEFT ) dispx = 0; if( cstr->getAlign() == ALIGN_CENTER ) dispx = ((width-lw)/2); if( cstr->getAlign() == ALIGN_RIGHT ) dispx = width - lw; } // テキストを設定 StringLabel* label = cstr->getStrLabel(); label->setAlign( TEXT_ALIGN_LEFT_BOTTOM ); //label->setPosition( dispx, dispy ); label->setOffsetX( dispx ); label->setOffsetY( dispy ); ret->addObject( label ); // ラベルサイズ取得 CCSize size = label->getContentSize(); dispx += size.width; } dispx = basex; } return ret; }
void HelloWorld::testCollisions(ccTime dt) { CCMutableArray<CCSprite*> *projectilesToDelete = new CCMutableArray<CCSprite*>; CCMutableArray<CCSprite *>::CCMutableArrayIterator it, jt; // iterate through projectiles for (it = _projectiles->begin(); it != _projectiles->end(); it++) { CCSprite *projectile = *it; CCRect projectileRect = CCRectMake(projectile->getPosition().x - (projectile->getContentSize().width/2), projectile->getPosition().y - (projectile->getContentSize().height/2), projectile->getContentSize().width, projectile->getContentSize().height); CCMutableArray<CCSprite*> *targetsToDelete = new CCMutableArray<CCSprite*>; // iterate through enemies, see if any intersect with current projectile for (jt = _enemies->begin(); jt != _enemies->end(); jt++) { CCSprite *target = *jt; CCRect targetRect = CCRectMake( target->getPosition().x - (target->getContentSize().width/2), target->getPosition().y - (target->getContentSize().height/2), target->getContentSize().width, target->getContentSize().height); if (CCRect::CCRectIntersectsRect(projectileRect, targetRect)) { targetsToDelete->addObject(target); } } // delete all hit enemies for (jt = targetsToDelete->begin(); jt != targetsToDelete->end(); jt++) { _enemies->removeObject(*jt); this->removeChild((*jt), true); } if (targetsToDelete->count() > 0) { projectilesToDelete->addObject(projectile); } targetsToDelete->release(); } // remove all the projectiles that hit. for (it = projectilesToDelete->begin(); it != projectilesToDelete->end(); it++) { CCSprite *projectile = *it; _projectiles->removeObject(projectile, true); this->removeChild(projectile, true); } for (jt = _enemies->begin(); jt != _enemies->end(); jt++) { CCSprite *target = *jt; CCRect targetRect = CCRectMake( target->getPosition().x - (target->getContentSize().width/2), target->getPosition().y - (target->getContentSize().height/2), target->getContentSize().width, target->getContentSize().height); if (CCRect::CCRectContainsPoint(targetRect, _player->getPosition())) { this->lose(); } } }
bool CCTMXTiledMap::initWithTMXFile(const char *tmxFile) { CCAssert(tmxFile != NULL && strlen(tmxFile)>0, "TMXTiledMap: tmx file should not bi nil"); setContentSize(CCSizeZero); CCTMXMapInfo *mapInfo = CCTMXMapInfo::formatWithTMXFile(tmxFile); if (! mapInfo) { return false; } CCAssert( mapInfo->getTilesets()->count() != 0, "TMXTiledMap: Map not found. Please check the filename."); m_tMapSize = mapInfo->getMapSize(); m_tTileSize = mapInfo->getTileSize(); m_nMapOrientation = mapInfo->getOrientation(); setObjectGroups(mapInfo->getObjectGroups()); setProperties(mapInfo->getProperties()); CC_SAFE_RELEASE(m_pTileProperties); m_pTileProperties = mapInfo->getTileProperties(); CC_SAFE_RETAIN(m_pTileProperties); int idx = 0; CCMutableArray<CCTMXLayerInfo*>* layers = mapInfo->getLayers(); if (layers && layers->count()>0) { if (NULL == m_pTMXLayers) { m_pTMXLayers = new CCDictionary<std::string, CCTMXLayer*>(); CCAssert(m_pTMXLayers, "Allocate memory failed!"); } CCTMXLayerInfo *layerInfo = NULL; CCMutableArray<CCTMXLayerInfo*>::CCMutableArrayIterator it; for (it = layers->begin(); it != layers->end(); ++it) { layerInfo = *it; if (layerInfo && layerInfo->m_bVisible) { CCTMXLayer *child = parseLayer(layerInfo, mapInfo); addChild((CCNode*)child, idx, idx); // record the CCTMXLayer object by it's name std::string layerName = child->getLayerName(); m_pTMXLayers->setObject(child, layerName); // update content size with the max size const CCSize& childSize = child->getContentSize(); CCSize currentSize = this->getContentSize(); currentSize.width = MAX( currentSize.width, childSize.width ); currentSize.height = MAX( currentSize.height, childSize.height ); this->setContentSize(currentSize); idx++; } } } return true; }
void GB2ShapeCache::addShapesWithFile(const std::string &plist) { const char *fullName = CCFileUtils::fullPathFromRelativePath(plist.c_str()); ObjectDict *dict = CCFileUtils::dictionaryWithContentsOfFile(fullName); CCAssert(dict != NULL, "Shape-file not found"); // not triggered - cocos2dx delivers empty dict if non was found CCAssert(dict->count() != 0, "plist file empty or not existing"); ObjectDict *metadataDict = (ObjectDict *)dict->objectForKey("metadata"); int format = static_cast<CCString *>(metadataDict->objectForKey("format"))->toInt(); ptmRatio = static_cast<CCString *>(metadataDict->objectForKey("ptm_ratio"))->toFloat(); CCAssert(format == 1, "Format not supported"); ObjectDict *bodyDict = (ObjectDict *)dict->objectForKey("bodies"); b2Vec2 vertices[b2_maxPolygonVertices]; ObjectDict::CCObjectMapIter iter; bodyDict->begin(); std::string bodyName; ObjectDict *bodyData; while ((bodyData = (ObjectDict *)bodyDict->next(&bodyName))) { BodyDef *bodyDef = new BodyDef(); bodyDef->anchorPoint = CCPointFromString(static_cast<CCString *>(bodyData->objectForKey("anchorpoint"))->toStdString().c_str()); CCMutableArray<ObjectDict *> *fixtureList = (CCMutableArray<ObjectDict *> *)(bodyData->objectForKey("fixtures")); FixtureDef **nextFixtureDef = &(bodyDef->fixtures); CCMutableArray<ObjectDict *>::CCMutableArrayIterator iter; for (iter = fixtureList->begin(); iter != fixtureList->end(); ++iter) { b2FixtureDef basicData; ObjectDict *fixtureData = *iter; basicData.filter.categoryBits = static_cast<CCString *>(fixtureData->objectForKey("filter_categoryBits"))->toInt(); basicData.filter.maskBits = static_cast<CCString *>(fixtureData->objectForKey("filter_maskBits"))->toInt(); basicData.filter.groupIndex = static_cast<CCString *>(fixtureData->objectForKey("filter_groupIndex"))->toInt(); basicData.friction = static_cast<CCString *>(fixtureData->objectForKey("friction"))->toFloat(); basicData.density = static_cast<CCString *>(fixtureData->objectForKey("density"))->toFloat(); basicData.restitution = static_cast<CCString *>(fixtureData->objectForKey("restitution"))->toFloat(); basicData.isSensor = (bool)static_cast<CCString *>(fixtureData->objectForKey("isSensor"))->toInt(); CCString *cb = static_cast<CCString *>(fixtureData->objectForKey("userdataCbValue")); int callbackData = 0; if (cb) callbackData = cb->toInt(); std::string fixtureType = static_cast<CCString *>(fixtureData->objectForKey("fixture_type"))->toStdString(); if (fixtureType == "POLYGON") { CCMutableArray<ObjectDict *> *polygonsArray = (CCMutableArray<ObjectDict *> *)(fixtureData->objectForKey("polygons")); CCMutableArray<ObjectDict *>::CCMutableArrayIterator iter; for (iter = polygonsArray->begin(); iter != polygonsArray->end(); ++iter) { FixtureDef *fix = new FixtureDef(); fix->fixture = basicData; // copy basic data fix->callbackData = callbackData; b2PolygonShape *polyshape = new b2PolygonShape(); int vindex = 0; CCMutableArray<CCString *> *polygonArray = (CCMutableArray<CCString *> *)(*iter); assert(polygonArray->count() <= b2_maxPolygonVertices); CCMutableArray<CCString *>::CCMutableArrayIterator piter; for (piter = polygonArray->begin(); piter != polygonArray->end(); ++piter) { CCPoint offset = CCPointFromString((*piter)->toStdString().c_str()); vertices[vindex].x = (offset.x / ptmRatio) ; vertices[vindex].y = (offset.y / ptmRatio) ; vindex++; } polyshape->Set(vertices, vindex); fix->fixture.shape = polyshape; // create a list *nextFixtureDef = fix; nextFixtureDef = &(fix->next); } } else if (fixtureType == "CIRCLE") { FixtureDef *fix = new FixtureDef(); fix->fixture = basicData; // copy basic data fix->callbackData = callbackData; ObjectDict *circleData = (ObjectDict *)fixtureData->objectForKey("circle"); b2CircleShape *circleShape = new b2CircleShape(); circleShape->m_radius = static_cast<CCString *>(circleData->objectForKey("radius"))->toFloat() / ptmRatio; CCPoint p = CCPointFromString(static_cast<CCString *>(circleData->objectForKey("position"))->toStdString().c_str()); circleShape->m_p = b2Vec2(p.x / ptmRatio, p.y / ptmRatio); fix->fixture.shape = circleShape; // create a list *nextFixtureDef = fix; nextFixtureDef = &(fix->next); } else { CCAssert(0, "Unknown fixtureType"); } // add the body element to the hash shapeObjects[bodyName] = bodyDef; } } }