Move clock utility to time.lisp
This commit is contained in:
parent
53de3d0cf6
commit
d6af14d552
4 changed files with 49 additions and 52 deletions
|
|
@ -7,6 +7,7 @@
|
|||
(:nicknames :core)
|
||||
(:use-reexport
|
||||
:stoe/core/utils
|
||||
:stoe/core/time
|
||||
:stoe/core/thread
|
||||
:stoe/core/containers
|
||||
:stoe/core/modules
|
||||
|
|
|
|||
46
core/time.lisp
Normal file
46
core/time.lisp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#|
|
||||
This file is a part of stoe project.
|
||||
Copyright (c) 2014 Renaud Casenave-Péré (renaud@casenave-pere.fr)
|
||||
|#
|
||||
|
||||
(uiop:define-package :stoe/core/time
|
||||
(:use :cl)
|
||||
(:export #:clock #:make-clock #:update-clock #:clock-delta))
|
||||
(in-package :stoe/core/time)
|
||||
|
||||
(defun get-current-time ()
|
||||
"Return the current time in seconds and microseconds."
|
||||
#+sbcl
|
||||
(multiple-value-bind (sec usec) (sb-ext:get-time-of-day)
|
||||
(+ (* sec 1000000) usec))
|
||||
#-sbcl
|
||||
(let* ((time (get-internal-real-time))
|
||||
(sec (/ time internal-time-units-per-second))
|
||||
(usec (* time (/ 1000000 internal-time-units-per-second))))
|
||||
(+ (* sec 1000000) usec)))
|
||||
|
||||
(defclass clock ()
|
||||
((current-time :initarg :time)
|
||||
(last-time :initarg :last-time)
|
||||
(delta-time :initform nil)
|
||||
(scale :initarg :scale)
|
||||
(pausep :initarg :pause)))
|
||||
|
||||
(defun make-clock (&optional (time 0 timep) (scale 1.0) pause)
|
||||
(unless timep
|
||||
(setf time (get-current-time)))
|
||||
(make-instance 'clock :time time :last-time time :scale scale :pause pause))
|
||||
|
||||
(defun update-clock (clock &optional (delta 0 deltap))
|
||||
(with-slots (current-time last-time delta-time scale pausep) clock
|
||||
(setf delta-time nil)
|
||||
(unless pausep
|
||||
(setf last-time current-time)
|
||||
(if deltap
|
||||
(incf current-time (* delta scale))
|
||||
(setf current-time (get-current-time))))))
|
||||
|
||||
(defun clock-delta (clock)
|
||||
(with-slots (current-time last-time delta-time) clock
|
||||
(or delta-time
|
||||
(setf delta-time (- current-time last-time)))))
|
||||
|
|
@ -10,9 +10,6 @@
|
|||
#:progress-step
|
||||
#:loop-with-progress
|
||||
#:add-hook #:remove-hook #:run-hook
|
||||
#:update-current-time #:get-delta-time
|
||||
#:make-clock #:clock-time #:clock-delta
|
||||
#:update-clock #:compare-clocks
|
||||
#:shared-object #:refcount #:inc-ref #:dec-ref
|
||||
#:error-implementation-unsupported
|
||||
#:get-command-line-option
|
||||
|
|
@ -40,7 +37,7 @@
|
|||
(if source (rec source nil) nil)))
|
||||
|
||||
(defmacro restartable (&body body)
|
||||
"Provide a Continue restart unless `unprotected' is t."
|
||||
"Provide a Continue restart."
|
||||
`(restart-case
|
||||
(progn
|
||||
,@body)
|
||||
|
|
@ -79,54 +76,6 @@
|
|||
hook)
|
||||
result))
|
||||
|
||||
(defun get-current-time ()
|
||||
"Return the current time in seconds and microseconds."
|
||||
#+sbcl (sb-ext:get-time-of-day)
|
||||
#-sbcl
|
||||
(let* ((time (get-internal-real-time))
|
||||
(sec (/ time internal-time-units-per-second))
|
||||
(usec (* time (/ 1000000 internal-time-units-per-second))))
|
||||
(values sec usec)))
|
||||
|
||||
(let ((last-time (cons 0 0))
|
||||
(current-time (cons 0 0)))
|
||||
|
||||
(defun update-current-time ()
|
||||
"Update the cached time in seconds and microseconds."
|
||||
(setf (car last-time) (car current-time))
|
||||
(setf (cdr last-time) (cdr current-time))
|
||||
(multiple-value-bind (sec usec) (get-current-time)
|
||||
(setf (car current-time) sec)
|
||||
(setf (cdr current-time) usec)))
|
||||
|
||||
(defun get-delta-time ()
|
||||
"Return the difference between the last two cached timers."
|
||||
(+ (* (- (car current-time) (car last-time)) 1000000)
|
||||
(- (cdr current-time) (cdr last-time)))))
|
||||
|
||||
(defstruct (clock (:constructor %make-clock))
|
||||
(time 0)
|
||||
(last-time 0)
|
||||
(scale 1.0)
|
||||
(paused nil))
|
||||
|
||||
(defun make-clock (&optional (time 0) (scale 1.0) (paused nil))
|
||||
"Create a new clock instance with specified parameters or using reasonable defaults."
|
||||
(%make-clock :time time :last-time time :scale scale :paused paused))
|
||||
|
||||
(defun update-clock (clock &optional delta-time)
|
||||
"Update clock using `sec' and `usec' values passed as parameter."
|
||||
(unless (clock-paused clock)
|
||||
(setf (clock-last-time clock) (clock-time clock))
|
||||
(incf (clock-time clock) (* (or delta-time (get-delta-time)) (clock-scale clock)))))
|
||||
|
||||
(defun clock-delta (clock)
|
||||
(- (clock-time clock) (clock-last-time clock)))
|
||||
|
||||
(defun compare-clocks (clock1 clock2)
|
||||
"Return the difference between `clock1' and `clock2'."
|
||||
(- (clock-time clock1) (clock-time clock2)))
|
||||
|
||||
(defclass shared-object ()
|
||||
((refcount :initform 0 :reader refcount)))
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
(uiop:define-package :stoe/engine/render
|
||||
(:use :cl :maths :shader
|
||||
:stoe/core/utils
|
||||
:stoe/core/time
|
||||
:stoe/core/modules
|
||||
:stoe/engine/viewport
|
||||
:stoe/engine/scene))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue