From 2b3814bf3f335a56c17b733caf90c17dbe229e91 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Mon, 9 Sep 2013 06:54:07 +0200 Subject: [PATCH] Better examples --- src/examples/composing-systems.scm | 74 +++++++++++++++++++ .../entity-component-functions.scm} | 60 +-------------- src/examples/making-systems.scm | 35 +++++++++ 3 files changed, 113 insertions(+), 56 deletions(-) create mode 100644 src/examples/composing-systems.scm rename src/{test.scm => examples/entity-component-functions.scm} (64%) create mode 100644 src/examples/making-systems.scm diff --git a/src/examples/composing-systems.scm b/src/examples/composing-systems.scm new file mode 100644 index 0000000..feecee3 --- /dev/null +++ b/src/examples/composing-systems.scm @@ -0,0 +1,74 @@ +;;; Gacela, a GNU Guile extension for fast games development +;;; Copyright (C) 2013 by Javier Sancho Fernandez +;;; +;;; This program is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published by +;;; the Free Software Foundation, either version 3 of the License, or +;;; (at your option) any later version. +;;; +;;; This program is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see . + + +(define-module (gacela examples composing-systems) + #:use-module (gacela system) + #:use-module (ice-9 receive)) + + +(define s1 + (make-system '(l) + (lambda (e) + (sleep 3) + (map + (lambda (e1) + (set-entity-components (car e1) `(l . ,(cons 1 (cdadr e1))))) + e)))) + +(define s2 + (make-system '(l) + (lambda (e) + (sleep 4) + (map + (lambda (e1) + (set-entity-components (car e1) `(l . ,(cons 2 (cdadr e1))))) + e)))) + +(define (composing-with-join) + (let ((entities '()) + (components '())) + (receive (e c) (modify-entities (list (new-entity '(l . ())) (new-entity '(l . ()))) entities components) + ((join-systems s1 s2) e c)))) + +(export composing-with-join) + + +(define (composing-with-threaded) + (let ((entities '()) + (components '())) + (receive (e c) (modify-entities (list (new-entity '(l . ())) (new-entity '(l . ()))) entities components) + ((threaded-systems s1 s2) e c)))) + +(export composing-with-threaded) + + +(define (join-vs-threaded) + (let ((entities '()) + (components '()) + (t (current-time))) + (receive (e c) (modify-entities (list (new-entity '(l . ())) (new-entity '(l . ()))) entities components) + (receive (e c) ((join-systems s1 s2) e c) + (format #t "~a~%~a~%Time: ~a~%~%" e c (- (current-time) t))))) + + (let ((entities '()) + (components '()) + (t (current-time))) + (receive (e c) (modify-entities (list (new-entity '(l . ())) (new-entity '(l . ()))) entities components) + (receive (e c) ((threaded-systems s1 s2) e c) + (format #t "~a~%~a~%Time: ~a~%~%" e c (- (current-time) t)))))) + +(export join-vs-threaded) diff --git a/src/test.scm b/src/examples/entity-component-functions.scm similarity index 64% rename from src/test.scm rename to src/examples/entity-component-functions.scm index 353987b..43316fc 100644 --- a/src/test.scm +++ b/src/examples/entity-component-functions.scm @@ -15,18 +15,15 @@ ;;; along with this program. If not, see . -(define-module (gacela test) +(define-module (gacela examples entity-component-functions) #:use-module (gacela system) #:use-module (ice-9 receive)) (define-component a x y) (define-component b) -(define-component c) -(export-component a) - -(define (test1) +(define (entity-component-functions) (let ((entities '()) (components '()) (key #f)) @@ -68,55 +65,6 @@ (receive (e c) (modify-entities (list (remove-entity key)) entities components) (set! entities e) (set! components c)) - (format #t "Remove last entity:~%~a~%~a~%~%" entities components) -)) - -(export test1) - - -(define (test2) - (let ((entities '()) - (components '())) - (receive (e c) (((make-system '() (lambda (e) (list (new-entity (make-a 1 2)) (new-entity (make-a 10 20))))) entities components)) - (set! entities e) - (set! components c)) - (format #t "Two new entities with a:~%~a~%~a~%~%" entities components) - - (((make-system '(a) (lambda (e) (display e) (newline) '())) entities components)) -)) - -(export test2) - - -(define (test3) - (let ((entities '()) - (components '()) - (s1 (make-system '(l) - (lambda (e) - (map - (lambda (e1) - `(,(car e1) . ((l . (cons 1 (cdr e1))))) - e))))) - (s2 (make-system '(l) - (lambda (e) - (map - (lambda (e1) - `(,(car e1) . ((l . (cons 2 (cdr e1)))))) - e))))) - (receive (e c) (set-entities `((#f . ((l . ()))) (#f . ((l . ())))) entities components) - ((join-systems s1 s2) e c)))) - -(export test3) - - -(define (test4) - (let ((f1 (lambda (e c) (sleep 3) (lambda (e2 c2) (values (+ 1 e2) c2)))) - (f2 (lambda (e c) (sleep 4) (lambda (e2 c2) (values e2 (+ 10 c2)))))) - (let ((t (current-time))) - (receive (e c) ((join-systems f1 f2) 2 2) (format #t "~a~%~a~%" e c)) - (display (- (current-time) t)) (newline) (newline)) - (let ((t (current-time))) - (receive (e c) ((threaded-systems f1 f2) 2 2) (format #t "~a~%~a~%" e c)) - (display (- (current-time) t)) (newline) (newline)))) + (format #t "Remove last entity:~%~a~%~a~%~%" entities components))) -(export test4) +(export entity-component-functions) diff --git a/src/examples/making-systems.scm b/src/examples/making-systems.scm new file mode 100644 index 0000000..b3f381d --- /dev/null +++ b/src/examples/making-systems.scm @@ -0,0 +1,35 @@ +;;; Gacela, a GNU Guile extension for fast games development +;;; Copyright (C) 2013 by Javier Sancho Fernandez +;;; +;;; This program is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published by +;;; the Free Software Foundation, either version 3 of the License, or +;;; (at your option) any later version. +;;; +;;; This program is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see . + + +(define-module (gacela examples making-systems) + #:use-module (gacela system) + #:use-module (ice-9 receive)) + + +(define-component a x y) + +(define (making-systems) + (let ((entities '()) + (components '())) + (receive (e c) (((make-system '() (lambda (e) (list (new-entity (make-a 1 2)) (new-entity (make-a 10 20))))) entities components)) + (set! entities e) + (set! components c)) + (format #t "Two new entities with a:~%~a~%~a~%~%" entities components) + + (((make-system '(a) (lambda (e) (display e) (newline) '())) entities components)))) + +(export making-systems) -- 2.39.2