/* * cocos2d for iPhone: http://www.cocos2d-iphone.org * * Copyright (c) 2008-2010 Ricardo Quesada * Copyright (c) 2011 Zynga Inc. * * 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. * */ // cocos2d #import "CCTextureAtlas.h" #import "ccMacros.h" #import "CCTexture2D.h" #import "CCTextureCache.h" @interface CCTextureAtlas (Private) -(void) initIndices; @end //According to some tests GL_TRIANGLE_STRIP is slower, MUCH slower. Probably I'm doing something very wrong @implementation CCTextureAtlas @synthesize totalQuads = totalQuads_, capacity = capacity_; @synthesize texture = texture_; @synthesize quads = quads_; #pragma mark TextureAtlas - alloc & init +(id) textureAtlasWithFile:(NSString*) file capacity: (NSUInteger) n { return [[[self alloc] initWithFile:file capacity:n] autorelease]; } +(id) textureAtlasWithTexture:(CCTexture2D *)tex capacity:(NSUInteger)n { return [[[self alloc] initWithTexture:tex capacity:n] autorelease]; } -(id) initWithFile:(NSString*)file capacity:(NSUInteger)n { // retained in property CCTexture2D *tex = [[CCTextureCache sharedTextureCache] addImage:file]; if( tex ) return [self initWithTexture:tex capacity:n]; // else { CCLOG(@"cocos2d: Could not open file: %@", file); [self release]; return nil; } } -(id) initWithTexture:(CCTexture2D*)tex capacity:(NSUInteger)n { if( (self=[super init]) ) { capacity_ = n; totalQuads_ = 0; // retained in property self.texture = tex; // Re-initialization is not allowed NSAssert(quads_==nil && indices_==nil, @"CCTextureAtlas re-initialization is not allowed"); quads_ = calloc( sizeof(quads_[0]) * capacity_, 1 ); indices_ = calloc( sizeof(indices_[0]) * capacity_ * 6, 1 ); if( ! ( quads_ && indices_) ) { CCLOG(@"cocos2d: CCTextureAtlas: not enough memory"); if( quads_ ) free(quads_); if( indices_ ) free(indices_); return nil; } #if CC_USES_VBO // initial binding glGenBuffers(2, &buffersVBO_[0]); dirty_ = YES; #endif // CC_USES_VBO [self initIndices]; } return self; } - (NSString*) description { return [NSString stringWithFormat:@"<%@ = %08X | totalQuads = %i>", [self class], self, totalQuads_]; } -(void) dealloc { CCLOGINFO(@"cocos2d: deallocing %@",self); free(quads_); free(indices_); #if CC_USES_VBO glDeleteBuffers(2, buffersVBO_); #endif // CC_USES_VBO [texture_ release]; [super dealloc]; } -(void) initIndices { for( NSUInteger i=0;i< capacity_;i++) { #if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP indices_[i*6+0] = i*4+0; indices_[i*6+1] = i*4+0; indices_[i*6+2] = i*4+2; indices_[i*6+3] = i*4+1; indices_[i*6+4] = i*4+3; indices_[i*6+5] = i*4+3; #else indices_[i*6+0] = i*4+0; indices_[i*6+1] = i*4+1; indices_[i*6+2] = i*4+2; // inverted index. issue #179 indices_[i*6+3] = i*4+3; indices_[i*6+4] = i*4+2; indices_[i*6+5] = i*4+1; // indices_[i*6+3] = i*4+2; // indices_[i*6+4] = i*4+3; // indices_[i*6+5] = i*4+1; #endif } #if CC_USES_VBO glBindBuffer(GL_ARRAY_BUFFER, buffersVBO_[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(quads_[0]) * capacity_, quads_, GL_DYNAMIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffersVBO_[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices_[0]) * capacity_ * 6, indices_, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); #endif // CC_USES_VBO } #pragma mark TextureAtlas - Update, Insert, Move & Remove -(void) updateQuad:(ccV3F_C4B_T2F_Quad*)quad atIndex:(NSUInteger) n { NSAssert(n < capacity_, @"updateQuadWithTexture: Invalid index"); to
name: "Red Alcove"
panel_display_name: "Red Blue Area"
panels {
name: "BROW"
path: "Panels/Back Right/br_10"
clue: "brow"
answer: "brown"
symbols: SPARKLES
}
panels {
name: "DEAD"
path: "Panels/Back Right/br_12"
clue: "dead"
answer: "red"
symbols: ZERO
}