]> git.jsancho.org Git - gacela.git/blob - src/examples/composing-systems.scm
Repairing composing systems example
[gacela.git] / src / examples / composing-systems.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 examples composing-systems)
19   #:use-module (gacela system)
20   #:use-module (ice-9 receive))
21
22
23 (define-system s1 ((with-l (l)))
24   (sleep 3)
25   (map
26    (lambda (e)
27      (set-entity-components (get-key e) `(l . ,(cons 1 (get-component 'l e)))))
28    with-l))
29
30 (define-system s2 ((with-l (l)))
31   (sleep 4)
32   (map
33    (lambda (e)
34      (set-entity-components (get-key e) `(l . ,(cons 2 (get-component 'l e)))))
35    with-l))
36
37 (define (composing-with-join)
38   (let ((entities '())
39         (components '()))
40     (receive (e c) (modify-entities (list (new-entity '(l . ())) (new-entity '(l . ()))) entities components)
41       (((join-systems s1 s2) e c)))))
42
43 (export composing-with-join)
44
45
46 (define (composing-with-threaded)
47   (let ((entities '())
48         (components '()))
49     (receive (e c) (modify-entities (list (new-entity '(l . ())) (new-entity '(l . ()))) entities components)
50       (((threaded-systems s1 s2) e c)))))
51
52 (export composing-with-threaded)
53
54
55 (define (join-vs-threaded)
56   (let ((entities '())
57         (components '())
58         (t (current-time)))
59     (receive (e c) (modify-entities (list (new-entity '(l . ())) (new-entity '(l . ()))) entities components)
60       (receive (e c) (((join-systems s1 s2) e c))
61         (format #t "~a~%~a~%Time: ~a~%~%" e c (- (current-time) t)))))
62
63   (let ((entities '())
64         (components '())
65         (t (current-time)))
66     (receive (e c) (modify-entities (list (new-entity '(l . ())) (new-entity '(l . ()))) entities components)
67       (receive (e c) (((threaded-systems s1 s2) e c))
68         (format #t "~a~%~a~%Time: ~a~%~%" e c (- (current-time) t))))))
69
70 (export join-vs-threaded)