-
Notifications
You must be signed in to change notification settings - Fork 54
/
TextureMgr.m
272 lines (206 loc) · 6.07 KB
/
TextureMgr.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* cocos2d for iPhone
*
* http://www.cocos2d-iphone.org
*
* Copyright (C) 2008,2009 Ricardo Quesada
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the 'cocos2d for iPhone' license.
*
* You will find a copy of this license within the cocos2d for iPhone
* distribution inside the "LICENSE" file.
*
*/
#import "TextureMgr.h"
#import "ccMacros.h"
#import "Director.h"
#import "Support/FileUtils.h"
#import "Support/Texture2D.h"
@interface AsyncObject : NSObject
{
SEL selector_;
id target_;
id data_;
}
@property (readwrite,assign) SEL selector;
@property (readwrite,retain) id target;
@property (readwrite,retain) id data;
@end
@implementation AsyncObject
@synthesize selector = selector_;
@synthesize target = target_;
@synthesize data = data_;
- (void) dealloc
{
CCLOG(@"deallocing %@", self);
[target_ release];
[data_ release];
[super dealloc];
}
@end
@implementation TextureMgr
#pragma mark TextureMgr - Alloc, Init & Dealloc
static TextureMgr *sharedTextureMgr;
+ (TextureMgr *)sharedTextureMgr
{
@synchronized([TextureMgr class])
{
if (!sharedTextureMgr)
[[TextureMgr alloc] init];
return sharedTextureMgr;
}
// to avoid compiler warning
return nil;
}
+(id)alloc
{
@synchronized([TextureMgr class])
{
NSAssert(sharedTextureMgr == nil, @"Attempted to allocate a second instance of a singleton.");
sharedTextureMgr = [super alloc];
return sharedTextureMgr;
}
// to avoid compiler warning
return nil;
}
-(id) init
{
if( (self=[super init]) ) {
textures = [[NSMutableDictionary dictionaryWithCapacity: 10] retain];
dictLock = [[NSLock alloc] init];
}
return self;
}
-(void) dealloc
{
CCLOG( @"deallocing %@", self);
[textures release];
[dictLock release];
sharedTextureMgr = nil;
[super dealloc];
}
#pragma mark TextureMgr - Add Images
-(void) addImageWithAsyncObject:(AsyncObject*)async
{
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
// textures will be created on the main OpenGL context
EAGLContext *k_context = [[[EAGLContext alloc]
initWithAPI:kEAGLRenderingAPIOpenGLES1
sharegroup:[[[[Director sharedDirector] openGLView] context] sharegroup]] autorelease];
[EAGLContext setCurrentContext:k_context];
// load / create the texture
Texture2D *tex = [self addImage:async.data];
// The callback will be executed on the main thread
[async.target performSelectorOnMainThread:async.selector withObject:tex waitUntilDone:NO];
[autoreleasepool release];
}
-(void) addImageAsync: (NSString*) filename target:(id)target selector:(SEL)selector
{
NSAssert(filename != nil, @"TextureMgr: fileimage MUST not be nill");
// optimization
Texture2D * tex;
if( (tex=[textures objectForKey: filename] ) ) {
[target performSelector:selector withObject:tex];
return;
}
// schedule the load
AsyncObject *asyncObject = [[AsyncObject alloc] init];
asyncObject.selector = selector;
asyncObject.target = target;
asyncObject.data = filename;
[NSThread detachNewThreadSelector:@selector(addImageWithAsyncObject:) toTarget:self withObject:asyncObject];
[asyncObject release];
}
-(Texture2D*) addImage: (NSString*) path
{
NSAssert(path != nil, @"TextureMgr: fileimage MUST not be nill");
Texture2D * tex = nil;
// MUTEX:
// Needed since addImageAsync calls this method from a different thread
[dictLock lock];
tex=[textures objectForKey: path];
if( ! tex ) {
// Split up directory and filename
NSString *fullpath = [FileUtils fullPathFromRelativePath: path ];
// all images are handled by UIImage except PVR extension that is handled by our own handler
if ( [[path lowercaseString] hasSuffix:@".pvr"] )
tex = [self addPVRTCImage:fullpath];
else {
tex = [ [Texture2D alloc] initWithImage: [UIImage imageWithContentsOfFile: fullpath ] ];
[textures setObject: tex forKey:path];
[tex release];
}
}
[dictLock unlock];
return tex;
}
-(Texture2D*) addPVRTCImage: (NSString*) path bpp:(int)bpp hasAlpha:(BOOL)alpha width:(int)w
{
NSAssert(path != nil, @"TextureMgr: fileimage MUST not be nill");
NSAssert( bpp==2 || bpp==4, @"TextureMgr: bpp must be either 2 or 4");
Texture2D * tex;
if( (tex=[textures objectForKey: path] ) ) {
return tex;
}
// Split up directory and filename
NSString *fullpath = [FileUtils fullPathFromRelativePath:path];
NSData *nsdata = [[NSData alloc] initWithContentsOfFile:fullpath];
tex = [[Texture2D alloc] initWithPVRTCData:[nsdata bytes] level:0 bpp:bpp hasAlpha:alpha length:w];
[textures setObject: tex forKey:path];
[nsdata release];
return [tex autorelease];
}
-(Texture2D*) addPVRTCImage: (NSString*) fileimage
{
NSAssert(fileimage != nil, @"TextureMgr: fileimage MUST not be nill");
Texture2D * tex;
if( (tex=[textures objectForKey: fileimage] ) ) {
return tex;
}
tex = [[Texture2D alloc] initWithPVRTCFile: fileimage];
if( tex )
[textures setObject: tex forKey:fileimage];
return [tex autorelease];
}
-(Texture2D*) addCGImage: (CGImageRef) image
{
NSAssert(image != nil, @"TextureMgr: image MUST not be nill");
NSString *key = [NSString stringWithFormat:@"%08X",(unsigned long)image];
return [self addCGImage: image forKey: key];
}
-(Texture2D*) addCGImage: (CGImageRef) image forKey: (NSString *)key
{
NSAssert(image != nil, @"TextureMgr: image MUST not be nill");
Texture2D * tex;
if( (tex=[textures objectForKey: key] ) ) {
return tex;
}
tex = [[Texture2D alloc] initWithImage: [UIImage imageWithCGImage:image]];
[textures setObject: tex forKey:key];
return [tex autorelease];
}
#pragma mark TextureMgr - Cache
-(void) removeAllTextures
{
[textures removeAllObjects];
}
-(void) removeUnusedTextures
{
NSArray *keys = [textures allKeys];
for( id key in keys ) {
id value = [textures objectForKey:key];
if( [value retainCount] == 1 ) {
CCLOG(@"removing texture: %@", key);
[textures removeObjectForKey:key];
}
}
}
-(void) removeTexture: (Texture2D*) tex
{
if( ! tex )
return;
NSArray *keys = [textures allKeysForObject:tex];
for( NSUInteger i = 0; i < [keys count]; i++ )
[textures removeObjectForKey:[keys objectAtIndex:i]];
}
@end