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))
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)
34 (let loop ((property (get-component (car property-path) (car entities)))
35 (path (cdr property-path)))
36 (cond ((or (null? path) (not property))
39 (loop (assoc-ref property (car path)) (cdr path)))))))))
41 (define* (set-property! property-path value #:key (engine (current-engine)))
42 (define (loop property path)
46 (assoc-set! (or property '()) (car path) (loop (assoc-ref property (car path)) (cdr path))))))
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)))))
52 (set-entity-components! (get-key (car entities)) `(,(car property-path) . ,(loop (get-component (car property-path) (car entities)) (cdr property-path))))))))
58 ;;; Engine Inner Properties
60 (define (default-step) 0.1)
62 (define (default-engine-inner-properties)
63 `(engine-inner-properties (step . ,(default-step))))
66 ;;; Engine definitions
68 (define-record-type engine
69 (make-engine-record entities mutex running-mutex system)
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!))
76 (set-record-type-printer! engine
78 (format port "#<[engine] state: ~a, entities: ~a>"
79 (if (engine-running? record) "Running" "Stopped")
80 (length (car (engine-entities record))))))
82 (define (make-engine . systems)
84 (receive (e c) ((new-entity (default-engine-inner-properties)) '() '())
88 (apply group-systems systems)))
90 (define-syntax define-engine
94 (make-engine system ...)))))
96 (define (engine-running? engine)
97 (mutex-locked? (engine-running-mutex engine)))
104 ;;; Engine Access Protocol Interface
106 (define current-engine-mutex (make-mutex))
107 (define current-engine-list '())
109 (define (current-engine)
110 (with-mutex current-engine-mutex
111 (assoc-ref current-engine-list (current-thread))))
113 (define (set-current-engine! engine)
114 (with-mutex current-engine-mutex
115 (set! current-engine-list
117 (assoc-set! current-engine-list (current-thread) engine))
119 (assoc-remove! current-engine-list (current-thread)))))))
121 (define* (get-entity key #:key (engine (current-engine)))
122 (assoc key (car (engine-entities engine))))
124 (define* (get-entities-by-components component-types #:key (engine (current-engine)))
126 (get-entity e #:engine engine))
127 (find-entities-by-components (cadr (engine-entities engine)) component-types)))
129 (define-syntax define-entity-setter
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))
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)
145 (define-syntax with-engine
148 (let ((old-engine (current-engine)))
149 (set-current-engine! engine)
150 (let ((res (with-mutex (engine-mutex engine)
153 (set-current-engine! old-engine)
156 (define (set-engine-systems! engine . systems)
157 (with-mutex (engine-mutex engine)
158 (set-engine-system! engine (apply group-systems systems))))
160 (export current-engine
163 get-entities-by-components
167 set-entity-components!
168 remove-entity-components!
175 (define (start-engine engine)
176 (cond ((not (engine-running? engine))
177 (with-mutex (engine-running-mutex engine)
179 (let ((t (current-utime))
183 (receive (e c) ((apply (engine-system engine) (engine-entities engine)))
184 (set-engine-entities! engine (list e c)))
185 (set! delay (- (inexact->exact (* (get-property '(engine-inner-properties step)) 1000000))
186 (- (current-utime) t)))
187 (set! halt (engine-stopping? #:clean #t)))
193 (define (stop-engine engine)
195 (new-entity! '(engine-halt . #t)))
198 (define* (engine-stopping? #:key (engine (current-engine)) (clean #f))
200 (let halt-engine ((halts (get-entities-by-components '(engine-halt))))
201 (cond ((not (null? halts))
204 (remove-entity! (caar halts))
205 (halt-engine (cdr halts)))))))