Skip to content

Commit 46e3dc9

Browse files
committed
Lazy/weak caching of sprite frame texture.
1 parent 09a3416 commit 46e3dc9

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

cocos2d/CCSpriteFrame.m

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#import "CCSpriteFrameCache.h"
3434

3535
@implementation CCSpriteFrame
36+
{
37+
__weak CCTexture *_lazyTexture;
38+
}
39+
3640
@synthesize textureFilename = _textureFilename;
3741
@synthesize rotated = _rotated;
3842

@@ -137,15 +141,18 @@ -(void) setTexture:(CCTexture *)texture
137141
}
138142
}
139143

140-
-(CCTexture*) texture
144+
-(CCTexture *)lazyTexture
141145
{
142-
if( _texture )
143-
return _texture;
144-
145-
if( _textureFilename )
146-
return [[CCTextureCache sharedTextureCache] addImage:_textureFilename];
146+
CCTexture *texture = _lazyTexture;
147+
if(!texture && _textureFilename){
148+
_lazyTexture = texture = [[CCTextureCache sharedTextureCache] addImage:_textureFilename];
149+
}
150+
151+
return texture;
152+
}
147153

148-
// no texture or texture filename
149-
return nil;
154+
-(CCTexture*) texture
155+
{
156+
return (_texture ?: self.lazyTexture);
150157
}
151158
@end

0 commit comments

Comments
 (0)