src/fps.lisp @ 9122a5749085

Episode 35: Intro to Fractals
author Steve Losh <steve@stevelosh.com>
date Tue, 05 Jul 2016 13:58:12 +0000
parents bd237d342ac4
children e05ab7ec7e6c
(in-package #:coding-math.fps)

;;;; FPS
(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 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: ~,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)))))