Skip to content

Commit b6f5e8c

Browse files
author
pandamicro
committed
Improve CCB reader performance
1 parent c1c76b0 commit b6f5e8c

File tree

6 files changed

+266
-282
lines changed

6 files changed

+266
-282
lines changed

extensions/ccb-reader/CCBAnimationManager.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
THE SOFTWARE.
2525
****************************************************************************/
2626

27+
(function () {
28+
29+
var _pos = cc.p();
30+
2731
cc.BuilderAnimationManagerDelegate = cc.Class.extend({
2832
completedAnimationSequenceNamed: function (name) {
2933
}
@@ -445,7 +449,7 @@ cc.BuilderAnimationManager = cc.Class.extend({
445449
} else if (propName === "opacity") {
446450
return cc.fadeTo(duration, keyframe1.getValue());
447451
} else if (propName === "color") {
448-
var selColor = keyframe1.getValue().getColor();
452+
var selColor = keyframe1.getValue();
449453
return cc.tintTo(duration, selColor.r, selColor.g, selColor.b);
450454
} else if (propName === "visible") {
451455
var isVisible = keyframe1.getValue();
@@ -519,7 +523,9 @@ cc.BuilderAnimationManager = cc.Class.extend({
519523

520524
x = value[0];
521525
y = value[1];
522-
node.setPosition(cc._getAbsolutePosition(x, y, nType, this.getContainerSize(node.getParent()), propName));
526+
cc._getAbsolutePosition(x, y, nType, this.getContainerSize(node.getParent()), propName, _pos);
527+
node._position.x = _pos.x;
528+
node._position.y = _pos.y;
523529
} else if (propName === "scale") {
524530
getArr = this._getBaseValue(node, propName);
525531
nType = getArr[2];
@@ -531,33 +537,34 @@ cc.BuilderAnimationManager = cc.Class.extend({
531537
} else if (propName === "skew") {
532538
x = value[0];
533539
y = value[1];
534-
node.setSkewX(x);
535-
node.setSkewY(y);
540+
node._skewX = x;
541+
node._skewY = y;
536542
} else {
537543
// [node setValue:value forKey:name];
538544
// TODO only handle rotation, opacity, displayFrame, color
539545
if (propName === "rotation") {
540546
node.setRotation(value);
541-
} else if(propName === "rotationX") {
542-
node.setRotationSkewX(value);
543-
} else if(propName === "rotationY") {
544-
node.setRotationSkewY(value);
545-
} else if(propName === "opacity") {
546-
node.setOpacity(value);
547-
} else if(propName === "displayFrame") {
547+
} else if (propName === "rotationX") {
548+
node._rotationX = value;
549+
} else if (propName === "rotationY") {
550+
node._rotationY = value;
551+
} else if (propName === "opacity") {
552+
node._realOpacity = value;
553+
} else if (propName === "displayFrame") {
548554
node.setSpriteFrame(value);
549-
} else if(propName === "color") {
550-
var ccColor3B = value.getColor();
551-
if(ccColor3B.r !== 255 || ccColor3B.g !== 255 || ccColor3B.b !== 255){
552-
node.setColor(ccColor3B);
555+
} else if (propName === "color") {
556+
if (value.r !== 255 || value.g !== 255 || value.b !== 255) {
557+
node.setColor(value);
553558
}
554559
} else if (propName === "visible") {
555560
value = value || false;
556561
node.setVisible(value);
557562
} else {
558563
cc.log("unsupported property name is " + propName);
564+
return;
559565
}
560566
}
567+
node.setNodeDirty();
561568
}
562569
},
563570

@@ -764,4 +771,4 @@ cc.BuilderSoundEffect.create = function (file, pitch, pan, gain) {
764771
}
765772
return null;
766773
};
767-
774+
})();

extensions/ccb-reader/CCBReader.js

Lines changed: 48 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,6 @@ var CCB_SIZETYPE_MULTIPLY_RESOLUTION = 5;
102102
var CCB_SCALETYPE_ABSOLUTE = 0;
103103
var CCB_SCALETYPE_MULTIPLY_RESOLUTION = 1;
104104

105-
cc.BuilderFile = cc.Node.extend({
106-
_ccbFileNode:null,
107-
108-
getCCBFileNode:function () {
109-
return this._ccbFileNode;
110-
},
111-
setCCBFileNode:function (node) {
112-
this._ccbFileNode = node;
113-
}
114-
});
115-
116-
cc.BuilderFile.create = function () {
117-
return new cc.BuilderFile();
118-
};
119-
120105
/**
121106
* Parse CCBI file which is generated by CocosBuilder
122107
*/
@@ -340,13 +325,31 @@ cc.BuilderReader = cc.Class.extend({
340325

341326
readInt: function (signed) {
342327
var numBits = 0;
343-
while (!this._getBit()) {
328+
var data = this._data[this._currentByte];
329+
var bit = !!(data & (1 << this._currentBit++));
330+
while (!bit) {
344331
numBits++;
332+
bit = !!(data & (1 << this._currentBit++));
333+
if (this._currentBit >= 8) {
334+
this._currentBit = 0;
335+
this._currentByte++;
336+
data = this._data[this._currentByte];
337+
if (this._currentByte > this._data.length)
338+
throw new Error("out of the data bound");
339+
}
345340
}
346341

347342
var current = 0;
348343
for (var a = numBits - 1; a >= 0; a--) {
349-
if (this._getBit()) {
344+
bit = !!(data & (1 << this._currentBit++));
345+
if (this._currentBit >= 8) {
346+
this._currentBit = 0;
347+
this._currentByte++;
348+
data = this._data[this._currentByte];
349+
if (this._currentByte > this._data.length)
350+
throw new Error("out of the data bound");
351+
}
352+
if (bit) {
350353
current |= 1 << a;
351354
}
352355
}
@@ -364,7 +367,10 @@ cc.BuilderReader = cc.Class.extend({
364367
num = current - 1;
365368
}
366369

367-
this._alignBits();
370+
if (this._currentBit) {
371+
this._currentBit = 0;
372+
this._currentByte++;
373+
}
368374

369375
return num;
370376
},
@@ -376,11 +382,11 @@ cc.BuilderReader = cc.Class.extend({
376382
},
377383

378384
readBool: function () {
379-
return (0 !== this.readByte());
385+
return !!this._data[this._currentByte++];
380386
},
381387

382388
readFloat: function () {
383-
var type = this.readByte();
389+
var type = this._data[this._currentByte++];
384390

385391
switch (type) {
386392
case CCB_FLOAT0:
@@ -406,7 +412,9 @@ cc.BuilderReader = cc.Class.extend({
406412
_decodeFloat: function (precisionBits, exponentBits) {
407413
var length = precisionBits + exponentBits + 1;
408414
var size = length >> 3;
409-
this._checkSize(length);
415+
if (this._currentByte + size >= this._data.length) {
416+
throw new Error("Index out of bound");
417+
}
410418

411419
var bias = Math.pow(2, exponentBits - 1) - 1;
412420
var signal = this._readBitsOnly(precisionBits + exponentBits, 1, size);
@@ -415,7 +423,7 @@ cc.BuilderReader = cc.Class.extend({
415423
var divisor = 2;
416424
var curByte = 0; //length + (-precisionBits >> 3) - 1;
417425
do {
418-
var byteValue = this._readByteOnly(++curByte, size);
426+
var byteValue = this._data[this._currentByte + size - (++curByte) - 1];
419427
var startBit = precisionBits % 8 || 8;
420428
var mask = 1 << startBit;
421429
while (mask >>= 1) {
@@ -440,34 +448,26 @@ cc.BuilderReader = cc.Class.extend({
440448
var lastByte = size + (-(start + length) >> 3);
441449
var diff = curByte - lastByte;
442450

443-
var sum = (this._readByteOnly(curByte, size) >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1);
451+
var sum = (this._data[this._currentByte + size - curByte - 1] >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1);
444452

445453
if (diff && offsetLeft) {
446-
sum += (this._readByteOnly(lastByte++, size) & ((1 << offsetLeft) - 1)) << (diff-- << 3) - offsetRight;
454+
sum += (this._data[this._currentByte + size - lastByte - 1] & ((1 << offsetLeft) - 1)) << (diff-- << 3) - offsetRight;
455+
lastByte++;
447456
}
448457

449458
while (diff) {
450-
sum += this._shl(this._readByteOnly(lastByte++, size), (diff-- << 3) - offsetRight);
459+
sum += this._shl(this._data[this._currentByte + size - lastByte - 1], (diff-- << 3) - offsetRight);
460+
lastByte++;
451461
}
452462

453463
return sum;
454464
},
455465

456-
_readByteOnly: function (i, size) {
457-
return this._data[this._currentByte + size - i - 1];
458-
},
459-
460466
_shl: function (a, b) {
461467
for (++b; --b; a = ((a %= 0x7fffffff + 1) & 0x40000000) === 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1);
462468
return a;
463469
},
464470

465-
_checkSize: function (neededBits) {
466-
if (!(this._currentByte + Math.ceil(neededBits / 8) < this._data.length)) {
467-
throw new Error("Index out of bound");
468-
}
469-
},
470-
471471
readCachedString: function () {
472472
return this._stringCache[this.readInt(false)];
473473
},
@@ -670,12 +670,11 @@ cc.BuilderReader = cc.Class.extend({
670670
keyframe.setEasingOpt(easingOpt);
671671

672672
if (type === CCB_PROPTYPE_CHECK) {
673-
value = this.readBool();
673+
value = !!this._data[this._currentByte++];
674674
} else if (type === CCB_PROPTYPE_BYTE) {
675-
value = this.readByte();
675+
value = this._data[this._currentByte++];
676676
} else if (type === CCB_PROPTYPE_COLOR3) {
677-
var c = cc.color(this.readByte(), this.readByte(), this.readByte());
678-
value = cc.Color3BWapper.create(c);
677+
value = cc.color(this._data[this._currentByte++], this._data[this._currentByte++], this._data[this._currentByte++]);
679678
} else if (type === CCB_PROPTYPE_FLOATXY) {
680679
value = [this.readFloat(), this.readFloat()];
681680
} else if (type === CCB_PROPTYPE_DEGREES) {
@@ -727,7 +726,7 @@ cc.BuilderReader = cc.Class.extend({
727726
return false;
728727
}
729728

730-
this._jsControlled = this.readBool();
729+
this._jsControlled = !!this._data[this._currentByte++];
731730
this._animationManager._jsControlled = this._jsControlled;
732731
// no need to set if it is "jscontrolled". It is obvious.
733732
return true;
@@ -736,13 +735,13 @@ cc.BuilderReader = cc.Class.extend({
736735
_readStringFromBytes: function (startIndex, strLen, reverse) {
737736
reverse = reverse || false;
738737
var strValue = "";
739-
var i, locData = this._data, locCurrentByte = this._currentByte;
738+
var i, locData = this._data;
740739
if (reverse) {
741740
for (i = strLen - 1; i >= 0; i--)
742-
strValue += String.fromCharCode(locData[locCurrentByte + i]);
741+
strValue += String.fromCharCode(locData[startIndex + i]);
743742
} else {
744743
for (i = 0; i < strLen; i++)
745-
strValue += String.fromCharCode(locData[locCurrentByte + i]);
744+
strValue += String.fromCharCode(locData[startIndex + i]);
746745
}
747746
return strValue;
748747
},
@@ -755,8 +754,8 @@ cc.BuilderReader = cc.Class.extend({
755754
},
756755

757756
_readStringCacheEntry: function () {
758-
var b0 = this.readByte();
759-
var b1 = this.readByte();
757+
var b0 = this._data[this._currentByte++];
758+
var b1 = this._data[this._currentByte++];
760759

761760
var numBytes = b0 << 8 | b1;
762761

@@ -838,9 +837,9 @@ cc.BuilderReader = cc.Class.extend({
838837
ccNodeLoader.parseProperties(node, parent, this);
839838

840839
//handle sub ccb files(remove middle node)
841-
var isCCBFileNode = node instanceof cc.BuilderFile;
840+
var isCCBFileNode = !!node.ccbFileNode;
842841
if (isCCBFileNode) {
843-
var embeddedNode = node.getCCBFileNode();
842+
var embeddedNode = node.ccbFileNode;
844843
embeddedNode.setPosition(node.getPosition());
845844
embeddedNode.setRotation(node.getRotation());
846845
embeddedNode.setScaleX(node.getScaleX());
@@ -850,7 +849,7 @@ cc.BuilderReader = cc.Class.extend({
850849
//embeddedNode.ignoreAnchorPointForPosition(node.isIgnoreAnchorPointForPosition());
851850

852851
locActionManager.moveAnimationsFromNode(node, embeddedNode);
853-
node.setCCBFileNode(null);
852+
node.ccbFileNode = null;
854853
node = embeddedNode;
855854
}
856855
var target = null, locMemberAssigner = null;
@@ -926,27 +925,7 @@ cc.BuilderReader = cc.Class.extend({
926925
return node;
927926
},
928927

929-
_getBit:function () {
930-
var bit = (this._data[this._currentByte] & (1 << this._currentBit)) !== 0;
931-
this._currentBit++;
932-
933-
if (this._currentBit >= 8) {
934-
this._currentBit = 0;
935-
this._currentByte++;
936-
if(this._currentByte > this._data.length)
937-
throw new Error("out of the data bound");
938-
}
939-
return bit;
940-
},
941-
942-
_alignBits:function () {
943-
if (this._currentBit) {
944-
this._currentBit = 0;
945-
this._currentByte++;
946-
}
947-
},
948-
949-
_readUTF8:function () {
928+
_readUTF8: function () {
950929
}
951930
});
952931

0 commit comments

Comments
 (0)