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)
25 #:use-module (system repl server)
26 #:use-module (system repl coop-server))
29 ;;; Engine Inner Properties
31 (define (default-step) 0.1)
33 (define (default-engine-inner-properties)
34 `(engine-inner-properties (step . ,(default-step))))
37 ;;; Engine definitions
39 (define-record-type engine
40 (make-engine-record entities mutex running-mutex systems)
42 (entities engine-entities set-engine-entities!)
43 (running-mutex engine-running-mutex set-engine-running-mutex!)
44 (systems engine-systems set-engine-systems!))
46 (set-record-type-printer! engine
48 (format port "#<[engine] state: ~a, entities: ~a>"
49 (if (engine-running? record) "Running" "Stopped")
50 (entity-count (engine-entities record)))))
52 (define (make-engine . systems)
54 (make-entity-set (new-entity (default-engine-inner-properties)))
58 (define-syntax define-engine
62 (make-engine system ...)))))
64 (define (engine-running? engine)
65 (mutex-locked? (engine-running-mutex engine)))
74 (define* (start-engine engine #:optional (socket #f))
78 (with-mutex (engine-running-mutex engine)
79 (let loop ((coop-server (if socket (spawn-coop-repl-server socket) #f)))
80 (let ((t (current-utime))
84 (lambda (s) (eval-system s engine))
85 (engine-systems engine))
86 (set! delay (- (inexact->exact (* (engine-property engine 'step) 1000000))
87 (- (current-utime) t)))
88 (set! halt (engine-stopping? engine #:clean #t))
90 (poll-coop-repl-server coop-server))
97 (stop-server-and-clients!))))))))
101 (define (eval-system system engine)
103 (lambda () (system (engine-entities engine)))
105 (let ((changes (car vals)))
106 (cond ((entities-changes? changes)
107 (set-engine-entities! engine
108 (modify-entities (engine-entities engine)
109 (get-entities-changes changes))))))
110 (apply values vals))))
112 (define-syntax with-engine
114 ((_ engine component-types form ...)
115 (with-mutex (engine-mutex engine)
116 (eval-system (make-system component-types form ...) engine)))))
118 (define (stop-engine engine)
119 (with-engine engine ()
122 (new-entity '(engine-halt . #t)))))
125 (define* (engine-stopping? engine #:key (clean #f))
126 (let ((halts (eval-system
127 (make-system ((halt (engine-halt))) halt)
129 (cond ((and clean (not (null? halts)))
131 (make-system () (entities-changes (map (lambda (h) (remove-entity (car h))) halts)))
133 (not (null? halts))))
142 (define (engine-property engine name)
144 (make-system ((props (engine-inner-properties)))
146 (assoc-ref (cdar props) 'engine-inner-properties)
150 (define (set-engine-property! engine name value)
152 (make-system ((props (engine-inner-properties)))
155 (set-entity (caar props)
157 (assoc-set! (cdar props) 'engine-inner-properties
158 (assoc-set! (assoc-ref (cdar props) 'engine-inner-properties)
164 (export engine-property
165 set-engine-property!)