Skip to content

Commit

Permalink
Merge branch 'master' into work
Browse files Browse the repository at this point in the history
  • Loading branch information
shashachu committed Jan 12, 2011
2 parents d759454 + 52e967a commit ced617a
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 48 deletions.
18 changes: 17 additions & 1 deletion cocos2d-android/src/org/cocos2d/nodes/CCNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,8 @@ public void convertToNodeSpace(float x, float y, CGPoint ret) {
worldToNodeTransform(temp);

CGPointUtil.applyAffineTransform(x, y, temp, ret);

pool.free(temp);
}

/** converts local coordinate to world space
Expand All @@ -1089,7 +1091,21 @@ public CGPoint convertToWorldSpace(float x, float y) {
CGPoint nodePoint = CGPoint.make(x, y);
return CGPoint.applyAffineTransform(nodePoint, nodeToWorldTransform());
}


/**
* This is analog method, result is written to ret. No garbage.
*/
public void convertToWorldSpace(float x, float y , CGPoint ret) {
OneClassPool<CGAffineTransform> pool = PoolHolder.getInstance().getCGAffineTransformPool();

CGAffineTransform temp = pool.get();
nodeToWorldTransform(temp);

CGPointUtil.applyAffineTransform(x, y, temp, ret);

pool.free(temp);
}

/** converts a world coordinate to local coordinate
treating the returned/received node point as anchor relative
@since v0.7.1
Expand Down
108 changes: 65 additions & 43 deletions cocos2d-android/src/org/cocos2d/particlesystem/CCParticleSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
import org.cocos2d.types.ccBlendFunc;
import org.cocos2d.types.ccColor4F;
import org.cocos2d.types.ccPointSprite;
import org.cocos2d.types.util.CGPointUtil;
import org.cocos2d.types.util.PoolHolder;
import org.cocos2d.types.util.ccColor4FUtil;
import org.cocos2d.utils.pool.OneClassPool;

// typedef void (*CC_UPDATE_PARTICLE_IMP)(id, SEL, tCCParticle*, CGPoint);

Expand Down Expand Up @@ -95,7 +99,7 @@ public abstract class CCParticleSystem extends CCNode implements CCTextureProtoc
*/
static class CCParticle {
static class ParticleModeA {
CGPoint dir;
CGPoint dir = new CGPoint();
float radialAccel;
float tangentialAccel;
}
Expand All @@ -108,11 +112,11 @@ static class ParticleModeB {
float deltaRadius;
}

CGPoint pos;
CGPoint startPos;
CGPoint pos = new CGPoint();
CGPoint startPos = new CGPoint();

ccColor4F color;
ccColor4F deltaColor;
ccColor4F color = new ccColor4F();
ccColor4F deltaColor = new ccColor4F();

float size;
float deltaSize;
Expand Down Expand Up @@ -448,7 +452,7 @@ public float getRadialAccelVar() {

public void setGravity(CGPoint g) {
assert emitterMode == kCCParticleModeGravity:"Particle Mode should be Gravity";
modeA.gravity = g;
modeA.gravity.set(g);
}

/**
Expand Down Expand Up @@ -719,27 +723,29 @@ private void initParticle(CCParticle particle) {
particle.timeToLive = Math.max(0, life + lifeVar * ccMacros.CCRANDOM_MINUS1_1() );

// position
particle.pos = CGPoint.make(centerOfGravity.x + posVar.x * ccMacros.CCRANDOM_MINUS1_1(),
particle.pos.set(centerOfGravity.x + posVar.x * ccMacros.CCRANDOM_MINUS1_1(),
centerOfGravity.y + posVar.y * ccMacros.CCRANDOM_MINUS1_1());

// Color
ccColor4F start = new ccColor4F();
start.r = Math.min(1, Math.max(0, startColor.r + startColorVar.r * ccMacros.CCRANDOM_MINUS1_1() ) );
start.g = Math.min(1, Math.max(0, startColor.g + startColorVar.g * ccMacros.CCRANDOM_MINUS1_1() ) );
start.b = Math.min(1, Math.max(0, startColor.b + startColorVar.b * ccMacros.CCRANDOM_MINUS1_1() ) );
start.a = Math.min(1, Math.max(0, startColor.a + startColorVar.a * ccMacros.CCRANDOM_MINUS1_1() ) );

ccColor4F end = new ccColor4F();
end.r = Math.min(1, Math.max(0, endColor.r + endColorVar.r * ccMacros.CCRANDOM_MINUS1_1() ) );
end.g = Math.min(1, Math.max(0, endColor.g + endColorVar.g * ccMacros.CCRANDOM_MINUS1_1() ) );
end.b = Math.min(1, Math.max(0, endColor.b + endColorVar.b * ccMacros.CCRANDOM_MINUS1_1() ) );
end.a = Math.min(1, Math.max(0, endColor.a + endColorVar.a * ccMacros.CCRANDOM_MINUS1_1() ) );

particle.color = start;
particle.deltaColor = new ccColor4F( (end.r - start.r) / particle.timeToLive,
(end.g - start.g) / particle.timeToLive,
(end.b - start.b) / particle.timeToLive,
(end.a - start.a) / particle.timeToLive);
// ccColor4F start = new ccColor4F();
float start_r = Math.min(1, Math.max(0, startColor.r + startColorVar.r * ccMacros.CCRANDOM_MINUS1_1() ) );
float start_g = Math.min(1, Math.max(0, startColor.g + startColorVar.g * ccMacros.CCRANDOM_MINUS1_1() ) );
float start_b = Math.min(1, Math.max(0, startColor.b + startColorVar.b * ccMacros.CCRANDOM_MINUS1_1() ) );
float start_a = Math.min(1, Math.max(0, startColor.a + startColorVar.a * ccMacros.CCRANDOM_MINUS1_1() ) );

// ccColor4F end = new ccColor4F();
float end_r = Math.min(1, Math.max(0, endColor.r + endColorVar.r * ccMacros.CCRANDOM_MINUS1_1() ) );
float end_g = Math.min(1, Math.max(0, endColor.g + endColorVar.g * ccMacros.CCRANDOM_MINUS1_1() ) );
float end_b = Math.min(1, Math.max(0, endColor.b + endColorVar.b * ccMacros.CCRANDOM_MINUS1_1() ) );
float end_a = Math.min(1, Math.max(0, endColor.a + endColorVar.a * ccMacros.CCRANDOM_MINUS1_1() ) );

ccColor4FUtil.set(particle.color, start_r, start_g, start_b, start_a);

ccColor4FUtil.set(particle.deltaColor,
(end_r - start_r) / particle.timeToLive,
(end_g - start_g) / particle.timeToLive,
(end_b - start_b) / particle.timeToLive,
(end_a - start_a) / particle.timeToLive);

// size
float startS = Math.max(0, startSize + startSizeVar * ccMacros.CCRANDOM_MINUS1_1() ); // no negative size
Expand All @@ -761,22 +767,22 @@ private void initParticle(CCParticle particle) {

// position
if( positionType == kCCPositionTypeFree )
particle.startPos = this.convertToWorldSpace(0, 0);
this.convertToWorldSpace(0, 0, particle.startPos);

// direction
float a = ccMacros.CC_DEGREES_TO_RADIANS( angle + angleVar * ccMacros.CCRANDOM_MINUS1_1() );

// Mode Gravity: A
if (emitterMode == kCCParticleModeGravity) {
CGPoint v = CGPoint.make((float)Math.cos(a), (float)Math.sin(a));
float s = modeA.speed + modeA.speedVar * ccMacros.CCRANDOM_MINUS1_1();

if (particle.modeA == null) {
particle.modeA = new CCParticle.ParticleModeA();
}

// direction
particle.modeA.dir = CGPoint.ccpMult( v, s );
particle.modeA.dir.set((float)Math.cos(a), (float)Math.sin(a));
CGPointUtil.mult(particle.modeA.dir, s);

// radial accel
particle.modeA.radialAccel = modeA.radialAccel + modeA.radialAccelVar * ccMacros.CCRANDOM_MINUS1_1();
Expand Down Expand Up @@ -1035,9 +1041,14 @@ public void update(float dt) {

particleIdx = 0;

CGPoint currentPosition = CGPoint.zero();
OneClassPool<CGPoint> pointPool = PoolHolder.getInstance().getCGPointPool();
CGPoint currentPosition = pointPool.get();
CGPoint tmp = pointPool.get();
CGPoint radial = pointPool.get();
CGPoint tangential = pointPool.get();

if( positionType_ == kCCPositionTypeFree )
currentPosition = convertToWorldSpace(0, 0);
convertToWorldSpace(0, 0, currentPosition);

while( particleIdx < particleCount ) {
CCParticle p = particles[particleIdx];
Expand All @@ -1046,27 +1057,28 @@ public void update(float dt) {
if( p.timeToLive > 0 ) {
// Mode A: gravity, direction, tangential accel & radial accel
if( emitterMode == kCCParticleModeGravity ) {
CGPoint tmp, radial, tangential;
// CGPoint tmp, radial, tangential;

radial = CGPoint.zero();
CGPointUtil.zero(radial);
// radial acceleration
if(p.pos.x != 0 || p.pos.y != 0)
radial = CGPoint.ccpNormalize(p.pos);
tangential = radial;
radial = CGPoint.ccpMult(radial, p.modeA.radialAccel);
CGPointUtil.normalize(p.pos, radial);
tangential.set(radial);
CGPointUtil.mult(radial, p.modeA.radialAccel);

// tangential acceleration
float newy = tangential.x;
tangential.x = -tangential.y;
tangential.y = newy;
tangential = CGPoint.ccpMult(tangential, p.modeA.tangentialAccel);
CGPointUtil.mult(tangential, p.modeA.tangentialAccel);

// (gravity + radial + tangential) * dt
tmp = CGPoint.ccpAdd(CGPoint.ccpAdd( radial, tangential), modeA.gravity);
tmp = CGPoint.ccpMult( tmp, dt);
p.modeA.dir = CGPoint.ccpAdd( p.modeA.dir, tmp);
tmp = CGPoint.ccpMult(p.modeA.dir, dt);
p.pos = CGPoint.ccpAdd( p.pos, tmp );
CGPointUtil.add(radial, tangential, tmp);
CGPointUtil.add(tmp, modeA.gravity);
CGPointUtil.mult(tmp, dt);
CGPointUtil.add(p.modeA.dir, tmp);
CGPointUtil.mult(p.modeA.dir, dt, tmp);
CGPointUtil.add( p.pos, tmp );
}
// Mode B: radius movement
else {
Expand All @@ -1093,8 +1105,10 @@ public void update(float dt) {
CGPoint newPos;

if( positionType_ == kCCPositionTypeFree ) {
CGPoint diff = CGPoint.ccpSub( currentPosition, p.startPos );
newPos = CGPoint.ccpSub(p.pos, diff);
CGPoint diff = tmp;
CGPointUtil.sub(currentPosition, p.startPos, diff);
CGPointUtil.sub(p.pos, diff, diff);
newPos = diff;
} else {
newPos = p.pos;
}
Expand All @@ -1118,8 +1132,11 @@ public void update(float dt) {

} else {
// life < 0
if( particleIdx != particleCount-1 )
if( particleIdx != particleCount-1 ) {
CCParticle tmpPart = particles[particleIdx];
particles[particleIdx] = particles[particleCount-1];
particles[particleCount-1] = tmpPart;
}
particleCount--;

if( particleCount == 0 && autoRemoveOnFinish_ ) {
Expand All @@ -1129,6 +1146,11 @@ public void update(float dt) {
}
}
}

pointPool.free(currentPosition);
pointPool.free(tmp);
pointPool.free(radial);
pointPool.free(tangential);

postStep();
}
Expand Down
23 changes: 21 additions & 2 deletions cocos2d-android/src/org/cocos2d/tests/ParticleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import org.cocos2d.types.ccColor3B;
import org.cocos2d.types.ccColor4B;
import org.cocos2d.types.ccColor4F;
import org.cocos2d.utils.javolution.MathLib;
import org.cocos2d.utils.javolution.TextBuilder;

import android.app.Activity;
import android.os.Bundle;
Expand Down Expand Up @@ -259,12 +261,29 @@ public boolean ccTouchesEnded(MotionEvent e) {
return true;
}

private TextBuilder particleCountString = new TextBuilder();

// String.format("%4d", emitter.getParticleCount())
private TextBuilder getParticleCountString(int c) {
int len = MathLib.digitLength(c);

int zeros = 4 - len;

while (zeros-- > 0) {
particleCountString.append('0');
}
particleCountString.append(c);

return particleCountString;
}

public void update(float dt) {
CCLabelAtlas atlas = (CCLabelAtlas) getChild(kTagLabelAtlas);

String str = String.format("%4d", emitter.getParticleCount());
atlas.setString(str);
particleCountString.reset();

// String str = String.format("%4d", emitter.getParticleCount());
atlas.setString(getParticleCountString(emitter.getParticleCount()));
}

public void toggleCallback(Object sender) {
Expand Down
29 changes: 27 additions & 2 deletions cocos2d-android/src/org/cocos2d/types/util/CGPointUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public final class CGPointUtil {
* @param v - src/dst point
* @param s - factor value
*/
public static void mult(CGPoint v, final float s) {
public static void mult(CGPoint v, float s) {
v.x *= s;
v.y *= s;
}
Expand All @@ -21,7 +21,7 @@ public static void mult(CGPoint v, final float s) {
* @param s - factor value
* @param res - dst point
*/
public static void mult(CGPoint v, final float s, CGPoint res) {
public static void mult(CGPoint v, float s, CGPoint res) {
res.x = v.x * s;
res.y = v.y * s;
}
Expand All @@ -39,4 +39,29 @@ public static void zero(CGPoint p) {
p.x = 0;
p.y = 0;
}

public static void normalize(CGPoint src, CGPoint dst) {
float invLen = 1 / CGPoint.ccpLength(src);
dst.set(src.x * invLen, src.y * invLen);
}

public static void add(CGPoint first, CGPoint second, CGPoint ret) {
ret.x = first.x + second.x;
ret.y = first.y + second.y;
}

public static void add(CGPoint v, CGPoint toAdd) {
v.x += toAdd.x;
v.y += toAdd.y;
}

public static void sub(CGPoint first, CGPoint second, CGPoint ret) {
ret.x = first.x - second.x;
ret.y = first.y - second.y;
}

public static void sub(CGPoint v, CGPoint toAdd) {
v.x -= toAdd.x;
v.y -= toAdd.y;
}
}
20 changes: 20 additions & 0 deletions cocos2d-android/src/org/cocos2d/types/util/ccColor4FUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.cocos2d.types.util;

import org.cocos2d.types.ccColor4F;

public final class ccColor4FUtil {

public static void copy(ccColor4F src, ccColor4F dst) {
dst.a = src.a;
dst.r = src.r;
dst.g = src.g;
dst.b = src.b;
}

public static void set(ccColor4F dst, float r, float g, float b, float a) {
dst.a = a;
dst.r = r;
dst.g = g;
dst.b = b;
}
}

0 comments on commit ced617a

Please sign in to comment.