fixed #134, fullPathFromRelativeFile, modify CCTextureCache::addImage

This commit is contained in:
Walzer 2010-09-08 01:28:28 +00:00
parent 4b86b49b08
commit 64d0d7a7e1
3 changed files with 13 additions and 14 deletions

View File

@ -72,7 +72,7 @@ public:
* Otherwise it will return a reference of a previosly loaded image. * Otherwise it will return a reference of a previosly loaded image.
* Supported image extensions: .png, .bmp, .tiff, .jpeg, .pvr, .gif * Supported image extensions: .png, .bmp, .tiff, .jpeg, .pvr, .gif
*/ */
CCTexture2D* addImage(const char* fileimage, const char *key = NULL); CCTexture2D* addImage(const char* fileimage);
/** Returns a Texture2D object given a file image /** Returns a Texture2D object given a file image
* If the file image was not previously loaded, it will create a new CCTexture2D object and it will return it. * If the file image was not previously loaded, it will create a new CCTexture2D object and it will return it.

View File

@ -228,7 +228,7 @@ namespace cocos2d {
// Try to get the texture from the cache // Try to get the texture from the cache
char *textureName = (char *)valueForKey("textureFileName", dictionary); char *textureName = (char *)valueForKey("textureFileName", dictionary);
std::string fullpath = CCFileUtils::fullPathFromRelativeFile(textureName, m_sPlistFile.c_str()); std::string fullpath = CCFileUtils::fullPathFromRelativeFile(textureName, m_sPlistFile.c_str());
this->m_pTexture = CCTextureCache::sharedTextureCache()->addImage(fullpath.c_str(), textureName); this->m_pTexture = CCTextureCache::sharedTextureCache()->addImage(fullpath.c_str());
// if it fails, try to get it from the base64-gzipped data // if it fails, try to get it from the base64-gzipped data
if ( ! m_pTexture ) if ( ! m_pTexture )
@ -250,7 +250,7 @@ namespace cocos2d {
NSAssert(isOK, "CCParticleSystem: error init image with Data"); NSAssert(isOK, "CCParticleSystem: error init image with Data");
CCX_BREAK_IF(!isOK); CCX_BREAK_IF(!isOK);
m_pTexture = CCTextureCache::sharedTextureCache()->addUIImage(image, textureName); m_pTexture = CCTextureCache::sharedTextureCache()->addUIImage(image, fullpath.c_str());
} }
} }
NSAssert( this->m_pTexture != NULL, "CCParticleSystem: error loading the texture"); NSAssert( this->m_pTexture != NULL, "CCParticleSystem: error loading the texture");

View File

@ -172,11 +172,13 @@ void CCTextureCache::addImageAsync(const char* filename, NSObject *target, fpAsy
// [asyncObject release]; // [asyncObject release];
} }
CCTexture2D * CCTextureCache::addImage(const char * path, const char *key) CCTexture2D * CCTextureCache::addImage(const char * path)
{ {
NSAssert(path != NULL, "TextureCache: fileimage MUST not be NULL"); NSAssert(path != NULL, "TextureCache: fileimage MUST not be NULL");
CCTexture2D * texture = NULL; CCTexture2D * texture = NULL;
// Split up directory and filename
std::string fullpath(CCFileUtils::fullPathFromRelativePath(path));
std::string temp(path); std::string temp(path);
// MUTEX: // MUTEX:
@ -184,13 +186,10 @@ CCTexture2D * CCTextureCache::addImage(const char * path, const char *key)
m_pDictLock->lock(); m_pDictLock->lock();
texture = m_pTextures->objectForKey(!key ? key : temp); texture = m_pTextures->objectForKey(fullpath);
if( ! texture ) if( ! texture )
{ {
// Split up directory and filename
std::string fullpath(CCFileUtils::fullPathFromRelativePath(path));
// all images are handled by UIImage except PVR extension that is handled by our own handler // all images are handled by UIImage except PVR extension that is handled by our own handler
// if ( [[path lowercaseString] hasSuffix:@".pvr"] ) // if ( [[path lowercaseString] hasSuffix:@".pvr"] )
for (unsigned int i = 0; i < temp.length(); ++i) for (unsigned int i = 0; i < temp.length(); ++i)
@ -217,7 +216,7 @@ CCTexture2D * CCTextureCache::addImage(const char * path, const char *key)
CCX_SAFE_DELETE(image);// image->release(); CCX_SAFE_DELETE(image);// image->release();
if( texture ) if( texture )
m_pTextures->setObject(texture, !key ? key : path); m_pTextures->setObject(texture, fullpath);
else else
CCLOG("cocos2d: Couldn't add image:%s in CCTextureCache", path); CCLOG("cocos2d: Couldn't add image:%s in CCTextureCache", path);
@ -310,12 +309,12 @@ CCTexture2D * CCTextureCache::addPVRTCImage(const char* fileimage)
}*/ }*/
CCTexture2D* CCTextureCache::addUIImage(UIImage *image, const char *key) CCTexture2D* CCTextureCache::addUIImage(UIImage *image, const char *key)
{ {
NSAssert(image != NULL, "TextureCache: image MUST not be nill"); NSAssert(image != NULL && key != NULL, "TextureCache: image MUST not be nill");
CCTexture2D * texture = NULL; CCTexture2D * texture = NULL;
std::string forKey = key;
// If key is nil, then create a new texture each time // If key is nil, then create a new texture each time
if( key && (texture = m_pTextures->objectForKey(key)) ) if(texture = m_pTextures->objectForKey(forKey))
{ {
return texture; return texture;
} }
@ -324,9 +323,9 @@ CCTexture2D* CCTextureCache::addUIImage(UIImage *image, const char *key)
texture = new CCTexture2D(); texture = new CCTexture2D();
texture->initWithImage(image); texture->initWithImage(image);
if(texture && key) if(texture)
{ {
m_pTextures->setObject(texture, key); m_pTextures->setObject(texture, forKey);
} }
else else
{ {