summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--starling/kernel.scm25
1 files changed, 18 insertions, 7 deletions
diff --git a/starling/kernel.scm b/starling/kernel.scm
index 193eb7b..25e92b6 100644
--- a/starling/kernel.scm
+++ b/starling/kernel.scm
@@ -52,9 +52,11 @@
update-hz
window
gl-context
+ avg-frame-time
current-kernel
boot-kernel
elapsed-time
+ fps
current-scene
push-scene
pop-scene
@@ -75,6 +77,7 @@
(window #:accessor window)
(gl-context #:accessor gl-context)
(default-viewport #:accessor default-viewport)
+ (avg-frame-time #:accessor avg-frame-time #:init-form 0.0)
(controllers #:accessor controllers #:init-thunk make-hash-table)
(repl #:accessor repl)
;; The scene stack.
@@ -208,13 +211,18 @@
(gpu-reap!))
(define-method (render* (kernel <kernel>) alpha)
- ;; Switch to the null viewport to ensure that
- ;; the default viewport will be re-applied and
- ;; clear the screen.
- (gpu-state-set! *viewport-state* null-viewport)
- (with-viewport (default-viewport kernel)
- (next-method))
- (swap-gl-window (window kernel)))
+ (let ((start-time (elapsed-time)))
+ ;; Switch to the null viewport to ensure that
+ ;; the default viewport will be re-applied and
+ ;; clear the screen.
+ (gpu-state-set! *viewport-state* null-viewport)
+ (with-viewport (default-viewport kernel)
+ (next-method))
+ (swap-gl-window (window kernel))
+ ;; Compute FPS.
+ (set! (avg-frame-time kernel)
+ (+ (* (- (elapsed-time) start-time) 0.1)
+ (* (avg-frame-time kernel) 0.9)))))
(define-method (on-error (kernel <kernel>) stack key args)
(if (developer-mode? kernel)
@@ -227,6 +235,9 @@
(define (elapsed-time)
(sdl-ticks))
+(define-method (fps kernel)
+ (/ 1000.0 (avg-frame-time kernel)))
+
(define-method (boot-kernel (kernel <kernel>) (scene <scene>))
(sdl-init)
;; This will throw an error if any audio subsystem is unavailable,