]> git.jsancho.org Git - gacela.git/blobdiff - src/gacela.scm
Frames per second.
[gacela.git] / src / gacela.scm
index a45337a1ad41cd14d92660981ce632faa47b3a70..256e940c78c64f9df172551a3698b14428288fd6 100644 (file)
 ;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+(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
+           init-gacela
+           quit-gacela
+           game-loop
+           game-running?
+           set-game-code)
+  #:export-syntax (game)
+  #: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))
+
+
 ;;; Default values for Gacela
 
 (define *title* "Gacela")
 
 (define resources-cache (make-weak-value-hash-table))
 
-(define from-cache #f)
-(define into-cache #f)
-
-(let ()
-  (set! from-cache
-       (lambda (key)
-         (hash-ref resources-cache key)))
-
-  (set! into-cache
-       (lambda (key res)
-         (hash-set! resources-cache key res))))
-
-
-;;; GaCeLa Functions
-
-(define set-frames-per-second #f)
-(define init-frame-time #f)
-(define get-frame-time #f)
-(define delay-frame #f)
+(define (from-cache key)
+  (hash-ref resources-cache key))
 
-(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))))
+(define (into-cache key res)
+  (hash-set! resources-cache key res))
 
-  (set! init-frame-time
-       (lambda ()
-         (set! time (SDL_GetTicks))))
+(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)))))))
 
-  (set! get-frame-time
-       (lambda ()
-         time))
+(use-cache-with (gacela video) load-texture)
+(use-cache-with (gacela video) load-font)
 
-  (set! delay-frame
-       (lambda ()
-         (let ((frame-time (- (SDL_GetTicks) time)))
-           (cond ((< frame-time time-per-frame)
-                  (SDL_Delay (- time-per-frame frame-time))))))))
 
+;;; Game Properties
 
 (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 ((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)))))
+
+
+;;; Main Loop
+
+(define loop-flag #f)
+(define game-code #f)
+
+(define-macro (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))
-  (set! game-loop
-       (lambda ()
-         (refresh-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)))
-                  (refresh-active-mobs)
-                  (if (procedure? game-code)
-                      (catch #t
-                             (lambda () (game-code))
-                             (lambda (key . args) #f)))
-                  (cond ((video-mode-on?)
-                         (run-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 (init-gacela)
+  (call-with-new-thread (lambda () (game))))
+
+(define (quit-gacela)
+  (set! loop-flag #f))
+
+(define (game-loop)
+;        (refresh-active-mobs)
+  (set! loop-flag #t)
+  (init-video 640 480 32)
+  (while loop-flag
+        (init-frame-time)
+;          (check-connections)
+        (process-events)
+        (cond ((not (quit?))
+               (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))