Skip to content

Commit 8a2903e

Browse files
authored
Merge pull request #7 from eonarheim/master
adjust excalibur benchmark to be more accurate to it's performance
2 parents b2fe94e + 3f93488 commit 8a2903e

File tree

2 files changed

+49
-53
lines changed

2 files changed

+49
-53
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"dependencies": {
1717
"babylonjs": "^8.23.1",
18-
"excalibur": "^0.30.3",
18+
"excalibur": "0.31.0",
1919
"fpsmeter": "^0.3.1",
2020
"hilojs": "^2.0.2",
2121
"kaboom": "^2000.2.10",

src/scripts/excalibur.js

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,81 +6,77 @@ const spriteImage = new ex.ImageSource('sprite.png');
66
const loader = new ex.Loader([spriteImage]);
77

88
export class Scene extends ex.Scene {
9-
onInitialize() {
10-
this.sprite = spriteImage.toSprite();
11-
}
12-
9+
particles = [];
1310
onActivate(ctx) {
14-
this.engine = ctx.data.engine;
11+
this.benchmarkEngine = ctx.data.engine;
1512

1613
this.clear();
1714

1815
// Particle creation
19-
const particles = Array.from({ length: this.engine.count });
16+
this.particles = new Array(this.benchmarkEngine.count);
2017
const rnd = [1, -1];
18+
const spriteImage = new ex.ImageSource('sprite.png');
19+
spriteImage.load();
20+
const sprite = ex.Sprite.from(spriteImage);
2121

22-
const graphicsGroup = new ex.GraphicsGroup({ members: [] });
22+
const random = new ex.Random();
23+
let randomCircles = [];
2324

24-
for (let i = 0; i < this.engine.count; i++) {
25+
for (let i = 0; i < 80; i++) {
26+
const size = 10 + Math.random() * 80;
27+
const x = Math.random() * this.benchmarkEngine.width;
28+
const y = Math.random() * (this.benchmarkEngine.height - size);
29+
30+
const circle = new ex.Circle({
31+
x: x,
32+
y: y,
33+
radius: size,
34+
color:
35+
this.benchmarkEngine.type === 'stroke'
36+
? ex.Color.Transparent
37+
: ex.Color.fromRGB(255, 255, 255),
38+
strokeColor:
39+
this.benchmarkEngine.type === 'stroke'
40+
? ex.Color.fromRGB(255, 255, 255)
41+
: ex.Color.fromRGB(0, 0, 0),
42+
strokeWidth: this.benchmarkEngine.type === 'stroke' ? 1 : undefined,
43+
});
44+
randomCircles.push(circle);
45+
}
46+
for (let i = 0; i < this.benchmarkEngine.count; i++) {
2547
const size = 10 + Math.random() * 80;
26-
const x = Math.random() * this.engine.width;
27-
const y = Math.random() * (this.engine.height - size);
48+
const x = Math.random() * this.benchmarkEngine.width;
49+
const y = Math.random() * (this.benchmarkEngine.height - size);
2850
const [dx, dy] = [
2951
3 * Math.random() * rnd[Math.floor(Math.random() * 2)],
3052
3 * Math.random() * rnd[Math.floor(Math.random() * 2)],
3153
];
3254

33-
if (this.engine.type === 'sprite') {
34-
graphicsGroup.members.push({
35-
graphic: this.sprite,
36-
offset: ex.vec(x, y),
37-
});
55+
this.particles[i] = { x, y, size: size, dx, dy, graphics: null };
56+
if (this.benchmarkEngine.type === 'sprite') {
57+
this.particles[i].graphics = sprite;
3858
} else {
39-
const circle = new ex.Circle({
40-
x: x,
41-
y: y,
42-
radius: size,
43-
color:
44-
this.engine.type === 'stroke'
45-
? ex.Color.Transparent
46-
: ex.Color.fromRGB(255, 255, 255),
47-
strokeColor:
48-
this.engine.type === 'stroke'
49-
? ex.Color.fromRGB(255, 255, 255)
50-
: ex.Color.fromRGB(0, 0, 0),
51-
strokeWidth: this.engine.type === 'stroke' ? 1 : undefined,
52-
});
53-
graphicsGroup.members.push({
54-
graphic: circle,
55-
offset: ex.vec(x, y),
56-
});
59+
this.particles[i].graphics = random.pickOne(randomCircles);
5760
}
58-
59-
particles[i] = { x, y, size: size, dx, dy, el: graphicsGroup.members[i] };
6061
}
61-
62-
const screenElement = new ex.ScreenElement();
63-
64-
screenElement.graphics.use(graphicsGroup);
65-
66-
this.add(screenElement);
67-
68-
this.particles = particles;
6962
}
70-
71-
onPostUpdate(engine) {
72-
for (let i = 0; i < this.engine.count; i++) {
63+
onPostUpdate() {
64+
for (let i = 0; i < this.benchmarkEngine.count; i++) {
7365
const r = this.particles[i];
7466
r.x -= r.dx;
7567
r.y -= r.dy;
7668
if (r.x + r.size < 0) r.dx *= -1;
7769
else if (r.y + r.size < 0) r.dy *= -1;
78-
if (r.x > engine.screen.drawWidth) r.dx *= -1;
79-
else if (r.y > engine.screen.drawHeight) r.dy *= -1;
80-
r.el.offset.setTo(r.x, r.y);
70+
if (r.x > this.engine.screen.drawWidth) r.dx *= -1;
71+
else if (r.y > this.engine.screen.drawHeight) r.dy *= -1;
72+
}
73+
this.benchmarkEngine.fpsmeter.tick();
74+
}
75+
onPostDraw(ctx) {
76+
for (let i = 0; i < this.benchmarkEngine.count; i++) {
77+
const particle = this.particles[i];
78+
particle.graphics.draw(ctx, particle.x, particle.y);
8179
}
82-
83-
this.engine.fpsmeter.tick();
8480
}
8581
}
8682

@@ -103,9 +99,9 @@ class ExcaliburEngine extends Engine {
10399
width: this.width,
104100
height: this.height,
105101
canvasElement: this.canvas,
102+
physics: false, // this benchmark is only doing drawing
106103
backgroundColor: ex.Color.fromRGB(26, 26, 26),
107104
suppressPlayButton: true,
108-
physics: { enabled: false },
109105
scenes: { scene: Scene },
110106
});
111107
this.game = game;

0 commit comments

Comments
 (0)