Skip to content

Commit d176dc4

Browse files
committed
Merge pull request cocos2d#3282 from pandamicro/develop
Made profiler using DOM
2 parents f213088 + cd81895 commit d176dc4

File tree

3 files changed

+43
-50
lines changed

3 files changed

+43
-50
lines changed

cocos2d/core/renderer/RendererCanvas.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ cc.rendererCanvas = {
9999

100100
_removeCache: function (instanceID) {
101101
instanceID = instanceID || this._currentID;
102-
this._cacheToCanvasCmds[instanceID].length = 0;
103-
delete this._cacheToCanvasCmds[instanceID];
102+
var cmds = this._cacheToCanvasCmds[instanceID];
103+
if (cmds) {
104+
cmds.length = 0;
105+
delete this._cacheToCanvasCmds[instanceID];
106+
}
104107

105108
var locIDs = this._cacheInstanceIds;
106109
cc.arrayRemoveObject(locIDs, instanceID);

cocos2d/core/renderer/RendererWebGL.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,11 @@ return {
198198

199199
_removeCache: function (instanceID) {
200200
instanceID = instanceID || this._currentID;
201-
this._cacheToBufferCmds[instanceID].length = 0;
202-
delete this._cacheToBufferCmds[instanceID];
201+
var cmds = this._cacheToBufferCmds[instanceID];
202+
if (cmds) {
203+
cmds.length = 0;
204+
delete this._cacheToBufferCmds[instanceID];
205+
}
203206

204207
var locIDs = this._cacheInstanceIds;
205208
cc.arrayRemoveObject(locIDs, instanceID);

cocos2d/core/utils/CCProfiler.js

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
cc.profiler = (function () {
2-
var _inited = _showFPS = false;
3-
var _frames = _frameRate = _lastSPF = _accumDt = 0;
4-
var _afterProjection = _afterVisitListener = _FPSLabel = _SPFLabel = _drawsLabel = null;
2+
var _inited = false, _showFPS = false;
3+
var _frames = 0, _frameRate = 0, _lastSPF = 0, _accumDt = 0;
4+
var _afterVisitListener = null,
5+
_FPSLabel = document.createElement('div'),
6+
_SPFLabel = document.createElement('div'),
7+
_drawsLabel = document.createElement('div'),
8+
_fps = document.createElement('div');
59

610
var LEVEL_DET_FACTOR = 0.6, _levelDetCycle = 10;
711
var LEVELS = [0, 10, 20, 30];
812
var _fpsCount = [0, 0, 0, 0];
913
var _currLevel = 3, _analyseCount = 0, _totalFPS = 0;
1014

11-
var createStatsLabel = function () {
12-
var fontSize = 0;
13-
var w = cc.winSize.width, h = cc.winSize.height;
14-
var locStatsPosition = cc.DIRECTOR_STATS_POSITION;
15-
if (w > h)
16-
fontSize = 0 | (h / 320 * 24);
17-
else
18-
fontSize = 0 | (w / 320 * 24);
19-
20-
_FPSLabel = new cc.LabelTTF("000.0", "Arial", fontSize);
21-
_SPFLabel = new cc.LabelTTF("0.000", "Arial", fontSize);
22-
_drawsLabel = new cc.LabelTTF("0000", "Arial", fontSize);
23-
24-
_FPSLabel.setVertexZ(0.9999);
25-
_SPFLabel.setVertexZ(0.9999);
26-
_SPFLabel.setVertexZ(0.9999);
27-
28-
_drawsLabel.setPosition(_drawsLabel.width / 2 + locStatsPosition.x, _drawsLabel.height * 5 / 2 + locStatsPosition.y);
29-
_SPFLabel.setPosition(_SPFLabel.width / 2 + locStatsPosition.x, _SPFLabel.height * 3 / 2 + locStatsPosition.y);
30-
_FPSLabel.setPosition(_FPSLabel.width / 2 + locStatsPosition.x, _FPSLabel.height / 2 + locStatsPosition.y);
31-
};
15+
_fps.id = 'fps';
16+
_fps.style.position = 'absolute';
17+
_fps.style.padding = '3px';
18+
_fps.style.textAlign = 'left';
19+
_fps.style.backgroundColor = 'rgb(0, 0, 34)';
20+
_fps.style.bottom = cc.DIRECTOR_STATS_POSITION.y + '0px';
21+
_fps.style.left = cc.DIRECTOR_STATS_POSITION.x + 'px';
22+
_fps.style.width = '45px';
23+
_fps.style.height = '60px';
24+
25+
var labels = [_drawsLabel, _SPFLabel, _FPSLabel];
26+
for (var i = 0; i < 3; ++i) {
27+
var style = labels[i].style;
28+
style.color = 'rgb(0, 255, 255)';
29+
style.font = 'bold 12px Helvetica, Arial';
30+
style.lineHeight = '20px';
31+
style.width = '100%';
32+
_fps.appendChild(labels[i]);
33+
}
3234

3335
var analyseFPS = function (fps) {
3436
var lastId = LEVELS.length - 1, i = lastId, ratio, average = 0;
@@ -82,23 +84,11 @@ cc.profiler = (function () {
8284
}
8385

8486
if (_showFPS) {
85-
_SPFLabel.string = _lastSPF.toFixed(3);
86-
_FPSLabel.string = _frameRate.toFixed(1);
87-
_drawsLabel.string = (0 | cc.g_NumberOfDraws).toString();
87+
_SPFLabel.innerText = _lastSPF.toFixed(3);
88+
_FPSLabel.innerText = _frameRate.toFixed(1);
89+
_drawsLabel.innerText = (0 | cc.g_NumberOfDraws).toString();
8890
}
8991
}
90-
91-
if (_showFPS) {
92-
_FPSLabel.visit();
93-
_SPFLabel.visit();
94-
_drawsLabel.visit();
95-
}
96-
};
97-
98-
var afterProjection = function(){
99-
_FPSLabel._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
100-
_SPFLabel._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
101-
_drawsLabel._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
10292
};
10393

10494
var profiler = {
@@ -133,22 +123,19 @@ cc.profiler = (function () {
133123
if (!_inited) {
134124
this.init();
135125
}
136-
if (cc.LabelTTF && !_FPSLabel) {
137-
createStatsLabel();
138-
}
139-
if (_FPSLabel) {
140-
_showFPS = true;
141-
}
126+
127+
cc.container.appendChild(_fps);
128+
_showFPS = true;
142129
},
143130

144131
hideStats: function () {
145132
_showFPS = false;
133+
cc.container.removeChild(_fps);
146134
},
147135

148136
init: function () {
149137
if (!_inited) {
150138
_afterVisitListener = cc.eventManager.addCustomListener(cc.Director.EVENT_AFTER_VISIT, afterVisit);
151-
_afterProjection = cc.eventManager.addCustomListener(cc.Director.EVENT_PROJECTION_CHANGED, afterProjection);
152139
_inited = true;
153140
}
154141
}

0 commit comments

Comments
 (0)