issue #7 : add CCTextureAtlas.h and implement CCTextureAtlas.cpp partly, add CCTextureCache.h

This commit is contained in:
Walzer 2010-07-16 06:15:06 +00:00
parent 1d0f9bd582
commit aaf5b5859f
7 changed files with 652 additions and 0 deletions

View File

@ -0,0 +1,142 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCTEXTURE_ATLAS_H__
#define __CCTEXTURE_ATLAS_H__
#include "CCTexture2D.h"
#include "ccTypes.h"
#include "ccConfig.h"
/** A class that implements a Texture Atlas.
Supported features:
* The atlas file can be a PVRTC, PNG or any other fomrat supported by Texture2D
* Quads can be udpated in runtime
* Quads can be added in runtime
* Quads can be removed in runtime
* Quads can be re-ordered in runtime
* The TextureAtlas capacity can be increased or decreased in runtime
* OpenGL component: V3F, C4B, T2F.
The quads are rendered using an OpenGL ES VBO.
To render the quads using an interleaved vertex array list, you should modify the ccConfig.h file
*/
class CCTextureAtlas : public NSObject
{
protected:
GLushort *m_pIndices;
#if CC_TEXTURE_ATLAS_USES_VBO
GLuint buffersVBO_[2]; //0: vertex 1: indices
#endif // CC_TEXTURE_ATLAS_USES_VBO
/** quantity of quads that are going to be drawn */
CCX_PROPERTY_READONLY(UINT32, m_uTotalQuads, TotalQuads)
/** quantity of quads that can be stored with the current texture atlas size */
CCX_PROPERTY_READONLY(UINT32, m_uCapacity, Capacity)
/** Texture of the texture atlas */
CCX_PROPERTY(CCTexture2D, m_pTexture, Texture)
/** Quads that are going to be rendered */
CCX_PROPERTY(ccV3F_C4B_T2F_Quad, m_pQuads, Quads)
public:
/** creates a TextureAtlas with an filename and with an initial capacity for Quads.
* The TextureAtlas capacity can be increased in runtime.
*/
static CCTextureAtlas * textureAtlasWithFile(NSString* file , UINT32 capacity);
/** initializes a TextureAtlas with a filename and with a certain capacity for Quads.
* The TextureAtlas capacity can be increased in runtime.
*
* WARNING: Do not reinitialize the TextureAtlas because it will leak memory (issue #706)
*/
CCTextureAtlas * initWithFile(NSString* file, UINT32 capacity);
/** creates a TextureAtlas with a previously initialized Texture2D object, and
* with an initial capacity for n Quads.
* The TextureAtlas capacity can be increased in runtime.
*/
static CCTextureAtlas * textureAtlasWithTexture(CCTexture2D *tex, UINT32 capacity);
/** initializes a TextureAtlas with a previously initialized Texture2D object, and
* with an initial capacity for Quads.
* The TextureAtlas capacity can be increased in runtime.
*
* WARNING: Do not reinitialize the TextureAtlas because it will leak memory (issue #706)
*/
CCTextureAtlas * initWithTexture(CCTexture2D *tex, UINT32 capacity);
/** updates a Quad (texture, vertex and color) at a certain index
* index must be between 0 and the atlas capacity - 1
@since v0.8
*/
void updateQuad(ccV3F_C4B_T2F_Quad* quad, UINT32 index);
/** Inserts a Quad (texture, vertex and color) at a certain index
index must be between 0 and the atlas capacity - 1
@since v0.8
*/
void insertQuad(ccV3F_C4B_T2F_Quad* quad, UINT32 index);
/** Removes the quad that is located at a certain index and inserts it at a new index
This operation is faster than removing and inserting in a quad in 2 different steps
@since v0.7.2
*/
void insertQuadFromIndex(UINT32 fromIndex, UINT32 newIndex);
/** removes a quad at a given index number.
The capacity remains the same, but the total number of quads to be drawn is reduced in 1
@since v0.7.2
*/
void removeQuadAtIndex(UINT32 index);
/** removes all Quads.
The TextureAtlas capacity remains untouched. No memory is freed.
The total number of quads to be drawn will be 0
@since v0.7.2
*/
void removeAllQuads();
/** resize the capacity of the Texture Atlas.
* The new capacity can be lower or higher than the current one
* It returns YES if the resize was successful.
* If it fails to resize the capacity it will return NO with a new capacity of 0.
*/
bool resizeCapacity(UINT32 n);
/** draws n quads
* n can't be greater than the capacity of the Atlas
*/
void drawNumberOfQuads(UINT32 n);
/** draws all the Atlas's Quads
*/
void drawQuads();
};
#endif //__CCTEXTURE_ATLAS_H__

View File

@ -0,0 +1,123 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCTEXTURE_CACHE_H__
#define __CCTEXTURE_CACHE_H__
#include "../cocoa/NSObject.h"
//#import <Foundation/Foundation.h>
//#import <CoreGraphics/CGImage.h>
class CCTexture2D;
/** Singleton that handles the loading of textures
* Once the texture is loaded, the next time it will return
* a reference of the previously loaded texture reducing GPU & CPU memory
*/
class CCTextureCache : public NSObject
{
/** @todo NSMutableDictionary, NSLock
NSMutableDictionary *textures;
NSLock *dictLock;
NSLock *contextLock;*/
/** Retruns ths shared instance of the cache */
static CCTextureCache * sharedTextureCache();
/** purges the cache. It releases the retained instance.
@since v0.99.0
*/
static void purgeSharedTextureCache();
/** Returns a Texture2D object given an file image
* If the file image was not previously loaded, it will create a new CCTexture2D
* object and it will return it. It will use the filename as a key.
* Otherwise it will return a reference of a previosly loaded image.
* Supported image extensions: .png, .bmp, .tiff, .jpeg, .pvr, .gif
*/
CCTexture2D* addImage(NSString* fileimage);
/** Returns a Texture2D object given a file image
* If the file image was not previously loaded, it will create a new CCTexture2D object and it will return it.
* 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.
* The callback will be called from the main thread, so it is safe to create any cocos2d object from the callback.
* Supported image extensions: .png, .bmp, .tiff, .jpeg, .pvr, .gif
* @since v0.8
*/
/// @todo -(void) addImageAsync:(NSString*) filename target:(id)target selector:(SEL)selector;
/** Returns a Texture2D object given an PVRTC RAW filename
* If the file 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 previosly loaded image
*
* It can only load square images: width == height, and it must be a power of 2 (128,256,512...)
* bpp can only be 2 or 4. 2 means more compression but lower quality.
* hasAlpha: whether or not the image contains alpha channel
*/
CCTexture2D* addPVRTCImage(NSString* fileimage, int bpp, bool hasAlpha, int width);
/** Returns a Texture2D object given an PVRTC filename
* If the file 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 previosly loaded image
*/
CCTexture2D* addPVRTCImage(NSString* filename);
/** 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.
* @since v0.8
*/
/// @todo CGImageRef CCTexture2D* addCGImage(CGImageRef image, NSString * key);
/** Purges the dictionary of loaded textures.
* Call this method if you receive the "Memory Warning"
* In the short term: it will free some resources preventing your app from being killed
* In the medium term: it will allocate more resources
* In the long term: it will be the same
*/
void removeAllTextures();
/** Removes unused textures
* Textures that have a retain count of 1 will be deleted
* It is convinient to call this method after when starting a new Scene
* @since v0.8
*/
void removeUnusedTextures();
/** Deletes a texture from the cache given a texture
*/
void removeTexture(CCTexture2D* tex);
/** Deletes a texture from the cache given a its key name
@since v0.99.4
*/
void removeTextureForKey(NSString* textureKeyName);
};
#endif //__CCTEXTURE_CACHE_H__

View File

@ -68,6 +68,41 @@ CCPVRTexture::~CCPVRTexture()
glDeleteTextures(1, &m_uName); glDeleteTextures(1, &m_uName);
} }
GLuint CCPVRTexture::getName()
{
return m_uName;
}
uint32_t CCPVRTexture::getWidth()
{
return m_uWidth;
}
uint32_t CCPVRTexture::getHeight()
{
return m_uHeight;
}
GLenum CCPVRTexture::getInternalFormat()
{
return m_uInternalFormat;
}
bool CCPVRTexture::getHasAlpha()
{
return m_bHasAlpha;
}
bool CCPVRTexture::getRetainName()
{
return m_bRetainName;
}
void CCPVRTexture::setRetainName(bool var)
{
m_bRetainName = var;
}
/** @todo NSData uint8_t /** @todo NSData uint8_t
- (BOOL)unpackPVRData:(NSData *)data - (BOOL)unpackPVRData:(NSData *)data
{ {

View File

@ -0,0 +1,26 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCTextureAtlas.h"

View File

@ -0,0 +1,326 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCTextureCache.h"
/*
#import "CCTextureCache.h"
#import "CCTexture2D.h"
#import "ccMacros.h"
#import "CCDirector.h"
#import "Support/CCFileUtils.h"
/// @todo EAGLContext static EAGLContext *auxEAGLcontext = NULL;
@interface CCAsyncObject : NSObject
{
SEL selector_;
id target_;
id data_;
}
@property (readwrite,assign) SEL selector;
@property (readwrite,retain) id target;
@property (readwrite,retain) id data;
@end
@implementation CCAsyncObject
@synthesize selector = selector_;
@synthesize target = target_;
@synthesize data = data_;
- (void) dealloc
{
CCLOGINFO(@"cocos2d: deallocing %@", self);
[target_ release];
[data_ release];
[super dealloc];
}
@end
@implementation CCTextureCache
#pragma mark TextureCache - Alloc, Init & Dealloc
static CCTextureCache *sharedTextureCache;
+ (CCTextureCache *)sharedTextureCache
{
if (!sharedTextureCache)
sharedTextureCache = [[CCTextureCache alloc] init];
return sharedTextureCache;
}
+(id)alloc
{
NSAssert(sharedTextureCache == nil, @"Attempted to allocate a second instance of a singleton.");
return [super alloc];
}
+(void)purgeSharedTextureCache
{
[sharedTextureCache release];
}
-(id) init
{
if( (self=[super init]) ) {
textures = [[NSMutableDictionary dictionaryWithCapacity: 10] retain];
dictLock = [[NSLock alloc] init];
contextLock = [[NSLock alloc] init];
}
return self;
}
- (NSString*) description
{
return [NSString stringWithFormat:@"<%@ = %08X | num of textures = %i>", [self class], self, [textures count]];
}
-(void) dealloc
{
CCLOG(@"cocos2d: deallocing %@", self);
[textures release];
[dictLock release];
[contextLock release];
[auxEAGLcontext release];
auxEAGLcontext = nil;
sharedTextureCache = nil;
[super dealloc];
}
#pragma mark TextureCache - Add Images
-(void) addImageWithAsyncObject:(CCAsyncObject*)async
{
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
// textures will be created on the main OpenGL context
// it seems that in SDK 2.2.x there can't be 2 threads creating textures at the same time
// the lock is used for this purpose: issue #472
[contextLock lock];
if( auxEAGLcontext == nil ) {
auxEAGLcontext = [[EAGLContext alloc]
initWithAPI:kEAGLRenderingAPIOpenGLES1
sharegroup:[[[[CCDirector sharedDirector] openGLView] context] sharegroup]];
if( ! auxEAGLcontext )
CCLOG(@"cocos2d: TextureCache: Could not create EAGL context");
}
if( [EAGLContext setCurrentContext:auxEAGLcontext] ) {
// load / create the texture
CCTexture2D *tex = [self addImage:async.data];
// The callback will be executed on the main thread
[async.target performSelectorOnMainThread:async.selector withObject:tex waitUntilDone:NO];
[EAGLContext setCurrentContext:nil];
} else {
CCLOG(@"cocos2d: TetureCache: EAGLContext error");
}
[contextLock unlock];
[autoreleasepool release];
}
-(void) addImageAsync: (NSString*) filename target:(id)target selector:(SEL)selector
{
NSAssert(filename != nil, @"TextureCache: fileimage MUST not be nill");
// optimization
CCTexture2D * tex;
if( (tex=[textures objectForKey: filename] ) ) {
[target performSelector:selector withObject:tex];
return;
}
// schedule the load
CCAsyncObject *asyncObject = [[CCAsyncObject alloc] init];
asyncObject.selector = selector;
asyncObject.target = target;
asyncObject.data = filename;
[NSThread detachNewThreadSelector:@selector(addImageWithAsyncObject:) toTarget:self withObject:asyncObject];
[asyncObject release];
}
-(CCTexture2D*) addImage: (NSString*) path
{
NSAssert(path != nil, @"TextureCache: fileimage MUST not be nill");
CCTexture2D * 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 = [CCFileUtils 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 {
// prevents overloading the autorelease pool
UIImage *image = [ [UIImage alloc] initWithContentsOfFile: fullpath ];
tex = [ [CCTexture2D alloc] initWithImage: image ];
[image release];
if( tex )
[textures setObject: tex forKey:path];
else
CCLOG(@"cocos2d: Couldn't add image:%@ in CCTextureCache", path);
[tex release];
}
}
[dictLock unlock];
return tex;
}
-(CCTexture2D*) addPVRTCImage: (NSString*) path bpp:(int)bpp hasAlpha:(BOOL)alpha width:(int)w
{
NSAssert(path != nil, @"TextureCache: fileimage MUST not be nill");
NSAssert( bpp==2 || bpp==4, @"TextureCache: bpp must be either 2 or 4");
CCTexture2D * tex;
if( (tex=[textures objectForKey: path] ) ) {
return tex;
}
// Split up directory and filename
NSString *fullpath = [CCFileUtils fullPathFromRelativePath:path];
NSData *nsdata = [[NSData alloc] initWithContentsOfFile:fullpath];
tex = [[CCTexture2D alloc] initWithPVRTCData:[nsdata bytes] level:0 bpp:bpp hasAlpha:alpha length:w];
if( tex )
[textures setObject: tex forKey:path];
else
CCLOG(@"cocos2d: Couldn't add PVRTCImage:%@ in CCTextureCache",path);
[nsdata release];
return [tex autorelease];
}
-(CCTexture2D*) addPVRTCImage: (NSString*) fileimage
{
NSAssert(fileimage != nil, @"TextureCache: fileimage MUST not be nill");
CCTexture2D * tex;
if( (tex=[textures objectForKey: fileimage] ) ) {
return tex;
}
tex = [[CCTexture2D alloc] initWithPVRTCFile: fileimage];
if( tex )
[textures setObject: tex forKey:fileimage];
else
CCLOG(@"cocos2d: Couldn't add PVRTCImage:%@ in CCTextureCache",fileimage);
return [tex autorelease];
}
-(CCTexture2D*) addCGImage: (CGImageRef) imageref forKey: (NSString *)key
{
NSAssert(imageref != nil, @"TextureCache: image MUST not be nill");
CCTexture2D * tex = nil;
// If key is nil, then create a new texture each time
if( key && (tex=[textures objectForKey: key] ) ) {
return tex;
}
// prevents overloading the autorelease pool
UIImage *image = [[UIImage alloc] initWithCGImage:imageref];
tex = [[CCTexture2D alloc] initWithImage: image];
[image release];
if(tex && key)
[textures setObject: tex forKey:key];
else
CCLOG(@"cocos2d: Couldn't add CGImage in CCTextureCache");
return [tex autorelease];
}
#pragma mark TextureCache - Remove
-(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(@"cocos2d: CCTextureCache: removing unused texture: %@", key);
[textures removeObjectForKey:key];
}
}
}
-(void) removeTexture: (CCTexture2D*) tex
{
if( ! tex )
return;
NSArray *keys = [textures allKeysForObject:tex];
for( NSUInteger i = 0; i < [keys count]; i++ )
[textures removeObjectForKey:[keys objectAtIndex:i]];
}
-(void) removeTextureForKey:(NSString*)name
{
if( ! name )
return;
[textures removeObjectForKey:name];
}
@end
*/