static void loadImageData(AsyncStruct *pAsyncStruct) { const char *filename = pAsyncStruct->filename.c_str(); // compute image type CCImage::EImageFormat imageType = computeImageFormatType(pAsyncStruct->filename); if (imageType == CCImage::kFmtUnKnown) { CCLOG("unsupported format %s",filename); delete pAsyncStruct; return; } // generate image CCImage *pImage = new CCImage(); if( pImage ) { bool bRet = pImage->initWithImageFileThreadSafe(filename, imageType); if ( !bRet ) { std::string strFileName = filename; if( imageType == CCImage::kFmtPng ) { size_t pos = strFileName.length(); string strOld = ".png"; if((pos = strFileName.rfind(strOld, pos)) != string::npos) { strFileName = strFileName.replace(pos, strOld.length(), ".fkp"); imageType = CCImage::kFmtFKP; bRet = pImage->initWithImageFileThreadSafe(strFileName.c_str(), imageType); } } if( !bRet ) { CC_SAFE_RELEASE(pImage); CCLOG("can not load %s", filename); return; } } } // generate image info ImageInfo *pImageInfo = new ImageInfo(); pImageInfo->asyncStruct = pAsyncStruct; pImageInfo->image = pImage; pImageInfo->imageType = imageType; // put the image info into the queue pthread_mutex_lock(&s_ImageInfoMutex); s_pImageQueue->push(pImageInfo); pthread_mutex_unlock(&s_ImageInfoMutex); }
static void loadImageData(AsyncStruct *pAsyncStruct) { const char *filename = pAsyncStruct->filename.c_str(); // compute image type CCImage::EImageFormat imageType = computeImageFormatType(pAsyncStruct->filename); if (imageType == CCImage::kFmtUnKnown) { CCLOG("unsupported format %s",filename); delete pAsyncStruct; return; } // generate image CCImage *pImage = new CCImage(); if (pImage && !pImage->initWithImageFileThreadSafe(filename, imageType)) { CC_SAFE_RELEASE(pImage); CCLOG("can not load %s", filename); return; } // generate image info ImageInfo *pImageInfo = new ImageInfo(); pImageInfo->asyncStruct = pAsyncStruct; pImageInfo->image = pImage; pImageInfo->imageType = imageType; // put the image info into the queue pthread_mutex_lock(&s_ImageInfoMutex); s_pImageQueue->push(pImageInfo); pthread_mutex_unlock(&s_ImageInfoMutex); }
bool FDPixelSprite::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { if (this->isContainTouchLocation(pTouch) ) { ccColor4B c = {0, 0, 0, 0}; CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCPoint touchPoint = pTouch->getLocationInView(); CCSize cSize = this->getContentSize(); CCPoint point =this->getAnchorPointInPoints(); point = ccp(cSize.width - point.x,cSize.height- point.y); CCPoint pos(this->getPositionX() - point.x,winSize.height-this->getPositionY()- point.y); CCPoint localPoint = ccp(touchPoint.x - pos.x, touchPoint.y -pos.y); unsigned int x = localPoint.x, y = localPoint.y; //This method is currently only supports symmetric image //unsigned char *data_ = this->getTexture()->getFDImageData(); //Efficiency of this method is relatively low CCImage * img = new CCImage(); img->initWithImageFileThreadSafe(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(TESTPNG) ); unsigned char *data_ = img->getData(); unsigned int *pixel = (unsigned int *)data_; pixel = pixel + (y * (int)this->getContentSize().width)* 1 + x * 1; c.r = *pixel & 0xff; c.g = (*pixel >> 8) & 0xff; c.b = (*pixel >> 16) & 0xff; c.a = (*pixel >> 24) & 0xff; if (c.a == 0) { CCLog("firedragonpzy:ccTouchBegan----------"); return false; }else { return true; } }
void PlistAsyncLoader::LoadImageWorker() { UniqueMutexLock lock(_image_load_sleep_mutex); while (true) { if (_btw_q.Empty()) { if (_quit_worker) break; else _image_load_sleep_cond.wait(lock); continue; } auto item = _btw_q.GetTop(); auto pngname = item->name; pngname.replace(pngname.find_last_of('.'), sizeof(".plist"), ".png"); CCImage *pImage = new CCImage(); if (pImage && !pImage->initWithImageFileThreadSafe(pngname.c_str(), CCImage::kFmtPng)) { CC_SAFE_RELEASE(pImage); CCLOG("FATAL: Can not load %s", pngname.c_str()); delete item; return; } ImageInfoPtr info = new _ImageInfo(); info->baseinfo = item; info->image = pImage; CCLOG("### LoadImageWorker: pngname = %s, pImage = %#x", pngname.c_str(), pImage); _itm_q.Push(info); } }
static void* loadImage(void* data) { AsyncStruct *pAsyncStruct = NULL; while (true) { // create autorelease pool for iOS CCThread thread; thread.createAutoreleasePool(); std::queue<AsyncStruct*> *pQueue = s_pAsyncStructQueue; pthread_mutex_lock(&s_asyncStructQueueMutex);// get async struct from queue if (pQueue->empty()) { pthread_mutex_unlock(&s_asyncStructQueueMutex); if (need_quit) { break; } else { pthread_cond_wait(&s_SleepCondition, &s_SleepMutex); continue; } } else { pAsyncStruct = pQueue->front(); pQueue->pop(); pthread_mutex_unlock(&s_asyncStructQueueMutex); } const char *filename = pAsyncStruct->filename.c_str(); // compute image type CCImage::EImageFormat imageType = computeImageFormatType(pAsyncStruct->filename); if (imageType == CCImage::kFmtUnKnown) { CCLOG("unsupported format %s",filename); delete pAsyncStruct; continue; } // generate image CCImage *pImage = new CCImage(); if (pImage && !pImage->initWithImageFileThreadSafe(filename, imageType)) { CC_SAFE_RELEASE(pImage); CCLOG("can not load %s", filename); continue; } // generate image info ImageInfo *pImageInfo = new ImageInfo(); pImageInfo->asyncStruct = pAsyncStruct; pImageInfo->image = pImage; pImageInfo->imageType = imageType; // put the image info into the queue pthread_mutex_lock(&s_ImageInfoMutex); s_pImageQueue->push(pImageInfo); pthread_mutex_unlock(&s_ImageInfoMutex); } if( s_pAsyncStructQueue != NULL ) { delete s_pAsyncStructQueue; s_pAsyncStructQueue = NULL; delete s_pImageQueue; s_pImageQueue = NULL; pthread_mutex_destroy(&s_asyncStructQueueMutex); pthread_mutex_destroy(&s_ImageInfoMutex); pthread_mutex_destroy(&s_SleepMutex); pthread_cond_destroy(&s_SleepCondition); } return 0; }
static void* loadImage(void* data) { // create autorelease pool for iOS CCThread thread; thread.createAutoreleasePool(); AsyncStruct *pAsyncStruct = NULL; while (true) { // wait for rendering thread to ask for loading if s_pAsyncStructQueue is empty int semWaitRet = sem_wait(s_pSem); if( semWaitRet < 0 ) { CCLOG( "CCTextureCache async thread semaphore error: %s\n", strerror( errno ) ); break; } std::queue<AsyncStruct*> *pQueue = s_pAsyncStructQueue; pthread_mutex_lock(&s_asyncStructQueueMutex);// get async struct from queue if (pQueue->empty()) { pthread_mutex_unlock(&s_asyncStructQueueMutex); if (need_quit) break; else continue; } else { pAsyncStruct = pQueue->front(); pQueue->pop(); pthread_mutex_unlock(&s_asyncStructQueueMutex); } const char *filename = pAsyncStruct->filename.c_str(); // compute image type CCImage::EImageFormat imageType = computeImageFormatType(pAsyncStruct->filename); if (imageType == CCImage::kFmtUnKnown) { CCLOG("unsupported format %s",filename); delete pAsyncStruct; continue; } // generate image CCImage *pImage = new CCImage(); if (! pImage->initWithImageFileThreadSafe(filename, imageType)) { delete pImage; CCLOG("can not load %s", filename); continue; } // generate image info ImageInfo *pImageInfo = new ImageInfo(); pImageInfo->asyncStruct = pAsyncStruct; pImageInfo->image = pImage; pImageInfo->imageType = imageType; // put the image info into the queue pthread_mutex_lock(&s_ImageInfoMutex); s_pImageQueue->push(pImageInfo); pthread_mutex_unlock(&s_ImageInfoMutex); } if( s_pSem != NULL ) { #if CC_ASYNC_TEXTURE_CACHE_USE_NAMED_SEMAPHORE sem_unlink(CC_ASYNC_TEXTURE_CACHE_SEMAPHORE); sem_close(s_pSem); #else sem_destroy(s_pSem); #endif s_pSem = NULL; delete s_pAsyncStructQueue; delete s_pImageQueue; } return 0; }
static void* loadImage(void* data) { AsyncStruct *pAsyncStruct = NULL; while (true) { // create autorelease pool for iOS CCThread thread; thread.createAutoreleasePool(); // wait for rendering thread to ask for loading if s_pAsyncStructQueue is empty int semWaitRet = sem_wait(s_pSem); if( semWaitRet < 0 ) { CCLOG( "CCTextureCache async thread semaphore error: %s\n", strerror( errno ) ); break; } std::queue<AsyncStruct*>* pQueue = s_pAsyncStructQueue; pthread_mutex_lock(&s_asyncStructQueueMutex);// get async struct from queue if (pQueue->empty()) { pthread_mutex_unlock(&s_asyncStructQueueMutex); if (need_quit) break; else continue; } else { pAsyncStruct = pQueue->front(); pQueue->pop(); pthread_mutex_unlock(&s_asyncStructQueueMutex); } const char* filename = pAsyncStruct->filename.c_str(); // compute image type CCImage::EImageFormat imageType = CCImage::computeImageFormatType(pAsyncStruct->filename); if (imageType == CCImage::kFmtUnKnown) { CCLOG("unsupported format %s",filename); delete pAsyncStruct; continue; } // generate image CCImage* pImage = pAsyncStruct->image; if (pImage && !pImage->initWithImageFileThreadSafe(filename, imageType)) { pAsyncStruct->image = NULL; CC_SAFE_RELEASE(pImage); CCLOG("can not load %s", filename); continue; } unsigned char* tempData = CCImage::convertToRequiredFormat(pImage); if (tempData != pImage->getData()) { CCSize size = CCSizeMake(pImage->getWidth(), pImage->getHeight()); size_t bpp = pImage->getBitsPerComponent(); CCTexture2DPixelFormat pixelFormat = pImage->hasAlpha() ? CCTexture2D::getDefaultAlphaPixelFormat() : (bpp >= 8 ? kCCTexture2DPixelFormat_RGB888 : kCCTexture2DPixelFormat_RGB565); pAsyncStruct->image = NULL; CC_SAFE_RELEASE(pImage); pAsyncStruct->data = tempData; pAsyncStruct->size = size; pAsyncStruct->pixelFormat = pixelFormat; } // generate image info ImageInfo* pImageInfo = new ImageInfo(); pImageInfo->asyncStruct = pAsyncStruct; pImageInfo->imageType = imageType; // put the image info into the queue pthread_mutex_lock(&s_ImageInfoMutex); s_pImageQueue->push(pImageInfo); pthread_mutex_unlock(&s_ImageInfoMutex); } if( s_pSem != NULL ) { #if CC_ASYNC_TEXTURE_CACHE_USE_NAMED_SEMAPHORE sem_unlink(CC_ASYNC_TEXTURE_CACHE_SEMAPHORE); sem_close(s_pSem); #else sem_destroy(s_pSem); #endif s_pSem = NULL; delete s_pAsyncStructQueue; delete s_pImageQueue; } return 0; }