set-game-code
show-mob-hash
hide-mob-hash
- hide-all-mobs)
+ get-active-mobs
+ hide-all-mobs
+ get-current-mob-id
+ get-mob-function-name
+ map-mobs
+ translate-mob)
#: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?
+ 3d-mode?))
;;; Resources Cache
(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)
- (glmatrix-block (m)))
- mobs))
+ (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
+ (for-each
+ (lambda (m)
+ (set! current-mob-id (m 'get-mob-id))
+ (glmatrix-block (m)))
+ sorted-mobs)
+ (set! current-mob-id #f)))
;;; Making mobs
-(define-macro (the-mob type attr publish . body)
- (let ((mob-id-symbol (gensym))
- (type-symbol (gensym))
- (time-symbol (gensym))
+(define mob-functions (make-hash-table))
+
+(define (get-mob-function-name mob-name)
+ (let ((name (hash-ref mob-functions mob-name)))
+ (cond ((not name)
+ (set! name (gensym))
+ (hash-set! mob-functions mob-name name)))
+ name))
+
+(define-macro (the-mob type init-data fun-name)
+ `(let ((mob-id (gensym))
+ (mob-z-index 0)
+ (mob-time 0)
+ (mob-data ,init-data)
+ (saved-data ,init-data))
+ (lambda* (#:optional (option #f))
+ (define (save-data)
+ (let ((time (get-frame-time)))
+ (cond ((not (= time mob-time))
+ (set! mob-time time)
+ (set! saved-data mob-data)))))
+ (case option
+ ((get-mob-id)
+ mob-id)
+ ((get-z-index)
+ mob-z-index)
+ ((get-type)
+ ,type)
+ ((get-data)
+ (save-data)
+ saved-data)
+ (else
+ (save-data)
+ (let ((res (,fun-name mob-id mob-data)))
+ (set! mob-z-index (car res))
+ (set! mob-data (cadr res))))))))
+
+(define-macro (define-mob-function head . body)
+ (let ((fun-name (car head))
+ (attr (map (lambda (a) (if (list? a) a (list a #f))) (cdr head)))
+ (mob-id-symbol (gensym))
+ (mob-id-z-index (gensym))
(data-symbol (gensym)))
- `(let ((,mob-id-symbol (gensym))
- (,type-symbol ,type)
- (,time-symbol 0)
- (,data-symbol '())
- ,@attr)
- (lambda* (#:optional (option #f))
+ `(define (,fun-name ,mob-id-symbol ,data-symbol)
+ (let ((,mob-id-z-index 0))
(define (kill-me)
(hide-mob-hash ,mob-id-symbol))
- (define (save-data)
- (let ((time (get-frame-time)))
- (cond ((not (= time ,time-symbol))
- (set! ,time-symbol time)
- (set! ,data-symbol ,(cons 'list (map (lambda (x) `(cons ',(car x) ,(car x))) publish)))))))
- (define (get-data)
- ,data-symbol)
- (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-symbol)
- ((get-type)
- ,type-symbol)
- ((get-data)
- (save-data)
- ,data-symbol)
- (else
- (save-data)
- (catch #t
- (lambda () ,@body)
- (lambda (key . args) #f))))))))
+ (define* (translate x y #:optional (z 0))
+ (cond ((3d-mode?)
+ (translate-mob x y z))
+ (else
+ (set! ,mob-id-z-index (+ ,mob-id-z-index z))
+ (translate-mob x y))))
+ (let ,attr
+ ,@(map
+ (lambda (a)
+ `(let ((val (assoc-ref ,data-symbol ',(car a))))
+ (cond (val (set! ,(car a) val)))))
+ attr)
+ (catch #t
+ (lambda* () ,@body)
+ (lambda (key . args) #f))
+ (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
(define-macro (define-mob mob-head . body)
- (let ((name (car mob-head)) (attr (cdr mob-head)))
- `(define ,(string->symbol (string-concatenate (list "make-" (symbol->string name))))
- (lambda* ,(if (null? attr) '() `(#:key ,@attr))
- (the-mob ',name () ,attr ,@body)))))
+ (let* ((name (car mob-head)) (attr (cdr mob-head))
+ (fun-name (get-mob-function-name name)))
+ `(begin
+ (define-mob-function ,(cons fun-name attr) ,@body)
+ (define ,(string->symbol (string-concatenate (list "make-" (symbol->string name))))
+ (lambda* ,(if (null? attr) '() `(#:key ,@attr))
+ (the-mob ',name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)) ,fun-name))))))
(define-macro (lambda-mob attr . body)
- `(the-mob 'undefined ,attr '() ,@body))
+ (let ((fun-name (gensym)))
+ `(begin
+ (define-mob-function ,(cons fun-name attr) ,@body)
+ (the-mob 'undefined '() ,fun-name))))
+
+;;; Functions for checking mobs (collisions and more)
-;;; Collisions
+(define translate-mob translate)
-;; (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))))