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