Skip to content

Commit 20181f0

Browse files
s1ddoks1ddok
authored andcommitted
xcasset support for texture cache + refactoring
1 parent 87f484b commit 20181f0

File tree

1 file changed

+42
-47
lines changed

1 file changed

+42
-47
lines changed

cocos2d/CCTextureCache.m

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -290,58 +290,53 @@ -(CCTexture*) addImage: (NSString*) path
290290

291291
CGFloat contentScale;
292292
NSString *fullpath = [fileUtils fullPathForFilename:path contentScale:&contentScale];
293-
if( ! fullpath ) {
294-
CCLOG(@"cocos2d: Couldn't find file:%@", path);
295-
return nil;
296-
}
297-
298-
NSString *lowerCase = [fullpath lowercaseString];
299-
293+
300294
// all images are handled by UIKit/AppKit except PVR extension that is handled by cocos2d's handler
295+
// main bundle loading is priotirized for backwards compatibility reasons
296+
if( fullpath ) {
297+
NSString *lowerCase = [fullpath lowercaseString];
301298

302-
if ( [lowerCase hasSuffix:@".pvr"] || [lowerCase hasSuffix:@".pvr.gz"] || [lowerCase hasSuffix:@".pvr.ccz"] )
303-
tex = [self addPVRImage:path];
304-
299+
if ( [lowerCase hasSuffix:@".pvr"] || [lowerCase hasSuffix:@".pvr.gz"] || [lowerCase hasSuffix:@".pvr.ccz"] )
300+
tex = [self addPVRImage:path];
305301
#if __CC_PLATFORM_IOS
306-
307-
else {
308-
UIImage *image = [[UIImage alloc] initWithContentsOfFile:fullpath];
309-
tex = [[CCTexture alloc] initWithCGImage:image.CGImage contentScale:contentScale];
310-
311-
CCLOGINFO(@"Texture loaded: %@", path);
312-
313-
if( tex ){
314-
dispatch_sync(_dictQueue, ^{
315-
[_textures setObject: tex forKey:path];
316-
CCLOGINFO(@"Texture %@ cached: %p", path, tex);
317-
});
318-
}else{
319-
CCLOG(@"cocos2d: Couldn't create texture for file:%@ in CCTextureCache", path);
320-
}
321-
}
322-
323-
302+
else {
303+
UIImage *image = [[UIImage alloc] initWithContentsOfFile:fullpath];
304+
tex = [[CCTexture alloc] initWithCGImage:image.CGImage contentScale:contentScale];
305+
}
324306
#elif __CC_PLATFORM_MAC
325-
else {
326-
327-
NSData *data = [[NSData alloc] initWithContentsOfFile:fullpath];
328-
NSBitmapImageRep *image = [[NSBitmapImageRep alloc] initWithData:data];
329-
tex = [ [CCTexture alloc] initWithCGImage:[image CGImage] contentScale:contentScale];
330-
331-
332-
if( tex ){
333-
dispatch_sync(_dictQueue, ^{
334-
[_textures setObject: tex forKey:path];
335-
});
336-
}else{
337-
CCLOG(@"cocos2d: Couldn't create texture for file:%@ in CCTextureCache", path);
338-
}
339-
340-
// autorelease prevents possible crash in multithreaded environments
341-
//[tex autorelease];
342-
}
307+
else {
308+
NSData *data = [[NSData alloc] initWithContentsOfFile:fullpath];
309+
NSBitmapImageRep *image = [[NSBitmapImageRep alloc] initWithData:data];
310+
tex = [ [CCTexture alloc] initWithCGImage:[image CGImage] contentScale:contentScale];
311+
312+
// autorelease prevents possible crash in multithreaded environments
313+
//[tex autorelease];
314+
}
343315
#endif // __CC_PLATFORM_MAC
344-
316+
// if we cant find a file in the main bundle then we trying to load it from xcassets
317+
} else {
318+
#if __CC_PLATFORM_IOS
319+
UIImage *image = [UIImage imageNamed:path];
320+
321+
if (image)
322+
tex = [[CCTexture alloc] initWithCGImage:image.CGImage contentScale:image.scale];
323+
#elif __CC_PLATFORM_MAC
324+
NSImage *image = [NSImage imageNamed:path];
325+
326+
if (image)
327+
tex = [[CCTexture alloc] initWithCGImage:[image CGImageForProposedRect:nil context:nil hints:nil] contentScale:contentScale];
328+
#endif
329+
}
330+
331+
//if we could load a tex from anywhere we add it to the cache
332+
if( tex ){
333+
dispatch_sync(_dictQueue, ^{
334+
[_textures setObject: tex forKey:path];
335+
});
336+
}else{
337+
CCLOG(@"cocos2d: Couldn't create texture for file:%@ in CCTextureCache", path);
338+
return nil;
339+
}
345340
}
346341

347342
return((id)tex.proxy);

0 commit comments

Comments
 (0)