Created
August 3, 2015 10:44
-
-
Save richardgroves/a79320ce8557fcb17ba3 to your computer and use it in GitHub Desktop.
cocos2d - Category on CCTextureCache to allow specification of image scale when adding a texture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CCTextureCache+AddTexture.h | |
// | |
// | |
// Created by Richard Groves on 12/06/2014. | |
// | |
// Extension for Cocos2d to support creating textures from images that are already loaded and have a contentScale != 1.0 | |
#import "CCTextureCache.h" | |
@interface CCTextureCache (AddTexture) | |
/** | |
* Returns a Texture2D object given an CGImageRef image. | |
* | |
* If the image was not previously loaded, it will create a new CCTexture2D object and it will return it. | |
* Otherwise it will return a reference of a previously loaded image. | |
* The "key" parameter will be used as the "key" for the cache. | |
* If "key" is nil, then a new texture will be created each time. | |
* | |
* @param image CG image to create texture from. | |
* @param contentScale Content scale. | |
* @param key Key used to define texture in cache. | |
* | |
* @return Texture. | |
*/ | |
-(CCTexture*) addCGImage: (CGImageRef) image contentScale: (CGFloat)contentScale forKey: (NSString *)key; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CCTextureCache+AddTexture.m | |
// | |
// | |
// Created by Richard Groves on 12/06/2014. | |
// | |
// Extension for Cocos2d to support creating textures from images that are already loaded and have a contentScale != 1.0 | |
#import "CCTextureCache+AddTexture.h" | |
#import "CCTexture_Private.h" | |
@implementation CCTextureCache (AddTexture) | |
-(CCTexture*) addCGImage: (CGImageRef) imageref contentScale: (CGFloat)contentScale forKey: (NSString *)key | |
{ | |
NSAssert(imageref != nil, @"TextureCache: image MUST not be nill"); | |
__block CCTexture * tex = nil; | |
// If key is nil, then create a new texture each time | |
if( key ) { | |
dispatch_sync(_dictQueue, ^{ | |
tex = [_textures objectForKey:key]; | |
}); | |
if(tex) | |
return((id)tex.proxy); | |
} | |
tex = [[CCTexture alloc] initWithCGImage:imageref contentScale:contentScale]; | |
if(tex && key){ | |
dispatch_sync(_dictQueue, ^{ | |
[_textures setObject: tex forKey:key]; | |
}); | |
}else{ | |
CCLOG(@"cocos2d: Couldn't add CGImage in CCTextureCache"); | |
} | |
return((id)tex.proxy); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment