# HG changeset patch # User Steve Losh # Date 1461882587 0 # Node ID bd237d342ac4eafa8fb72512dbda40d789708b65 # Parent b4b4043dd88a5e2df91c5d92303c18a5d55e26ba Clean up the FPS measuring stuff diff -r b4b4043dd88a -r bd237d342ac4 package.lisp --- a/package.lisp Sun Apr 24 21:46:22 2016 +0000 +++ b/package.lisp Thu Apr 28 22:29:47 2016 +0000 @@ -103,7 +103,7 @@ #:coding-math.quickutils #:coding-math.utils) (:export - #:calc-fps + #:with-fps #:draw-fps)) (defpackage #:coding-math diff -r b4b4043dd88a -r bd237d342ac4 src/fps.lisp --- a/src/fps.lisp Sun Apr 24 21:46:22 2016 +0000 +++ b/src/fps.lisp Thu Apr 28 22:29:47 2016 +0000 @@ -4,18 +4,36 @@ (defvar *last-draw* 0) (defvar *fps* 0.0) (defvar *mspf* 0.0) +(defvar *frame* 0) + + +(defparameter *rolling-average* 0.0) +(defparameter *rolling-average-count* 10) -(defun calc-fps (frames) - (let* ((current-draw (get-internal-real-time)) - (elapsed (float (/ (- current-draw *last-draw*) - internal-time-units-per-second)))) - (setf *last-draw* current-draw) - (setf *mspf* (* 1000 (/ elapsed frames))) - (setf *fps* (* frames (/ 1 elapsed))))) +(defun update-average (frame-time) + (setf *rolling-average* + (/ (+ frame-time + (* *rolling-average* *rolling-average-count*)) + (1+ *rolling-average-count*)))) + +(defun update-fps () + (setf *mspf* (* 1000.0 + (/ *rolling-average* + internal-time-units-per-second)) + *fps* (/ 1000.0 *mspf*))) (defun draw-fps () - (text (format nil "MSPF: ~,1F" *mspf*) 0 0) - (text (format nil "FPS: ~,1F" *fps*) 0 20)) + (text (format nil "MSPF: ~,2F" *mspf*) 0 0) + (text (format nil "PFPS: ~,2F" *fps*) 0 20)) +(defmacro with-fps (&body body) + (let ((start (gensym "start"))) + `(let ((,start (get-internal-real-time))) + ,@body + (update-average (- (get-internal-real-time) ,start)) + (draw-fps) + (incf *frame*) + (when (dividesp *frame* 15) + (update-fps))))) diff -r b4b4043dd88a -r bd237d342ac4 src/main.lisp --- a/src/main.lisp Sun Apr 24 21:46:22 2016 +0000 +++ b/src/main.lisp Thu Apr 28 22:29:47 2016 +0000 @@ -44,37 +44,32 @@ :height *height* :debug :scancode-d) ((mouse) - (frame) (r) (mr) ) - (background (gray 1)) - (incf frame) - ;; + (with-fps + (background (gray 1)) + ;; - (setf (getf mr :x) (getf mouse :x)) - (setf (getf mr :y) (getf mouse :y)) + (setf (getf mr :x) (getf mouse :x)) + (setf (getf mr :y) (getf mouse :y)) - (with-pen (make-pen :stroke (gray 0.5) - :fill (cond - ((rects-collide-p r mr) - (rgb 0 0 0.7 0.5)) - (t (gray 0.9)))) - (draw-rect r) - (draw-rect mr)) - - ;; - (when (zerop (mod frame 20)) - (calc-fps 20)) - (draw-fps)) + (with-pen (make-pen :stroke (gray 0.5) + :fill (cond + ((rects-collide-p r mr) + (rgb 0 0 0.7 0.5)) + (t (gray 0.9)))) + (draw-rect r) + (draw-rect mr)) + ;; + )) (defun make-cm () (make-sketch 'cm (mouse (list :x 0 :y 0)) - (frame 1) (r (list :x 20 :y 50 :width (- *width* 40) :height 30)) - (mr (list :x 300 :y 300 :width 90 :height 90)) - )) + (mr (list :x 300 :y 300 :width 90 :height 90)))) + ;;;; Mouse