|
1 | 1 | 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'); |
5 | 9 |
|
6 | 10 | var LEVEL_DET_FACTOR = 0.6, _levelDetCycle = 10;
|
7 | 11 | var LEVELS = [0, 10, 20, 30];
|
8 | 12 | var _fpsCount = [0, 0, 0, 0];
|
9 | 13 | var _currLevel = 3, _analyseCount = 0, _totalFPS = 0;
|
10 | 14 |
|
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 | + } |
32 | 34 |
|
33 | 35 | var analyseFPS = function (fps) {
|
34 | 36 | var lastId = LEVELS.length - 1, i = lastId, ratio, average = 0;
|
@@ -82,23 +84,11 @@ cc.profiler = (function () {
|
82 | 84 | }
|
83 | 85 |
|
84 | 86 | 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(); |
88 | 90 | }
|
89 | 91 | }
|
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); |
102 | 92 | };
|
103 | 93 |
|
104 | 94 | var profiler = {
|
@@ -133,22 +123,19 @@ cc.profiler = (function () {
|
133 | 123 | if (!_inited) {
|
134 | 124 | this.init();
|
135 | 125 | }
|
136 |
| - if (cc.LabelTTF && !_FPSLabel) { |
137 |
| - createStatsLabel(); |
138 |
| - } |
139 |
| - if (_FPSLabel) { |
140 |
| - _showFPS = true; |
141 |
| - } |
| 126 | + |
| 127 | + cc.container.appendChild(_fps); |
| 128 | + _showFPS = true; |
142 | 129 | },
|
143 | 130 |
|
144 | 131 | hideStats: function () {
|
145 | 132 | _showFPS = false;
|
| 133 | + cc.container.removeChild(_fps); |
146 | 134 | },
|
147 | 135 |
|
148 | 136 | init: function () {
|
149 | 137 | if (!_inited) {
|
150 | 138 | _afterVisitListener = cc.eventManager.addCustomListener(cc.Director.EVENT_AFTER_VISIT, afterVisit);
|
151 |
| - _afterProjection = cc.eventManager.addCustomListener(cc.Director.EVENT_PROJECTION_CHANGED, afterProjection); |
152 | 139 | _inited = true;
|
153 | 140 | }
|
154 | 141 | }
|
|
0 commit comments