bool CCSprite::initWithFile(const char *pszFilename) { CCAssert(pszFilename != NULL, "Invalid filename for sprite"); CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(pszFilename); if (pTexture) { CCRect rect = CCRectZero; rect.size = pTexture->getContentSize(); setSpriteImageName(pszFilename); return initWithTexture(pTexture, rect); } // don't release here. // when load texture failed, it's better to get a "transparent" sprite then a crashed program // this->release(); return false; }
// on "init" you need to initialize your instance bool MainMenuScene::init() { CCSprite *fondo = CCSprite::create("fondo.png"); fondo->setPosition(VisibleRect::center()); addChild(fondo); CCSprite *leftGrass = CCSprite::create("grass_left.png"); leftGrass->setAnchorPoint(ccp(0,0)); leftGrass->setPosition(VisibleRect::leftBottom()); addChild(leftGrass); CCSprite *rightGrass = CCSprite::create("grass_right.png"); rightGrass->setAnchorPoint(ccp(1,0)); rightGrass->setPosition(VisibleRect::rightBottom()); addChild(rightGrass); CCSprite *spriteLogo = CCSprite::create("logo_opciones.png"); spriteLogo->setPosition(ccp(VisibleRect::center().x, (VisibleRect::top().y - (spriteLogo->getContentSize().height / 2) - 20))); addChild(spriteLogo); int mySize = VisibleRect::center().y / 3; CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("botoia_normal.png"); CCPoint anchorPoint = ccp(0.5f, 0.5f); CCLabelTTF *pLabel1 = CCLabelTTF::create("Jokatu", "fonts/PT_Sans-Web-Bold.ttf", 30.0); CCLabelTTF *pLabel2 = CCLabelTTF::create("Sailkapena", "fonts/PT_Sans-Web-Bold.ttf", 30.0); CCLabelTTF *pLabel3 = CCLabelTTF::create("Honi Buruz", "fonts/PT_Sans-Web-Bold.ttf", 30.0); // Button JOKATU SpriteButton *pButton1 = SpriteButton::create(texture ,this, callfuncO_selector(MainMenuScene::menuSelector), 1.0f); pButton1->setTag(1); pButton1->setAnchorPoint(anchorPoint); pButton1->setPosition(ccp(VisibleRect::center().x, (mySize * 3) - texture->getContentSize().height /2)); pLabel1->setPosition(ccp(VisibleRect::center().x, (mySize * 3) - texture->getContentSize().height /2)); // Button SAILKAPENA SpriteButton *pButton2 = SpriteButton::create(texture ,this, callfuncO_selector(MainMenuScene::menuSelector), 1.0f); pButton2->setTag(2); pButton2->setAnchorPoint(anchorPoint); pButton2->setPosition(ccp(VisibleRect::center().x, (mySize * 2) - texture->getContentSize().height /2)); pLabel2->setPosition(ccp(VisibleRect::center().x, (mySize * 2) - texture->getContentSize().height /2)); // Button HONI BURUZ SpriteButton *pButton3 = SpriteButton::create(texture ,this, callfuncO_selector(MainMenuScene::menuSelector), 1.0f); pButton3->setTag(3); pButton3->setAnchorPoint(anchorPoint); pButton3->setPosition(ccp(VisibleRect::center().x, mySize - texture->getContentSize().height /2)); pLabel3->setPosition(ccp(VisibleRect::center().x, mySize - texture->getContentSize().height /2)); addChild(pButton1); addChild(pLabel1); addChild(pButton2); addChild(pLabel2); addChild(pButton3); addChild(pLabel3); return true; }
Player* Player::spriteWithFile(const char *pszFileName, int columns, int rows) { // CCSpriteFrame *frame = cacher->spriteFrameByName( pszFileName); CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(pszFileName); CCArray* aFrames = CCArray::createWithCapacity(columns * rows); float textureWidth = texture->getPixelsWide(); float textureHeight = texture->getPixelsHigh(); float frameWidth = textureWidth / columns; float frameHeight = textureHeight / rows; for (int c = 0; c < columns; c++) { for (int r = 0; r < rows; r++) { CCSpriteFrame *frame0 = CCSpriteFrame::createWithTexture(texture, CCRectMake((c*frameWidth), (r*frameHeight), 100, 65)); aFrames->addObject(frame0); } } CCAnimation *animation = CCAnimation::createWithSpriteFrames(aFrames, 0.2f); CCAnimate *animate = CCAnimate::create(animation); CCActionInterval* seq = (CCActionInterval*)(CCSequence::create( animate, animate->copy()->autorelease(), CCFlipX::create(false), NULL) ); Player *pobSprite = new Player(); if (pobSprite && pobSprite->initWithSpriteFrame((CCSpriteFrame*) aFrames->objectAtIndex(0))) { pobSprite->scheduleUpdate(); pobSprite->autorelease(); pobSprite->setForwardMarch(false); pobSprite->setMightAsWellJump(false); pobSprite->setBackwardMarch(false); pobSprite->runAction(CCRepeatForever::create( seq ) ); // pobSprite->setAnimFrames( aFrames); // pobSprite->setCurrentFrame(0); return pobSprite; } CC_SAFE_DELETE(pobSprite); return NULL; }
bool DefaultLayer::init() { if (!CCLayer::init()) return false; // ask director the window size CCSize window_size = CCDirector::sharedDirector()->getWinSize(); CCImage* temp_img = new CCImage(); temp_img->initWithImageFile("atlas.png"); CCTexture2D* texture = new CCTexture2D(); texture->initWithImage(temp_img); texture->autorelease(); delete temp_img; // this will have a capacity for 4 sprites instead of the default which is 20 CCSpriteBatchNode* batch = CCSpriteBatchNode::createWithTexture(texture, 4); // Since I don't have HD versions of the image but I've set enableRetinaDisplay to true, the framework // is using a scaling factor of 2 to divide the image by. This is because it thinks the image was from the low resolution // version, which would mean that running in retina (i.e. 2x larger screen) should make the image look half as big. By calling // getContentSize over getContentSizeInPixels I'm getting the size of the image as it is represented for the appropriate // draw mode. If I actually had HD art, I believe I could just call getContentSizeInPixels and that they'd both be the same. CCSize tex_size(texture->getContentSize()); for (int i = 0; i < 4; i++) { CCSprite* sprite = CCSprite::create(); // The texture rect coords are luckily normal and just like a Photoshop canvas CCRect r((i % 2) == 0 ? 0 : tex_size.width / 2.0f, (i / 2) == 0 ? 0 : tex_size.height / 2.0f, tex_size.width/2.0f, tex_size.height/2.0f); sprite->initWithTexture(texture, r); sprite->setPosition(ccp(sprite->getContentSize().width / 2.0f + (i % 2)*200, sprite->getContentSize().height + (i/2)*150)); batch->addChild(sprite); } addChild(batch); return true; }
void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouches, cocos2d::CCEvent *pEvent){ if(m_score<0){ return; } CCPoint point = pTouches->getLocation(); char pos[100]; int y_ = (int)((point.y-diamondRect.origin.y)/dh); int y = 7-y_; int x = (int)((point.x-diamondRect.origin.x)/dw); int result[64] = {0}; if(y>=8||x>=8){ return; } int count = handlerTouchInSquare(y*8+x, result); if(count < 3) return; for(int i = 0; i < count; i++){ CCSprite *sprite = diamonds[result[i]]; CCRotateBy *rotateAct = new CCRotateBy(); rotateAct->initWithDuration(0.2f, 90.0f); sprite->runAction(rotateAct); char str[20]; CCImage *image = new CCImage(); int color = getDiamond(str); base[result[i]] = color; image->initWithImageFile(str, CCImage::kFmtJpg); CCTexture2D* texture = new CCTexture2D(); texture->initWithImage(image); sprite->setTexture(texture); } //delete rotateAct; m_score += 5*count; char str[20]; sprintf(str, "Score: %d", m_score); pLabelScore->setString(str); //fflush(file); }
// Helper void CCLabelTTF::updateTexture() { CCTexture2D *tex; if (m_tDimensions.width == 0 || m_tDimensions.height == 0) { tex = new CCTexture2D(); tex->initWithString(m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR()) ; } else { tex = new CCTexture2D(); tex->initWithString(m_string.c_str(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment, m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR()); } // iPad ? //if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) { if (CCApplication::sharedApplication().isIpad()) { if (CC_CONTENT_SCALE_FACTOR() == 2) { tex->setResolutionType(kCCResolutioniPadRetinaDisplay); } else { tex->setResolutionType(kCCResolutioniPad); } } // iPhone ? else { if (CC_CONTENT_SCALE_FACTOR() == 2) { tex->setResolutionType(kCCResolutioniPhoneRetinaDisplay); } else { tex->setResolutionType(kCCResolutioniPhone); } } this->setTexture(tex); tex->release(); CCRect rect = CCRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); }
void HSEmailSystemLayer::Load() { CCSprite* pBackground = HSCCSprite::create("Image/BEIJING.png"); pBackground->setPosition(HSBase::GetTemplateScreenCentre()); this->addChild(pBackground); HSTool::SetNodeFilldScreen(pBackground); HSReadUI::ShareReadUI()->ReadUI("Image/EmailSystem.data","Image/",this); CCSprite* pMoneyFrame = HS_FIND_UI_PANEL_SPRITE("UI_Jinbidiandikuang","Jinbidiandikuang"); CCTexture2D* pMoneyTexture = CCTextureCache::sharedTextureCache()->addImage("Image/moneyNumber.png"); m_pMoney = CCLabelAtlas::create("0","Image/moneyNumber.png",pMoneyTexture->getPixelsWide() / 11,pMoneyTexture->getPixelsHigh(),'0'); m_pMoney->setAnchorPoint(HS_ANCHOR_CENTER); m_pMoney->setPosition(HS_SizeHalf_Point(pMoneyFrame->getContentSize())); pMoneyFrame->addChild(m_pMoney); CCSprite* pEmailSystemFrame = HS_FIND_UI_PANEL_SPRITE("UI_FriendFrame","gerenxinxilanfanwei"); m_pEmailListView = HSEmailListVeiw::create(pEmailSystemFrame->getContentSize(),CCSizeMake(505,100),HS_GAME_CACHE()->m_EmailResponse.emaillist_size()); CCPoint pos = CCPointZero; pos.x = -pEmailSystemFrame->getContentSize().width / 2.f - 15.f; pos.y = pEmailSystemFrame->getPosition().y - pEmailSystemFrame->getContentSize().height / 2.f - 20.f; m_pEmailListView->setPosition(pos); pEmailSystemFrame->getParent()->addChild(m_pEmailListView,1000); m_pEmailListView->reloadData(); HS_SET_MENU_TARGET("UI_fh_01",this,HSEmailSystemLayer::Call_Back); HS_SET_MENU_TARGET("UI_Goumaijinbianniu_01",this,HSEmailSystemLayer::Call_Shop); if (HS_GAME_CACHE()->m_EmailResponse.emaillist_size() == 0) { CCMenu* pMenu = HS_FIND_UI_MENU("UI_quanbulingqu"); pMenu->setVisible(false); }else{ HS_SET_MENU_TARGET("UI_quanbulingqu",this,HSEmailSystemLayer::Call_AllRevc); } this->schedule(schedule_selector(HSEmailSystemLayer::Updata)); }
CCTexture2D* getTexture(std::string theImageFullPath, CCRect& theTextureRect){ // try to load from sprite sheet std::string anImageFileName; int aLastSlashIndex = MAX((int)theImageFullPath.find_last_of('/'), (int)theImageFullPath.find_last_of('\\')); if (aLastSlashIndex != std::string::npos) { anImageFileName = theImageFullPath.substr(aLastSlashIndex + 1); } else { anImageFileName = theImageFullPath; } CCSpriteFrame *aSpriteFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(anImageFileName.c_str()); if (aSpriteFrame) { theTextureRect = aSpriteFrame->getRect(); return aSpriteFrame->getTexture(); } CCTexture2D* aTexture = CCTextureCache::sharedTextureCache()->addImage(theImageFullPath.c_str()); theTextureRect.origin = CCPointZero; theTextureRect.size = aTexture->getContentSize(); return aTexture; }
bool ItemFireString::init(CCDictionary * dict) { Item::init(); _type = IT_FIRESTRING; setPositionByProperty(dict); CCTexture2D * bossBullet = CCTextureCache::sharedTextureCache()->addImage("bossBullet.png"); setTexture(bossBullet); setTextureRect(CCRectMake(0, 0, bossBullet->getContentSize().width, bossBullet->getContentSize().height)); setScale(.6f); setAnchorPoint(ccp(0, 0.5f)); int begAngle = dict->valueForKey("begAngle")->intValue(); setRotation(begAngle); int time = dict->valueForKey("time")->intValue(); runAction(CCRepeatForever::create(CCRotateBy::create(time, 360))); return true; }
void TilemapRender::generateBackground() { ostringstream texturePath; texturePath<<"resources.pak/terrain/"; texturePath<<m_SetID<<"-14.png"; CCRenderTexture* rt = CCRenderTexture::create(64,64); rt->begin(); CCSprite* tile1 = CCSprite::create(texturePath.str().c_str()); tile1->setAnchorPoint(ccp(0,0)); tile1->setPosition(ccp(0,0)); tile1->visit(); rt->end(); CCTexture2D* texture = rt->getSprite()->getTexture(); ccTexParams tp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT}; texture->setTexParameters(&tp); m_SpriteB->setTexture(texture); m_SpriteB->setTextureRect(CCRectMake(0, 0, m_MapWidth*64, m_MapHeight*64)); }
void CSharpTollgate::setStageList(const vector<CStage>& stageList, int chapter, int openChapter) { m_stageList = stageList; m_chapter = chapter; m_openChapter = openChapter; m_tableView->setCountOfCell(stageList.size()); m_tableView->reloadData(); CTableViewCell *cell = m_tableView->cellAtIndex(3); cell->setPositionX(cell->getPositionX()+115); CImageView *name = (CImageView*)(m_ui->findWidgetById("name")); CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(CCString::createWithFormat("tollgate/chapter_%d.png",chapter)->getCString()); if(!texture) { texture = CCTextureCache::sharedTextureCache()->addImage("tollgate/chapter_1.png"); } name->setTexture(texture); name->setTextureRect(CCRectMake(0,0,texture->getContentSize().width,texture->getContentSize().height)); }
void UITextInputField::CreateWhiteBack() { if(m_pWhiteBack != NULL) { m_pWhiteBack->removeFromParentAndCleanup(true); m_pWhiteBack = NULL; } if(m_pTextInputField == NULL) { return; } CCSize dimension = m_pTextInputField->getDimensions(); int dimensionWidth = dimension.width; int dimensionHeight = dimension.height; if(dimensionWidth != 0 && dimensionHeight != 0) { int *pixels = new int[(dimensionWidth + 8) * (dimensionHeight + 8)]; for (int i=0; i<dimensionWidth+8; ++i) { for (int j=0; j<dimensionHeight+8; ++j) { //pixels[i][j] = 0xffffffff; pixels[i*4+j] = 0xffffffff; } } CCTexture2D *texture = new CCTexture2D(); texture->initWithData(pixels, kCCTexture2DPixelFormat_RGB888, 1, 1, CCSizeMake(dimensionWidth + 8, dimensionHeight + 8) ); delete[] pixels; m_pWhiteBack = CCSprite::createWithTexture(texture); texture->release(); } }
//-------------------------------------------------------- bool CShaderNode::initWithVertex( const char* image,const char *vert, const char *frag) { initShader(vert,frag); CCTexture2D* pTex = CCTextureCache::sharedTextureCache()->addImage( image ); if( pTex == NULL ) { return false; } m_pTexture = pTex->getName(); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); setContentSize( winSize );//pTex->getContentSize() ); setPosition(ccp(winSize.width / 2 - getContentSize().width / 2, winSize.height / 2 - getContentSize().height / 2 )); m_fTime = 0; // 开启自更新 scheduleUpdate(); return true; }
// on "init" you need to initialize your instance bool kBaseScene::init() { // 0.alloc memory for shared data ksd = new kSharedData; // common use resource load into memory CCTexture2D *pTextureShadow = CCTextureCache::sharedTextureCache()->addImage("yy.png"); CCSpriteFrame* shadowFrame = CCSpriteFrame::createWithTexture(pTextureShadow, CCRectMake(0, 0, pTextureShadow->getPixelsWide(), pTextureShadow->getPixelsHigh())); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFrame(shadowFrame, "yy.png"); // test fake data // you should init the shared data after login scene ksd->rd.rid = 1; ksd->rd.mid = 1; ksd->rd.x = 0; ksd->rd.y = 0; ksd->rd.lv = 18; // test fake data over // 1.make the network connection int connectresult=0; ks = new kClientSocket; connectresult = ks->start("127.0.0.1", 8810); if(connectresult == SOCKET_ERROR) { // to information and exit CCMessageBox("wrong", "Error connect to net,check your configuration plz"); delete ks; ks = NULL; } else { } // 2.run base ui manager kuimgr = kUiManager::create(); kuimgr->setTag(CLOSEABLE_LAYER_Z); // frame view add this->addChild(kuimgr,CLOSEABLE_LAYER_Z); // 3.net work msg receive loop this->schedule(schedule_selector(kBaseScene::onupdate),0.04); // 4.create the gameview base layer gameview = kGameView::create(); this->addChild(gameview,GAMELAYER_Z); // should start login view here,and from login view call relate initview function to // load view // here.run base scene from role data for test kSceneInterface* normapScene = kNormalMapScene::create(); char mapid[16]; sprintf(mapid,"map/%d", ksd->rd.mid); gameview->changeScene(normapScene,mapid); return true; }
// Loads a texture frame from a file CCSpriteFrame *CocosTile::loadSpriteFrame(std::string filename) { // First attempt to get it from the frame cache, in case it was already loaded from an atlas CCSpriteFrame *retVal = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(filename.c_str()); // If not found, create the frame by loading texture if (!retVal) { CCTexture2D *frameTexture = CCTextureCache::sharedTextureCache()->addImage(filename.c_str()); if (frameTexture) { retVal = CCSpriteFrame::frameWithTexture(frameTexture, CCRect(0, 0, frameTexture->getContentSize().width, frameTexture->getContentSize().height)); // Add to frame cache CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFrame(retVal, filename.c_str()); } } return retVal; }
//------------------------------------------------------------------------- bool FKCW_UI_ProgressIndicator::initWithMessage(const string& message) { if(!CCLayerColor::initWithColor(m_tagDimColor)) { return false; } // 保存消息 m_strMessage = message; // 创建Label m_pMsgLabel = CCLabelTTF::create(m_strMessage.c_str(), "Helvetica", 28 / CC_CONTENT_SCALE_FACTOR()); // 提示器 CCImage* image = new CCImage(); image->initWithImageData((void*)s_SpinnerPng, 1043); CCTexture2D* tex = new CCTexture2D(); tex->initWithImage(image); m_pIndicator = CCSprite::createWithTexture(tex); CC_SAFE_RETAIN(m_pIndicator); image->release(); tex->release(); _RelayOut(); addChild(m_pIndicator); addChild(m_pMsgLabel); // 开启事件 setTouchEnabled(true); setTouchMode(kCCTouchesOneByOne); setTouchPriority(-MAX_INT); setKeypadEnabled(true); // 开启帧更新 scheduleUpdate(); return true; }
bool CCProgressHUD::initWithMessage(const string& message) { if(!CCLayerColor::initWithColor(m_dimColor)) { return false; } // set message m_message = message; // create label m_msgLabel = CCLabelTTF::create(m_message.c_str(), "Helvetica", 28 / CC_CONTENT_SCALE_FACTOR()); // indicator CCImage* image = new CCImage(); image->initWithImageData((void*)s_spinner_png, 1043); CCTexture2D* tex = new CCTexture2D(); tex->initWithImage(image); m_indicator = CCSprite::createWithTexture(tex); CC_SAFE_RETAIN(m_indicator); CC_SAFE_RELEASE(image); CC_SAFE_RELEASE(tex); // layout relayout(); addChild(m_indicator); addChild(m_msgLabel); // enable event setTouchEnabled(true); setTouchMode(kCCTouchesOneByOne); setTouchPriority(-MAX_INT); setKeypadEnabled(true); // schedule update scheduleUpdate(); return true; }
bool CCTextInput::initWithLabel(const char* placeHolder, CCNode* label, CCSize designSize, int align, unsigned int limit) { m_uAlign = align; m_pColor = dynamic_cast<CCRGBAProtocol*>(label); m_pLabelNode = label; m_pLabel = dynamic_cast<CCLabelProtocol*>(label); if (placeHolder!=NULL) { m_pPlaceHolder = (placeHolder) ? new std::string(placeHolder) : new std::string; m_pLabel->setString(placeHolder); m_pColor->setColor(m_ColorSpaceHolder); } this->setLimitNum(limit); int cursorHeight = (int)(designSize.height * 0.9); int* pixels = new int[2*cursorHeight]; for (int i=0; i<cursorHeight; ++i) { for (int j=0; j<2; ++j) { pixels[i*2+j] = 0x00000000; } } CCTexture2D *texture = new CCTexture2D(); texture->initWithData(pixels, kCCTexture2DPixelFormat_RGB888, 1, 1, CCSizeMake(2, cursorHeight)); delete[] pixels; m_pCursorSprite = CCSprite::createWithTexture(texture); m_pCursorSprite->setAnchorPoint(CCPointMake(0, 0.5)); m_cursorPos = CCPointMake(0, designSize.height / 2); addChild(m_pCursorSprite, 1, 1); m_pCursorAction = CCRepeatForever::create((CCActionInterval *) CCSequence::create(CCFadeOut::create(0.25f), CCFadeIn::create(0.25f), NULL)); m_pCursorSprite->runAction(m_pCursorAction); m_pCursorSprite->setVisible(false); // add view addChild(m_pLabelNode, 0, 0); setString(m_pLabel->getString()); return true; }
ToyBrick* ToyLayer::CreateBrick_Rectangle() { int num = CommonHelper::GetRandomNum(1, 20); //image && texture char path[255]; sprintf(path, "Images/ToyBrick/Rectangle/%d.png", num); CCImage *img = new CCImage(); img->autorelease(); if (!img->initWithImageFile(path, cocos2d::CCImage::kFmtPng)) { return NULL; } CCTexture2D *texture = new CCTexture2D(); texture->autorelease(); if (!texture->initWithImage(img)) { return NULL; } // ToyBrick *brick = ToyBrick::create(); CCSize size = texture->getContentSize(); brick->Init(texture, CCRectMake(0, 0, size.width, size.height)); //vector CCPointArray *vector = CCPointArray::create(4); vector->addControlPoint(ccp(-52, -52)); vector->addControlPoint(ccp(52, -52)); vector->addControlPoint(ccp(52, 52)); vector->addControlPoint(ccp(-52, 52)); brick->SetAsPolygon(vector); return brick; }
void RequestTopaz::onHttpRequestCompletedNoEncrypt(CCNode *sender, void *data) { CCHttpResponse* res = (CCHttpResponse*) data; char dumpData[110*110*2]; // 프로필 사진 받아오기 실패 if (!res || !res->isSucceed()) { //CCLog("res failed. error buffer: %s", res->getErrorBuffer()); return; } // dump data std::vector<char> *buffer = res->getResponseData(); for (unsigned int i = 0 ; i < buffer->size() ; i++) dumpData[i] = (*buffer)[i]; dumpData[buffer->size()] = NULL; // make texture2D CCImage* img = new CCImage; img->initWithImageData(dumpData, (int)buffer->size()); CCTexture2D* texture = new CCTexture2D(); texture->initWithImage(img); // set CCSprite (profile 모음 리스트에 갱신) int numOfList = friendList.size(); int index = atoi(res->getHttpRequest()->getTag()); ProfileSprite* psp = ProfileSprite::GetObj(friendList[index]->GetImageUrl()); psp->SetSprite(texture); psp->SetLoadingDone(true); // 화면에 보이는 스프라이트 교체 if (spriteClassScroll == NULL) return; spriteClassScroll->ChangeSprite(-888*(numOfList-index), psp->GetProfile()); ((CCSprite*)spriteClassScroll->FindSpriteByTag(-777*(numOfList-index)))->setOpacity(255); }
CCSize CCtrlEdit::GetTextSize(const char * text) { CCSize textSize(0.0f,0.0f); if (NULL == text) { return textSize; } do { CCTexture2D *tex = new CCTexture2D(); CC_BREAK_IF(!tex); float fContentScaleFactor= CCDirector::sharedDirector()->getContentScaleFactor(); bool bRet = tex->initWithString( text, m_strFontName.c_str(), m_nFontSize * fContentScaleFactor, CCSizeZero, m_pTextField->getHorizontalAlignment(), m_pTextField->getVerticalAlignment()); if (bRet) { float fOffsetX = tex->getPixelsWide(); fOffsetX /= fContentScaleFactor; textSize.width += fOffsetX; float fOffsetY = tex->getPixelsHigh(); fOffsetY /= fContentScaleFactor; textSize.height += fOffsetY; } tex->release(); }while(false); return textSize; }
InputPad() { _pad = CCSprite::create(s_InputPad); this->addChild(_pad,Z_INPUTPAD); CCTexture2D* gridTexture = CCTextureCache::sharedTextureCache()->addImage(s_GridUp); CCSize s = gridTexture->getContentSize(); CCPoint gridOrigin(ccp(0,0)); for(int i=0;i<ROW_NUM;i++) { for(int j=0;j<COL_NUM;j++) { Grid* pGrid = Grid::GridWithTexture(gridTexture,(i*COL_NUM+j+1),true); float x = gridOrigin.x+j*(g_gridSize.width+1)+1; float y = gridOrigin.y+i*(g_gridSize.height+1)+1; //CCLOG("input grid:x:%0.1f, y:%0.1f",x,y); pGrid->GetNode()->setPosition(ccp(x,y)); pGrid->GetNode()->setAnchorPoint(ccp(0.0f,0.0f)); _pad->addChild(pGrid->GetNode(), Z_INPUT_GRID_NUMBER); } } //CCLayerColor* pBackGround = CCLayerColor::create(ccc4(255,255,0,255),90,90); //this->addChild(pBackGround,0); }
void SpriteMask::setMask(const char *pMaskFile) { if (_isSetMask) return; _maskTexture = CCTextureCache::sharedTextureCache()->addImage(pMaskFile); //_maskTexture->setAliasTexParameters(); _maskTexture->retain(); CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(SpriteMask::listenBackToForeground), EVNET_COME_TO_FOREGROUND, NULL); initProgram(); _isSetMask = true; }
/* @todo CGImageRef -(CCTexture2D*) addCGImage: (CGImageRef) imageref forKey: (string & )key { CCAssert(imageref != nil, @"TextureCache: image MUST not be nill"); CCTexture2D * tex = nil; // If key is nil, then create a new texture each time if( key && (tex=[textures objectForKey: key] ) ) { return tex; } // prevents overloading the autorelease pool UIImage *image = [[UIImage alloc] initWithCGImage:imageref]; tex = [[CCTexture2D alloc] initWithImage: image]; [image release]; if(tex && key) [textures setObject: tex forKey:key]; else CCLOG(@"cocos2d: Couldn't add CGImage in CCTextureCache"); return [tex autorelease]; }*/ CCTexture2D* CCTextureCache::addUIImage(CCImage *image, const char *key) { CCAssert(image != NULL && key != NULL, "TextureCache: image MUST not be nill"); CCTexture2D * texture = NULL; std::string forKey = key; m_pDictLock->lock(); do { // If key is nil, then create a new texture each time if(texture = m_pTextures->objectForKey(forKey)) { break; } // prevents overloading the autorelease pool texture = new CCTexture2D(); texture->initWithImage(image); if(texture) { m_pTextures->setObject(texture, forKey); texture->autorelease(); } else { CCLOG("cocos2d: Couldn't add UIImage in CCTextureCache"); } } while (0); m_pDictLock->unlock(); return texture; }
FMMainScene::FMMainScene() :m_currentScene(kLoadingNode) { //load textures for test NEAnimManager::sharedManager()->loadSpriteframesFromFile(CCString::create("Elements.plist")); NEAnimManager::sharedManager()->loadSpriteframesFromFile(CCString::create("Map.plist")); CCTexture2D * tex = CCTextureCache::sharedTextureCache()->textureForKey("Map.pvr.ccz"); tex->setAliasTexParameters(); CC_PROFILER_START("GameNode Init"); m_energyManager = FMEnergyManager::manager(); schedule(schedule_selector(FMMainScene::update), 1.f); m_ui = GAMEUI_Scene::uiSystem(); addChild(m_ui, 1000); m_worldMap = FMWorldMapNode::create(); m_worldMap->retain(); m_game = FMGameNode::create(); m_game->retain(); FMStatusbar * statusbar = (FMStatusbar *)FMDataManager::sharedManager()->getUI(kUI_Statusbar); m_statusbar = statusbar; addChild(statusbar, 2000); //tutorial FMTutorial * tut = FMTutorial::tut(); m_tutorial = tut; m_tutorial->setVisible(false); addChild(tut, 3000); CC_PROFILER_STOP("GameNode Init"); CC_PROFILER_DISPLAY_TIMERS(); }
void CTextField::initCursorSprite(int nHeight) { using namespace cocos2d; int column = 4; // int pixels[nHeight][column]; int (*pixels)[4] = new int[nHeight][4]; for (int i = 0; i < nHeight; ++i) { for (int j = 0; j < column; ++j) { pixels[i][j] = 0xff696969; } } CCTexture2D * texture = new CCTexture2D(); texture->initWithData(pixels, kCCTexture2DPixelFormat_RGB888, 1, 1, CCSizeMake(column, nHeight)); m_pCursorSprite = CCSprite::createWithTexture(texture); CCSize parentSize = getContentSize(); m_pCursorSprite->setPosition(ccp(0, parentSize.height / 2)); addChild(m_pCursorSprite); /* CCFadeOut * fadeOut = CCFadeOut::create(0.25f); CCFadeIn * fadeIn = CCFadeIn::create(0.25f); CCSequence * seq = CCSequence::create(fadeOut, fadeIn, NULL); CCRepeatForever * forever = CCRepeatForever::create(seq); m_pCursorSprite->runAction(forever); */ m_pCursorSprite->setVisible(false); delete []pixels; pixels = NULL; }
// on "init" you need to initialize your instance bool LevelGameScene::init() { if ( !CCLayerColor::initWithColor( ccc4(100, 100, 100, 255) ) ) { return false; } CCSize vs = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); // Fondo con la textura de madera ccTexParams tp = { GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT}; CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("purty_wood.png"); texture->setTexParameters(&tp); CCSprite* background = CCSprite::createWithTexture(texture, CCRectMake(0, 0, vs.width, vs.height)); background->setPosition( ccp( vs.width/2, vs.height/2 ) ); this->addChild(background, 1); CCMenuItem* level1 = CCMenuItemImage::create("btn_level_1.png", "btn_level_1_h.png", this, menu_selector(LevelGameScene::level1Callback)); level1->setPosition(ccp(vs.width/2 - 275, vs.height/2)); CCMenuItem* level2 = CCMenuItemImage::create("btn_level_2.png", "btn_level_2_h.png", this, menu_selector(LevelGameScene::level2Callback)); level2->setPosition(ccp(vs.width/2, vs.height/2)); CCMenuItem* level3 = CCMenuItemImage::create("btn_level_3.png", "btn_level_3_h.png", this, menu_selector(LevelGameScene::level3Callback)); level3->setPosition(ccp(vs.width/2 + 275, vs.height/2)); CCMenu* menu = CCMenu::create(level1, level2, level3, NULL); menu->setPosition(ccp(0,0)); this->addChild(menu, 5); this->setTouchEnabled(true); return true; }
// Helper bool CCLabelTTF::updateTexture() { CCTexture2D *tex; tex = new CCTexture2D(); if (!tex) return false; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) ccFontDefinition texDef = _prepareTextDefinition(true); tex->initWithString( m_string.c_str(), &texDef ); #else tex->initWithString( m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment); #endif // set the texture this->setTexture(tex); // release it tex->release(); // set the size in the sprite CCRect rect =CCRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); //ok return true; }
bool CCGridBase::initWithSize(const ccGridSize& gridSize) { CCDirector *pDirector = CCDirector::sharedDirector(); CCSize s = pDirector->getWinSizeInPixels(); unsigned int POTWide = (unsigned int) s.width; unsigned int POTHigh = (unsigned int) s.height; bool needPOT = !g_Render->isRenderTargetNPOTSupported(); if (needPOT) { POTWide = PixelFormat::calcNextPot(POTWide); POTHigh = PixelFormat::calcNextPot(POTHigh); } // we only use rgba8888 CCTexture2DPixelFormat format = kCCTexture2DPixelFormat_RGBA8888; CCTexture2D *pTexture = new CCTexture2D(); if (! pTexture) { CCLOG("cocos2d: CCGrid: error creating texture"); delete this; return false; } pTexture->initWithTexture( new GLESTexture(POTWide, POTHigh, CCTexture2D::ToNitPixelFormat(format, false))); initWithSize(gridSize, pTexture, false); pTexture->release(); return true; }
bool CCGridBase::initWithSize(ccGridSize gridSize) { CCDirector *pDirector = CCDirector::sharedDirector(); CGSize s = pDirector->getWinSizeInPixels(); unsigned int POTWide = ccNextPOT((unsigned int)s.width); unsigned int POTHigh = ccNextPOT((unsigned int)s.height); // on mac, it use kCCTexture2DPixelFormat_RGBA8888 CCTexture2DPixelFormat format = kCCTexture2DPixelFormat_RGBA8888; void *data = calloc((int)(POTWide * POTHigh * 4), 1); if (! data) { CCLOG("cocos2d: CCGrid: not enough memory."); this->release(); return false; } CCTexture2D *pTexture = new CCTexture2D(); pTexture->initWithData(data, format, POTWide, POTHigh, s); free(data); if (! pTexture) { CCLOG("cocos2d: CCGrid: error creating texture"); delete this; return false; } initWithSize(gridSize, pTexture, false); pTexture->release(); return true; }