mirror of
https://github.com/sehugg/8bitworkshop.git
synced 2024-06-02 12:41:30 +00:00
made AnimationTimer a class
This commit is contained in:
parent
b6db88bc97
commit
d575cda583
85
src/emu.ts
85
src/emu.ts
|
@ -209,59 +209,70 @@ export class RAM {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export var AnimationTimer = function(frequencyHz:number, callback:() => void) {
|
export class AnimationTimer {
|
||||||
var intervalMsec = 1000.0 / frequencyHz;
|
|
||||||
var running : boolean = false;
|
|
||||||
var pulsing : boolean = false;
|
|
||||||
var lastts = 0;
|
|
||||||
var useReqAnimFrame = false; //TODO window.requestAnimationFrame ? (frequencyHz>40) : false;
|
|
||||||
var nframes, startts; // for FPS calc
|
|
||||||
this.frameRate = frequencyHz;
|
|
||||||
|
|
||||||
function scheduleFrame(msec:number) {
|
callback;
|
||||||
if (useReqAnimFrame)
|
running : boolean = false;
|
||||||
window.requestAnimationFrame(nextFrame);
|
pulsing : boolean = false;
|
||||||
|
lastts = 0;
|
||||||
|
useReqAnimFrame = false; //TODO window.requestAnimationFrame ? (frequencyHz>40) : false;
|
||||||
|
nframes;
|
||||||
|
startts; // for FPS calc
|
||||||
|
frameRate;
|
||||||
|
intervalMsec;
|
||||||
|
|
||||||
|
constructor(frequencyHz:number, callback:() => void) {
|
||||||
|
this.frameRate = frequencyHz;
|
||||||
|
this.intervalMsec = 1000.0 / frequencyHz;
|
||||||
|
this.callback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
scheduleFrame(msec:number) {
|
||||||
|
if (this.useReqAnimFrame)
|
||||||
|
window.requestAnimationFrame(this.nextFrame.bind(this));
|
||||||
else
|
else
|
||||||
setTimeout(nextFrame, msec);
|
setTimeout(this.nextFrame.bind(this), msec);
|
||||||
}
|
}
|
||||||
function nextFrame(ts:number) {
|
|
||||||
|
nextFrame(ts:number) {
|
||||||
if (!ts) ts = Date.now();
|
if (!ts) ts = Date.now();
|
||||||
if (ts - lastts < intervalMsec*10) {
|
if (ts - this.lastts < this.intervalMsec*10) {
|
||||||
lastts += intervalMsec;
|
this.lastts += this.intervalMsec;
|
||||||
} else {
|
} else {
|
||||||
lastts = ts + intervalMsec; // frames skipped, catch up
|
this.lastts = ts + this.intervalMsec; // frames skipped, catch up
|
||||||
}
|
}
|
||||||
if (!useReqAnimFrame || lastts - ts > intervalMsec/2) {
|
if (!this.useReqAnimFrame || this.lastts - ts > this.intervalMsec/2) {
|
||||||
if (running) {
|
if (this.running) {
|
||||||
callback();
|
this.callback();
|
||||||
}
|
}
|
||||||
if (nframes == 0) startts = ts;
|
if (this.nframes == 0)
|
||||||
if (nframes++ == 300) {
|
this.startts = ts;
|
||||||
console.log("Avg framerate: " + nframes*1000/(ts-startts) + " fps");
|
if (this.nframes++ == 300) {
|
||||||
|
console.log("Avg framerate: " + this.nframes*1000/(ts-this.startts) + " fps");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (running) {
|
if (this.running) {
|
||||||
scheduleFrame(lastts - ts);
|
this.scheduleFrame(this.lastts - ts);
|
||||||
} else {
|
} else {
|
||||||
pulsing = false;
|
this.pulsing = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.isRunning = function() {
|
isRunning() {
|
||||||
return running;
|
return this.running;
|
||||||
}
|
}
|
||||||
this.start = function() {
|
start() {
|
||||||
if (!running) {
|
if (!this.running) {
|
||||||
running = true;
|
this.running = true;
|
||||||
lastts = 0;
|
this.lastts = 0;
|
||||||
nframes = 0;
|
this.nframes = 0;
|
||||||
if (!pulsing) {
|
if (!this.pulsing) {
|
||||||
scheduleFrame(0);
|
this.scheduleFrame(0);
|
||||||
pulsing = true;
|
this.pulsing = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.stop = function() {
|
stop() {
|
||||||
running = false;
|
this.running = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user