diff --git a/cocos2dx/platform/CCImage.h b/cocos2dx/platform/CCImage.h index a4a0928b8d..c83b2daea1 100644 --- a/cocos2dx/platform/CCImage.h +++ b/cocos2dx/platform/CCImage.h @@ -40,6 +40,7 @@ public: kFmtJpg = 0, kFmtPng, kFmtRawData, + kFmtUnKnown }EImageFormat; typedef enum diff --git a/cocos2dx/textures/CCTextureCache.cpp b/cocos2dx/textures/CCTextureCache.cpp index d27de8589a..2c6d901e05 100644 --- a/cocos2dx/textures/CCTextureCache.cpp +++ b/cocos2dx/textures/CCTextureCache.cpp @@ -68,6 +68,22 @@ static sem_t s_sem; static std::queue *s_pAsyncStructQueue; static std::queue *s_pImageQueue; +static CCImage::EImageFormat computeImageFormatType(string& filename) +{ + CCImage::EImageFormat ret = CCImage::kFmtUnKnown; + + if ((std::string::npos != filename.find(".jpg")) || (std::string::npos != filename.find(".jpeg"))) + { + ret = CCImage::kFmtJpg; + } + else if ((std::string::npos != filename.find(".png")) || (std::string::npos != filename.find(".PNG"))) + { + ret = CCImage::kFmtPng; + } + + return ret; +} + static void* loadImage(void* data) { // create autorelease pool for iOS @@ -98,40 +114,24 @@ static void* loadImage(void* data) const char *filename = pAsyncStruct->filename.c_str(); - CCLOG("thread 0x%x is loading image %s", pthread_self(), filename); - - // generate image - CCImage *pImage = NULL; - CCImage::EImageFormat imageType; - if (std::string::npos != pAsyncStruct->filename.find(".jpg") || std::string::npos != pAsyncStruct->filename.find(".jpeg")) - { - pImage = new CCImage(); - if (!pImage->initWithImageFileThreadSafe(filename, cocos2d::CCImage::kFmtJpg)) - { - delete pImage; - CCLOG("can not load %s", filename); - imageType = CCImage::kFmtJpg; - continue; - } - } - else if (std::string::npos != pAsyncStruct->filename.find(".png")) - { - pImage = new CCImage(); - if (! pImage->initWithImageFileThreadSafe(filename, cocos2d::CCImage::kFmtPng)) - { - delete pImage; - CCLOG("can not load %s", filename); - imageType = CCImage::kFmtPng; - continue; - } - } - else + // compute image type + CCImage::EImageFormat imageType = computeImageFormatType(pAsyncStruct->filename); + if (imageType == CCImage::kFmtUnKnown) { CCLOG("unsupportted 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();