X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=src%2Fgacela.scm;h=7fb242fdb94561afce498a328e8629105bed7e03;hb=52af2b21c93a97e9ff5b8a22d0ba5df2cba766ec;hp=86c46053fd468dc3b134ccf7186ad2710ced04cc;hpb=62f29c535971a4af5c4a957444a53eb6007b32e3;p=gacela.git diff --git a/src/gacela.scm b/src/gacela.scm index 86c4605..7fb242f 100644 --- a/src/gacela.scm +++ b/src/gacela.scm @@ -1,5 +1,5 @@ ;;; Gacela, a GNU Guile extension for fast games development -;;; Copyright (C) 2009 by Javier Sancho Fernandez +;;; 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 @@ -16,139 +16,46 @@ (define-module (gacela gacela) - #:use-module (gacela events) - #:use-module (gacela video) - #:use-module (gacela audio) - #:use-module (ice-9 optargs) - #:export (load-texture - load-font - set-game-properties! - get-game-properties - init-gacela - quit-gacela - game-loop - game-running? - set-game-code) - #:export-syntax (game) - #:re-export (get-current-color - set-current-color - with-color - progn-textures - draw - draw-texture - draw-line - draw-quad - draw-rectangle - draw-square - draw-cube - translate - rotate - to-origin - add-light - set-camera - camera-look - render-text)) + #:use-module (gacela system) + #:use-module (ice-9 threads) + #:use-module (srfi srfi-1)) -;;; Resources Cache +;;; Entities and components -(define resources-cache (make-weak-value-hash-table)) +(define entities-mutex (make-mutex)) +(define game-entities '()) +(define game-components '()) -(define (from-cache key) - (hash-ref resources-cache key)) -(define (into-cache key res) - (hash-set! resources-cache key res)) +(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-macro (use-cache-with module proc) - (let* ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache"))))) - `(begin - (define ,pwc (@ ,module ,proc)) - (define (,proc . param) - (let* ((key param) - (res (from-cache key))) - (cond (res - res) - (else - (set! res (apply ,pwc param)) - (into-cache key res) - res))))))) -(use-cache-with (gacela video) load-texture) -(use-cache-with (gacela video) load-font) +(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))))))))) -;;; Game Properties +(define (get-entity key) + (with-mutex entities-mutex + (assoc key game-entities))) -(define title "Gacela") -(define width-screen 640) -(define height-screen 480) -(define bpp-screen 32) -(define frames-per-second 20) -(define mode '2d) -(define* (set-game-properties! #:key title width height bpp fps mode) - (if title - (set-screen-title! title)) - (if bpp - (set-screen-bpp! bpp)) - (if (or width height) - (begin - (if (not width) (set! width (get-screen-width))) - (if (not height) (set! height (get-screen-height))) - (resize-screen width height))) - (if fps - (set-frames-per-second fps)) - (if mode - (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))) - (get-game-properties)) - -(define (get-game-properties) - `((title . ,(get-screen-title)) (width . ,(get-screen-width)) (height . ,(get-screen-height)) (bpp . ,(get-screen-bpp)) (fps . ,pfps) (mode . ,pmode))) - - -;;; Main Loop - -(define loop-flag #f) -(define game-code #f) - -(define-macro (game . code) - `(let ((game-function ,(if (null? code) - `(lambda () #f) - `(lambda () ,@code)))) - (set-game-code game-function) - (cond ((not (game-running?)) - (game-loop))))) - -(define (init-gacela) - (call-with-new-thread (lambda () (game)))) - -(define (quit-gacela) - (set! loop-flag #f)) - -(define (game-loop) -; (refresh-active-mobs) - (set! loop-flag #t) - (init-video 640 480 32) - (while loop-flag - (init-frame-time) -; (check-connections) - (process-events) - (cond ((not (quit?)) - (clear-screen) - (to-origin) -; (refresh-active-mobs) - (if (procedure? game-code) - (catch #t - (lambda () (game-code)) - (lambda (key . args) #f))) -; (run-mobs) - (flip-screen) - (delay-frame)))) - (quit-video)) - -(define (game-running?) - loop-flag) - -(define (set-game-code game-function) - (set! game-code game-function)) +(export entity + get-entity)