Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #767 from linshun/BonsaiDen-fixes

Bonsai den fixes
  • Loading branch information...
commit d08a5c29c5ef6eef753f13e8d86a330b0030d01b 2 parents 901291b + f68b0e5
@linshun linshun authored
View
4 AUTHORS.txt
@@ -44,6 +44,10 @@ Surith Thekkiam(Zynga) @folecr match Cocos2dx api
Robert Boyd @rboyd fix stackable actions
+Ivo Wetzel(Zynga Germany) @BonsaiDen Fixes for Audio Support Detection,
+ Texture Support in Particle System
+ and Performance improvements
+
Cocos2d-x and cocos2d-html5 can not grow so fast without the active community.
View
22 CocosDenshion/SimpleAudioEngine.js
@@ -58,6 +58,14 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
this._capabilities.mp3 = ("no" != au.canPlayType("audio/mpeg"))
&& ("" != au.canPlayType("audio/mpeg"));
+ this._capabilities.mp4 = ("no" != au.canPlayType("audio/mp4"))
+ && ("" != au.canPlayType("audio/mp4"));
+
+ this._capabilities.m4a = (("no" != au.canPlayType("audio/x-m4a"))
+ && ("" != au.canPlayType("audio/x-m4a")))
+ || (("no" != au.canPlayType("audio/aac"))
+ && ("" != au.canPlayType("audio/aac")));
+
this._capabilities.ogg = ("no" != au.canPlayType('audio/ogg; codecs="vorbis"'))
&& ("" != au.canPlayType('audio/ogg; codecs="vorbis"'));
@@ -65,7 +73,9 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
&& ("" != au.canPlayType('audio/wav; codecs="1"'));
// enable sound if any of the audio format is supported
- this._soundEnable = this._capabilities.mp3 || this._capabilities.ogg || this._capabilities.wav;
+ this._soundEnable = this._capabilities.mp3 || this._capabilities.mp4
+ || this._capabilities.m4a || this._capabilities.ogg
+ || this._capabilities.wav;
}
},
@@ -533,6 +543,16 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
return;
}
+ // check for MP4
+ if (this._capabilities.mp4) {
+ this._supportedFormat.push("mp4");
+ }
+
+ // check for M4A
+ if (this._capabilities.m4a) {
+ this._supportedFormat.push("m4a");
+ }
+
// check for MP3
if (this._capabilities.mp3) {
this._supportedFormat.push("mp3");
View
22 cocos2d/particle_nodes/CCParticleSystem.js
@@ -846,6 +846,8 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
* @param {cc.Color4F} startColor
*/
setStartColor:function (startColor) {
+ if (startColor instanceof cc.Color3B)
+ startColor = cc.c4FFromccc3B(startColor);
this._startColor = startColor;
},
@@ -863,6 +865,8 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
* @param {cc.Color4F} startColorVar
*/
setStartColorVar:function (startColorVar) {
+ if (startColorVar instanceof cc.Color3B)
+ startColorVar = cc.c4FFromccc3B(startColorVar);
this._startColorVar = startColorVar;
},
@@ -881,6 +885,8 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
* @param {cc.Color4F} endColor
*/
setEndColor:function (endColor) {
+ if (endColor instanceof cc.Color3B)
+ endColor = cc.c4FFromccc3B(endColor);
this._endColor = endColor;
},
@@ -898,6 +904,8 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
* @param {cc.Color4F} endColorVar
*/
setEndColorVar:function (endColorVar) {
+ if (endColorVar instanceof cc.Color3B)
+ endColorVar = cc.c4FFromccc3B(endColorVar);
this._endColorVar = endColorVar;
},
@@ -1369,13 +1377,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();
View
67 cocos2d/sprite_nodes/CCSprite.js
@@ -37,33 +37,60 @@ cc.SPRITE_INDEX_NOT_INITIALIZED = -1;
* @param {HTMLImageElement} texture
* @return {Array}
*/
+
cc.generateTextureCacheForColor = function (texture) {
+
+ if (texture.hasOwnProperty('channelCache')) {
+ return texture.channelCache;
+ }
+
+ var ref = cc.generateTextureCacheForColor;
+
var w = texture.width;
var h = texture.height;
- var textureCache = [];
+ var textureCache = [
+ document.createElement("canvas"),
+ document.createElement("canvas"),
+ document.createElement("canvas"),
+ ];
+
+ textureCache[0].width = w;
+ textureCache[0].height = h;
+ textureCache[1].width = w;
+ textureCache[1].height = h;
+ textureCache[2].width = w;
+ textureCache[2].height = h;
+
+ if (w > ref.canvas.width || h > ref.canvas.height) {
+ ref.canvas.width = w;
+ ref.canvas.height = h;
- var canvas = document.createElement("canvas");
- canvas.width = w;
- canvas.height = h;
+ } else {
+ ref.canvas.width = ref.canvas.width;
+ ref.canvas.height = ref.canvas.height;
+ }
- var ctx = canvas.getContext("2d");
+ var ctx = ref.canvas.getContext("2d");
ctx.drawImage(texture, 0, 0);
- var tempCanvas = document.createElement("canvas");
- tempCanvas.width = w;
- tempCanvas.height = h;
- var tempCtx = tempCanvas.getContext('2d');
+ if (w > ref.tempCanvas.width || h > ref.tempCanvas.height) {
+ ref.tempCanvas.width = w;
+ ref.tempCanvas.height = h;
+
+ } else {
+ ref.tempCanvas.width = ref.tempCanvas.width;
+ ref.tempCanvas.height = ref.tempCanvas.height;
+ }
var pixels = ctx.getImageData(0, 0, w, h).data;
for (var rgbI = 0; rgbI < 3; rgbI++) {
- var cacheCanvas = document.createElement("canvas");
- cacheCanvas.width = w;
- cacheCanvas.height = h;
- var cacheCtx = cacheCanvas.getContext('2d');
- tempCtx.drawImage(texture, 0, 0);
- var to = tempCtx.getImageData(0, 0, w, h);
+ var cacheCtx = textureCache[rgbI].getContext('2d');
+ cacheCtx.getImageData(0, 0, w, h).data;
+ ref.tempCtx.drawImage(texture, 0, 0);
+
+ var to = ref.tempCtx.getImageData(0, 0, w, h);
var toData = to.data;
for (var i = 0; i < pixels.length; i += 4) {
@@ -72,12 +99,20 @@ cc.generateTextureCacheForColor = function (texture) {
toData[i + 2] = (rgbI === 2) ? pixels[i + 2] : 0;
toData[i + 3] = pixels[i + 3];
}
+
cacheCtx.putImageData(to, 0, 0);
- textureCache.push(cacheCanvas);
+
}
+
+ texture.channelCache = textureCache;
return textureCache;
};
+cc.generateTextureCacheForColor.canvas = document.createElement('canvas');
+cc.generateTextureCacheForColor.tempCanvas = document.createElement('canvas');
+cc.generateTextureCacheForColor.tempCtx = cc.generateTextureCacheForColor.tempCanvas.getContext('2d');
+
+
cc.generateTintImage2 = function (texture, color, rect) {
if (!rect) {
rect = cc.rect(0, 0, texture.width, texture.height);
View
4 cocos2d/textures/CCTextureCache.js
@@ -102,6 +102,8 @@ cc.TextureCache = cc.Class.extend(/** @lends cc.TextureCache# */{
}
else {
texture = new Image();
+ texture.crossOrigin = "Anonymous";
+
var that = this;
texture.addEventListener("load", function () {
that._addImageAsyncCallBack(target, selector);
@@ -162,6 +164,8 @@ cc.TextureCache = cc.Class.extend(/** @lends cc.TextureCache# */{
}
else {
texture = new Image();
+ texture.crossOrigin = "Anonymous";
+
var that = this;
texture.addEventListener("load", function () {
View
27 cocos2d/touch_dispatcher/CCTouchDispatcher.js
@@ -597,18 +597,25 @@ cc.TouchDispatcher.preTouchPoint = cc.p(0, 0);
cc.TouchDispatcher.isRegisterEvent = false;
cc.getHTMLElementPosition = function (element) {
- var pos = null;
- if (element instanceof HTMLCanvasElement) {
- pos = {left:0, top:0, width:element.width, height:element.height};
+ var docElem = document.documentElement;
+ var win = window;
+ var box = null;
+ if (typeof element.getBoundingClientRect === 'function') {
+ box = element.getBoundingClientRect();
+
} else {
- pos = {left:0, top:0, width:parseInt(element.style.width), height:parseInt(element.style.height)};
- }
- while (element != null) {
- pos.left += element.offsetLeft;
- pos.top += element.offsetTop;
- element = element.offsetParent;
+ if (element instanceof HTMLCanvasElement) {
+ box = {left:0, top:0, width:element.width, height:element.height};
+ } else {
+ box = {left:0, top:0, width:parseInt(element.style.width), height:parseInt(element.style.height)};
+ }
}
- return pos;
+ return {
+ left: box.left + win.pageXOffset - docElem.clientLeft,
+ top: box.top + win.pageYOffset - docElem.clientTop,
+ width: box.width,
+ height: box.height
+ };
};
cc.ProcessMouseupEvent = function (element, event) {
Please sign in to comment.
Something went wrong with that request. Please try again.