changeset 78:4c0f2af742ba

Added timestamp to the layer context.
author Mikhail Kryshen <mikhail@kryshen.net>
date Wed, 01 Sep 2010 21:13:55 +0400
parents 1ca7872b889b
children 5fd50e400124
files src/net/kryshen/indyvon/core.clj src/net/kryshen/indyvon/layers.clj
diffstat 2 files changed, 23 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/net/kryshen/indyvon/core.clj	Tue Aug 31 02:20:24 2010 +0400
+++ b/src/net/kryshen/indyvon/core.clj	Wed Sep 01 21:13:55 2010 +0400
@@ -44,7 +44,12 @@
 
 (def ^Shape *clip*)
 
-(def *root*)
+(def ^{:doc "The root (background) layer of the scene."}
+     *root*)
+
+(def ^{:doc "Time in nanoseconds when the rendering of the current
+             frame starts."}
+     *time*)
 
 (def *event-dispatcher*)
 
@@ -326,7 +331,8 @@
                *event-dispatcher* event-dispatcher
                *width* width
                *height* height
-               *clip* (Rectangle2D$Double. 0 0 width height)]
+               *clip* (Rectangle2D$Double. 0 0 width height)
+               *time* (System/nanoTime)]
        ;; (.setRenderingHint graphics
        ;;                    RenderingHints/KEY_INTERPOLATION
        ;;                    RenderingHints/VALUE_INTERPOLATION_BILINEAR)
--- a/src/net/kryshen/indyvon/layers.clj	Tue Aug 31 02:20:24 2010 +0400
+++ b/src/net/kryshen/indyvon/layers.clj	Wed Sep 01 21:13:55 2010 +0400
@@ -284,27 +284,30 @@
           (.drawRect *graphics* x y w h))))
    width height))
 
+(defn- fps-label [text]
+  (padding (label text :right :bottom) 5))
+
 (defn fps-layer
   "Creates layer that draws content and displays
    the frames per seconds rate." 
   [content]
-  (let [update-interval 0.1
+  (let [update-interval 2E8 ; 0.2 s in nanoseconds
         frames (ref 0)
-        prev-time (ref 0)
-        display (ref nil)]
+        prev-time (ref nil)
+        display (ref (fps-label "fps n/a"))]
     (decorate-layer content [_]
        (draw! content)
        (draw!
         (dosync
-         (alter frames + 1)
-         (let [time (System/nanoTime)
-               elapsed (/ (- time @prev-time) 1E9)
-               fps (/ @frames elapsed)]
-           (when (or (> elapsed update-interval) (not @display))
-             (ref-set display
-              (border (label (format "%.1f" fps) :right :bottom) 0 5))
-             (ref-set frames 0)
-             (ref-set prev-time time)))
+         (alter frames inc)
+         (if @prev-time
+           (let [elapsed (- *time* @prev-time)]
+             (when (> elapsed update-interval)
+               (let [fps (/ @frames (/ elapsed 1E9))]
+                 (ref-set display (fps-label (format "%.1f" fps)))
+                 (ref-set frames 0)
+                 (ref-set prev-time *time*))))
+           (ref-set prev-time *time*))
          @display)))))
 
 ;;