Skip to content

Commit

Permalink
Implement centralized texture binding
Browse files Browse the repository at this point in the history
This is to avoid redundant texture binding changes which are
costly, especially when most of the graphics are stored in a texture
atlas anyway
  • Loading branch information
alco committed Nov 29, 2011
1 parent 3706eeb commit e511c6a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cocos2d/CCDirector.h
Expand Up @@ -187,7 +187,8 @@ and when to execute the Scenes.
/** returns a shared instance of the director */
+(CCDirector *)sharedDirector;


- (void)resetTexture;
- (void)bindTexture:(GLuint)tex_name;

// Window size

Expand Down
17 changes: 17 additions & 0 deletions cocos2d/CCDirector.m
Expand Up @@ -86,6 +86,23 @@ @implementation CCDirector
@synthesize runningThread = runningThread_;
@synthesize notificationNode = notificationNode_;
@synthesize projectionDelegate = projectionDelegate_;


static GLuint _tex_name;

- (void)resetTexture
{
_tex_name = 0;
}

- (void)bindTexture:(GLuint)tex_name
{
if (tex_name != _tex_name) {
_tex_name = tex_name;
glBindTexture(GL_TEXTURE_2D, tex_name);
}
}

//
// singleton stuff
//
Expand Down
2 changes: 1 addition & 1 deletion cocos2d/CCParticleSystemQuad.m
Expand Up @@ -270,7 +270,7 @@ -(void) draw
// Needed states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
// Unneeded states: -

glBindTexture(GL_TEXTURE_2D, [texture_ name]);
[[CCDirector sharedDirector] bindTexture:[texture_ name]];

#define kQuadSize sizeof(quads_[0].bl)

Expand Down
2 changes: 1 addition & 1 deletion cocos2d/CCProgressTimer.m
Expand Up @@ -518,7 +518,7 @@ -(void)draw
/// ========================================================================
// Replaced [texture_ drawAtPoint:CGPointZero] with my own vertexData
// Everything above me and below me is copied from CCTextureNode's draw
glBindTexture(GL_TEXTURE_2D, sprite_.texture.name);
[[CCDirector sharedDirector] bindTexture:sprite_.texture.name];
glVertexPointer(2, GL_FLOAT, sizeof(ccV2F_C4B_T2F), &vertexData_[0].vertices);
glTexCoordPointer(2, GL_FLOAT, sizeof(ccV2F_C4B_T2F), &vertexData_[0].texCoords);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ccV2F_C4B_T2F), &vertexData_[0].colors);
Expand Down
2 changes: 1 addition & 1 deletion cocos2d/CCSprite.m
Expand Up @@ -606,7 +606,7 @@ -(void) draw
glBlendFunc( blendFunc_.src, blendFunc_.dst );

#define kQuadSize sizeof(quad_.bl)
glBindTexture(GL_TEXTURE_2D, [texture_ name]);
[[CCDirector sharedDirector] bindTexture:[texture_ name]];

long offset = (long)&quad_;

Expand Down
2 changes: 1 addition & 1 deletion cocos2d/CCTextureAtlas.m
Expand Up @@ -315,7 +315,7 @@ -(void) drawNumberOfQuads: (NSUInteger) n fromIndex: (NSUInteger) start
// Needed states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
// Unneeded states: -

glBindTexture(GL_TEXTURE_2D, [texture_ name]);
[[CCDirector sharedDirector] bindTexture:[texture_ name]];
#define kQuadSize sizeof(quads_[0].bl)
#if CC_USES_VBO
glBindBuffer(GL_ARRAY_BUFFER, buffersVBO_[0]);
Expand Down

0 comments on commit e511c6a

Please sign in to comment.