コード例 #1
0
ファイル: FileHelper.cpp プロジェクト: banweima/PokerKing
CCTexture2D* FileHelper::getImageFileByName(string pFileName){
 
    //第一先获取文件的路径
    string path = CCFileUtils::sharedFileUtils()->getWriteablePath() + pFileName;
    CCLOG("path = %s",path.c_str());
    
    //创建一个文件指针
    FILE* file = fopen(path.c_str(), "r");
    
    if (file) {
        char* buf;  //要获取的字符串
        int len;    //获取的长度
        /*获取长度*/
        fseek(file, 0, SEEK_END);   //移到尾部
        len = ftell(file);          //提取长度
        rewind(file);               //回归原位
        CCLOG("count the file content len = %d",len);
        //分配buf空间
        buf = (char*)malloc(sizeof(char) * len + 1);
        if (!buf) {
            CCLOG("malloc space is not enough.");
            return NULL;
        }
        
        //读取文件
        //读取进的buf,单位大小,长度,文件指针
        int rLen = fread(buf, sizeof(char), len, file);
        buf[rLen] = '\0';
        CCLOG("has read Length = %d",rLen);
        CCLOG("has read content = %s",buf);
        
        CCImage* img = new CCImage();
        img->initWithImageData((unsigned char*)buf, len);
        CCLog("img size = %d, %d", img->getWidth(), img->getHeight());
        CCTexture2D* texture2D = new CCTexture2D();
        texture2D->initWithImage(img);
        texture2D->retain();
        
        string result = buf;
        fclose(file);
        free(buf);
        return texture2D;
    }
    else
        CCLOG("open file error.");
    
    return NULL;
}
コード例 #2
0
ファイル: bitmap.cpp プロジェクト: cjv123/RPGR
Bitmap::Bitmap(const char *filename) : p(0)
{
	p = new BitmapPrivate;

	string* path = new string(SceneMain::workPath+filename);

	CCImage* image = new CCImage;
	string filename_c=*path;
	if (path->find(".png") == string::npos)
	{
		filename_c=*path + ".png";
		if (filename_c.find("Characters") != string::npos)
		{
			int pos = filename_c.find('-');
			if(pos!=string::npos)
			{
				char c = filename_c[pos+1];
				filename_c[pos+1]=toupper(c);
			}
		}
	}

	bool ret =image->initWithImageFile(filename_c.c_str());

	if (!ret)
	{
		filename_c = *path + ".jpg";
		ret = image->initWithImageFile(filename_c.c_str(),CCImage::kFmtJpg);
	}

	if (ret)
	{
		ThreadHandler hander={handler_method_create_sprite,(int)this,(void*)image};
		pthread_mutex_lock(&s_thread_handler_mutex);
		p->m_filename = filename_c;
		p->m_width = image->getWidth();
		p->m_height = image->getHeight();
		ThreadHandlerMananger::getInstance()->pushHandler(hander,this);
		pthread_mutex_unlock(&s_thread_handler_mutex);
	}


}
コード例 #3
0
CCSprite* CCColorUtil::graylightWithCCSprite(CCSprite* oldSprite,bool isLight)
{
	//CCSprite转成CCimage
	CCPoint ptOldAnchor = oldSprite->getAnchorPoint();
	CCPoint ptOld = oldSprite->getPosition();
	oldSprite->setAnchorPoint(ccp(0,0));
	oldSprite->setPosition(ccp(0,0));
	CCRenderTexture *outTexture = CCRenderTexture::create((int)oldSprite->getContentSize().width*1,(int)oldSprite->getContentSize().height*1);
	outTexture->begin();
	oldSprite->visit();
	outTexture->end();
	oldSprite->setAnchorPoint(ptOldAnchor);
	oldSprite->setPosition(ptOld);

	CCImage* finalImage = outTexture->newCCImage();
	unsigned char *pData = finalImage->getData();
	int iIndex = 0;

	if(isLight)
	{
		for (int i = 0; i < finalImage->getHeight(); i ++)
		{
			for (int j = 0; j < finalImage->getWidth(); j ++)
			{
				// highlight
				int iHightlightPlus = 50;
				int iBPos = iIndex;
				unsigned int iB = pData[iIndex];
				iIndex ++;
				unsigned int iG = pData[iIndex];
				iIndex ++;
				unsigned int iR = pData[iIndex];
				iIndex ++;
				//unsigned int o = pData[iIndex];
				iIndex ++;  //原来的示例缺少
				iB = (iB + iHightlightPlus > 255 ? 255 : iB + iHightlightPlus);
				iG = (iG + iHightlightPlus > 255 ? 255 : iG + iHightlightPlus);
				iR = (iR + iHightlightPlus > 255 ? 255 : iR + iHightlightPlus);
				//            iR = (iR < 0 ? 0 : iR);
				//            iG = (iG < 0 ? 0 : iG);
				//            iB = (iB < 0 ? 0 : iB);
				pData[iBPos] = (unsigned char)iB;
				pData[iBPos + 1] = (unsigned char)iG;
				pData[iBPos + 2] = (unsigned char)iR;
			}
		}
	}else{
		for (int i = 0; i < finalImage->getHeight(); i ++)
		{
			for (int j = 0; j < finalImage->getWidth(); j ++)
			{
				// gray
				int iBPos = iIndex;
				unsigned int iB = pData[iIndex];
				iIndex ++;
				unsigned int iG = pData[iIndex];
				iIndex ++;
				unsigned int iR = pData[iIndex];
				iIndex ++;
				//unsigned int o = pData[iIndex];
				iIndex ++; //原来的示例缺少
				unsigned int iGray = 0.3 * iR + 0.4 * iG + 0.2 * iB;
				pData[iBPos] = pData[iBPos + 1] = pData[iBPos + 2] = (unsigned char)iGray;
			}
		}
	}

	CCTexture2D *texture = new CCTexture2D;
	texture->initWithImage(finalImage);
	CCSprite* newSprite = CCSprite::createWithTexture(texture);
	delete finalImage;
	texture->release();
	return newSprite;
}
コード例 #4
0
ファイル: GameUtil.cpp プロジェクト: faransae/Classes
CCRect GameUtil::getFitRect(CCSprite* pSprite)
{
	// 원본의 원래위치 백업
	CCPoint ptOrg = pSprite->getPosition();

	// 원본 스프라이트 위치를 Center로 설정
	pSprite->setPosition(ccp(pSprite->getContentSize().width/2, pSprite->getContentSize().height/2));

	// CCImage를 만들기 위해서 RenderTexture 생성
	CCRenderTexture * rtNew = CCRenderTexture::create((int)pSprite->getContentSize().width, (int)pSprite->getContentSize().height);
	rtNew->begin();
	pSprite->visit();
    rtNew->end();

	// CCImage 생성
	CCImage *pImage = rtNew->newCCImage();
	int width = pImage->getWidth();
	int height = pImage->getHeight();

	int bytesPerComponent = pImage->getBitsPerComponent() / 8; // 기본값 8bit
	bool bAlpha = pImage->hasAlpha();

	int nRGBA = 4;
	if (!bAlpha)
		nRGBA = 3;

	// 픽셀 데이터 (RGBA 순서, 각각 bytesPerComponent 크기만큼)
	unsigned char* pData = pImage->getData();

	int index = 0;
	int l=5000;	// left
	int r=0;	// right
	int t=5000;	// top
	int b=0;	// bottom

	unsigned char* ptr;

	for (int x=0; x<width; x++) {
		for (int y=0; y<height; y++) {

			index = (y*width + x) * bytesPerComponent * nRGBA;

			ptr = pData + index;

			if (0 != ptr[0] || 0 != ptr[1] || 0 != ptr[2]) 
			{
				l = min(l, x); // left
				r = max(r, x); // right
				t = min(t, y); // top
				b = max(b, y); // bottpm
			}
		}
	}

	// 좌표계 보정 (픽셀데이터는 좌상단을 0,0 으로 하기때문에)
	int b2 = pSprite->getContentSize().height - t;
	int t2 = pSprite->getContentSize().height - b;

	CC_SAFE_DELETE(pImage);

	// 원래 위치로 설정
	pSprite->setPosition(ptOrg);

	CCRect rc;
	rc.origin.x = l;
	rc.origin.y = t2;
	rc.size.width = r - l;
	rc.size.height = b2 - t2;

	return rc;
}
コード例 #5
0
ファイル: GameUtil.cpp プロジェクト: faransae/Classes
CCSprite* GameUtil::fitSprite(CCSprite* pSprite)
{
	// 원본의 원래위치 백업
	CCPoint ptOrg = pSprite->getPosition();

	// 원본 스프라이트 위치를 Center로 설정
	pSprite->setPosition(ccp(pSprite->getContentSize().width/2, pSprite->getContentSize().height/2));

	// CCImage를 만들기 위해서 RenderTexture 생성
	CCRenderTexture * rtNew = CCRenderTexture::create((int)pSprite->getContentSize().width, (int)pSprite->getContentSize().height);
	rtNew->begin();
	pSprite->visit();
    rtNew->end();

	// CCImage 생성
	CCImage *pImage = rtNew->newCCImage();
	int width = pImage->getWidth();
	int height = pImage->getHeight();

	int bytesPerComponent = pImage->getBitsPerComponent() / 8; // 기본값 8bit
	bool bAlpha = pImage->hasAlpha();

	int nRGBA = 4;
	if (!bAlpha)
		nRGBA = 3;

	// 픽셀 데이터 (RGBA 순서, 각각 bytesPerComponent 크기만큼)
	unsigned char* pData = pImage->getData();

	int index = 0;
	int l=5000;	// left
	int r=0;	// right
	int t=0;	// top
	int b=5000;	// bottom

	unsigned char* ptr;

	for (int x=0; x<width; x++) {
		for (int y=0; y<height; y++) {

			index = (y*width + x) * bytesPerComponent * nRGBA;

			ptr = pData + index;

			if (0 != ptr[0] || 0 != ptr[1] || 0 != ptr[2]) 
			{
				l = min(l, x); // left
				r = max(r, x); // right
				t = max(t, y); // top
				b = min(b, y); // bottom
			}
		}
	}

	// 좌표계 보정 (픽셀데이터는 좌상단을 0,0 으로 하기때문에)
	int b2 = pSprite->getContentSize().height - t;
	int t2 = pSprite->getContentSize().height - b;

	CC_SAFE_DELETE(pImage);

	// 원래 위치로 설정
	pSprite->setPosition(ptOrg);

	// 스프라이트로 생성
	CCSprite* pCrop = CCSprite::createWithTexture(pSprite->getTexture());
	pCrop->setTextureRect(CCRect(l, b2, r-l, t2-b2));
	pCrop->setPosition(ccp(pCrop->getContentSize().width/2, pCrop->getContentSize().height/2));
	pCrop->setFlipY(true);

	// 크기에 꼭 맞는 새로운 RenderTexture를 만든다.
	CCRenderTexture * rtNew2 = CCRenderTexture::create(r-l, t2-b2);
	rtNew2->begin();
	pCrop->visit();
    rtNew2->end();

	// 크기에 꼭 맞는 새로운 Sprite를 만든다.
	CCSprite *retSprite = CCSprite::createWithTexture(rtNew2->getSprite()->getTexture());
	retSprite->setFlipY(true);

	rtNew2->release();
	pCrop->release();
	
	return retSprite;
}
コード例 #6
0
ファイル: CCTextureCache.cpp プロジェクト: c0i/Dorothy
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;
}