#:use-module (gacela video)
#:use-module (gacela audio)
#:use-module (ice-9 optargs)
- #:export (load-texture
- load-font
- *title*
+ #:export (*title*
*width-screen*
*height-screen*
*bpp-screen*
game-loop
gacela-script
game-running?
- set-game-code
show-mob-hash
hide-mob-hash
get-active-mobs
define-mob
lambda-mob
define-checking-mobs)
- #: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
- get-frame-time
- key?
- key-pressed?
- key-released?
+ #:re-export (get-frame-time
3d-mode?))
-;;; Resources Cache
-
-(define resources-cache (make-weak-value-hash-table))
-
-(define (from-cache key)
- (hash-ref resources-cache key))
-
-(define (into-cache key res)
- (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")))))
- `(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)
-
-
;;; Main Loop
-(define loop-flag #f)
-(define game-code #f)
+(define game-loop-flag #f)
(define game-loop-thread #f)
+(define game-loop-procedure #f)
(define-macro (run-in-game-loop proc)
(let ((pgl (string->symbol (string-concatenate (list (symbol->string proc) "-in-game-loop"))))
(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)))))
+ `(set! game-loop-procedure
+ ,(if (null? code)
+ `#f
+ `(lambda (game-elements) ,@code))))
(define (init-gacela)
- (hide-all-mobs)
- (set-game-code (lambda () #f))
+; (hide-all-mobs)
(cond ((not game-loop-thread)
- (set! game-loop-thread (call-with-new-thread (lambda () (game))))))
- (while (not loop-flag))
+ (set! game-loop-thread (call-with-new-thread (lambda () (cond ((not (game-running?)) (game-loop))))))))
+ (while (not game-loop-flag))
#t)
(define (quit-gacela)
- (hide-all-mobs)
- (set-game-code (lambda () #f))
+; (hide-all-mobs)
(set! game-loop-thread #f)
- (set! loop-flag #f))
+ (set! game-loop-flag #f)
+ (quit-video))
(define (game-loop)
- (refresh-active-mobs)
+; (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)
+ (set! game-loop-flag #t)
+ (let loop ((game-elements '()))
+ (cond (game-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)))
+ (process-events)
+ (cond ((quit-signal?)
+ (quit-gacela))
+ (else
+ (clear-screen)
+ (to-origin)
+; (refresh-active-mobs)
+; (run-mobs)
+; (run-extensions)
+ (if game-loop-procedure
+ (catch #t
+ (lambda () (set! game-elements (game-loop-procedure game-elements)))
+ (lambda (key . args) #f)))
+ (process-game-elements game-elements)
+ (flip-screen)
+ (delay-frame)
+ (loop game-elements)))))))
(define (game-running?)
- loop-flag)
+ game-loop-flag)
+
+(define (process-game-elements elements)
+ (cond ((not (list? elements))
+ (process-game-elements (list elements)))
+ (else
+ (draw-meshes (filter (lambda (e) (mesh? e)) elements)))))
+
+(define (draw-meshes meshes)
+ (cond ((null? meshes) #t)
+ (else
+ (catch #t
+ (lambda () (mesh-draw (car meshes)))
+ (lambda (key . args) #f))
+ (draw-meshes (cdr meshes)))))
+
+;;; Extensions to main loop
+
+(define extensions '())
-(define (set-game-code game-function)
- (set! game-code game-function))
+(define (add-extension! proc pri)
+ "Add an extension with a priority to the main loop"
+ (set! extensions
+ (sort (assoc-set! extensions proc pri)
+ (lambda (a b)
+ (< (cdr a) (cdr b))))))
+
+(define (remove-extension! proc)
+ "Remove an extension from the main loop"
+ (set! extensions
+ (assoc-remove! extensions proc)))
+
+(define (run-extensions)
+ (for-each (lambda (x) ((car x))) extensions))
;;; Game Properties
(define *bpp-screen* 32)
(define *frames-per-second* 20)
(define *mode* '2d)
+(define *fullscreen* 'off)
-(define* (set-game-properties! #:key title width height bpp fps mode)
+(define* (set-game-properties! #:key title width height bpp fps mode fullscreen)
(if title
(set-screen-title! title))
(if bpp
(set-frames-per-second! fps))
(if mode
(if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
+ (if fullscreen
+ (set-fullscreen! fullscreen))
(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))))
+ `((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)) (fullscreen . ,(get-fullscreen))))
;;; Mobs Factory
(hash-set! mob-functions mob-name name)))
name))
-(define-macro (the-mob type init-data fun-name)
+(define-macro (the-mob mob-name init-data)
`(let ((mob-id (gensym))
(mob-z-index 0)
(mob-time 0)
((get-z-index)
mob-z-index)
((get-type)
- ,type)
+ (procedure-name ,mob-name))
((get-data)
(save-data)
saved-data)
(assoc-ref saved-data (keyword->symbol option)))
(else
(save-data)
- (let ((res (,fun-name mob-id mob-data)))
+ (let ((res (,mob-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)))
+(define-macro (define-mob-function attr . body)
+ (let ((attr (map (lambda (a) (if (list? a) a (list a #f))) attr))
(mob-id-symbol (gensym))
(mob-id-z-index (gensym))
(data-symbol (gensym)))
- `(define (,fun-name ,mob-id-symbol ,data-symbol)
+ `(lambda (,mob-id-symbol ,data-symbol)
(let ((,mob-id-z-index 0))
(define (kill-me)
(hide-mob-hash ,mob-id-symbol))
(else
(set! ,mob-id-z-index (+ ,mob-id-z-index z))
(translate-mob x y))))
- (let ,attr
+ (let* ,attr
,@(map
(lambda (a)
`(let ((val (assoc-ref ,data-symbol ',(car a))))
(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))
- (fun-name (get-mob-function-name name)))
- `(begin
- (define-mob-function ,(cons fun-name attr) ,@body)
- (define ,name
+ (let* ((name (car mob-head))
+ (attr (cdr mob-head))
+ (make-fun-symbol (gensym))
+ (mob-fun-symbol (gensym))
+ (params-symbol (gensym)))
+ `(define (,name . ,params-symbol)
+ (define ,make-fun-symbol
(lambda* ,(if (null? attr) '() `(#:key ,@attr))
- (the-mob ',name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)) ,fun-name))))))
+ (the-mob ,name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)))))
+ (define ,mob-fun-symbol
+ (define-mob-function ,attr ,@body))
+ (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
+ (apply ,make-fun-symbol ,params-symbol))
+ (else
+ (apply ,mob-fun-symbol ,params-symbol))))))
(define-macro (lambda-mob attr . body)
(let ((fun-name (gensym)))
(define-macro (define-scene name . body)
`(define (,name)
,@body))
+
+
+(module-map (lambda (sym var)
+ (if (not (eq? sym '%module-public-interface))
+ (module-export! (current-module) (list sym))))
+ (current-module))