;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
-;;; Default values for Gacela
-(define *width-screen* 640)
-(define *height-screen* 480)
-(define *bpp-screen* 32)
-(define *frames-per-second* 20)
-
-
-;;; SDL Initialization Subsystem
-(define init-sdl #f)
-(define quit-sdl #f)
-
-(let ((initialized #f))
- (set! init-sdl
- (lambda ()
- (cond ((not initialized) (SDL_Init SDL_INIT_EVERYTHING) (set! initialized #t))
- (else initialized))))
-
- (set! quit-sdl
- (lambda ()
- (SDL_Quit)
- (set! initialized #f))))
-
-
-;;; Video Subsystem
-(define init-video-mode #f)
-(define resize-screen #f)
-(define apply-mode-change #f)
-(define quit-video-mode #f)
-
-(let ((screen #f) (flags 0) (current-width *width-screen*) (current-height *height-screen*) (current-bpp *bpp-screen*))
- (set! init-video-mode
- (lambda* (#:optional (width current-width) (height current-height) (bpp current-bpp))
- (cond ((not screen)
- (init-sdl)
- (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
- (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
- (if (= (assoc-ref (SDL_GetVideoInfo) 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
- (if (= (assoc-ref (SDL_GetVideoInfo) 'blit_hw) 0) 0 SDL_HWACCEL)))
- (set! screen (SDL_SetVideoMode width height bpp flags))
- (init-GL)
- (resize-screen-GL width height)
- (set! current-width width)
- (set! current-height height)
- (set! current-bpp bpp))
- (else #t))))
-
- (set! resize-screen
- (lambda* (width height #:optional (bpp current-bpp))
- (cond (screen (set! screen (SDL_SetVideoMode width height bpp flags))
- (resize-screen-GL width height)))
- (set! current-width width)
- (set! current-height height)))
-
- (set! apply-mode-change
- (lambda () (resize-screen-GL current-width current-height)))
-
- (set! quit-video-mode
- (lambda () (set! screen #f))))
-
-(define (set-2d-mode)
- (cond ((not (3d-mode?))
- (init-video-mode)
- (glDisable GL_DEPTH_TEST)
- (apply-mode-change))))
-
-(define (set-3d-mode)
- (cond ((3d-mode?)
- (init-video-mode)
- (glClearDepth 1)
- (glEnable GL_DEPTH_TEST)
- (glDepthFunc GL_LEQUAL)
- (apply-mode-change))))
-
-(define (3d-mode?)
- (eq? (assoc-ref (get-game-properties) 'mode) '3d))
-
-(define (init-GL)
- (glShadeModel GL_SMOOTH)
- (glClearColor 0 0 0 0)
-; (glClearDepth 1)
-; (glDepthFunc GL_LEQUAL)
-; (glEnable GL_BLEND)
-; (glBlendFunc GL_SRC_ALPHA GL_ONE)
- (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
- #t)
-
-(define (init-lighting)
- (init-video-mode)
- (glEnable GL_LIGHTING))
-
-(define (resize-screen-GL width height)
- (glViewPort 0 0 width height)
- (glMatrixMode GL_PROJECTION)
- (glLoadIdentity)
- (cond ((3d-mode?) (let ((ratio (if (= height 0) width (/ width height))))
- (gluPerspective 45 ratio 0.1 100))) ;0.1
- (else (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
- (glOrtho -w w -h h 0 1))))
- (glMatrixMode GL_MODELVIEW)
- (glLoadIdentity)
- #t)
-
-(define get-current-color #f)
-(define set-current-color #f)
-
-(let ((current-color '(1 1 1 1)))
- (set! get-current-color
- (lambda ()
- current-color))
-
- (set! set-current-color
- (lambda* (red green blue #:optional (alpha 1))
- (set! current-color (list red green blue alpha))
- (glColor4f red green blue alpha))))
-
-(define* (load-image image-file #:key transparent-color)
- (init-video-mode)
- (let ((loaded-image (IMG_Load image-file)))
- (cond ((= loaded-image 0) #f)
- (else (let ((optimized-image (SDL_DisplayFormat loaded-image)))
- (SDL_FreeSurface loaded-image)
- (cond ((= optimized-image 0) #f)
- ((not transparent-color) optimized-image)
- (else (SDL_SetColorKey optimized-image
- SDL_SRCCOLORKEY
- (SDL_MapRGB (surface-format optimized-image)
- (car transparent-color)
- (cadr transparent-color)
- (caddr transparent-color)))
- optimized-image)))))))
-
-
-;;; Audio Subsystem
-(define init-audio #f)
-(define quit-audio #f)
-
-(let ((audio #f))
- (set! init-audio
- (lambda ()
- (cond ((not audio) (begin (init-sdl) (set! audio (Mix_OpenAudio 22050 MIX_DEFAULT_FORMAT 2 4096))))
- (else audio))))
-
- (set! quit-audio
- (lambda ()
- (Mix_CloseAudio)
- (set! audio #f))))
-
-
-;;; GaCeLa Functions
-(define set-frames-per-second #f)
-(define init-frame-time #f)
-(define delay-frame #f)
-
-(let ((time 0) (time-per-frame (/ 1000.0 *frames-per-second*)))
- (set! set-frames-per-second
- (lambda (fps)
- (set! time-per-frame (/ 1000.0 fps))))
-
- (set! init-frame-time
- (lambda ()
- (set! time (SDL_GetTicks))))
-
- (set! delay-frame
- (lambda ()
- (let ((frame-time (- (SDL_GetTicks) time)))
- (cond ((< frame-time time-per-frame)
- (SDL_Delay (- time-per-frame frame-time))))))))
-
-
-(define set-game-properties #f)
-(define get-game-properties #f)
-
-(let ((ptitle "") (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode '2d))
- (set! set-game-properties
- (lambda* (#:key title width height bpp fps mode)
- (init-video-mode)
- (if title (begin (set! ptitle title) (SDL_WM_SetCaption title "")))
- (if (or width height bpp)
- (begin
- (if width (set! pwidth width))
- (if height (set! pheight height))
- (if bpp (set! pbpp bpp))
- (resize-screen pwidth pheight pbpp)))
- (if fps (begin (set! pfps fps) (set-frames-per-second fps)))
- (if mode (begin (set! pmode mode) (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
- (get-game-properties)))
-
- (set! get-game-properties
- (lambda ()
- `((title . ,ptitle) (width . ,pwidth) (height . ,pheight) (bpp . ,pbpp) (fps . ,pfps) (mode . ,pmode)))))
-
-
-(define-macro (run-game . code)
- `(let ((game-function (lambda () ,@code)))
- (init-video-mode)
+(define-module (gacela gacela)
+ #:use-module (gacela events)
+ #:use-module (gacela video)
+ #:use-module (gacela audio)
+ #:use-module (ice-9 optargs)
+ #:export (load-texture
+ load-font
+ *title*
+ *width-screen*
+ *height-screen*
+ *bpp-screen*
+ *frames-per-second*
+ *mode*
+ set-game-properties!
+ get-game-properties
+ init-gacela
+ quit-gacela
+ game-loop
+ game-running?
+ set-game-code
+ show-mob-hash
+ hide-mob-hash
+ hide-all-mobs
+ 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
+ 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))
+
+
+;;; 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-thread #f)
+
+(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 game-loop #f)
-(define game-running? #f)
-(define set-game-code #f)
-
-(let ((running #f) (game-code #f))
- (set! game-loop
- (lambda ()
- (set! running #t)
- (do () ((quit?))
- (init-frame-time)
- (check-connections)
- (eval-from-clients)
- (process-events)
- (cond ((not (quit?))
- (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
- (to-origin)
- (refresh-active-objects)
- (if (functionp game-code) (funcall game-code))
- (render-objects)
- (SDL_GL_SwapBuffers)
- (delay-frame))))
- (set! running #f)))
-
- (set! game-running?
- (lambda ()
- running))
-
- (set! set-game-code
- (lambda (game-function)
- (set! game-code game-function))))
-
-(define (quit-game)
-; (free-all-resources)
- (quit-audio)
- (quit-video-mode)
-; (quit-all-mobs)
-; (kill-all-objects)
-; (clear-events)
-; (quit-events)
- (quit-sdl))
+(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)))))
+
+(define (quit-gacela)
+ (set! game-loop-thread #f)
+ (set! loop-flag #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*)
+ (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 (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
+ (run-in-game-loop (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))))
+ (if fps
+ (set-frames-per-second! fps))
+ (if mode
+ (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
+
+(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)))
+ (else
+ `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
+
+(define-macro (hide-mob mob)
+ (cond ((list? mob)
+ `(let ((m ,mob))
+ (hide-mob-hash (m 'get-mob-id))))
+ (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)
+ (set! current-mob-id #f))
+
+
+;;; Making mobs
+
+(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-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)))))
+; (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)
+ ((get-type)
+ ,type)
+ ((get-data)
+ (save-data)
+ saved-data)
+ (else
+ (save-data)
+ (set! mob-data (,fun-name mob-id mob-data)))))))
+
+(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))
+ (data-symbol (gensym)))
+ `(define (,fun-name ,mob-id-symbol ,data-symbol)
+ (define (kill-me)
+ (hide-mob-hash ,mob-id-symbol))
+ (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 ,@(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 ,(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)
+ (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)
+
+(define (map-mobs fun)
+ (let ((mobs (filter (lambda (m) (not (eq? (m 'get-mob-id) (get-current-mob-id)))) (get-active-mobs))))
+ (map fun mobs)))
+
+(define-macro (define-checking-mobs head mob-attr . body)
+ `(define ,head
+
+;; (define-macro (lambda-mob-data attr . body)
+;; `(lambda ,attr ,@body))
+
+;; (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)))