]> git.jsancho.org Git - gacela.git/blobdiff - src/gacela.scm
Running gacela functions inside game loop thread for avoiding GL problems with threads
[gacela.git] / src / gacela.scm
index c0451f67e84acad58e1ed83de9a98d3ef94ff6d1..e07ef2f641b94a6702a56937fc98aa846e665164 100644 (file)
            set-game-code
            show-mob-hash
            hide-mob-hash
+           get-active-mobs
            hide-all-mobs
-           get-mob-function-name)
+           get-current-mob-id
+           get-mob-function-name
+           map-mobs)
   #:export-syntax (game
                   show-mob
                   hide-mob
                   the-mob
                   define-mob-function
                   define-mob
-                  lambda-mob)
+                  lambda-mob
+                  define-checking-mobs)
   #:re-export (get-current-color
               set-current-color
               with-color
               set-camera
               camera-look
               render-text
-              get-frame-time))
+              get-frame-time
+              key?
+              key-pressed?
+              key-released?))
 
 
 ;;; Resources Cache
@@ -78,7 +85,7 @@
   (hash-set! resources-cache key res))
 
 (define-macro (use-cache-with module proc)
-  (let* ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
+  (let ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
     `(begin
        (define ,pwc (@ ,module ,proc))
        (define (,proc . param)
 (define game-code #f)
 (define game-loop-thread #f)
 
+(define-macro (run-in-game-loop proc)
+  (let ((pgl (string->symbol (string-concatenate (list (symbol->string proc) "-in-game-loop"))))
+       (flag-symbol (gensym))
+       (value-symbol (gensym)))
+    `(begin
+       (define ,pgl ,proc)
+       (define (,proc . param)
+        (cond ((and game-loop-thread (not (eq? game-loop-thread (current-thread))))
+               (let ((,flag-symbol #f))
+                 (define ,value-symbol)
+                 (system-async-mark
+                  (lambda ()
+                    (catch #t
+                          (lambda () (set! ,value-symbol (apply ,pgl param)))
+                          (lambda (key . args) #f))
+                    (set! ,flag-symbol #t))
+                  game-loop-thread)
+                 (while (not ,flag-symbol))
+                 ,value-symbol))
+              (else
+               (apply ,pgl param)))))))
+
+(run-in-game-loop load-texture)
+(run-in-game-loop load-font)
+(run-in-game-loop set-screen-bpp!)
+(run-in-game-loop resize-screen)
+
 (define-macro (game . code)
   `(let ((game-function ,(if (null? code)
                             `(lambda () #f)
      (cond ((not (game-running?))
            (game-loop)))))
 
-(define-macro (run-in-game-loop . code)
-  `(if game-loop-thread
-       (system-async-mark (lambda () ,@code) game-loop-thread)
-       (begin ,@code)))
-
 (define (init-gacela)
-  (set! game-loop-thread (call-with-new-thread (lambda () (game)))))
+  (set! game-loop-thread (call-with-new-thread (lambda () (game))))
+  (while (not loop-flag))
+  #t)
 
 (define (quit-gacela)
   (set! game-loop-thread #f)
 
 (define (game-loop)
   (refresh-active-mobs)
-  (set! loop-flag #t)
   (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
+  (set! loop-flag #t)
   (while loop-flag
         (init-frame-time)
 ;          (check-connections)
   (if title
       (set-screen-title! title))
   (if bpp
-      (run-in-game-loop (set-screen-bpp! 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)))
-       (run-in-game-loop (resize-screen width height))))
+       (resize-screen width height)))
   (if fps
       (set-frames-per-second! fps))
   (if mode
-      (if (eq? mode '3d) (set-3d-mode) (set-2d-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 . ,(get-frames-per-second)) (mode . ,(if (3d-mode?) '3d '2d))))
        (else
         `(hide-mob-hash (,mob 'get-mob-id)))))
 
+(define current-mob-id #f)
+
+(define (get-current-mob-id)
+  current-mob-id)
+
 (define* (run-mobs #:optional (mobs (get-active-mobs)))
   (for-each
    (lambda (m)
+     (set! current-mob-id (m 'get-mob-id))
      (glmatrix-block (m)))
-   mobs))
+   mobs)
+  (set! current-mob-id #f))
 
 
 ;;; Making mobs
           (cond ((not (= time mob-time))
                  (set! mob-time time)
                  (set! saved-data mob-data)))))
-;       (define (filter-mobs type fun)
-;       #t)
-;       (define (map-mobs fun type)
-;       (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) ,mob-id-symbol)))) (get-active-mobs))))
-;         (map (lambda (m) (fun (m 'get-data))) mobs)))
        (case option
         ((get-mob-id)
          mob-id)
        (the-mob 'undefined '() ,fun-name))))
 
 
-;;; Collisions
+;;; Functions for checking mobs (collisions and more)
 
-;; (define-macro (lambda-mob-data attr . body)
-;;   `(lambda ,attr ,@body))
+(define (map-mobs fun type)
+  (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
+    (map (lambda (m) (fun (m 'get-data))) mobs)))
 
-;; (define-macro (define-collision-check name mobs . body)
-;;   `(defmacro* ,name (#:optional m)
-;;      `(let ,(cond (m `((mob-id (,m 'get-mob-id)) (mob-type (,m 'get-type))))
-;;               (else `()))
-       
-;;     mob-id)))
+(define-macro (define-checking-mobs head mob-def . body)
+  (let ((type (car mob-def)) (attr (cdr mob-def)))
+    `(define ,head
+       (map-mobs
+       (lambda (m)
+         (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)
+           ,@body))
+       ',type))))