if(typeof(CanvasGame) != "object"){ CanvasGame = {}; }
CanvasGame.FPSEngine = {
props:{
current:0,
max:60,
paused:false,
canvasContext:null,
lastDraw:(new Date)*1 - 1;
lastTenFPS:[],
currentKey:0
},
pause:function(){
CanvasGame.FPSEngine.props.paused = true;
},
resume:function(){
CanvasGame.FPSEngine.props.paused = false;
CanvasGame.FPSEngine.props.lastDraw = (new Date)*1 - 1;
CanvasGame.FPSEngine.callDraw();
},
setMaximumFPS:function(fpsMax){
CanvasGame.FPSEngine.props.max = fpsMax;
},
redrawTime:function(){
return (1000 / CanvasGame.FPSEngine.getAverage());
},
setLast:function(){
CanvasGame.FPSEngine.props.lastTenFPS[CanvasGame.FPSEngine.props.currentKey] = CanvasGame.FPSEngine.props.current;
CanvasGame.FPSEngine.props.currentKey++;
if(CanvasGame.FPSEngine.props.currentKey > 10){
CanvasGame.FPSEngine.props.currentKey = 0;
}
},
getAverage:function(){
var total = 0;
var count = 0;
for(var id in CanvasGame.FPSEngine.props.lastTenFPS){
total += CanvasGame.FPSEngine.props.lastTenFPS[id]
count++;
}
return (total / count);
},
setCanvasContext:function(context){
CanvasGame.FPSEngine.props.canvasContext = context;
},
reDraw:function(){
CanvasGame.FPSEngine.props.current = ((now=new Date) - CanvasGame.FPSEngine.props.lastDraw);
CanvasGame.FPSEngine.setLast();
if(CanvasGame.FPSEngine.props.max > 0 && CanvasGame.FPSEngine.props.current > CanvasGame.FPSEngine.props.max){
CanvasGame.FPSEngine.props.current = CanvasGame.FPSEngine.props.max;
}
CanvasGame.FPSEngine.props.lastDraw = (new Date)*1 - 1;
if(!CanvasGame.FPSEngine.props.paused)
setTimeout("CanvasGame.FPSEngine.callDraw()", CanvasGame.FPSEngine.redrawTime());
},
callDraw:function(){
CanvasGame.FPSEngine.draw(CanvasGame.FPSEngine.props.canvasContext);
CanvasGame.FPSEngine.reDraw();
},
draw:function(canvas){
}
} |