Skip to content

Commit bb0c171

Browse files
author
pandamicro
committed
Reduce cc.Color memory footprint
1 parent c2bce43 commit bb0c171

File tree

5 files changed

+247
-324
lines changed

5 files changed

+247
-324
lines changed

cocos2d/core/layers/CCLayerWebGLRenderCmd.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@
6666
_t._squareColorsAB = new ArrayBuffer(16);
6767

6868
var locSquareVerticesAB = _t._squareVerticesAB, locSquareColorsAB = _t._squareColorsAB;
69-
var locVertex3FLen = cc.Vertex3F.BYTES_PER_ELEMENT, locColorLen = cc.Color.BYTES_PER_ELEMENT;
69+
var locVertex3FLen = cc.Vertex3F.BYTES_PER_ELEMENT, locColorLen = cc._WebGLColor.BYTES_PER_ELEMENT;
7070
_t._squareVertices = [new cc.Vertex3F(0, 0, 0, locSquareVerticesAB, 0),
7171
new cc.Vertex3F(0, 0, 0, locSquareVerticesAB, locVertex3FLen),
7272
new cc.Vertex3F(0, 0, 0, locSquareVerticesAB, locVertex3FLen * 2),
7373
new cc.Vertex3F(0, 0, 0, locSquareVerticesAB, locVertex3FLen * 3)];
74-
_t._squareColors = [cc.color(0, 0, 0, 255, locSquareColorsAB, 0),
75-
cc.color(0, 0, 0, 255, locSquareColorsAB, locColorLen),
76-
cc.color(0, 0, 0, 255, locSquareColorsAB, locColorLen * 2),
77-
cc.color(0, 0, 0, 255, locSquareColorsAB, locColorLen * 3)];
74+
_t._squareColors = [new cc._WebGLColor(0, 0, 0, 255, locSquareColorsAB, 0),
75+
new cc._WebGLColor(0, 0, 0, 255, locSquareColorsAB, locColorLen),
76+
new cc._WebGLColor(0, 0, 0, 255, locSquareColorsAB, locColorLen * 2),
77+
new cc._WebGLColor(0, 0, 0, 255, locSquareColorsAB, locColorLen * 3)];
7878
_t._verticesFloat32Buffer = cc._renderContext.createBuffer();
7979
_t._colorsUint8Buffer = cc._renderContext.createBuffer();
8080

@@ -269,9 +269,9 @@
269269
this._squareColorsAB = new ArrayBuffer(verticesLen * 4);
270270
locColors.length = 0;
271271
var locSquareColorsAB = this._squareColorsAB;
272-
var locColorLen = cc.Color.BYTES_PER_ELEMENT;
273-
for(i = 0; i < verticesLen; i++){
274-
locColors.push(cc.color(0, 0, 0, 255, locSquareColorsAB, locColorLen * i));
272+
var locColorLen = cc._WebGLColor.BYTES_PER_ELEMENT;
273+
for (i = 0; i < verticesLen; i++) {
274+
locColors.push(new cc._WebGLColor(0, 0, 0, 255, locSquareColorsAB, locColorLen * i));
275275
}
276276
}
277277

cocos2d/core/platform/CCTypes.js

Lines changed: 237 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,55 @@
3434
* @see cc.color
3535
*/
3636
cc.Color = function (r, g, b, a) {
37-
this.r = r || 0;
38-
this.g = g || 0;
39-
this.b = b || 0;
40-
this.a = (a == null) ? 255 : a;
37+
r = r || 0;
38+
g = g || 0;
39+
b = b || 0;
40+
a = a || 0;
41+
this._val = ((r << 24) >>> 0) + (g << 16) + (b << 8) + a;
4142
};
4243

44+
var _p = cc.Color.prototype;
45+
_p._getR = function () {
46+
return (this._val & 0xff000000) >>> 24;
47+
};
48+
_p._setR = function (value) {
49+
this._val = (this._val & 0x00ffffff) | ((value << 24) >>> 0);
50+
};
51+
_p._getG = function () {
52+
return (this._val & 0x00ff0000) >> 16;
53+
};
54+
_p._setG = function (value) {
55+
this._val = (this._val & 0xff00ffff) | (value << 16);
56+
};
57+
_p._getB = function () {
58+
return (this._val & 0x0000ff00) >> 8;
59+
};
60+
_p._setB = function (value) {
61+
this._val = (this._val & 0xffff00ff) | (value << 8);
62+
};
63+
64+
_p._getA = function () {
65+
return this._val & 0x000000ff;
66+
};
67+
68+
_p.setA = function (value) {
69+
this._val = (this._val & 0xffffff00) | value;
70+
};
71+
72+
73+
/** @expose */
74+
_p.r;
75+
cc.defineGetterSetter(_p, "r", _p._getR, _p._setR);
76+
/** @expose */
77+
_p.g;
78+
cc.defineGetterSetter(_p, "g", _p._getG, _p._setG);
79+
/** @expose */
80+
_p.b;
81+
cc.defineGetterSetter(_p, "b", _p._getB, _p._setB);
82+
/** @expose */
83+
_p.a;
84+
cc.defineGetterSetter(_p, "a", _p._getA, _p._setA);
85+
4386
/**
4487
* Generate a color object based on multiple forms of parameters
4588
* @example
@@ -354,10 +397,10 @@ cc.V3F_C4B_T2F = function (vertices, colors, texCoords, arrayBuffer, offset) {
354397
new cc.Vertex3F(0, 0, 0, locArrayBuffer, locOffset);
355398

356399
locOffset += cc.Vertex3F.BYTES_PER_ELEMENT;
357-
this._colors = colors ? new cc.Color(colors.r, colors.g, colors.b, colors.a, locArrayBuffer, locOffset) :
358-
new cc.Color(0, 0, 0, 0, locArrayBuffer, locOffset);
400+
this._colors = colors ? new cc._WebGLColor(colors.r, colors.g, colors.b, colors.a, locArrayBuffer, locOffset) :
401+
new cc._WebGLColor(0, 0, 0, 0, locArrayBuffer, locOffset);
359402

360-
locOffset += cc.Color.BYTES_PER_ELEMENT;
403+
locOffset += cc._WebGLColor.BYTES_PER_ELEMENT;
361404
this._texCoords = texCoords ? new cc.Tex2F(texCoords.u, texCoords.v, locArrayBuffer, locOffset) :
362405
new cc.Tex2F(0, 0, locArrayBuffer, locOffset);
363406
};
@@ -570,9 +613,9 @@ cc.V2F_C4B_T2F = function (vertices, colors, texCoords, arrayBuffer, offset) {
570613
this._vertices = vertices ? new cc.Vertex2F(vertices.x, vertices.y, locArrayBuffer, locOffset) :
571614
new cc.Vertex2F(0, 0, locArrayBuffer, locOffset);
572615
locOffset += cc.Vertex2F.BYTES_PER_ELEMENT;
573-
this._colors = colors ? cc.color(colors.r, colors.g, colors.b, colors.a, locArrayBuffer, locOffset) :
574-
cc.color(0, 0, 0, 0, locArrayBuffer, locOffset);
575-
locOffset += cc.Color.BYTES_PER_ELEMENT;
616+
this._colors = colors ? new cc._WebGLColor(colors.r, colors.g, colors.b, colors.a, locArrayBuffer, locOffset) :
617+
new cc._WebGLColor(0, 0, 0, 0, locArrayBuffer, locOffset);
618+
locOffset += cc._WebGLColor.BYTES_PER_ELEMENT;
576619
this._texCoords = texCoords ? new cc.Tex2F(texCoords.u, texCoords.v, locArrayBuffer, locOffset) :
577620
new cc.Tex2F(0, 0, locArrayBuffer, locOffset);
578621
};
@@ -753,7 +796,7 @@ cc.hexToColor = function (hex) {
753796
var r = c >> 16;
754797
var g = (c >> 8) % 256;
755798
var b = c % 256;
756-
return cc.color(r, g, b);
799+
return new cc.Color(r, g, b);
757800
};
758801

759802
/**
@@ -950,9 +993,188 @@ cc.FontDefinition.prototype._getCanvasFontStr = function () {
950993
};
951994

952995
cc.game.addEventListener(cc.game.EVENT_RENDERER_INITED, function () {
953-
if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
954-
cc.assert(cc.isFunction(cc._tmp.PrototypeColor), cc._LogInfos.MissingFile, "CCTypesPropertyDefine.js");
955-
cc._tmp.PrototypeColor();
956-
delete cc._tmp.PrototypeColor;
996+
if (cc._renderType === cc.game.RENDER_TYPE_WEBGL) {
997+
//redefine Color
998+
cc._WebGLColor = function (r, g, b, a, arrayBuffer, offset) {
999+
this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc._WebGLColor.BYTES_PER_ELEMENT);
1000+
this._offset = offset || 0;
1001+
1002+
var locArrayBuffer = this._arrayBuffer, locOffset = this._offset;
1003+
this._view = new Uint8Array(locArrayBuffer, locOffset, 4);
1004+
1005+
this._view[0] = r || 0;
1006+
this._view[1] = g || 0;
1007+
this._view[2] = b || 0;
1008+
this._view[3] = (a == null) ? 255 : a;
1009+
1010+
if (a === undefined)
1011+
this.a_undefined = true;
1012+
};
1013+
cc._WebGLColor.BYTES_PER_ELEMENT = 4;
1014+
_p = cc._WebGLColor.prototype;
1015+
_p._getR = function () {
1016+
return this._view[0];
1017+
};
1018+
_p._setR = function (value) {
1019+
this._view[0] = value < 0 ? 0 : value;
1020+
};
1021+
_p._getG = function () {
1022+
return this._view[1];
1023+
};
1024+
_p._setG = function (value) {
1025+
this._view[1] = value < 0 ? 0 : value;
1026+
};
1027+
_p._getB = function () {
1028+
return this._view[2];
1029+
};
1030+
_p._setB = function (value) {
1031+
this._view[2] = value < 0 ? 0 : value;
1032+
};
1033+
_p._getA = function () {
1034+
return this._view[3];
1035+
};
1036+
_p._setA = function (value) {
1037+
this._view[3] = value < 0 ? 0 : value;
1038+
};
1039+
cc.defineGetterSetter(_p, "r", _p._getR, _p._setR);
1040+
cc.defineGetterSetter(_p, "g", _p._getG, _p._setG);
1041+
cc.defineGetterSetter(_p, "b", _p._getB, _p._setB);
1042+
cc.defineGetterSetter(_p, "a", _p._getA, _p._setA);
9571043
}
9581044
});
1045+
1046+
_p = cc.color;
1047+
/**
1048+
* White color (255, 255, 255, 255)
1049+
* @returns {cc.Color}
1050+
* @private
1051+
*/
1052+
_p._getWhite = function () {
1053+
return cc.color(255, 255, 255);
1054+
};
1055+
1056+
/**
1057+
* Yellow color (255, 255, 0, 255)
1058+
* @returns {cc.Color}
1059+
* @private
1060+
*/
1061+
_p._getYellow = function () {
1062+
return cc.color(255, 255, 0);
1063+
};
1064+
1065+
/**
1066+
* Blue color (0, 0, 255, 255)
1067+
* @type {cc.Color}
1068+
* @private
1069+
*/
1070+
_p._getBlue = function () {
1071+
return cc.color(0, 0, 255);
1072+
};
1073+
1074+
/**
1075+
* Green Color (0, 255, 0, 255)
1076+
* @type {cc.Color}
1077+
* @private
1078+
*/
1079+
_p._getGreen = function () {
1080+
return cc.color(0, 255, 0);
1081+
};
1082+
1083+
/**
1084+
* Red Color (255, 0, 0, 255)
1085+
* @type {cc.Color}
1086+
* @private
1087+
*/
1088+
_p._getRed = function () {
1089+
return cc.color(255, 0, 0);
1090+
};
1091+
1092+
/**
1093+
* Magenta Color (255, 0, 255, 255)
1094+
* @type {cc.Color}
1095+
* @private
1096+
*/
1097+
_p._getMagenta = function () {
1098+
return cc.color(255, 0, 255);
1099+
};
1100+
1101+
/**
1102+
* Black Color (0, 0, 0, 255)
1103+
* @type {cc.Color}
1104+
* @private
1105+
*/
1106+
_p._getBlack = function () {
1107+
return cc.color(0, 0, 0);
1108+
};
1109+
1110+
/**
1111+
* Orange Color (255, 127, 0, 255)
1112+
* @type {_p}
1113+
* @private
1114+
*/
1115+
_p._getOrange = function () {
1116+
return cc.color(255, 127, 0);
1117+
};
1118+
1119+
/**
1120+
* Gray Color (166, 166, 166, 255)
1121+
* @type {_p}
1122+
* @private
1123+
*/
1124+
_p._getGray = function () {
1125+
return cc.color(166, 166, 166);
1126+
};
1127+
1128+
/** @expose */
1129+
_p.WHITE;
1130+
cc.defineGetterSetter(_p, "WHITE", _p._getWhite);
1131+
/** @expose */
1132+
_p.YELLOW;
1133+
cc.defineGetterSetter(_p, "YELLOW", _p._getYellow);
1134+
/** @expose */
1135+
_p.BLUE;
1136+
cc.defineGetterSetter(_p, "BLUE", _p._getBlue);
1137+
/** @expose */
1138+
_p.GREEN;
1139+
cc.defineGetterSetter(_p, "GREEN", _p._getGreen);
1140+
/** @expose */
1141+
_p.RED;
1142+
cc.defineGetterSetter(_p, "RED", _p._getRed);
1143+
/** @expose */
1144+
_p.MAGENTA;
1145+
cc.defineGetterSetter(_p, "MAGENTA", _p._getMagenta);
1146+
/** @expose */
1147+
_p.BLACK;
1148+
cc.defineGetterSetter(_p, "BLACK", _p._getBlack);
1149+
/** @expose */
1150+
_p.ORANGE;
1151+
cc.defineGetterSetter(_p, "ORANGE", _p._getOrange);
1152+
/** @expose */
1153+
_p.GRAY;
1154+
cc.defineGetterSetter(_p, "GRAY", _p._getGray);
1155+
1156+
cc.BlendFunc._disable = function(){
1157+
return new cc.BlendFunc(cc.ONE, cc.ZERO);
1158+
};
1159+
cc.BlendFunc._alphaPremultiplied = function(){
1160+
return new cc.BlendFunc(cc.ONE, cc.ONE_MINUS_SRC_ALPHA);
1161+
};
1162+
cc.BlendFunc._alphaNonPremultiplied = function(){
1163+
return new cc.BlendFunc(cc.SRC_ALPHA, cc.ONE_MINUS_SRC_ALPHA);
1164+
};
1165+
cc.BlendFunc._additive = function(){
1166+
return new cc.BlendFunc(cc.SRC_ALPHA, cc.ONE);
1167+
};
1168+
1169+
/** @expose */
1170+
cc.BlendFunc.DISABLE;
1171+
cc.defineGetterSetter(cc.BlendFunc, "DISABLE", cc.BlendFunc._disable);
1172+
/** @expose */
1173+
cc.BlendFunc.ALPHA_PREMULTIPLIED;
1174+
cc.defineGetterSetter(cc.BlendFunc, "ALPHA_PREMULTIPLIED", cc.BlendFunc._alphaPremultiplied);
1175+
/** @expose */
1176+
cc.BlendFunc.ALPHA_NON_PREMULTIPLIED;
1177+
cc.defineGetterSetter(cc.BlendFunc, "ALPHA_NON_PREMULTIPLIED", cc.BlendFunc._alphaNonPremultiplied);
1178+
/** @expose */
1179+
cc.BlendFunc.ADDITIVE;
1180+
cc.defineGetterSetter(cc.BlendFunc, "ADDITIVE", cc.BlendFunc._additive);

0 commit comments

Comments
 (0)