Skip to content

Commit

Permalink
refactor Demo.init
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Aug 25, 2015
1 parent c0b24da commit fb76605
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 57 deletions.
62 changes: 21 additions & 41 deletions demo/js/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,31 @@
Inspector = MatterTools.Inspector;
}

// initialise the demo

Demo.create = function() {
return {
isMobile: _isMobile,
Demo.create = function(options) {
var defaults = {
sceneName: 'mixed',
sceneEvents: []
};

return Common.extend(defaults, options);
};

Demo.init = function() {
var demo = Demo.create();

// some example engine options
var options = {
positionIterations: 6,
velocityIterations: 4,
enableSleeping: false,
metrics: { extended: true }
};

// create a Matter engine
// NOTE: this is actually Matter.Engine.create(), see the aliases at top of this file
if (_isBrowser) {
var container = document.getElementById('canvas-container');
demo.engine = Engine.create(container, options);

// add a mouse controlled constraint
demo.mouseConstraint = MouseConstraint.create(demo.engine);
World.add(demo.engine.world, demo.mouseConstraint);
} else {
demo.engine = Engine.create(options);
demo.engine.render = {};
demo.engine.render.options = {};
}

// demo instance reference for external use
Matter.Demo._demo = demo;
// get container element for the canvas
demo.container = document.getElementById('canvas-container');

// skip runner when performing automated tests
if (_isAutomatedTest) return;
// create an example engine (see /examples/engine.js)
demo.engine = Example.engine(demo);

// run the engine
demo.runner = Engine.run(demo.engine);

// default scene function name
demo.sceneName = 'mixed';

// add a mouse controlled constraint
demo.mouseConstraint = MouseConstraint.create(demo.engine);
World.add(demo.engine.world, demo.mouseConstraint);

// get the scene function name from hash
if (window.location.hash.length !== 0)
demo.sceneName = window.location.hash.replace('#', '').replace('-inspect', '');
Expand All @@ -88,18 +66,20 @@

// set up demo interface (see end of this file)
Demo.initControls(demo);

return demo;
};

// call init when the page has loaded fully

if (window.addEventListener) {
window.addEventListener('load', Demo.init);
} else if (window.attachEvent) {
window.attachEvent('load', Demo.init);
if (!_isAutomatedTest) {
if (window.addEventListener) {
window.addEventListener('load', Demo.init);
} else if (window.attachEvent) {
window.attachEvent('load', Demo.init);
}
}

// the functions for the demo interface and controls below

Demo.initControls = function(demo) {
var demoSelect = document.getElementById('demo-select'),
demoReset = document.getElementById('demo-reset');
Expand Down
17 changes: 17 additions & 0 deletions examples/engine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(function() {

var Engine = Matter.Engine;

Example.engine = function(demo) {
// some example engine options
var options = {
positionIterations: 6,
velocityIterations: 4,
enableSleeping: false,
metrics: { extended: true }
};

return Engine.create(demo.container, options);
};

})();
6 changes: 0 additions & 6 deletions examples/wreckingBall.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
pointA: { x: 300, y: 100 },
bodyB: ball
}));

if (!demo.isMobile) {
var renderOptions = engine.render.options;
renderOptions.showCollisions = true;
renderOptions.showVelocity = true;
}
};

})();
20 changes: 16 additions & 4 deletions test/browser/TestDemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,30 @@ var test = function(status) {
worldEndDiffPath = diffsPath + '/' + demo + '/' + demo + '-' + frames + '.json';

var worldStart = page.evaluate(function(demo) {
var engine = Matter.Demo._demo.engine;
if (!(demo in Matter.Example)) {
throw '\'' + demo + '\' is not defined in Matter.Demo';
}
Matter.Demo.reset(Matter.Demo._demo);
Matter.Example[demo](Matter.Demo._demo);

var _demo = Matter.Demo.create(),
engine = Matter.Example.engine(_demo),
runner = Matter.Runner.create();

Matter.Demo._demo = _demo;
_demo.engine = engine;
_demo.engine.render = {};
_demo.engine.render.options = {};
_demo.runner = runner;
_demo.mouseConstraint = Matter.MouseConstraint.create(engine);

Matter.Demo.reset(_demo);
Matter.Example[demo](_demo);

return engine.world;
}, demo);

var worldEnd = page.evaluate(function(demo, frames) {
var engine = Matter.Demo._demo.engine,
runner = Matter.Runner.create();
runner = Matter.Demo._demo.runner;

for (var j = 0; j <= frames; j += 1) {
Matter.Runner.tick(runner, engine, j * runner.delta);
Expand Down
16 changes: 10 additions & 6 deletions test/node/TestDemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ var compare = require('fast-json-patch').compare;
var path = require('path');
var $ = require('cheerio');
var Matter = require('../../build/matter-dev.js');
Matter.Demo = require('../../demo/js/Demo.js');
Matter.Example = require('../../demo/js/Examples.js');
Matter.Demo = require('../../demo/js/Demo.js');

var demo,
frames = 10,
Expand Down Expand Up @@ -42,17 +42,21 @@ var test = function() {
worldStartDiffPath = diffsPath + '/' + demo + '/' + demo + '-0.json',
worldEndDiffPath = diffsPath + '/' + demo + '/' + demo + '-' + frames + '.json';

Matter.Demo.init();

var engine = Matter.Demo._demo.engine,
var _demo = Matter.Demo.create(),
engine = Matter.Example.engine(_demo),
runner = Matter.Runner.create();

_demo.engine = engine;
_demo.engine.render = {};
_demo.engine.render.options = {};
_demo.runner = runner;

if (!(demo in Matter.Example)) {
throw '\'' + demo + '\' is not defined in Matter.Example';
}

Matter.Demo.reset(Matter.Demo._demo);
Matter.Example[demo](Matter.Demo._demo);
Matter.Demo.reset(_demo);
Matter.Example[demo](_demo);

var worldStart = JSON.parse(resurrect.stringify(engine.world, precisionLimiter));

Expand Down

0 comments on commit fb76605

Please sign in to comment.