changeset 1405:05e8d3ffa703

Change the changeScene to sleep less when the render speed is too slow.
author wycc
date Mon, 04 Apr 2011 11:59:19 +0800
parents c297ceec37f2
children 88c8c874f4b8
files nodejs/mbapp.js
diffstat 1 files changed, 32 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/nodejs/mbapp.js	Sun Apr 03 22:13:37 2011 +0800
+++ b/nodejs/mbapp.js	Mon Apr 04 11:59:19 2011 +0800
@@ -123,8 +123,15 @@
     this.keymap={};
     this.onKeyPress = null;
     this.svg = new svg.loadSVG(this.mb_rt,this.mb_rt.root,null);
-    this.frame_interval = 1000/24; // 12 frame per second
+    this.frame_interval = 1000/30; // 12 frame per second
     this.timer = null;
+    this._time = Date.now();
+}
+
+app.prototype.ts=function(m) {
+    var now = Date.now();
+    var t = now-this._time;
+    sys.puts("["+t+"] "+m);
 }
 app.prototype.loadSVG=function(fname) {
     this.svg.load(this.mb_rt,this.mb_rt.root,fname);
@@ -155,6 +162,7 @@
 }
 app.prototype.refresh=function() {
     this.mb_rt.redraw_changed();
+    //this.mb_rt.redraw_all();
     this.mb_rt.flush();
 }
 app.prototype.dump=function() {
@@ -176,6 +184,10 @@
     src.hide();
     // Duplicate the group
     var nodes = src.children;
+    if (src.dup) {
+        //src.dup.remove();
+        //src.dup = null;
+    }
     if (src.dup == null) {
         var dup = this.mb_rt.coord_new(src.parent);
 	for (i in nodes) {
@@ -277,7 +289,7 @@
         nth = this.svg.getFrameNumber(s);
 	if (nth == -1) return;
     }
-    sys.puts("goto to scene "+nth);
+    this.ts("goto to scene "+nth);
     this.currentScene = nth;
     var scenes = this.svg.scenes;
     if (scenes == null) return;
@@ -312,7 +324,9 @@
 	}
     }
     this.get(scenes[i].ref).hide();
+    this.ts("refresh");
     this.refresh();
+    this.ts("refresh done");
 }
 
 app.prototype.runToScene=function(s) {
@@ -334,17 +348,19 @@
     this.startScene = this.currentScene;
     this.starttime = Date.now();
     if (this.timer == null) {
-        this.timer = setTimeout(function() {
-	    self.skipFrame()
-        }, this.frame_interval);
+        this.skipFrame();
     }
 }
 
 app.prototype.skipFrame=function() {
     var self = this;
     if (this.currentScene != this.targetScene) {
-        var step = Math.round((Date.now() - this.starttime)/this.frame_interval)*this.skipdir;
+        var now = Date.now();
+        var step = Math.round((now - this.starttime)/this.frame_interval)*this.skipdir;
         nextframe = this.startScene + step
+        //nextframe = this.currentScene + this.skipdir;
+	this.ts("goto begin");
+	
 	if (this.skipdir>0) {
 	    if (nextframe > this.targetScene) 
 	        nextframe = this.targetScene;
@@ -352,14 +368,22 @@
 	    if (nextframe < this.targetScene)
 	        nextframe = this.targetScene;
 	}
-        this.changeScene(nextframe);
 	if (nextframe != this.targetScene) {
+	    var timegap = (nextframe-this.startScene)*this.skipdir*this.frame_interval+this.starttime - Date.now();
+	    sys.puts("goto "+timegap);
+	    if (timegap <200) {
+	        timegap = 0;
+	    } else {
+	    }
             this.timer = setTimeout(function() {
    	        self.skipFrame()
-	    }, this.frame_interval);
+	    }, timegap);
 	} else {
 	    this.timer = null;
 	}
+        this.changeScene(nextframe);
+	now = Date.now();
+	this.ts("goto end");
     }
 }