/libs/cocos2d/CCPointParticleSystem.m

http://github.com/kstenerud/ObjectAL-for-iPhone · Objective C · 200 lines · 119 code · 40 blank · 41 comment · 15 complexity · 1ce879774660616320a33ee5dc862350 MD5 · raw file

  1. /*
  2. * cocos2d for iPhone: http://www.cocos2d-iphone.org
  3. *
  4. * Copyright (c) 2008-2010 Ricardo Quesada
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. // opengl
  26. #import <OpenGLES/ES1/gl.h>
  27. // cocos2d
  28. #import "CCPointParticleSystem.h"
  29. #import "CCTextureCache.h"
  30. #import "ccMacros.h"
  31. // support
  32. #import "Support/OpenGL_Internal.h"
  33. #import "Support/CGPointExtension.h"
  34. @implementation CCPointParticleSystem
  35. -(id) initWithTotalParticles:(int) numberOfParticles
  36. {
  37. if( (self=[super initWithTotalParticles:numberOfParticles]) ) {
  38. vertices = malloc( sizeof(ccPointSprite) * totalParticles );
  39. if( ! vertices ) {
  40. NSLog(@"cocos2d: Particle system: not enough memory");
  41. [self release];
  42. return nil;
  43. }
  44. #if CC_USES_VBO
  45. glGenBuffers(1, &verticesID);
  46. // initial binding
  47. glBindBuffer(GL_ARRAY_BUFFER, verticesID);
  48. glBufferData(GL_ARRAY_BUFFER, sizeof(ccPointSprite)*totalParticles, vertices, GL_DYNAMIC_DRAW);
  49. glBindBuffer(GL_ARRAY_BUFFER, 0);
  50. #endif
  51. }
  52. return self;
  53. }
  54. -(void) dealloc
  55. {
  56. free(vertices);
  57. #if CC_USES_VBO
  58. glDeleteBuffers(1, &verticesID);
  59. #endif
  60. [super dealloc];
  61. }
  62. -(void) updateQuadWithParticle:(tCCParticle*)p newPosition:(CGPoint)newPos
  63. {
  64. // place vertices and colos in array
  65. vertices[particleIdx].pos = newPos;
  66. vertices[particleIdx].size = p->size;
  67. vertices[particleIdx].colors = p->color;
  68. }
  69. -(void) postStep
  70. {
  71. #if CC_USES_VBO
  72. glBindBuffer(GL_ARRAY_BUFFER, verticesID);
  73. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(ccPointSprite)*particleCount, vertices);
  74. glBindBuffer(GL_ARRAY_BUFFER, 0);
  75. #endif
  76. }
  77. -(void) draw
  78. {
  79. if (particleIdx==0)
  80. return;
  81. // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
  82. // Needed states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY
  83. // Unneeded states: GL_TEXTURE_COORD_ARRAY
  84. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  85. glBindTexture(GL_TEXTURE_2D, texture_.name);
  86. glEnable(GL_POINT_SPRITE_OES);
  87. glTexEnvi( GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE );
  88. #define kPointSize sizeof(vertices[0])
  89. #if CC_USES_VBO
  90. glBindBuffer(GL_ARRAY_BUFFER, verticesID);
  91. glVertexPointer(2,GL_FLOAT, kPointSize, 0);
  92. glColorPointer(4, GL_FLOAT, kPointSize, (GLvoid*) offsetof(ccPointSprite, colors) );
  93. glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
  94. glPointSizePointerOES(GL_FLOAT, kPointSize, (GLvoid*) offsetof(ccPointSprite, size) );
  95. #else // Uses Vertex Array List
  96. int offset = (int)vertices;
  97. glVertexPointer(2,GL_FLOAT, kPointSize, (GLvoid*) offset);
  98. int diff = offsetof(ccPointSprite, colors);
  99. glColorPointer(4, GL_FLOAT, kPointSize, (GLvoid*) (offset+diff));
  100. glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
  101. diff = offsetof(ccPointSprite, size);
  102. glPointSizePointerOES(GL_FLOAT, kPointSize, (GLvoid*) (offset+diff));
  103. #endif
  104. BOOL newBlend = NO;
  105. if( blendFunc_.src != CC_BLEND_SRC || blendFunc_.dst != CC_BLEND_DST ) {
  106. newBlend = YES;
  107. glBlendFunc( blendFunc_.src, blendFunc_.dst );
  108. }
  109. glDrawArrays(GL_POINTS, 0, particleIdx);
  110. // restore blend state
  111. if( newBlend )
  112. glBlendFunc( CC_BLEND_SRC, CC_BLEND_DST);
  113. #if CC_USES_VBO
  114. // unbind VBO buffer
  115. glBindBuffer(GL_ARRAY_BUFFER, 0);
  116. #endif
  117. glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
  118. glDisable(GL_POINT_SPRITE_OES);
  119. // restore GL default state
  120. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  121. }
  122. #pragma mark Non supported properties
  123. //
  124. // SPIN IS NOT SUPPORTED
  125. //
  126. -(void) setStartSpin:(float)a
  127. {
  128. NSAssert(a == 0, @"PointParticleSystem doesn't support spinning");
  129. [super setStartSpin:a];
  130. }
  131. -(void) setStartSpinVar:(float)a
  132. {
  133. NSAssert(a == 0, @"PointParticleSystem doesn't support spinning");
  134. [super setStartSpin:a];
  135. }
  136. -(void) setEndSpin:(float)a
  137. {
  138. NSAssert(a == 0, @"PointParticleSystem doesn't support spinning");
  139. [super setStartSpin:a];
  140. }
  141. -(void) setEndSpinVar:(float)a
  142. {
  143. NSAssert(a == 0, @"PointParticleSystem doesn't support spinning");
  144. [super setStartSpin:a];
  145. }
  146. //
  147. // SIZE > 64 IS NOT SUPPORTED
  148. //
  149. -(void) setStartSize:(float)size
  150. {
  151. NSAssert(size >= 0 && size <= CC_MAX_PARTICLE_SIZE, @"PointParticleSystem only supports 0 <= size <= 64");
  152. [super setStartSize:size];
  153. }
  154. -(void) setEndSize:(float)size
  155. {
  156. NSAssert( (size == kCCParticleStartSizeEqualToEndSize) ||
  157. ( size >= 0 && size <= CC_MAX_PARTICLE_SIZE), @"PointParticleSystem only supports 0 <= size <= 64");
  158. [super setEndSize:size];
  159. }
  160. @end