(define game-running? #f)
(define set-game-code #f)
-(let ((running #f) (game-code #f) (mobs-function (lambda () #f)))
+(let ((running #f) (game-code #f) (mobs '()))
(set! game-loop
(lambda ()
+ (set! mobs (get-active-mobs))
(set! running #t)
(quit? #f)
(do () ((quit?))
(cond ((not (quit?))
(glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
(to-origin)
- (cond ((reload-mobs?)
- (set! mobs-function (get-mobs-function))
- (mobs-reloaded)))
+ (cond ((mobs-changed?) (set! mobs (get-active-mobs))))
(if (procedure? game-code) (game-code))
- (mobs-function)
+ (process-mobs mobs)
(SDL_GL_SwapBuffers)
(delay-frame))))
(set! running #f)))
(cond ((not (null? events))
(let ((event (car events)))
(cond ((= (assoc-ref event 'type) SDL_KEYDOWN) (key-press (assoc-ref event 'key.keysym.sym)))
- ((= (assoc-ref event 'type) SDL_KEYUP) (key-release (assoc-ref event :key.keysym.sym)))))
+ ((= (assoc-ref event 'type) SDL_KEYUP) (key-release (assoc-ref event 'key.keysym.sym)))))
(process-keyboard-events (cdr events)))))
(define key? #f)
;;; Mobs Factory
-(define add-mob-symbol #f)
+(define add-mob-lambda #f)
(define kill-mob-symbol #f)
(define get-active-mobs #f)
-(define reload-mobs? #f)
-(define mobs-reloaded #f)
+(define mobs-changed? #f)
-(let ((active-mobs '()) (reload #f))
- (set! add-mob-symbol
+(let ((active-mobs '()) (changed #f))
+ (set! add-mob-lambda
(lambda (mob)
(pushnew mob active-mobs)
- (set! reload #t)))
+ (set! changed #t)))
(set! kill-mob-symbol
(lambda (mob)
(set! active-mobs (lset-difference eq? active-mobs (list mob)))
- (set! reload #t)))
+ (set! changed #t)))
(set! get-active-mobs
- (lambda () active-mobs))
+ (lambda* (#:optional (refreshed #t))
+ (set! changed (not refreshed))
+ active-mobs))
- (set! reload-mobs?
- (lambda () reload))
+ (set! mobs-changed?
+ (lambda () changed)))
- (set! mobs-reloaded
- (lambda () (set! reload #f))))
(define-macro (add-mob mob)
- `(add-mob-symbol ',mob))
+ `(add-mob-lambda (lambda (option) (,mob option))))
(define-macro (kill-mob mob)
`(kill-mob-symbol ',mob))
-(define-macro (get-mobs-function)
- (let ((mobs (get-active-mobs)))
- (cond ((null? mobs)
- `(lambda () #f))
- (else
- `(lambda () ,@(map (lambda (mob) `(,mob #:render)) mobs))))))
+(define (process-mobs mobs)
+ (for-each (lambda (m) (m #:render)) mobs))
(define-macro (define-mob mob-head . look)
(let ((name (car mob-head)) (attr (cdr mob-head)))