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