From a72b7f4b6526a5ceea28b96a679acd2124ba4ca3 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Wed, 5 Jun 2013 15:58:38 +0200 Subject: [PATCH] Structures for managing entities and components, using mutex for future synchronization with game loop * src/gacela.scm: entity get-entity --- src/gacela.scm | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/gacela.scm diff --git a/src/gacela.scm b/src/gacela.scm new file mode 100644 index 0000000..7fb242f --- /dev/null +++ b/src/gacela.scm @@ -0,0 +1,61 @@ +;;; 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 gacela) + #:use-module (gacela system) + #:use-module (ice-9 threads) + #:use-module (srfi srfi-1)) + + +;;; Entities and components + +(define entities-mutex (make-mutex)) +(define game-entities '()) +(define game-components '()) + + +(define (entity . components) + (with-mutex entities-mutex + (let ((key (gensym))) + (set! game-entities + (acons key + (map (lambda (c) (list (get-component-type c) c)) components) + game-entities)) + (set! game-components (register-components key components)) + key))) + + +(define* (register-components entity components #:optional (clist game-components)) + (cond ((null? components) clist) + (else + (let* ((type (get-component-type (car components))) + (elist (assoc-ref clist type))) + (register-components entity (cdr components) + (assoc-set! clist type + (cond (elist + (lset-adjoin eq? elist entity)) + (else + (list entity))))))))) + + +(define (get-entity key) + (with-mutex entities-mutex + (assoc key game-entities))) + + +(export entity + get-entity) -- 2.39.2