Skip to content
This repository

Bonsai den fixes #767

Merged
merged 7 commits into from over 1 year ago

1 participant

SeanLin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
4 AUTHORS.txt
@@ -44,6 +44,10 @@ Surith Thekkiam(Zynga) @folecr match Cocos2dx api
44 44
45 45 Robert Boyd @rboyd fix stackable actions
46 46
  47 +Ivo Wetzel(Zynga Germany) @BonsaiDen Fixes for Audio Support Detection,
  48 + Texture Support in Particle System
  49 + and Performance improvements
  50 +
47 51
48 52 Cocos2d-x and cocos2d-html5 can not grow so fast without the active community.
49 53
22 CocosDenshion/SimpleAudioEngine.js
@@ -58,6 +58,14 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
58 58 this._capabilities.mp3 = ("no" != au.canPlayType("audio/mpeg"))
59 59 && ("" != au.canPlayType("audio/mpeg"));
60 60
  61 + this._capabilities.mp4 = ("no" != au.canPlayType("audio/mp4"))
  62 + && ("" != au.canPlayType("audio/mp4"));
  63 +
  64 + this._capabilities.m4a = (("no" != au.canPlayType("audio/x-m4a"))
  65 + && ("" != au.canPlayType("audio/x-m4a")))
  66 + || (("no" != au.canPlayType("audio/aac"))
  67 + && ("" != au.canPlayType("audio/aac")));
  68 +
61 69 this._capabilities.ogg = ("no" != au.canPlayType('audio/ogg; codecs="vorbis"'))
62 70 && ("" != au.canPlayType('audio/ogg; codecs="vorbis"'));
63 71
@@ -65,7 +73,9 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
65 73 && ("" != au.canPlayType('audio/wav; codecs="1"'));
66 74
67 75 // enable sound if any of the audio format is supported
68   - this._soundEnable = this._capabilities.mp3 || this._capabilities.ogg || this._capabilities.wav;
  76 + this._soundEnable = this._capabilities.mp3 || this._capabilities.mp4
  77 + || this._capabilities.m4a || this._capabilities.ogg
  78 + || this._capabilities.wav;
69 79 }
70 80 },
71 81
@@ -533,6 +543,16 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
533 543 return;
534 544 }
535 545
  546 + // check for MP4
  547 + if (this._capabilities.mp4) {
  548 + this._supportedFormat.push("mp4");
  549 + }
  550 +
  551 + // check for M4A
  552 + if (this._capabilities.m4a) {
  553 + this._supportedFormat.push("m4a");
  554 + }
  555 +
536 556 // check for MP3
537 557 if (this._capabilities.mp3) {
538 558 this._supportedFormat.push("mp3");
22 cocos2d/particle_nodes/CCParticleSystem.js
@@ -846,6 +846,8 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
846 846 * @param {cc.Color4F} startColor
847 847 */
848 848 setStartColor:function (startColor) {
  849 + if (startColor instanceof cc.Color3B)
  850 + startColor = cc.c4FFromccc3B(startColor);
849 851 this._startColor = startColor;
850 852 },
851 853
@@ -863,6 +865,8 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
863 865 * @param {cc.Color4F} startColorVar
864 866 */
865 867 setStartColorVar:function (startColorVar) {
  868 + if (startColorVar instanceof cc.Color3B)
  869 + startColorVar = cc.c4FFromccc3B(startColorVar);
866 870 this._startColorVar = startColorVar;
867 871 },
868 872
@@ -881,6 +885,8 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
881 885 * @param {cc.Color4F} endColor
882 886 */
883 887 setEndColor:function (endColor) {
  888 + if (endColor instanceof cc.Color3B)
  889 + endColor = cc.c4FFromccc3B(endColor);
884 890 this._endColor = endColor;
885 891 },
886 892
@@ -898,6 +904,8 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
898 904 * @param {cc.Color4F} endColorVar
899 905 */
900 906 setEndColorVar:function (endColorVar) {
  907 + if (endColorVar instanceof cc.Color3B)
  908 + endColorVar = cc.c4FFromccc3B(endColorVar);
901 909 this._endColorVar = endColorVar;
902 910 },
903 911
@@ -1369,13 +1377,21 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
1369 1377
1370 1378 var img = new Image();
1371 1379 img.src = "data:image/png;base64," + newImageData;
1372   - this._texture = img;
1373   -
1374   - //save image to TextureCache
1375 1380 cc.TextureCache.getInstance().cacheImage(fullpath, img);
  1381 +
  1382 + // Manually decode the base 64 image size since the browser will only do so asynchronously
  1383 + var w = (buffer[16] << 24) + (buffer[17] << 16) + (buffer[18] << 8) + (buffer[19]),
  1384 + h = (buffer[20] << 24) + (buffer[21] << 16) + (buffer[22] << 8) + (buffer[23]);
  1385 +
  1386 + // Patch this on so we can correctly create the draw rect later on
  1387 + img.textureWidth = w;
  1388 + img.textureHeight = h;
  1389 +
  1390 + this._texture = img;
1376 1391 }
1377 1392 }
1378 1393 cc.Assert(this._texture != null, "cc.ParticleSystem: error loading the texture");
  1394 + this.setTexture(this._texture);;
1379 1395 }
1380 1396 ret = true;
1381 1397 }
22 cocos2d/particle_nodes/CCParticleSystemQuad.js
@@ -305,6 +305,7 @@ cc.ParticleSystemQuad = cc.ParticleSystem.extend(/** @lends cc.ParticleSystemQua
305 305 this._pointRect = rect;
306 306 this.initTexCoordsWithRect(rect);
307 307 }
  308 + this.setDrawMode(cc.PARTICLE_TEXTURE_MODE);
308 309 },
309 310
310 311 // super methods
@@ -357,7 +358,8 @@ cc.ParticleSystemQuad = cc.ParticleSystem.extend(/** @lends cc.ParticleSystemQua
357 358 }
358 359 var size = null;
359 360 if ((texture instanceof HTMLImageElement) || (texture instanceof HTMLCanvasElement)) {
360   - size = cc.size(texture.width, texture.height);
  361 + // Fall back to the read-out texture size
  362 + size = cc.size(texture.width || texture.textureWidth, texture.height || texture.textureHeight);
361 363 } else {
362 364 size = texture.getContentSize();
363 365 }
@@ -484,21 +486,29 @@ cc.ParticleSystemQuad = cc.ParticleSystem.extend(/** @lends cc.ParticleSystemQua
484 486 var particle = this._particles[i];
485 487 var lpx = (0 | (particle.size * 0.5));
486 488
487   - //TODO these are temporary code, need modifier
488 489 if (this._drawMode == cc.PARTICLE_TEXTURE_MODE) {
489 490 var drawTexture = this.getTexture();
  491 +
  492 + // Delay drawing until the texture is fully loaded by the browser
  493 + if (!drawTexture.width || !drawTexture.height)
  494 + continue;
  495 +
490 496 if (particle.isChangeColor) {
491   - var cacheTextureForColor = cc.TextureCache.getInstance().getTextureColors(this.getTexture());
  497 + var cacheTextureForColor = cc.TextureCache.getInstance().getTextureColors(drawTexture);
492 498 if (cacheTextureForColor)
493   - drawTexture = cc.generateTintImage(this.getTexture(), cacheTextureForColor, particle.color, this._pointRect);
  499 + drawTexture = cc.generateTintImage(drawTexture, cacheTextureForColor, particle.color, this._pointRect);
494 500 }
495 501 context.save();
496 502 context.globalAlpha = particle.color.a;
497 503 context.translate(0 | particle.drawPos.x, -(0 | particle.drawPos.y));
  504 +
498 505 if (particle.rotation)
499 506 context.rotate(cc.DEGREES_TO_RADIANS(particle.rotation));
500   - context.drawImage(drawTexture, -lpx, -lpx,
501   - particle.size, particle.size);
  507 +
  508 + var w = this._pointRect.size.width;
  509 + var h = this._pointRect.size.height;
  510 + context.scale((1 / w) * particle.size, (1 / h) * particle.size);
  511 + context.drawImage(drawTexture, -(0 | (w / 2)), -(0 | (h / 2)), w, h);
502 512 context.restore();
503 513 } else {
504 514 context.save();
67 cocos2d/sprite_nodes/CCSprite.js
@@ -37,33 +37,60 @@ cc.SPRITE_INDEX_NOT_INITIALIZED = -1;
37 37 * @param {HTMLImageElement} texture
38 38 * @return {Array}
39 39 */
  40 +
40 41 cc.generateTextureCacheForColor = function (texture) {
  42 +
  43 + if (texture.hasOwnProperty('channelCache')) {
  44 + return texture.channelCache;
  45 + }
  46 +
  47 + var ref = cc.generateTextureCacheForColor;
  48 +
41 49 var w = texture.width;
42 50 var h = texture.height;
43   - var textureCache = [];
  51 + var textureCache = [
  52 + document.createElement("canvas"),
  53 + document.createElement("canvas"),
  54 + document.createElement("canvas"),
  55 + ];
  56 +
  57 + textureCache[0].width = w;
  58 + textureCache[0].height = h;
  59 + textureCache[1].width = w;
  60 + textureCache[1].height = h;
  61 + textureCache[2].width = w;
  62 + textureCache[2].height = h;
  63 +
  64 + if (w > ref.canvas.width || h > ref.canvas.height) {
  65 + ref.canvas.width = w;
  66 + ref.canvas.height = h;
44 67
45   - var canvas = document.createElement("canvas");
46   - canvas.width = w;
47   - canvas.height = h;
  68 + } else {
  69 + ref.canvas.width = ref.canvas.width;
  70 + ref.canvas.height = ref.canvas.height;
  71 + }
48 72
49   - var ctx = canvas.getContext("2d");
  73 + var ctx = ref.canvas.getContext("2d");
50 74 ctx.drawImage(texture, 0, 0);
51 75
52   - var tempCanvas = document.createElement("canvas");
53   - tempCanvas.width = w;
54   - tempCanvas.height = h;
55   - var tempCtx = tempCanvas.getContext('2d');
  76 + if (w > ref.tempCanvas.width || h > ref.tempCanvas.height) {
  77 + ref.tempCanvas.width = w;
  78 + ref.tempCanvas.height = h;
  79 +
  80 + } else {
  81 + ref.tempCanvas.width = ref.tempCanvas.width;
  82 + ref.tempCanvas.height = ref.tempCanvas.height;
  83 + }
56 84
57 85 var pixels = ctx.getImageData(0, 0, w, h).data;
58 86
59 87 for (var rgbI = 0; rgbI < 3; rgbI++) {
60   - var cacheCanvas = document.createElement("canvas");
61   - cacheCanvas.width = w;
62   - cacheCanvas.height = h;
63   - var cacheCtx = cacheCanvas.getContext('2d');
64 88
65   - tempCtx.drawImage(texture, 0, 0);
66   - var to = tempCtx.getImageData(0, 0, w, h);
  89 + var cacheCtx = textureCache[rgbI].getContext('2d');
  90 + cacheCtx.getImageData(0, 0, w, h).data;
  91 + ref.tempCtx.drawImage(texture, 0, 0);
  92 +
  93 + var to = ref.tempCtx.getImageData(0, 0, w, h);
67 94 var toData = to.data;
68 95
69 96 for (var i = 0; i < pixels.length; i += 4) {
@@ -72,12 +99,20 @@ cc.generateTextureCacheForColor = function (texture) {
72 99 toData[i + 2] = (rgbI === 2) ? pixels[i + 2] : 0;
73 100 toData[i + 3] = pixels[i + 3];
74 101 }
  102 +
75 103 cacheCtx.putImageData(to, 0, 0);
76   - textureCache.push(cacheCanvas);
  104 +
77 105 }
  106 +
  107 + texture.channelCache = textureCache;
78 108 return textureCache;
79 109 };
80 110
  111 +cc.generateTextureCacheForColor.canvas = document.createElement('canvas');
  112 +cc.generateTextureCacheForColor.tempCanvas = document.createElement('canvas');
  113 +cc.generateTextureCacheForColor.tempCtx = cc.generateTextureCacheForColor.tempCanvas.getContext('2d');
  114 +
  115 +
81 116 cc.generateTintImage2 = function (texture, color, rect) {
82 117 if (!rect) {
83 118 rect = cc.rect(0, 0, texture.width, texture.height);
4 cocos2d/textures/CCTextureCache.js
@@ -102,6 +102,8 @@ cc.TextureCache = cc.Class.extend(/** @lends cc.TextureCache# */{
102 102 }
103 103 else {
104 104 texture = new Image();
  105 + texture.crossOrigin = "Anonymous";
  106 +
105 107 var that = this;
106 108 texture.addEventListener("load", function () {
107 109 that._addImageAsyncCallBack(target, selector);
@@ -162,6 +164,8 @@ cc.TextureCache = cc.Class.extend(/** @lends cc.TextureCache# */{
162 164 }
163 165 else {
164 166 texture = new Image();
  167 + texture.crossOrigin = "Anonymous";
  168 +
165 169 var that = this;
166 170 texture.addEventListener("load", function () {
167 171
27 cocos2d/touch_dispatcher/CCTouchDispatcher.js
@@ -597,18 +597,25 @@ cc.TouchDispatcher.preTouchPoint = cc.p(0, 0);
597 597 cc.TouchDispatcher.isRegisterEvent = false;
598 598
599 599 cc.getHTMLElementPosition = function (element) {
600   - var pos = null;
601   - if (element instanceof HTMLCanvasElement) {
602   - pos = {left:0, top:0, width:element.width, height:element.height};
  600 + var docElem = document.documentElement;
  601 + var win = window;
  602 + var box = null;
  603 + if (typeof element.getBoundingClientRect === 'function') {
  604 + box = element.getBoundingClientRect();
  605 +
603 606 } else {
604   - pos = {left:0, top:0, width:parseInt(element.style.width), height:parseInt(element.style.height)};
605   - }
606   - while (element != null) {
607   - pos.left += element.offsetLeft;
608   - pos.top += element.offsetTop;
609   - element = element.offsetParent;
  607 + if (element instanceof HTMLCanvasElement) {
  608 + box = {left:0, top:0, width:element.width, height:element.height};
  609 + } else {
  610 + box = {left:0, top:0, width:parseInt(element.style.width), height:parseInt(element.style.height)};
  611 + }
610 612 }
611   - return pos;
  613 + return {
  614 + left: box.left + win.pageXOffset - docElem.clientLeft,
  615 + top: box.top + win.pageYOffset - docElem.clientTop,
  616 + width: box.width,
  617 + height: box.height
  618 + };
612 619 };
613 620
614 621 cc.ProcessMouseupEvent = function (element, event) {

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.