Skip to content

Commit

Permalink
fix: switch requestAnimationFrame to setInterval to handle monitors r…
Browse files Browse the repository at this point in the history
…efresh rates
  • Loading branch information
eugenioenko committed Aug 15, 2024
1 parent 7c98fc3 commit 37aab62
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 32 deletions.
19 changes: 3 additions & 16 deletions dist/firenice.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/firenice.min.js

Large diffs are not rendered by default.

17 changes: 2 additions & 15 deletions src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,16 @@ export class Game {
*/
constructor(canvas, resources) {
this.time = performance.now();
this.skip = 0;
this.engine = new Engine(canvas, resources);
this.levels = levels;
this.state = Consts.GameStatePlay;
this.engine.sound.soundtrack();
this.lastFrame = 0;
this.gameLoop = this.gameLoop_.bind(this); // jshint ignore:line
this.gameLoop();
setInterval(() => this.gameLoop(), 1000 / 60);
}

gameLoop_() {
const fps = Math.floor(1 / ((performance.now() - this.time) / 1000));
// skip every two frames if framerate is above 60Hz
if (fps > 90) {
this.skip += 1;
}
if (this.skip >= 2) {
this.skip = 0;
window.requestAnimationFrame(this.gameLoop);
return;
}
switch (this.state) {
case Consts.GameStateIntro:
this.doIntro();
Expand All @@ -42,9 +32,6 @@ export class Game {
this.engine.move();
break;
}

this.time = performance.now();
window.requestAnimationFrame(this.gameLoop);
}

createIntro() {
Expand Down

0 comments on commit 37aab62

Please sign in to comment.