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 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))
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)
33 (let loop ((property (get-component (car property-path) (car entities)))
34 (path (cdr property-path)))
35 (cond ((or (null? path) (not property))
38 (loop (assoc-ref property (car path)) (cdr path)))))))))
40 (define* (set-property! property-path value #:key (engine (current-engine)))
41 (define (loop property path)
45 (assoc-set! (or property '()) (car path) (loop (assoc-ref property (car path)) (cdr path))))))
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)))))
51 (set-entity-components! (get-key (car entities)) `(,(car property-path) . ,(loop (get-component (car property-path) (car entities)) (cdr property-path))))))))
57 ;;; Engine Inner Properties
59 (define (default-delay) 0.1)
61 (define (default-engine-inner-properties)
62 `(engine-inner-properties (delay . ,(default-delay))))
65 ;;; Engine definitions
67 (define-record-type engine
68 (make-engine-record entities mutex running-mutex system)
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!))
75 (set-record-type-printer! engine
77 (format port "#<[engine] state: ~a, entities: ~a>"
78 (if (engine-running? record) "Running" "Stopped")
79 (length (car (engine-entities record))))))
81 (define (make-engine . systems)
83 (receive (e c) ((new-entity (default-engine-inner-properties)) '() '())
87 (apply group-systems systems)))
89 (define-syntax define-engine
93 (make-engine system ...)))))
95 (define (engine-running? engine)
96 (mutex-locked? (engine-running-mutex engine)))
103 ;;; Engine Access Protocol Interface
105 (define current-engine-mutex (make-mutex))
106 (define current-engine-list '())
108 (define (current-engine)
109 (with-mutex current-engine-mutex
110 (assoc-ref current-engine-list (current-thread))))
112 (define (set-current-engine! engine)
113 (with-mutex current-engine-mutex
114 (set! current-engine-list
116 (assoc-set! current-engine-list (current-thread) engine))
118 (assoc-remove! current-engine-list (current-thread)))))))
120 (define* (get-entity key #:key (engine (current-engine)))
121 (assoc key (car (engine-entities engine))))
123 (define* (get-entities-by-components component-types #:key (engine (current-engine)))
125 (get-entity e #:engine engine))
126 (find-entities-by-components (cadr (engine-entities engine)) component-types)))
128 (define-syntax define-entity-setter
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))
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)
144 (define-syntax with-engine
147 (let ((old-engine (current-engine)))
148 (set-current-engine! engine)
149 (let ((res (with-mutex (engine-mutex engine)
152 (set-current-engine! old-engine)
155 (define (set-engine-systems! engine . systems)
156 (with-mutex (engine-mutex engine)
157 (set-engine-system! engine (apply group-systems systems))))
159 (export current-engine
162 get-entities-by-components
166 set-entity-components!
167 remove-entity-components!
174 (define (start-engine engine)
175 (cond ((not (engine-running? engine))
176 (with-mutex (engine-running-mutex 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))
187 (define (stop-engine engine)
189 (new-entity! '(engine-halt . #t)))
192 (define* (engine-stopping? engine #:key (clean #f))
195 (let halt-engine ((halts (get-entities-by-components '(engine-halt))))
196 (cond ((not (null? halts))
199 (remove-entity! (caar halts))
200 (halt-engine (cdr halts))))))))