Frame rate measurer deleted

This commit is contained in:
Mal 2024-04-04 21:48:01 +02:00
parent 8780d57df2
commit 3d4235766d
3 changed files with 4 additions and 107 deletions

View File

@ -1,84 +0,0 @@
import FrameRateMeasuredEvent from "./events/FrameRateMeasuredEvent.js";
export default class FrameRateMeasurer
{
constructor(rounds = 30)
{
this.rounds = rounds;
this.round = 0;
this.lastTimestamp = undefined;
this.frameDurations = [];
window.requestAnimationFrame(
(timestamp) => {
this.measureFrameRate(timestamp);
}
);
}
measureFrameRate(timestamp)
{
if (this.lastTimestamp !== undefined) {
this.round++;
this.frameDurations.push(timestamp - this.lastTimestamp);
this.lastTimestamp = timestamp;
if (this.round < this.rounds) {
window.requestAnimationFrame(
(timestamp) => {
this.measureFrameRate(timestamp);
}
);
} else {
window.dispatchEvent(new FrameRateMeasuredEvent(this.getAverageFrameRate()));
}
} else {
this.round = 0;
this.lastTimestamp = timestamp;
window.requestAnimationFrame(
(timestamp) => {
this.measureFrameRate(timestamp);
}
);
}
this.lastTimestamp = timestamp
}
getAverageFrameRate()
{
let frameDurationSum = 0;
this.frameDurations.forEach(
(duration) => {
frameDurationSum += duration;
}
);
let durationAverage = frameDurationSum / this.frameDurations.length;
return 1000 / durationAverage;
}
getFrameRate()
{
const DEFAULT_FRAME_RATES = [30, 60, 120];
let averageFrameRate = this.getAverageFrameRate();
let closestDistance = {frameRate: 0, distance: 99999};
DEFAULT_FRAME_RATES.forEach(
(frameRate) => {
let distance = Math.abs(frameRate - averageFrameRate);
if (closestDistance.distance > distance) {
closestDistance.frameRate = frameRate;
closestDistance.distance = distance;
}
}
);
return closestDistance.frameRate;
}
}

View File

@ -8,8 +8,6 @@ import UserInterface from "./ui/UserInterface.js";
import Key from "./Key.js"; import Key from "./Key.js";
import Setting from "./Setting.js"; import Setting from "./Setting.js";
import {EventBus} from "./events/EventBus.js"; import {EventBus} from "./events/EventBus.js";
import InterfaceEvent from "./events/InterfaceEvent.js";
import FrameRateMeasurer from "./FrameRateMeasurer.js";
export class Game export class Game
@ -18,8 +16,8 @@ export class Game
constructor(level) { constructor(level) {
this.level = level; this.level = level;
this.fps = 0; this.fps = 60;
this.frameDuration = 0; this.frameDuration = 1000.0 / this.fps;
this.lastRendered = undefined; this.lastRendered = undefined;
this.lastTimestamp = undefined; this.lastTimestamp = undefined;
this.canvas = document.getElementById('canvas'); this.canvas = document.getElementById('canvas');
@ -216,17 +214,9 @@ export class Game
this.architecture.setMovableToStartPosition(this.mrCroc); this.architecture.setMovableToStartPosition(this.mrCroc);
this.architecture.setMovableToTargetPosition(this.gisela); this.architecture.setMovableToTargetPosition(this.gisela);
EventBus.addEventListener( this.isPaused = false;
InterfaceEvent.FRAME_RATE_MEASURED,
(event) => {
this.isPaused = false;
this.fps = event.frameRate;
this.frameDuration = 1000.0 / this.fps;
window.requestAnimationFrame(loopFunction);
}
);
new FrameRateMeasurer(); window.requestAnimationFrame(loopFunction);
} }
isChromeBrowser() isChromeBrowser()

View File

@ -1,9 +0,0 @@
import InterfaceEvent from "./InterfaceEvent.js";
export default class FrameRateMeasuredEvent extends Event
{
constructor(frameRate) {
super(InterfaceEvent.FRAME_RATE_MEASURED);
this.frameRate = frameRate;
}
}