Skip to content

Commit

Permalink
removed Matter.Metrics from release builds (now dev only)
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Jan 25, 2015
1 parent 0849773 commit 968bba2
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 13 deletions.
15 changes: 14 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ module.exports = function(grunt) {
linkNatives: true
}
}
},
preprocess: {
options: {
inline: true,
context : {
DEBUG: false
}
},
js: {
src: 'build/<%= buildName %>.js',
dest: 'build/<%= buildName %>.js'
}
}
});

Expand All @@ -105,6 +117,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-yuidoc');
grunt.loadNpmTasks('grunt-preprocess');

grunt.registerTask('default', ['test', 'build']);
grunt.registerTask('test', ['jshint']);
Expand Down Expand Up @@ -134,7 +147,7 @@ module.exports = function(grunt) {
// edge build mode (default)
if (isEdge || (!isDev && !isRelease)) {
grunt.config.set('buildVersion', pkg.version + '-edge');
grunt.task.run('concat', 'uglify:min');
grunt.task.run('concat', 'preprocess', 'uglify:min');
}
});

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
],
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-jshint": "~0.6.3",
"grunt-contrib-uglify": "~0.2.7",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-connect": "~0.6.0",
"grunt-contrib-copy": "~0.5.0",
"grunt-contrib-jshint": "~0.6.3",
"grunt-contrib-uglify": "~0.2.7",
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-yuidoc": "~0.5.1"
"grunt-contrib-yuidoc": "~0.5.1",
"grunt-preprocess": "^4.1.0"
},
"scripts": {
"dev": "npm install && grunt dev",
Expand Down
24 changes: 20 additions & 4 deletions src/collision/Detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ var Detector = {};
*/
Detector.collisions = function(broadphasePairs, engine) {
var collisions = [],
metrics = engine.metrics,
pairsTable = engine.pairs.table;

// @if DEBUG
var metrics = engine.metrics;
// @endif

for (var i = 0; i < broadphasePairs.length; i++) {
var bodyA = broadphasePairs[i][0],
bodyB = broadphasePairs[i][1];
Expand All @@ -32,7 +35,9 @@ var Detector = {};
if (!Detector.canCollide(bodyA.collisionFilter, bodyB.collisionFilter))
continue;

// @if DEBUG
metrics.midphaseTests += 1;
// @endif

// mid phase
if (Bounds.overlaps(bodyA.bounds, bodyB.bounds)) {
Expand All @@ -51,14 +56,17 @@ var Detector = {};
// narrow phase
var collision = SAT.collides(bodyA, bodyB, previousCollision);

// @if DEBUG
metrics.narrowphaseTests += 1;

if (collision.reused)
metrics.narrowReuseCount += 1;
// @endif

if (collision.collided) {
collisions.push(collision);
// @if DEBUG
metrics.narrowDetections += 1;
// @endif
}
}
}
Expand All @@ -75,9 +83,12 @@ var Detector = {};
*/
Detector.bruteForce = function(bodies, engine) {
var collisions = [],
metrics = engine.metrics,
pairsTable = engine.pairs.table;

// @if DEBUG
var metrics = engine.metrics;
// @endif

for (var i = 0; i < bodies.length; i++) {
for (var j = i + 1; j < bodies.length; j++) {
var bodyA = bodies[i],
Expand All @@ -91,7 +102,9 @@ var Detector = {};
if (!Detector.canCollide(bodyA.collisionFilter, bodyB.collisionFilter))
continue;

// @if DEBUG
metrics.midphaseTests += 1;
// @endif

// mid phase
if (Bounds.overlaps(bodyA.bounds, bodyB.bounds)) {
Expand All @@ -110,14 +123,17 @@ var Detector = {};
// narrow phase
var collision = SAT.collides(bodyA, bodyB, previousCollision);

// @if DEBUG
metrics.narrowphaseTests += 1;

if (collision.reused)
metrics.narrowReuseCount += 1;
// @endif

if (collision.collided) {
collisions.push(collision);
// @if DEBUG
metrics.narrowDetections += 1;
// @endif
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/collision/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ var Grid = {};
buckets = grid.buckets,
bucket,
bucketId,
metrics = engine.metrics,
gridChanged = false;

// @if DEBUG
var metrics = engine.metrics;
metrics.broadphaseTests = 0;
// @endif

for (i = 0; i < bodies.length; i++) {
var body = bodies[i];
Expand All @@ -64,7 +66,9 @@ var Grid = {};
// if the body has changed grid region
if (!body.region || newRegion.id !== body.region.id || forceUpdate) {

// @if DEBUG
metrics.broadphaseTests += 1;
// @endif

if (!body.region || forceUpdate)
body.region = newRegion;
Expand Down
10 changes: 9 additions & 1 deletion src/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ var Engine = {};
engine.render = engine.render.controller.create(engine.render);
engine.world = World.create(engine.world);
engine.pairs = Pairs.create();
engine.metrics = engine.metrics || Metrics.create();
engine.broadphase = engine.broadphase.controller.create(engine.broadphase);
engine.metrics = engine.metrics || { extended: false };

// @if DEBUG
engine.metrics = engine.metrics || Metrics.create();
// @endif

return engine;
};
Expand Down Expand Up @@ -102,8 +106,10 @@ var Engine = {};
var allBodies = Composite.allBodies(world),
allConstraints = Composite.allConstraints(world);

// @if DEBUG
// reset metrics logging
Metrics.reset(engine.metrics);
// @endif

// if sleeping enabled, call the sleeping controller
if (engine.enableSleeping)
Expand Down Expand Up @@ -173,8 +179,10 @@ var Engine = {};
if (pairs.collisionEnd.length > 0)
Events.trigger(engine, 'collisionEnd', { pairs: pairs.collisionEnd });

// @if DEBUG
// update metrics log
Metrics.update(engine.metrics, engine);
// @endif

// clear force buffers
_bodiesClearForces(allBodies);
Expand Down
6 changes: 5 additions & 1 deletion src/core/Metrics.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @if DEBUG
/**
* _Internal Class_, not generally used outside of the engine's internals.
*
* @class Metrics
*/

var Metrics = {};
Expand All @@ -11,6 +11,7 @@ var Metrics = {};
/**
* Description
* @method create
* @private
* @return {metrics} A new metrics
*/
Metrics.create = function() {
Expand All @@ -35,6 +36,7 @@ var Metrics = {};
/**
* Description
* @method reset
* @private
* @param {metrics} metrics
*/
Metrics.reset = function(metrics) {
Expand All @@ -58,6 +60,7 @@ var Metrics = {};
/**
* Description
* @method update
* @private
* @param {metrics} metrics
* @param {engine} engine
*/
Expand All @@ -80,3 +83,4 @@ var Metrics = {};
};

})();
// @endif
5 changes: 4 additions & 1 deletion src/module/Outro.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Matter.Constraint = Constraint;
Matter.MouseConstraint = MouseConstraint;
Matter.Common = Common;
Matter.Engine = Engine;
Matter.Metrics = Metrics;
Matter.Mouse = Mouse;
Matter.Sleeping = Sleeping;
Matter.Bodies = Bodies;
Expand All @@ -40,6 +39,10 @@ Matter.Events = Events;
Matter.Query = Query;
Matter.Runner = Runner;

// @if DEBUG
Matter.Metrics = Metrics;
// @endif

// CommonJS module
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
Expand Down
4 changes: 3 additions & 1 deletion src/render/Render.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ var Render = {};
var text = "";
text += "fps: " + Math.round(engine.timing.fps) + space;

// @if DEBUG
if (engine.metrics.extended) {
text += "delta: " + engine.timing.delta.toFixed(3) + space;
text += "correction: " + engine.timing.correction.toFixed(3) + space;
Expand All @@ -247,7 +248,8 @@ var Render = {};
text += "broad: " + engine.metrics.broadEff + space;
text += "mid: " + engine.metrics.midEff + space;
text += "narrow: " + engine.metrics.narrowEff + space;
}
}
// @endif

render.debugString = text;
render.debugTimestamp = engine.timing.timestamp;
Expand Down

0 comments on commit 968bba2

Please sign in to comment.