Modifies frame-dropping behavior.
authordsc <david.schoonover@gmail.com>
Wed, 2 Feb 2011 19:48:25 +0000 (11:48 -0800)
committerdsc <david.schoonover@gmail.com>
Wed, 2 Feb 2011 19:48:25 +0000 (11:48 -0800)
src/ezl/loop/eventloop.cjs

index 02f1406..820a49b 100644 (file)
@@ -4,7 +4,6 @@ var Y = require('Y').Y
 ,   op = require('Y/op')
 ,   Emitter = require('Y/modules/y.event').Emitter
 
-,   MIN_TICK_INTERVAL = 0 // ms
 ,   NUM_SAMPLES = 33
 ,
 
@@ -25,6 +24,7 @@ Emitter.subclass('EventLoop', {
     
     timer     : null,
     running   : false,
+    dropped   : 0,    // Frames dropped since last tick
     times     : null, // Last `samples` frame durations
     
     
@@ -66,6 +66,7 @@ Emitter.subclass('EventLoop', {
         if (this.running) return;
         
         this.running = true;
+        this.dropped = 0;
         this.now = new Date().getTime() - (this.elapsedAtStop || this.targetTime);
         this.emit('start');
         
@@ -89,7 +90,13 @@ Emitter.subclass('EventLoop', {
     sleepMinus : function sleepMinus(tFrame){
         if (this.timer !== null) return;
         
-        var tickInt = Math.max(MIN_TICK_INTERVAL, this.targetTime - tFrame);
+        var tt = this.targetTime
+        ,   tickInt = tt - tFrame;
+        if ( tickInt <= 0 ) {
+            this.dropped = Math.floor(tFrame / tt);
+            tickInt = tt - (tFrame % tt);
+        }
+        
         this.timer = setTimeout(this.tick, tickInt);
     },
     
@@ -108,7 +115,8 @@ Emitter.subclass('EventLoop', {
         this.emit('tick', this, {
             'now'     : this.clock,
             'elapsed' : this.perceived,
-            'ticks'   : ++this.ticks
+            'ticks'   : ++this.ticks,
+            'dropped' : this.dropped
         });
         
         var tFrame = this.tFrame = new Date().getTime() - this.now;