et1337

TextureCutoutShape.mm

Jul 5th, 2011
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  TextureCutoutShape.mm
  3. //  DemoBros
  4. //
  5. //  Created by et1337 on 6/29/11.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import "TextureCutoutShape.h"
  10. #import "CCNode+Extensions.h"
  11.  
  12. @implementation TextureCutoutShape
  13. @synthesize vertexCount;
  14. @synthesize textureScale;
  15.  
  16. -(CCTexture2D*) texture
  17. {
  18.     return texture;
  19. }
  20.  
  21. -(void) setTexture:(CCTexture2D*)tex
  22. {
  23.     texture = tex;
  24.     ccTexParams params = { GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT };
  25.     [texture setTexParameters:&params];
  26. }
  27.  
  28. -(id) init
  29. {
  30.     NSAssert(false, @"Incorrect constructor for TextureCutoutShape.");
  31.     return NULL;
  32. }
  33.  
  34. -(id) initWithFile:(NSString*)file andVertexCount:(int)vertices
  35. {
  36.     if ((self = [self initWithTexture:[[CCTextureCache sharedTextureCache] addImage:file] andVertexCount:vertices]))
  37.     {
  38.     }
  39.     return self;
  40. }
  41.  
  42. -(id) initWithTexture:(CCTexture2D*)tex andVertexCount:(int)vertices
  43. {
  44.     if ((self = [super init]))
  45.     {
  46.         self.texture = tex;
  47.         vertexCount = vertices;
  48.         points = (CGPoint*)malloc(sizeof(CGPoint) * vertexCount);
  49.         coords = (CGPoint*)malloc(sizeof(CGPoint) * vertexCount);
  50.         colors = (ccColor4B*)malloc(sizeof(ccColor4B) * vertexCount);
  51.         self.color = ccc3(255, 255, 255);
  52.         self.opacity = 255;
  53.         textureScale = 0.0025f;
  54.     }
  55.     return self;
  56. }
  57.  
  58. -(ccColor3B) color
  59. {
  60.     return ccc3(colors[0].r, colors[0].g, colors[0].b);
  61. }
  62.  
  63. -(void) setColor:(ccColor3B)color
  64. {
  65.     for (int i = 0; i < vertexCount; i++)
  66.         colors[i] = ccc4(color.r, color.g, color.b, colors[i].a);
  67. }
  68.  
  69. -(GLubyte) opacity
  70. {
  71.     return colors[0].a;
  72. }
  73.  
  74. -(void) setOpacity:(GLubyte)opacity
  75. {
  76.     for (int i = 0; i < vertexCount; i++)
  77.         colors[i].a = opacity;
  78. }
  79.  
  80. -(void) dealloc
  81. {
  82.     [texture release];
  83.     free(points);
  84.     free(coords);
  85.     free(colors);
  86.     [super dealloc];
  87. }
  88.  
  89. -(void) draw
  90. {
  91. #ifdef DEBUG
  92.     [super draw];
  93. #endif
  94.    
  95.     for (int i = 0; i < vertexCount; i++)
  96.         coords[i] = ccpMult([self.layer convertToNodeSpace:[self convertToWorldSpace:points[i]]], textureScale);
  97.    
  98.     glBindTexture(GL_TEXTURE_2D, texture.name);
  99.     glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
  100.     glTexCoordPointer(2, GL_FLOAT, 0, coords);
  101.     glVertexPointer(2, GL_FLOAT, 0, points);
  102.     glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount);
  103. }
  104.  
  105. @end
Advertisement
Add Comment
Please, Sign In to add comment