1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2017 by Javier Sancho Fernandez <jsf at jsancho dot org>
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;;; GNU General Public License for more details.
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 (define-module (gacela react)
19 #:use-module (gacela scene)
26 (define time-stack '())
28 (define (push-init-time init-time)
29 (set! time-stack (cons init-time time-stack)))
31 (define (pop-init-time)
32 (set! time-stack (cdr time-stack)))
34 (define (head-init-time)
40 (define-syntax context
42 (define (transform vars)
43 (let ((datum (map (lambda (v) (syntax->datum v)) vars)))
44 (map (lambda (v) (datum->syntax x v))
46 (cond ((and (= (length v) 4) (equal? (caddr v) '<=))
47 (list (car v) (cadr v) (cadddr v)))
49 (list (car v) (cadr v) 0))
54 ((_ ((variable value <= init-value)
59 (let* ((variable init-value)
61 (?init-time (get-internal-real-time)))
63 (push-init-time ?init-time)
69 ((_ ((variable value value* ...)
72 (with-syntax ((((variable1 value1 init-value1) ...) (transform #'((variable value value* ...) ...))))
73 #'(context ((variable1 value1 <= init-value1)
78 ;; Rate based functions
80 (define (at-rate value)
81 (let ((delta (- (get-internal-real-time) (head-init-time))))
82 (* value (/ delta 1000.0))))