]> git.jsancho.org Git - gacela.git/blobdiff - src/gacela.scm
Include mobs in gacela module.
[gacela.git] / src / gacela.scm
index 9bea00ec2506d0487d83dc93734aac7f97ef4a68..a9d67711599253964c158718c3648ce0d29e9e26 100644 (file)
 ;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-;;; Default values for Gacela
+(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)
+  #:export-syntax (game
+                  show-mob
+                  hide-mob
+                  the-mob
+                  define-mob
+                  lambda-mob)
+  #: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-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 *frames-per-second* 20)
 (define *mode* '2d)
 
-
-;;; SDL Initialization Subsystem
-
-(define init-sdl #f)
-(define sdl-on? #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! sdl-on?
-       (lambda ()
-         (if initialized #t #f)))
-
-  (set! quit-sdl
-       (lambda ()
-         (SDL_Quit)
-         (set! initialized #f))))
-
-
-;;; Video Subsystem
-
-(define init-video-mode #f)
-(define video-mode-on? #f)
-(define resize-screen #f)
-(define quit-video-mode #f)
-
-(let ((screen #f) (flags 0))
-  (set! init-video-mode
-       (lambda ()
-         (cond ((not screen)
-                (init-sdl)
-                (let* ((props (get-game-properties))
-                       (width (assoc-ref props 'width)) (height (assoc-ref props 'height))
-                       (bpp (assoc-ref props 'bpp)) (title (assoc-ref props 'title))
-                       (mode (assoc-ref props 'mode))
-                       (info (SDL_GetVideoInfo)))
-                  (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
-                  (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
-                                 (if (= (assoc-ref info 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
-                                 (if (= (assoc-ref info 'blit_hw) 0) 0 SDL_HWACCEL)))
-                  (set! screen (SDL_SetVideoMode width height bpp flags))
-                  (SDL_WM_SetCaption title "")
-                  (init-gl)
-                  (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
-               (else #t))))
-
-  (set! video-mode-on?
-       (lambda () (if screen #t #f)))
-
-  (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! quit-video-mode
-       (lambda ()
-         (SDL_FreeSurface screen)
-         (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 (apply-mode-change)
-  (let* ((props (get-game-properties))
-        (width (assoc-ref props 'width)) (height (assoc-ref props 'height)))
-    (resize-screen-GL width height)))
-
-(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)
-  (glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA)
-  (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)) (h (/ height 2)))
-               (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))))
-
-
-;;; 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 *title*) (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode *mode*))
-  (set! set-game-properties
-       (lambda* (#:key title width height bpp fps mode)
-;        (init-video-mode)
-         (if title
-             (begin
-               (set! ptitle title)
-               (if (video-mode-on?) (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))
-               (if (video-mode-on?) (resize-screen pwidth pheight pbpp))))
-         (if fps
-             (begin
-               (set! pfps fps)
-               (set-frames-per-second fps)))
-         (if mode
-             (begin
-               (set! pmode mode)
-               (if (video-mode-on?)
-                   (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 ,(if (null? code)
-                            `(lambda () #f)
-                            `(lambda () ,@code))))
-     (init-video-mode)
-     (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) (mobs '()))
-  (set! game-loop
-       (lambda ()
-         (set! mobs (get-active-mobs))
-         (set! running #t)
-         (quit! #f)
-         (do () ((quit?))
-           (init-frame-time)
-           (check-connections)
-           (eval-from-clients)
-           (process-events)
-           (cond ((not (quit?))
-                  (cond ((video-mode-on?)
-                         (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
-                         (to-origin)))
-                  (cond ((mobs-changed?) (set! mobs (get-active-mobs))))
-                  (if (procedure? game-code)
-                      (catch #t
-                             (lambda () (game-code))
-                             (lambda (key . args) #f)))
-                  (run-mob-actions mobs)
-                  (cond ((video-mode-on?)
-                         (render-mobs mobs)
-                         (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* (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* (run-mobs #:optional (mobs (get-active-mobs)))
+  (for-each
+   (lambda (m)
+     (glmatrix-block (m)))
+   mobs))
+
+
+;;; Making mobs
+
+(define-macro (the-mob type attr publish . body)
+  (let ((mob-id-symbol (gensym))
+       (type-symbol (gensym))
+       (time-symbol (gensym))
+       (data-symbol (gensym)))
+    `(let ((,mob-id-symbol (gensym))
+          (,type-symbol ,type)
+          (,time-symbol 0)
+          (,data-symbol '())
+          ,@attr)
+       (lambda* (#:optional (option #f))
+        (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-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)))))
+
+(define-macro (lambda-mob attr . body)
+  `(the-mob 'undefined ,attr '() ,@body))
+
+
+;;; Collisions
+
+;; (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)))