bool CALabel::updateImage() { if (m_bRunning == false) { return false; } CAImage *tex = new CAImage(); if (!tex) { return false; } caFontDefinition fontDef = setFontDefiniton(true); CAImage *at = new CAImage(); at->initWithString("9m", fontDef.m_fontName.c_str(), fontDef.m_fontSize, CCSizeZero, fontDef.m_alignment, fontDef.m_vertAlignment); float fontHeight = at->getContentSize().height+1; at->release(); unsigned int linenumber = (int)this->getBounds().size.height/fontHeight; if (m_nNumberOfLine <= linenumber && m_nNumberOfLine != 0) { tex->initWithString(m_nText.c_str(), fontDef.m_fontName.c_str(), fontDef.m_fontSize, CCSizeZero, fontDef.m_alignment, fontDef.m_vertAlignment); } else { if (this->getBounds().size.width == 0 && this->getBounds().size.height == 0) { tex->initWithString(m_nText.c_str(), fontDef.m_fontName.c_str(), fontDef.m_fontSize, CCSizeZero, fontDef.m_alignment, fontDef.m_vertAlignment); } else { tex->initWithString(m_nText.c_str(), fontDef.m_fontName.c_str(), fontDef.m_fontSize, this->getBounds().size, fontDef.m_alignment, fontDef.m_vertAlignment); } } CCRect rect = CCRectZero; rect.size = tex->getContentSize(); m_cLabelSize = tex->getContentSize(); m_pTextImage->setImage(tex); m_pTextImage->setImageRect(rect); m_pTextImage->setColor(ccc3(m_nTextcolor.r, m_nTextcolor.g, m_nTextcolor.b)); m_pTextImage->setOpacity(m_nTextcolor.a); m_pTextImage->CAView::setFrameOrigin(CCPoint(0, (this->getBounds().size.height - rect.size.height)/2)); tex->release(); return true; }
void CALabel::updateImage() { CAImage *tex = new CAImage(); float fontHeight = getFontHeight(m_nfontName.c_str(), m_nfontSize); unsigned int linenumber = (int)this->getBounds().size.height/fontHeight; CCSize size = CCSizeZero; if ((m_nNumberOfLine <= linenumber && m_nNumberOfLine != 0)) { size = CCSizeMake(this->getBounds().size.width, fontHeight*m_nNumberOfLine); } else { size = this->getBounds().size; } tex->initWithString(m_nText.c_str(), m_nfontName.c_str(), m_nfontSize* CC_CONTENT_SCALE_FACTOR(), size, m_nTextAlignment, m_nVerticalTextAlignmet); m_cLabelSize = tex->getContentSize(); CCRect rect = CCRectZero; rect.size.width = this->getBounds().size.width; rect.size.height = tex->getContentSize().height; float width = MIN(this->getBounds().size.width,tex->getContentSize().width); rect.size.width = width; this->setImage(tex); switch (m_nVerticalTextAlignmet) { case CAVerticalTextAlignmentTop: pTextHeight =0; break; case CAVerticalTextAlignmentCenter: pTextHeight =(this->getBounds().size.height-rect.size.height)/2; break; case CAVerticalTextAlignmentBottom: pTextHeight =this->getBounds().size.height - rect.size.height; break; default: break; } this->setImageRect(rect); tex->release(); return ; }
void ToMainThread::runDelegate() { if (delegate) { CAImage *image = new CAImage(); if (image->initWithImageFile(_path)) { CAScheduler::unschedule(schedule_selector(ToMainThread::runDelegate), this); delegate->getSelectedImage(CAImage::generateMipmapsWithImage(image)); image->release(); } } }
NS_CC_BEGIN static unsigned int getFontHeight(const char *fontName, int fontSize) { unsigned int result=0; CAImage *image = new CAImage(); image->initWithString("9m", fontName, fontSize, CCSizeZero, CATextAlignmentLeft, CAVerticalTextAlignmentTop); CAImage *image1 = new CAImage(); image1->initWithString("9\nm", fontName, fontSize, CCSizeZero, CATextAlignmentLeft, CAVerticalTextAlignmentTop); result = image1->getContentSize().height-image->getContentSize().height; image->release(); image1->release(); return result; }
CAImage* CAImageCache::addImage(const std::string& path) { if (path.empty()) { return NULL; } CAImage* image = NULL; //pthread_mutex_lock(m_pDictLock); image = m_mImages.getValue(path); if (!image) { std::string lowerCase(path); for (unsigned int i = 0; i < lowerCase.length(); ++i) { lowerCase[i] = tolower(lowerCase[i]); } do { image = new CAImage(); if(image != NULL && image->initWithImageFile(path.c_str())) { m_mImages.erase(path); m_mImages.insert(path, image); image->release(); } else { CC_SAFE_DELETE(image); } } while (0); } //pthread_mutex_unlock(m_pDictLock); return image; }
void CAImageCache::addImageAsyncCallBack(float dt) { // the image is generated in loading thread std::queue<ImageInfo*> *imagesQueue = s_pImageQueue; if (!imagesQueue->empty()) { pthread_mutex_lock(&s_ImageInfoMutex); ImageInfo *pImageInfo = imagesQueue->front(); imagesQueue->pop(); pthread_mutex_unlock(&s_ImageInfoMutex); AsyncStruct *pAsyncStruct = pImageInfo->asyncStruct; CAImage *image = pImageInfo->image; image->premultipliedImageData(); CAObject *target = pAsyncStruct->target; SEL_CallFuncO selector = pAsyncStruct->selector; const char* filename = pAsyncStruct->filename.c_str(); // cache the image m_mImages.erase(filename); m_mImages.insert(filename, image); image->release(); if (target && selector) { (target->*selector)(image); target->release(); } delete pAsyncStruct; delete pImageInfo; --s_nAsyncRefCount; if (0 == s_nAsyncRefCount) { CAScheduler::unschedule(schedule_selector(CAImageCache::addImageAsyncCallBack), this); } } }
// Helper bool CCLabelTTF::updateTexture() { CAImage *tex; tex = new CAImage(); 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->setImage(tex); // release it tex->release(); // set the size in the sprite CCRect rect =CCRectZero; rect.size = m_pobImage->getContentSize(); this->setImageRect(rect); //ok return true; }
bool CCGridBase::initWithSize(const CCSize& gridSize) { CCDirector *pDirector = CCDirector::sharedDirector(); CCSize s = pDirector->getWinSizeInPixels(); unsigned long POTWide = ccNextPOT((unsigned int)s.width); unsigned long POTHigh = ccNextPOT((unsigned int)s.height); // we only use 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; } CAImage* pTexture = new CAImage(); pTexture->initWithData(data, format, POTWide, POTHigh, s); free(data); if (! pTexture) { CCLOG("cocos2d: CCGrid: error creating texture"); return false; } initWithSize(gridSize, pTexture, false); pTexture->release(); return true; }