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 (if (not (= (length systems) 1))
54 (join-systems systems)
57 (define-syntax define-engine
61 (make-engine system ...)))))
63 (define (engine-running? engine)
64 (mutex-locked? (engine-running-mutex engine)))
70 (export-component engine-inner-properties)
73 ;;; Engine Access Protocol Interface
75 (define current-engine-mutex (make-mutex))
76 (define current-engine-list '())
78 (define (current-engine)
79 (with-mutex current-engine-mutex
80 (assoc-ref current-engine-list (current-thread))))
82 (define (set-current-engine! engine)
83 (with-mutex current-engine-mutex
84 (set! current-engine-list
86 (assoc-set! current-engine-list (current-thread) engine))
88 (assoc-remove! current-engine-list (current-thread)))))))
90 (define* (get-entity key #:key (engine (current-engine)))
91 (assoc key (car (engine-entities engine))))
93 (define* (get-entities-by-components component-types #:key (engine (current-engine)))
95 (get-entity e #:engine engine))
96 (find-entities-by-components (cadr (engine-entities engine)) component-types)))
98 (define-syntax define-entity-setter
101 (define (name! . args)
102 (let ((f (apply name args))
103 (engine (current-engine)))
104 (receive (e c r) (f (car (engine-entities engine)) (cadr (engine-entities engine)))
105 (set-engine-entities! engine (list e c))
108 (define-entity-setter new-entity! new-entity)
109 (define-entity-setter remove-entity! remove-entity)
110 (define-entity-setter set-entity! set-entity)
111 (define-entity-setter set-entity-components! set-entity-components)
112 (define-entity-setter remove-entity-components! remove-entity-components)
114 (define-syntax with-engine
117 (let ((old-engine (current-engine)))
118 (set-current-engine! engine)
119 (let ((res (with-mutex (engine-mutex engine)
122 (set-current-engine! old-engine)
125 (export current-engine
128 get-entities-by-components
132 set-entity-components!
133 remove-entity-components!
139 (define (start-engine engine)
140 (cond ((not (engine-running? engine))
141 (with-mutex (engine-running-mutex engine)
145 (receive (e c) ((apply (engine-system engine) (engine-entities engine)))
146 (set-engine-entities! engine (list e c)))
147 (set! delay (engine-inner-properties-delay (get-component 'engine-inner-properties (car (get-entities-by-components '(engine-inner-properties)))))))
148 (usleep (inexact->exact (* delay 1000000))))
149 (if (not (engine-stopping? engine #:clean #t))
152 (define (stop-engine engine)
154 (new-entity! '(engine-halt . #t)))
157 (define* (engine-stopping? engine #:key (clean #f))
160 (let halt-engine ((halts (get-entities-by-components '(engine-halt))))
161 (cond ((not (null? halts))
164 (remove-entity! (caar halts))
165 (halt-engine (cdr halts))))))))