diff options
Diffstat (limited to 'libs/cocos2d/CCAnimationCache.m')
| -rwxr-xr-x | libs/cocos2d/CCAnimationCache.m | 101 |
1 files changed, 101 insertions, 0 deletions
| diff --git a/libs/cocos2d/CCAnimationCache.m b/libs/cocos2d/CCAnimationCache.m new file mode 100755 index 0000000..f508227 --- /dev/null +++ b/libs/cocos2d/CCAnimationCache.m | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | /* | ||
| 2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
| 3 | * | ||
| 4 | * Copyright (c) 2010 Ricardo Quesada | ||
| 5 | * Copyright (c) 2011 Zynga Inc. | ||
| 6 | * | ||
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 8 | * of this software and associated documentation files (the "Software"), to deal | ||
| 9 | * in the Software without restriction, including without limitation the rights | ||
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 11 | * copies of the Software, and to permit persons to whom the Software is | ||
| 12 | * furnished to do so, subject to the following conditions: | ||
| 13 | * | ||
| 14 | * The above copyright notice and this permission notice shall be included in | ||
| 15 | * all copies or substantial portions of the Software. | ||
| 16 | * | ||
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 23 | * THE SOFTWARE. | ||
| 24 | * | ||
| 25 | */ | ||
| 26 | |||
| 27 | #import "ccMacros.h" | ||
| 28 | #import "CCAnimationCache.h" | ||
| 29 | #import "CCAnimation.h" | ||
| 30 | #import "CCSprite.h" | ||
| 31 | |||
| 32 | |||
| 33 | @implementation CCAnimationCache | ||
| 34 | |||
| 35 | #pragma mark CCAnimationCache - Alloc, Init & Dealloc | ||
| 36 | |||
| 37 | static CCAnimationCache *sharedAnimationCache_=nil; | ||
| 38 | |||
| 39 | + (CCAnimationCache *)sharedAnimationCache | ||
| 40 | { | ||
| 41 | if (!sharedAnimationCache_) | ||
| 42 | sharedAnimationCache_ = [[CCAnimationCache alloc] init]; | ||
| 43 | |||
| 44 | return sharedAnimationCache_; | ||
| 45 | } | ||
| 46 | |||
| 47 | +(id)alloc | ||
| 48 | { | ||
| 49 | NSAssert(sharedAnimationCache_ == nil, @"Attempted to allocate a second instance of a singleton."); | ||
| 50 | return [super alloc]; | ||
| 51 | } | ||
| 52 | |||
| 53 | +(void)purgeSharedAnimationCache | ||
| 54 | { | ||
| 55 | [sharedAnimationCache_ release]; | ||
| 56 | sharedAnimationCache_ = nil; | ||
| 57 | } | ||
| 58 | |||
| 59 | -(id) init | ||
| 60 | { | ||
| 61 | if( (self=[super init]) ) { | ||
| 62 | animations_ = [[NSMutableDictionary alloc] initWithCapacity: 20]; | ||
| 63 | } | ||
| 64 | |||
| 65 | return self; | ||
| 66 | } | ||
| 67 | |||
| 68 | - (NSString*) description | ||
| 69 | { | ||
| 70 | return [NSString stringWithFormat:@"<%@ = %08X | num of animations = %i>", [self class], self, [animations_ count]]; | ||
| 71 | } | ||
| 72 | |||
| 73 | -(void) dealloc | ||
| 74 | { | ||
| 75 | CCLOGINFO(@"cocos2d: deallocing %@", self); | ||
| 76 | |||
| 77 | [animations_ release]; | ||
| 78 | [super dealloc]; | ||
| 79 | } | ||
| 80 | |||
| 81 | #pragma mark CCAnimationCache - load/get/del | ||
| 82 | |||
| 83 | -(void) addAnimation:(CCAnimation*)animation name:(NSString*)name | ||
| 84 | { | ||
| 85 | [animations_ setObject:animation forKey:name]; | ||
| 86 | } | ||
| 87 | |||
| 88 | -(void) removeAnimationByName:(NSString*)name | ||
| 89 | { | ||
| 90 | if( ! name ) | ||
| 91 | return; | ||
| 92 | |||
| 93 | [animations_ removeObjectForKey:name]; | ||
| 94 | } | ||
| 95 | |||
| 96 | -(CCAnimation*) animationByName:(NSString*)name | ||
| 97 | { | ||
| 98 | return [animations_ objectForKey:name]; | ||
| 99 | } | ||
| 100 | |||
| 101 | @end | ||
