1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2013 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 engine)
19 #:use-module (gacela misc)
20 #:use-module (gacela system)
21 #:use-module (ice-9 receive)
22 #:use-module (ice-9 threads)
23 #:use-module (srfi srfi-9)
24 #:use-module (srfi srfi-9 gnu))
27 ;;; Engine Inner Properties
29 (define (default-step) 0.1)
31 (define (default-engine-inner-properties)
32 `(engine-inner-properties (step . ,(default-step))))
35 ;;; Engine definitions
37 (define-record-type engine
38 (make-engine-record entities mutex running-mutex systems)
40 (entities engine-entities set-engine-entities!)
41 (mutex engine-mutex set-engine-mutex!)
42 (running-mutex engine-running-mutex set-engine-running-mutex!)
43 (systems engine-systems set-engine-systems!))
45 (set-record-type-printer! engine
47 (format port "#<[engine] state: ~a, entities: ~a>"
48 (if (engine-running? record) "Running" "Stopped")
49 (entity-count (engine-entities record)))))
51 (define (make-engine . systems)
53 (make-entity-set (new-entity (default-engine-inner-properties)))
58 (define-syntax define-engine
62 (make-engine system ...)))))
64 (define (engine-running? engine)
65 (mutex-locked? (engine-running-mutex engine)))
74 (define (start-engine engine)
75 (cond ((not (engine-running? engine))
76 (with-mutex (engine-running-mutex engine)
78 (let ((t (current-utime))
81 (with-mutex (engine-mutex engine)
83 (lambda (s) (eval-system s engine))
84 (engine-systems engine))
85 (set! delay (- (inexact->exact (* (engine-property engine 'step) 1000000))
86 (- (current-utime) t)))
87 (set! halt (engine-stopping? engine #:clean #t)))
93 (define (eval-system system engine)
95 (lambda () (system (engine-entities engine)))
97 (let ((changes (car vals)))
98 (cond ((entities-changes? changes)
99 (set-engine-entities! engine
100 (modify-entities (engine-entities engine)
101 (get-entities-changes changes))))))
102 (apply values vals))))
104 (define-syntax with-engine
106 ((_ engine component-types form ...)
107 (with-mutex (engine-mutex engine)
108 (eval-system (make-system component-types form ...) engine)))))
110 (define (stop-engine engine)
111 (with-engine engine ()
114 (new-entity '(engine-halt . #t)))))
117 (define* (engine-stopping? engine #:key (clean #f))
118 (let ((halts (eval-system
119 (make-system ((halt (engine-halt))) halt)
121 (cond ((and clean (not (null? halts)))
123 (make-system () (entities-changes (map (lambda (h) (remove-entity (car h))) halts)))
125 (not (null? halts))))
134 (define (engine-property engine name)
136 (make-system ((props (engine-inner-properties)))
138 (assoc-ref (cdar props) 'engine-inner-properties)
142 (define (set-engine-property! engine name value)
144 (make-system ((props (engine-inner-properties)))
147 (set-entity (caar props)
149 (assoc-set! (cdar props) 'engine-inner-properties
150 (assoc-set! (assoc-ref (cdar props) 'engine-inner-properties)
156 (export engine-property
157 set-engine-property!)