diff --git a/Gruntfile.js b/Gruntfile.js index 9ef441d..c1d275e 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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' + } } }); @@ -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']); @@ -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'); } }); diff --git a/package.json b/package.json index 6c7addb..98e8906 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/collision/Detector.js b/src/collision/Detector.js index aa46820..0577cf8 100644 --- a/src/collision/Detector.js +++ b/src/collision/Detector.js @@ -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]; @@ -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)) { @@ -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 } } } @@ -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], @@ -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)) { @@ -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 } } } diff --git a/src/collision/Grid.js b/src/collision/Grid.js index 7b5ad17..1b04571 100644 --- a/src/collision/Grid.js +++ b/src/collision/Grid.js @@ -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]; @@ -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; diff --git a/src/core/Engine.js b/src/core/Engine.js index 4a4664e..b51a855 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -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; }; @@ -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) @@ -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); diff --git a/src/core/Metrics.js b/src/core/Metrics.js index 6da79ba..03d3eb9 100644 --- a/src/core/Metrics.js +++ b/src/core/Metrics.js @@ -1,7 +1,7 @@ +// @if DEBUG /** * _Internal Class_, not generally used outside of the engine's internals. * -* @class Metrics */ var Metrics = {}; @@ -11,6 +11,7 @@ var Metrics = {}; /** * Description * @method create + * @private * @return {metrics} A new metrics */ Metrics.create = function() { @@ -35,6 +36,7 @@ var Metrics = {}; /** * Description * @method reset + * @private * @param {metrics} metrics */ Metrics.reset = function(metrics) { @@ -58,6 +60,7 @@ var Metrics = {}; /** * Description * @method update + * @private * @param {metrics} metrics * @param {engine} engine */ @@ -80,3 +83,4 @@ var Metrics = {}; }; })(); +// @endif \ No newline at end of file diff --git a/src/module/Outro.js b/src/module/Outro.js index 8646684..dfacc4e 100644 --- a/src/module/Outro.js +++ b/src/module/Outro.js @@ -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; @@ -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) { diff --git a/src/render/Render.js b/src/render/Render.js index 6ed8a11..ce62bc6 100644 --- a/src/render/Render.js +++ b/src/render/Render.js @@ -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; @@ -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;