Skip to content

Commit 6911395

Browse files
committed
moved Engine.run to Matter.Runner
1 parent a58fe2a commit 6911395

3 files changed

Lines changed: 139 additions & 96 deletions

File tree

src/core/Engine.js

Lines changed: 10 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* The `Matter.Engine` module contains methods for creating and manipulating engines.
33
* An engine is a controller that manages updating and rendering the simulation of the world.
4+
* See `Matter.Runner` for an optional game loop utility.
45
*
56
* See [Demo.js](https://github.com/liabru/matter-js/blob/master/demo/js/Demo.js)
67
* and [DemoMobile.js](https://github.com/liabru/matter-js/blob/master/demo/js/DemoMobile.js) for usage examples.
@@ -13,13 +14,8 @@ var Engine = {};
1314
(function() {
1415

1516
var _fps = 60,
16-
_deltaSampleSize = _fps,
1717
_delta = 1000 / _fps;
18-
19-
var _requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame
20-
|| window.mozRequestAnimationFrame || window.msRequestAnimationFrame
21-
|| function(callback){ window.setTimeout(function() { callback(Common.now()); }, _delta); };
22-
18+
2319
/**
2420
* Creates a new engine. The options parameter is an object that specifies any properties you wish to override the defaults.
2521
* All properties have default values, and many are pre-calculated automatically based on other properties.
@@ -50,7 +46,8 @@ var Engine = {};
5046
deltaMin: 1000 / _fps,
5147
deltaMax: 1000 / (_fps * 0.5),
5248
timeScale: 1,
53-
isFixed: false
49+
isFixed: false,
50+
frameRequestId: 0
5451
},
5552
render: {
5653
element: element,
@@ -72,95 +69,6 @@ var Engine = {};
7269
return engine;
7370
};
7471

75-
/**
76-
* An optional utility function that provides a game loop, that handles updating the engine for you.
77-
* Calls `Engine.update` and `Engine.render` on the `requestAnimationFrame` event automatically.
78-
* Handles time correction and non-fixed dynamic timing (if enabled).
79-
* Triggers `beforeTick`, `tick` and `afterTick` events.
80-
* @method run
81-
* @param {engine} engine
82-
*/
83-
Engine.run = function(engine) {
84-
var counterTimestamp = 0,
85-
frameCounter = 0,
86-
deltaHistory = [],
87-
timePrev,
88-
timeScalePrev = 1;
89-
90-
(function render(time){
91-
_requestAnimationFrame(render);
92-
93-
if (!engine.enabled)
94-
return;
95-
96-
var timing = engine.timing,
97-
delta,
98-
correction;
99-
100-
// create an event object
101-
var event = {
102-
timestamp: time
103-
};
104-
105-
Events.trigger(engine, 'beforeTick', event);
106-
107-
if (timing.isFixed) {
108-
// fixed timestep
109-
delta = timing.delta;
110-
} else {
111-
// dynamic timestep based on wall clock between calls
112-
delta = (time - timePrev) || timing.delta;
113-
timePrev = time;
114-
115-
// optimistically filter delta over a few frames, to improve stability
116-
deltaHistory.push(delta);
117-
deltaHistory = deltaHistory.slice(-_deltaSampleSize);
118-
delta = Math.min.apply(null, deltaHistory);
119-
120-
// limit delta
121-
delta = delta < timing.deltaMin ? timing.deltaMin : delta;
122-
delta = delta > timing.deltaMax ? timing.deltaMax : delta;
123-
124-
// time correction for delta
125-
correction = delta / timing.delta;
126-
127-
// update engine timing object
128-
timing.delta = delta;
129-
}
130-
131-
// time correction for time scaling
132-
if (timeScalePrev !== 0)
133-
correction *= timing.timeScale / timeScalePrev;
134-
135-
if (timing.timeScale === 0)
136-
correction = 0;
137-
138-
timeScalePrev = timing.timeScale;
139-
140-
// fps counter
141-
frameCounter += 1;
142-
if (time - counterTimestamp >= 1000) {
143-
timing.fps = frameCounter * ((time - counterTimestamp) / 1000);
144-
counterTimestamp = time;
145-
frameCounter = 0;
146-
}
147-
148-
Events.trigger(engine, 'tick', event);
149-
150-
// if world has been modified, clear the render scene graph
151-
if (engine.world.isModified)
152-
engine.render.controller.clear(engine.render);
153-
154-
// update
155-
Engine.update(engine, delta, correction);
156-
157-
// render
158-
Engine.render(engine);
159-
160-
Events.trigger(engine, 'afterTick', event);
161-
})();
162-
};
163-
16472
/**
16573
* Moves the simulation forward in time by `delta` ms.
16674
* Triggers `beforeUpdate` and `afterUpdate` events.
@@ -339,6 +247,12 @@ var Engine = {};
339247
}
340248
};
341249

250+
/**
251+
* An alias for Runner.run, see `Matter.Runner` for more information.
252+
* @method run
253+
* @param {engine} engine
254+
*/
255+
342256
/*
343257
*
344258
* Events Documentation

src/core/Runner.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* The `Matter.Runner` module is an optional utility which provides a game loop,
3+
* that handles updating and rendering a `Matter.Engine` for you within a browser.
4+
* Note that the method `Engine.run` is an alias for `Runner.run`.
5+
*
6+
* See [Demo.js](https://github.com/liabru/matter-js/blob/master/demo/js/Demo.js)
7+
* and [DemoMobile.js](https://github.com/liabru/matter-js/blob/master/demo/js/DemoMobile.js) for usage examples.
8+
*
9+
* @class Runner
10+
*/
11+
12+
var Runner = {};
13+
14+
(function() {
15+
16+
var _fps = 60,
17+
_deltaSampleSize = _fps,
18+
_delta = 1000 / _fps;
19+
20+
var _requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame
21+
|| window.mozRequestAnimationFrame || window.msRequestAnimationFrame
22+
|| function(callback){ window.setTimeout(function() { callback(Common.now()); }, _delta); };
23+
24+
var _cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame
25+
|| window.webkitCancelAnimationFrame || window.msCancelAnimationFrame;
26+
27+
/**
28+
* Provides a basic game loop that handles updating the engine for you.
29+
* Calls `Engine.update` and `Engine.render` on the `requestAnimationFrame` event automatically.
30+
* Handles time correction and non-fixed dynamic timing (if enabled).
31+
* Triggers `beforeTick`, `tick` and `afterTick` events.
32+
* @method run
33+
* @param {engine} engine
34+
*/
35+
Runner.run = function(engine) {
36+
var counterTimestamp = 0,
37+
frameCounter = 0,
38+
deltaHistory = [],
39+
timePrev,
40+
timeScalePrev = 1;
41+
42+
(function render(time){
43+
var timing = engine.timing,
44+
delta,
45+
correction;
46+
47+
timing.frameRequestId = _requestAnimationFrame(render);
48+
49+
if (!engine.enabled)
50+
return;
51+
52+
// create an event object
53+
var event = {
54+
timestamp: time
55+
};
56+
57+
Events.trigger(engine, 'beforeTick', event);
58+
59+
if (timing.isFixed) {
60+
// fixed timestep
61+
delta = timing.delta;
62+
} else {
63+
// dynamic timestep based on wall clock between calls
64+
delta = (time - timePrev) || timing.delta;
65+
timePrev = time;
66+
67+
// optimistically filter delta over a few frames, to improve stability
68+
deltaHistory.push(delta);
69+
deltaHistory = deltaHistory.slice(-_deltaSampleSize);
70+
delta = Math.min.apply(null, deltaHistory);
71+
72+
// limit delta
73+
delta = delta < timing.deltaMin ? timing.deltaMin : delta;
74+
delta = delta > timing.deltaMax ? timing.deltaMax : delta;
75+
76+
// time correction for delta
77+
correction = delta / timing.delta;
78+
79+
// update engine timing object
80+
timing.delta = delta;
81+
}
82+
83+
// time correction for time scaling
84+
if (timeScalePrev !== 0)
85+
correction *= timing.timeScale / timeScalePrev;
86+
87+
if (timing.timeScale === 0)
88+
correction = 0;
89+
90+
timeScalePrev = timing.timeScale;
91+
92+
// fps counter
93+
frameCounter += 1;
94+
if (time - counterTimestamp >= 1000) {
95+
timing.fps = frameCounter * ((time - counterTimestamp) / 1000);
96+
counterTimestamp = time;
97+
frameCounter = 0;
98+
}
99+
100+
Events.trigger(engine, 'tick', event);
101+
102+
// if world has been modified, clear the render scene graph
103+
if (engine.world.isModified)
104+
engine.render.controller.clear(engine.render);
105+
106+
// update
107+
Engine.update(engine, delta, correction);
108+
109+
// render
110+
Engine.render(engine);
111+
112+
Events.trigger(engine, 'afterTick', event);
113+
})();
114+
};
115+
116+
/**
117+
* Ends execution of `Runner.run` on the given `engine`, by canceling the animation frame request event loop.
118+
* If you wish to only temporarily pause the engine, see `engine.enabled` instead.
119+
* @method stop
120+
* @param {engine} engine
121+
*/
122+
Runner.stop = function(engine) {
123+
_cancelAnimationFrame(engine.timing.frameRequestId);
124+
};
125+
126+
})();

src/module/Outro.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ World.addBody = Composite.addBody;
77
World.addConstraint = Composite.addConstraint;
88
World.clear = Composite.clear;
99

10+
Engine.run = Runner.run;
11+
1012
// exports
1113

1214
Matter.Body = Body;
@@ -36,6 +38,7 @@ Matter.Render = Render;
3638
Matter.RenderPixi = RenderPixi;
3739
Matter.Events = Events;
3840
Matter.Query = Query;
41+
Matter.Runner = Runner;
3942

4043
// CommonJS module
4144
if (typeof exports !== 'undefined') {

0 commit comments

Comments
 (0)