From d1f607edb83de23362fec7607f71909ff5e2413a Mon Sep 17 00:00:00 2001 From: liabru Date: Fri, 2 May 2014 12:23:13 +0100 Subject: [PATCH] added Engine.render, moved respective engine event triggers into Engine.update and Engine.render --- src/core/Engine.js | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/core/Engine.js b/src/core/Engine.js index 7c321a6..4e9a324 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -81,7 +81,9 @@ var Engine = {}; }; /** - * Description + * A simple game loop utility, that calls `Engine.update` and `Engine.render` on the `requestAnimationFrame` event automatically. + * Handles time correction and non-fixed dynamic timing (if enabled). + * Triggers `beforeTick`, `tick` and `afterTick` events. * @method run * @param {engine} engine */ @@ -152,7 +154,7 @@ var Engine = {}; frameCounter = 0; } - Events.trigger(engine, 'tick beforeUpdate', event); + Events.trigger(engine, 'tick', event); // if world has been modified, clear the render scene graph if (engine.world.isModified) @@ -165,18 +167,15 @@ var Engine = {}; _triggerCollisionEvents(engine); _triggerMouseEvents(engine); - Events.trigger(engine, 'afterUpdate beforeRender', event); - // render - if (engine.render.options.enabled) - engine.render.controller.world(engine); + Engine.render(engine); - Events.trigger(engine, 'afterTick afterRender', event); + Events.trigger(engine, 'afterTick', event); })(); }; /** - * Description + * Moves the simulation forward in time by `delta`. Triggers `beforeUpdate` and `afterUpdate` events. * @method update * @param {engine} engine * @param {number} delta @@ -190,6 +189,13 @@ var Engine = {}; broadphasePairs = [], i; + // create an event object + var event = { + timestamp: engine.timing.timestamp + }; + + Events.trigger(engine, 'beforeUpdate', event); + // get lists of all bodies and constraints, no matter what composites they are in var allBodies = Composite.allBodies(world), allConstraints = Composite.allConstraints(world); @@ -264,8 +270,27 @@ var Engine = {}; if (world.isModified) Composite.setModified(world, false, false, true); + Events.trigger(engine, 'afterUpdate', event); + return engine; }; + + /** + * Renders the world by calling its defined renderer `engine.render.controller`. Triggers `beforeRender` and `afterRender` events. + * @method render + * @param {engine} engineA + * @param {engine} engineB + */ + Engine.render = function(engine) { + // create an event object + var event = { + timestamp: engine.timing.timestamp + }; + + Events.trigger(engine, 'beforeRender', event); + engine.render.controller.world(engine); + Events.trigger(engine, 'afterRender', event); + }; /** * Description