-(use-cache-with (gacela video) load-texture)
-(use-cache-with (gacela video) load-font)
-
-
-;;; Main Loop
-
-(define loop-flag #f)
-(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)
- `(lambda () ,@code))))
- (set-game-code game-function)
- (cond ((not (game-running?))
- (game-loop)))))
-
-(define (init-gacela)
- (hide-all-mobs)
- (set-game-code (lambda () #f))
- (cond ((not game-loop-thread)
- (set! game-loop-thread (call-with-new-thread (lambda () (game))))))
- (while (not loop-flag))
- #t)
-
-(define (quit-gacela)
- (hide-all-mobs)
- (set-game-code (lambda () #f))
- (set! game-loop-thread #f)
- (set! loop-flag #f))
-
-(define (game-loop)
- (refresh-active-mobs)
- (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)
- (process-events)
- (cond ((quit-signal?)
- (quit-gacela))
- (else
- (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 (gacela-script args)
- (while loop-flag (sleep 1)))
-
-(define (game-running?)
- loop-flag)
-
-(define (set-game-code game-function)
- (set! game-code game-function))
-
-
-;;; Game Properties
-
-(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 . ,(get-frames-per-second)) (mode . ,(if (3d-mode?) '3d '2d))))
-
-
-;;; Mobs Factory
-
-(define mobs-table (make-hash-table))
-(define active-mobs '())
-(define mobs-changed #f)
-
-(define (show-mob-hash mob)
- (hash-set! mobs-table (mob 'get-mob-id) mob)
- (set! mobs-changed #t))
-
-(define (hide-mob-hash mob-id)
- (hash-remove! mobs-table mob-id)
- (set! mobs-changed #t))
-
-(define (refresh-active-mobs)
- (cond (mobs-changed
- (set! mobs-changed #f)
- (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
-
-(define (get-active-mobs)
- active-mobs)
-
-(define (hide-all-mobs)
- (set! mobs-changed #t)
- (hash-clear! mobs-table))
-
-(define (mobs-changed?)
- mobs-changed)
-
-
-(define-macro (show-mob mob)
- (cond ((list? mob)
- `(let ((m ,mob))
- (show-mob-hash m)))