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))
26 ;;; Engine definitions
28 (define (default-delay) 0.1)
30 (define-component engine-inner-properties
33 (define-record-type engine
34 (make-engine-record entities mutex running-mutex system)
36 (entities engine-entities set-engine-entities!)
37 (mutex engine-mutex set-engine-mutex!)
38 (running-mutex engine-running-mutex set-engine-running-mutex!)
39 (system engine-system set-engine-system!))
41 (set-record-type-printer! engine
43 (format port "#<[engine] state: ~a, entities: ~a>"
44 (if (engine-running? record) "Running" "Stopped")
45 (length (car (engine-entities record))))))
47 (define (make-engine . systems)
49 (receive (e c) ((new-entity (make-engine-inner-properties (default-delay))) '() '())
53 (apply group-systems systems)))
55 (define-syntax define-engine
59 (make-engine system ...)))))
61 (define (engine-running? engine)
62 (mutex-locked? (engine-running-mutex engine)))
68 (export-component engine-inner-properties)
71 ;;; Engine Access Protocol Interface
73 (define current-engine-mutex (make-mutex))
74 (define current-engine-list '())
76 (define (current-engine)
77 (with-mutex current-engine-mutex
78 (assoc-ref current-engine-list (current-thread))))
80 (define (set-current-engine! engine)
81 (with-mutex current-engine-mutex
82 (set! current-engine-list
84 (assoc-set! current-engine-list (current-thread) engine))
86 (assoc-remove! current-engine-list (current-thread)))))))
88 (define* (get-entity key #:key (engine (current-engine)))
89 (assoc key (car (engine-entities engine))))
91 (define* (get-entities-by-components component-types #:key (engine (current-engine)))
93 (get-entity e #:engine engine))
94 (find-entities-by-components (cadr (engine-entities engine)) component-types)))
96 (define-syntax define-entity-setter
99 (define (name! . args)
100 (let ((f (apply name args))
101 (engine (current-engine)))
102 (receive (e c r) (f (car (engine-entities engine)) (cadr (engine-entities engine)))
103 (set-engine-entities! engine (list e c))
106 (define-entity-setter new-entity! new-entity)
107 (define-entity-setter remove-entity! remove-entity)
108 (define-entity-setter set-entity! set-entity)
109 (define-entity-setter set-entity-components! set-entity-components)
110 (define-entity-setter remove-entity-components! remove-entity-components)
112 (define-syntax with-engine
115 (let ((old-engine (current-engine)))
116 (set-current-engine! engine)
117 (let ((res (with-mutex (engine-mutex engine)
120 (set-current-engine! old-engine)
123 (define (set-engine-systems! engine . systems)
124 (with-mutex (engine-mutex engine)
125 (set-engine-system! engine (apply group-systems systems))))
127 (export current-engine
130 get-entities-by-components
134 set-entity-components!
135 remove-entity-components!
142 (define (start-engine engine)
143 (cond ((not (engine-running? engine))
144 (with-mutex (engine-running-mutex engine)
148 (receive (e c) ((apply (engine-system engine) (engine-entities engine)))
149 (set-engine-entities! engine (list e c)))
150 (set! delay (engine-inner-properties-delay (get-component 'engine-inner-properties (car (get-entities-by-components '(engine-inner-properties)))))))
151 (usleep (inexact->exact (* delay 1000000))))
152 (if (not (engine-stopping? engine #:clean #t))
155 (define (stop-engine engine)
157 (new-entity! '(engine-halt . #t)))
160 (define* (engine-stopping? engine #:key (clean #f))
163 (let halt-engine ((halts (get-entities-by-components '(engine-halt))))
164 (cond ((not (null? halts))
167 (remove-entity! (caar halts))
168 (halt-engine (cdr halts))))))))