]> git.jsancho.org Git - gacela.git/blob - src/engine.scm
Engine Execution
[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 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))
24
25
26 ;;; Engine definitions
27
28 (define (default-delay) 0.1)
29
30 (define-component engine-inner-properties
31   delay)
32
33 (define-record-type engine
34   (make-engine-record entities mutex running-mutex system)
35   engine?
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!))
40
41 (set-record-type-printer! engine
42   (lambda (record port)
43     (format port "#<[engine] state: ~a, entities: ~a>"
44             (if (engine-running? record) "Running" "Stopped")
45             (length (car (engine-entities record))))))
46
47 (define (make-engine . systems)
48   (make-engine-record
49    (receive (e c) ((new-entity (make-engine-inner-properties (default-delay))) '() '())
50      (list e c))
51    (make-mutex)
52    (make-mutex)
53    (if (not (= (length systems) 1))
54        (join-systems systems)
55        (car systems))))
56
57 (define-syntax define-engine
58   (syntax-rules ()
59     ((_ name system ...)
60      (define name
61        (make-engine system ...)))))
62
63 (define (engine-running? engine)
64   (mutex-locked? (engine-running-mutex engine)))
65
66 (export make-engine
67         define-engine
68         engine-running?)
69
70 (export-component engine-inner-properties)
71
72
73 ;;; Engine Access Protocol Interface
74
75 (define current-engine-mutex (make-mutex))
76 (define current-engine-list '())
77
78 (define (current-engine)
79   (with-mutex current-engine-mutex
80     (assoc-ref current-engine-list (current-thread))))
81
82 (define (set-current-engine! engine)
83   (with-mutex current-engine-mutex
84     (set! current-engine-list
85           (cond (engine
86                  (assoc-set! current-engine-list (current-thread) engine))
87                 (else
88                  (assoc-remove! current-engine-list (current-thread)))))))
89
90 (define* (get-entity key #:key (engine (current-engine)))
91   (assoc key (car (engine-entities engine))))
92
93 (define* (get-entities-by-components component-types #:key (engine (current-engine)))
94   (map (lambda (e)
95          (get-entity e #:engine engine))
96        (find-entities-by-components (cadr (engine-entities engine)) component-types)))
97
98 (define-syntax define-entity-setter
99   (syntax-rules ()
100     ((_ name! name)
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))
106            r))))))
107
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)
113
114 (define-syntax with-engine
115   (syntax-rules ()
116     ((_ engine body ...)
117      (let ((old-engine (current-engine)))
118        (set-current-engine! engine)
119        (let ((res (with-mutex (engine-mutex engine)
120                     body
121                     ...)))
122          (set-current-engine! old-engine)
123          res)))))
124
125 (export current-engine
126         set-current-engine!
127         get-entity
128         get-entities-by-components
129         new-entity!
130         remove-entity!
131         set-entity!
132         set-entity-components!
133         remove-entity-components!
134         with-engine)
135
136
137 ;;; Engine execution
138
139 (define (start-engine engine)
140   (cond ((not (engine-running? engine))
141          (with-mutex (engine-running-mutex engine)
142            (let loop ()
143              (let ((delay 0))
144                (with-engine 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))
150                  (loop)))))))
151
152 (define (stop-engine engine)
153   (with-engine engine
154     (new-entity! '(engine-halt . #t)))
155   'engine-halt)
156
157 (define* (engine-stopping? engine #:key (clean #f))
158   (let ((halt #f))
159     (with-engine engine
160       (let halt-engine ((halts (get-entities-by-components '(engine-halt))))
161         (cond ((not (null? halts))
162                (set! halt #t)
163                (cond (clean
164                       (remove-entity! (caar halts))
165                       (halt-engine (cdr halts))))))))
166     halt))
167
168 (export start-engine
169         stop-engine
170         engine-stopping?)