diff options
author | Starla Insigna <starla4444@gmail.com> | 2011-07-30 11:19:14 -0400 |
---|---|---|
committer | Starla Insigna <starla4444@gmail.com> | 2011-07-30 11:19:14 -0400 |
commit | 9cd57b731ab1c666d4a1cb725538fdc137763d12 (patch) | |
tree | 5bac45ae5157a1cb10c6e45500cbf72789917980 /libs/cocos2d/CCTextureCache.h | |
download | cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.gz cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.bz2 cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.zip |
Initial commit (version 0.2.1)
Diffstat (limited to 'libs/cocos2d/CCTextureCache.h')
-rwxr-xr-x | libs/cocos2d/CCTextureCache.h | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/libs/cocos2d/CCTextureCache.h b/libs/cocos2d/CCTextureCache.h new file mode 100755 index 0000000..7084793 --- /dev/null +++ b/libs/cocos2d/CCTextureCache.h | |||
@@ -0,0 +1,149 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2008-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 <Availability.h> | ||
28 | |||
29 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
30 | #import <CoreGraphics/CGImage.h> | ||
31 | #endif | ||
32 | |||
33 | #import <Foundation/Foundation.h> | ||
34 | |||
35 | @class CCTexture2D; | ||
36 | |||
37 | /** Singleton that handles the loading of textures | ||
38 | * Once the texture is loaded, the next time it will return | ||
39 | * a reference of the previously loaded texture reducing GPU & CPU memory | ||
40 | */ | ||
41 | @interface CCTextureCache : NSObject | ||
42 | { | ||
43 | NSMutableDictionary *textures_; | ||
44 | NSLock *dictLock_; | ||
45 | NSLock *contextLock_; | ||
46 | } | ||
47 | |||
48 | /** Retruns ths shared instance of the cache */ | ||
49 | + (CCTextureCache *) sharedTextureCache; | ||
50 | |||
51 | /** purges the cache. It releases the retained instance. | ||
52 | @since v0.99.0 | ||
53 | */ | ||
54 | +(void)purgeSharedTextureCache; | ||
55 | |||
56 | |||
57 | /** Returns a Texture2D object given an file image | ||
58 | * If the file image was not previously loaded, it will create a new CCTexture2D | ||
59 | * object and it will return it. It will use the filename as a key. | ||
60 | * Otherwise it will return a reference of a previosly loaded image. | ||
61 | * Supported image extensions: .png, .bmp, .tiff, .jpeg, .pvr, .gif | ||
62 | */ | ||
63 | -(CCTexture2D*) addImage: (NSString*) fileimage; | ||
64 | |||
65 | /** Returns a Texture2D object given a file image | ||
66 | * If the file image was not previously loaded, it will create a new CCTexture2D object and it will return it. | ||
67 | * Otherwise it will load a texture in a new thread, and when the image is loaded, the callback will be called with the Texture2D as a parameter. | ||
68 | * The callback will be called from the main thread, so it is safe to create any cocos2d object from the callback. | ||
69 | * Supported image extensions: .png, .bmp, .tiff, .jpeg, .pvr, .gif | ||
70 | * @since v0.8 | ||
71 | */ | ||
72 | -(void) addImageAsync:(NSString*) filename target:(id)target selector:(SEL)selector; | ||
73 | |||
74 | /** Returns a Texture2D object given an CGImageRef image | ||
75 | * If the image was not previously loaded, it will create a new CCTexture2D object and it will return it. | ||
76 | * Otherwise it will return a reference of a previously loaded image | ||
77 | * The "key" parameter will be used as the "key" for the cache. | ||
78 | * If "key" is nil, then a new texture will be created each time. | ||
79 | * @since v0.8 | ||
80 | */ | ||
81 | -(CCTexture2D*) addCGImage: (CGImageRef) image forKey: (NSString *)key; | ||
82 | |||
83 | /** Returns an already created texture. Returns nil if the texture doesn't exist. | ||
84 | @since v0.99.5 | ||
85 | */ | ||
86 | -(CCTexture2D *) textureForKey:(NSString *)key; | ||
87 | |||
88 | /** Purges the dictionary of loaded textures. | ||
89 | * Call this method if you receive the "Memory Warning" | ||
90 | * In the short term: it will free some resources preventing your app from being killed | ||
91 | * In the medium term: it will allocate more resources | ||
92 | * In the long term: it will be the same | ||
93 | */ | ||
94 | -(void) removeAllTextures; | ||
95 | |||
96 | /** Removes unused textures | ||
97 | * Textures that have a retain count of 1 will be deleted | ||
98 | * It is convinient to call this method after when starting a new Scene | ||
99 | * @since v0.8 | ||
100 | */ | ||
101 | -(void) removeUnusedTextures; | ||
102 | |||
103 | /** Deletes a texture from the cache given a texture | ||
104 | */ | ||
105 | -(void) removeTexture: (CCTexture2D*) tex; | ||
106 | |||
107 | /** Deletes a texture from the cache given a its key name | ||
108 | @since v0.99.4 | ||
109 | */ | ||
110 | -(void) removeTextureForKey: (NSString*) textureKeyName; | ||
111 | |||
112 | @end | ||
113 | |||
114 | |||
115 | @interface CCTextureCache (PVRSupport) | ||
116 | |||
117 | /** Returns a Texture2D object given an PVRTC RAW filename | ||
118 | * If the file image was not previously loaded, it will create a new CCTexture2D | ||
119 | * object and it will return it. Otherwise it will return a reference of a previosly loaded image | ||
120 | * | ||
121 | * It can only load square images: width == height, and it must be a power of 2 (128,256,512...) | ||
122 | * bpp can only be 2 or 4. 2 means more compression but lower quality. | ||
123 | * hasAlpha: whether or not the image contains alpha channel | ||
124 | * | ||
125 | * IMPORTANT: This method is only defined on iOS. It is not supported on the Mac version. | ||
126 | */ | ||
127 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
128 | -(CCTexture2D*) addPVRTCImage:(NSString*)fileimage bpp:(int)bpp hasAlpha:(BOOL)alpha width:(int)w; | ||
129 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED | ||
130 | |||
131 | /** Returns a Texture2D object given an PVR filename. | ||
132 | * If the file image was not previously loaded, it will create a new CCTexture2D | ||
133 | * object and it will return it. Otherwise it will return a reference of a previosly loaded image | ||
134 | * | ||
135 | */ | ||
136 | -(CCTexture2D*) addPVRImage:(NSString*) filename; | ||
137 | |||
138 | @end | ||
139 | |||
140 | |||
141 | @interface CCTextureCache (Debug) | ||
142 | /** Output to CCLOG the current contents of this CCTextureCache | ||
143 | * This will attempt to calculate the size of each texture, and the total texture memory in use | ||
144 | * | ||
145 | * @since v1.0 | ||
146 | */ | ||
147 | -(void) dumpCachedTextureInfo; | ||
148 | |||
149 | @end | ||