Permalink
Browse files

Add support for image based particles

  • Loading branch information...
1 parent 901291b commit 3ac3adf40d71ac46a86ab13750b16f2e655e4997 Ivo Wetzel committed Feb 5, 2013
Showing with 27 additions and 9 deletions.
  1. +11 −3 cocos2d/particle_nodes/CCParticleSystem.js
  2. +16 −6 cocos2d/particle_nodes/CCParticleSystemQuad.js
View
14 cocos2d/particle_nodes/CCParticleSystem.js
@@ -1369,13 +1369,21 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
var img = new Image();
img.src = "data:image/png;base64," + newImageData;
- this._texture = img;
-
- //save image to TextureCache
cc.TextureCache.getInstance().cacheImage(fullpath, img);
+
+ // Manually decode the base 64 image size since the browser will only do so asynchronously
+ var w = (buffer[16] << 24) + (buffer[17] << 16) + (buffer[18] << 8) + (buffer[19]),
+ h = (buffer[20] << 24) + (buffer[21] << 16) + (buffer[22] << 8) + (buffer[23]);
+
+ // Patch this on so we can correctly create the draw rect later on
+ img.textureWidth = w;
+ img.textureHeight = h;
+
+ this._texture = img;
}
}
cc.Assert(this._texture != null, "cc.ParticleSystem: error loading the texture");
+ this.setTexture(this._texture);;
}
ret = true;
}
View
22 cocos2d/particle_nodes/CCParticleSystemQuad.js
@@ -305,6 +305,7 @@ cc.ParticleSystemQuad = cc.ParticleSystem.extend(/** @lends cc.ParticleSystemQua
this._pointRect = rect;
this.initTexCoordsWithRect(rect);
}
+ this.setDrawMode(cc.PARTICLE_TEXTURE_MODE);
},
// super methods
@@ -357,7 +358,8 @@ cc.ParticleSystemQuad = cc.ParticleSystem.extend(/** @lends cc.ParticleSystemQua
}
var size = null;
if ((texture instanceof HTMLImageElement) || (texture instanceof HTMLCanvasElement)) {
- size = cc.size(texture.width, texture.height);
+ // Fall back to the read-out texture size
+ size = cc.size(texture.width || texture.textureWidth, texture.height || texture.textureHeight);
} else {
size = texture.getContentSize();
}
@@ -484,21 +486,29 @@ cc.ParticleSystemQuad = cc.ParticleSystem.extend(/** @lends cc.ParticleSystemQua
var particle = this._particles[i];
var lpx = (0 | (particle.size * 0.5));
- //TODO these are temporary code, need modifier
if (this._drawMode == cc.PARTICLE_TEXTURE_MODE) {
var drawTexture = this.getTexture();
+
+ // Delay drawing until the texture is fully loaded by the browser
+ if (!drawTexture.width || !drawTexture.height)
+ continue;
+
if (particle.isChangeColor) {
- var cacheTextureForColor = cc.TextureCache.getInstance().getTextureColors(this.getTexture());
+ var cacheTextureForColor = cc.TextureCache.getInstance().getTextureColors(drawTexture);
if (cacheTextureForColor)
- drawTexture = cc.generateTintImage(this.getTexture(), cacheTextureForColor, particle.color, this._pointRect);
+ drawTexture = cc.generateTintImage(drawTexture, cacheTextureForColor, particle.color, this._pointRect);
}
context.save();
context.globalAlpha = particle.color.a;
context.translate(0 | particle.drawPos.x, -(0 | particle.drawPos.y));
+
if (particle.rotation)
context.rotate(cc.DEGREES_TO_RADIANS(particle.rotation));
- context.drawImage(drawTexture, -lpx, -lpx,
- particle.size, particle.size);
+
+ var w = this._pointRect.size.width;
+ var h = this._pointRect.size.height;
+ context.scale((1 / w) * particle.size, (1 / h) * particle.size);
+ context.drawImage(drawTexture, -(0 | (w / 2)), -(0 | (h / 2)), w, h);
context.restore();
} else {
context.save();

0 comments on commit 3ac3adf

Please sign in to comment.