]> git.jsancho.org Git - gacela.git/blobdiff - src/video.scm
Now mobs use let*
[gacela.git] / src / video.scm
index 69c9aa2639944cde2f984482f3ed8d3a1e060671..4b211e92cecc16b3b571cf52a54d08992531a6b2 100644 (file)
            get-screen-height
            get-screen-width
            get-screen-bpp
+           set-screen-bpp!
            resize-screen
            quit-video
            clear-screen
            flip-screen
+           set-screen-title!
+           get-screen-title
            set-2d-mode
            set-3d-mode
            3d-mode?
-           set-frames-per-second
+           get-frames-per-second
+           set-frames-per-second!
            init-frame-time
            get-frame-time
            delay-frame
@@ -43,6 +47,7 @@
            progn-textures
            draw
            load-texture
+           get-texture-properties
            draw-texture
            draw-line
            draw-quad
            set-camera
            camera-look
            load-font
-           render-text))
+           render-text)
+  #:export-syntax (glmatrix-block)
+  #:re-export (glPushMatrix
+              glPopMatrix))
+
 
 
 ;;; Screen
 (define screen #f)
 (define flags 0)
 
-(define (init-video width height bpp #:key (mode '2d) (title ""))
-  (cond ((not screen)
-        (SDL_Init SDL_INIT_VIDEO)
-        (let ((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))))))
+(define* (init-video width height bpp #:key (mode '2d) (title "") (fps 20))
+  (SDL_Init SDL_INIT_VIDEO)
+  (let ((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))
+    (set-screen-title! title)
+    (set-frames-per-second! fps)
+    (init-gl)
+    (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
 
 (define (get-screen-height)
   (surface-h screen))
   (surface-w screen))
 
 (define (get-screen-bpp)
-  (surface-format-BytesPerPixel screen))
+  (* (surface-format-BytesPerPixel screen) 8))
+
+(define (set-screen-bpp! bpp)
+  (cond (screen
+        (set! screen (SDL_SetVideoMode (get-screen-width) (get-screen-height) bpp flags)))))
 
 (define (resize-screen width height)
   (cond (screen
   (SDL_GL_SwapBuffers))
 
 
+(define screen-title "")
+
+(define (set-screen-title! title)
+  (set! screen-title title)
+  (SDL_WM_SetCaption title ""))
+
+(define (get-screen-title)
+  screen-title)
+
+
 (define mode '2d)
 
 (define (set-2d-mode)
 
 ;;; Frames per second
 
-(define set-frames-per-second #f)
-(define init-frame-time #f)
-(define get-frame-time #f)
-(define delay-frame #f)
+(define time 0)
+(define frames-per-second 20)
+(define time-per-frame 50)   ;in ms
 
-;; (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 (get-frames-per-second)
+  frames-per-second)
 
-;;   (set! init-frame-time
-;;     (lambda ()
-;;       (set! time (SDL_GetTicks))))
+(define (set-frames-per-second! fps)
+  (set! frames-per-second fps)
+  (set! time-per-frame (/ 1000.0 fps)))
 
-;;   (set! get-frame-time
-;;     (lambda ()
-;;       time))
+(define (init-frame-time)
+  (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 (get-frame-time)
+  time)
+
+(define (delay-frame)
+  (let ((frame-time (- (SDL_GetTicks) time)))
+    (cond ((< frame-time time-per-frame)
+          (SDL_Delay (- time-per-frame frame-time))))))
 
 
 ;;; Drawing
             (set-texture-size! texture real-w real-h)
             texture))))))
 
+(define (get-texture-properties texture)
+  `((width . ,(texture-w texture)) (height . ,(texture-h texture))))
+
 (define* (draw-texture texture #:optional (zoom 1))
   (cond (texture
         (let ((width (texture-w texture))
      (glNormal3f 0 1 0)
      (draw-quad (list size size size) (list -size size size) (list -size size -size) (list size size -size) #:texture (or texture-3 texture) #:color (or color-3 color))
      (glNormal3f 0 -1 0)
-     (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) :texture (or texture-4 texture) #:color (or color-4 color))
+     (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) #:texture (or texture-4 texture) #:color (or color-4 color))
      (glNormal3f 1 0 0)
-     (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) :texture (or texture-5 texture) #:color (or color-5 color))
+     (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) #:texture (or texture-5 texture) #:color (or color-5 color))
      (glNormal3f -1 0 0)
-     (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) :texture (or texture-6 texture) #:color (or color-6 color)))))
+     (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) #:texture (or texture-6 texture) #:color (or color-6 color)))))
 
 (define* (translate x y #:optional (z 0))
   (glTranslatef x y z))
   (glLoadIdentity)
   (cond ((3d-mode?) (camera-look))))
 
+(define-macro (glmatrix-block . code)
+  `(let ((result #f))
+     (glPushMatrix)
+     (set! result (begin ,@code))
+     (glPopMatrix)
+     result))
+
 
 ;;; Lights
 
-(define* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
-  (init-lighting)
-  (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
-  (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
-  (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
-  (and turn-on (glEnable id))
-  id)
+;; (define* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
+;;   (init-lighting)
+;;   (and light (glLightfv id GL_DIFFUSE (car light) (cadr light) (caddr light) (cadddr light)))
+;;   (and light position (glLightfv GL_POSITION (car position) (cadr position) (caddr position) (cadddr position)))
+;;   (and ambient (glLightfv id GL_AMBIENT (car ambient) (cadr ambient) (caddr ambient) (cadddr ambient)))
+;;   (and turn-on (glEnable id))
+;;   id)
 
 
 ;;; Camera
 ;;; Text and fonts
 
 (define* (load-font font-file #:key (size 40) (encoding ft_encoding_unicode))
-  (let ((font (ftglCreateTextureFont font-file)))
+  (let ((font (ftglCreateTextureFont font-file size)))
     (ftglSetFontFaceSize font size 72)
     (ftglSetFontCharMap font encoding)
     font))
 
 (define* (render-text text font #:key (size #f))
-  (cond (size (ftglSetFontFaceSize font size 72)))
+  (cond (size
+        (cond ((not (= (ftglGetFontFaceSize font) size))
+               (ftglSetFontFaceSize font size 72))))
+       ((not (= (ftglGetFontFaceSize font) (font-size font)))
+        (ftglSetFontFaceSize font (font-size font) 72)))
   (ftglRenderFont font text FTGL_RENDER_ALL))