summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2018-08-28 17:20:49 -0400
committerDavid Thompson <dthompson2@worcester.edu>2018-08-28 17:20:49 -0400
commit8dd4d148bde3a947bd312d5c222f680b16043ef1 (patch)
tree0db6f49a1223c73f1b2d8264d4a2fc0465ee2742
parent313b20c51185618e7e2a6a5ae105fe2f04612b92 (diff)
kernel: Add FPS tracking.
-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,