]> git.jsancho.org Git - gacela.git/blob - src/engine.scm
2e3cbd32c1815358bef76f51890d122b96ab6d5f
[gacela.git] / src / engine.scm
1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2013 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
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.
8 ;;;
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.
13 ;;;
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/>.
16
17
18 (define-module (gacela engine)
19   #:use-module (gacela system)
20   #:use-module (ice-9 receive)
21   #:use-module (ice-9 threads)
22   #:use-module (srfi srfi-9)
23   #:use-module (srfi srfi-9 gnu))
24
25
26 ;;; Engine Properties
27
28 (define* (get-property property-path #:key (engine (current-engine)))
29   (let ((entities (get-entities-by-components (list (car property-path)) #:engine engine)))
30     (cond ((null? entities)
31            #f)
32           (else
33            (let loop ((property (get-component (car property-path) (car entities)))
34                       (path (cdr property-path)))
35              (cond ((or (null? path) (not property))
36                     property)
37                    (else
38                     (loop (assoc-ref property (car path)) (cdr path)))))))))
39
40 (define* (set-property! property-path value #:key (engine (current-engine)))
41   (define (loop property path)
42     (cond ((null? path)
43            value)
44           (else
45            (assoc-set! (or property '()) (car path) (loop (assoc-ref property (car path)) (cdr path))))))
46
47   (let ((entities (get-entities-by-components (list (car property-path)) #:engine engine)))
48     (cond ((null? entities)
49            (new-entity! `(,(car property-path) . ,(loop '() (cdr property-path)))))
50           (else
51            (set-entity-components! (get-key (car entities)) `(,(car property-path) . ,(loop (get-component (car property-path) (car entities)) (cdr property-path))))))))
52
53 (export get-property
54         set-property!)
55
56
57 ;;; Engine Inner Properties
58
59 (define (default-delay) 0.1)
60
61 (define (default-engine-inner-properties)
62   `(engine-inner-properties (delay . ,(default-delay))))
63
64
65 ;;; Engine definitions
66
67 (define-record-type engine
68   (make-engine-record entities mutex running-mutex system)
69   engine?
70   (entities engine-entities set-engine-entities!)
71   (mutex engine-mutex set-engine-mutex!)
72   (running-mutex engine-running-mutex set-engine-running-mutex!)
73   (system engine-system set-engine-system!))
74
75 (set-record-type-printer! engine
76   (lambda (record port)
77     (format port "#<[engine] state: ~a, entities: ~a>"
78             (if (engine-running? record) "Running" "Stopped")
79             (length (car (engine-entities record))))))
80
81 (define (make-engine . systems)
82   (make-engine-record
83    (receive (e c) ((new-entity (default-engine-inner-properties)) '() '())
84      (list e c))
85    (make-mutex)
86    (make-mutex)
87    (apply group-systems systems)))
88
89 (define-syntax define-engine
90   (syntax-rules ()
91     ((_ name system ...)
92      (define name
93        (make-engine system ...)))))
94
95 (define (engine-running? engine)
96   (mutex-locked? (engine-running-mutex engine)))
97
98 (export make-engine
99         define-engine
100         engine-running?)
101
102
103 ;;; Engine Access Protocol Interface
104
105 (define current-engine-mutex (make-mutex))
106 (define current-engine-list '())
107
108 (define (current-engine)
109   (with-mutex current-engine-mutex
110     (assoc-ref current-engine-list (current-thread))))
111
112 (define (set-current-engine! engine)
113   (with-mutex current-engine-mutex
114     (set! current-engine-list
115           (cond (engine
116                  (assoc-set! current-engine-list (current-thread) engine))
117                 (else
118                  (assoc-remove! current-engine-list (current-thread)))))))
119
120 (define* (get-entity key #:key (engine (current-engine)))
121   (assoc key (car (engine-entities engine))))
122
123 (define* (get-entities-by-components component-types #:key (engine (current-engine)))
124   (map (lambda (e)
125          (get-entity e #:engine engine))
126        (find-entities-by-components (cadr (engine-entities engine)) component-types)))
127
128 (define-syntax define-entity-setter
129   (syntax-rules ()
130     ((_ name! name)
131      (define (name! . args)
132        (let ((f (apply name args))
133              (engine (current-engine)))
134          (receive (e c r) (f (car (engine-entities engine)) (cadr (engine-entities engine)))
135            (set-engine-entities! engine (list e c))
136            r))))))
137
138 (define-entity-setter new-entity! new-entity)
139 (define-entity-setter remove-entity! remove-entity)
140 (define-entity-setter set-entity! set-entity)
141 (define-entity-setter set-entity-components! set-entity-components)
142 (define-entity-setter remove-entity-components! remove-entity-components)
143
144 (define-syntax with-engine
145   (syntax-rules ()
146     ((_ engine body ...)
147      (let ((old-engine (current-engine)))
148        (set-current-engine! engine)
149        (let ((res (with-mutex (engine-mutex engine)
150                     body
151                     ...)))
152          (set-current-engine! old-engine)
153          res)))))
154
155 (define (set-engine-systems! engine . systems)
156   (with-mutex (engine-mutex engine)
157     (set-engine-system! engine (apply group-systems systems))))
158
159 (export current-engine
160         set-current-engine!
161         get-entity
162         get-entities-by-components
163         new-entity!
164         remove-entity!
165         set-entity!
166         set-entity-components!
167         remove-entity-components!
168         with-engine
169         set-engine-systems!)
170
171
172 ;;; Engine execution
173
174 (define (start-engine engine)
175   (cond ((not (engine-running? engine))
176          (with-mutex (engine-running-mutex engine)
177            (let loop ()
178              (let ((delay 0))
179                (with-engine engine
180                  (receive (e c) ((apply (engine-system engine) (engine-entities engine)))
181                    (set-engine-entities! engine (list e c)))
182                  (set! delay (get-property '(engine-inner-properties delay))))
183                (usleep (inexact->exact (* delay 1000000))))
184              (if (not (engine-stopping? engine #:clean #t))
185                  (loop)))))))
186
187 (define (stop-engine engine)
188   (with-engine engine
189     (new-entity! '(engine-halt . #t)))
190   'engine-halt)
191
192 (define* (engine-stopping? engine #:key (clean #f))
193   (let ((halt #f))
194     (with-engine engine
195       (let halt-engine ((halts (get-entities-by-components '(engine-halt))))
196         (cond ((not (null? halts))
197                (set! halt #t)
198                (cond (clean
199                       (remove-entity! (caar halts))
200                       (halt-engine (cdr halts))))))))
201     halt))
202
203 (export start-engine
204         stop-engine
205         engine-stopping?)