render: sprite: Add support for drawing texture regions.
authorDavid Thompson <dthompson2@worcester.edu>
Wed, 18 Oct 2017 01:44:31 +0000 (21:44 -0400)
committerDavid Thompson <dthompson2@worcester.edu>
Wed, 18 Oct 2017 01:51:25 +0000 (21:51 -0400)
* chickadee/render/sprite.scm (%default-texture-region): Rename to...
(%default-texcoords): ...this.
(draw-sprite): Replace #:texture-region arg with #:texcoords.  Add
support for texture regions.  Update docstring.
(draw-nine-patch): Add support for texture regions.
* chickadee/render/font.scm (draw-text): Change #:texture-region to
 #:texcoords in draw-sprite call.

chickadee/render/font.scm
chickadee/render/sprite.scm
doc/api.texi

index 8f89b95..fab1977 100644 (file)
@@ -177,7 +177,7 @@ POSITION using FONT."
           (set-rect-height! rect (vec2-y dimensions))
           (draw-sprite (font-char-texture char)
                        rect
-                       #:texture-region (font-char-region char)
+                       #:texcoords (font-char-region char)
                        #:scale scale
                        #:rotation rotation
                        #:blend-mode blend-mode)
index 5c4ecb4..b07a969 100644 (file)
@@ -353,36 +353,53 @@ void main (void) {
         (lambda ()
           (set! *batch?* #f)))))
 
-(define %default-texture-region (make-rect 0.0 0.0 1.0 1.0))
+(define texture-gl-size
+  (@@ (chickadee render texture) texture-gl-size))
+(define texture-region-gl-size
+  (@@ (chickadee render texture) texture-region-gl-size))
+(define texture-region-gl-rect
+  (@@ (chickadee render texture) texture-region-gl-rect))
+
+(define %default-texcoords (make-rect 0.0 0.0 1.0 1.0))
 
 (define draw-sprite
   (let ((rect (make-rect 0.0 0.0 0.0 0.0)))
     (lambda* (texture region #:key
                       scale rotation (blend-mode 'alpha)
-                      texture-region
+                      (texcoords
+                       (if (texture-region? texture)
+                           (texture-region-gl-rect texture)
+                           %default-texcoords))
                       (shader (force default-shader)))
-      "Draw TEXTURE over the area defined by the rect REGION.  If
-TEXTURE-REGION is specified, the subset of the texture defined by it
-will be drawn rather than the entire texture.  ROTATION specifies by
-how many radians the sprite will be rotated.  SCALE specifies the
-scaling factor.  By default, alpha blending is used but can be changed
-by setting BLEND-MODE.  Finally, advanced users may pass SHADER to
-change the way the sprite is rendered entirely."
-      (let ((region (if (vec2? region)
-                        (begin
-                          (rect-move-vec2! rect region)
-                          (set-rect-width! rect (texture-width texture))
-                          (set-rect-height! rect (texture-height texture))
-                          rect)
-                        region))
-            (texture-region (or texture-region %default-texture-region)))
+      "Draw TEXTURE over the area defined by the rect REGION.  Instead
+of a rect, REGION may be a vec2 representing the position of the
+sprite, in which case the width and height of the sprite corresponds
+to the size of the texture.  TEXTURE may be a texture or a texture
+region.  ROTATION specifies by how many radians the sprite will be
+rotated.  SCALE specifies the scaling factor.  By default, alpha
+blending is used but can be changed by setting BLEND-MODE.  Finally,
+advanced users may pass SHADER to change the way the sprite is
+rendered entirely."
+      (let* ((size (if (texture-region? texture)
+                       (texture-region-gl-size texture)
+                       (texture-gl-size texture)))
+             (texture (if (texture-region? texture)
+                          (texture-region-texture texture)
+                          texture))
+             (region (if (rect? region)
+                         region
+                         (begin
+                           (rect-move-vec2! rect region)
+                           (set-rect-width! rect (f32vector-ref size 0))
+                           (set-rect-height! rect (f32vector-ref size 1))
+                           rect))))
         (if *batch?*
             (draw-sprite-batched texture region
                                  scale rotation blend-mode shader
-                                 texture-region)
+                                 texcoords)
             (draw-sprite-unbatched texture region
                                    scale rotation blend-mode shader
-                                   texture-region))))))
+                                   texcoords))))))
 
 \f
 ;;;
@@ -395,7 +412,6 @@ change the way the sprite is rendered entirely."
     (lambda* (texture region #:key (margin 0)
                       (top-margin margin) (bottom-margin margin)
                       (left-margin margin) (right-margin margin)
-                      (texture-region %default-texture-region)
                       scale rotation (blend-mode 'alpha)
                       (shader (force default-shader)))
       "Draw a \"nine patch\" sprite.  A nine patch sprite renders
@@ -410,7 +426,13 @@ graphical user interfaces.
 MARGIN specifies the margin size for all sides of the nine patch.  To
 make margins of differing sizes, the TOP-MARGIN, BOTTOM-MARGIN,
 LEFT-MARGIN, and RIGHT-MARGIN arguments may be used."
-      (let* ((w (rect-width region))
+      (let* ((texcoords (if (texture-region? texture)
+                            (texture-region-gl-rect texture)
+                            %default-texcoords))
+             (texsize (if (texture-region? texture)
+                          (texture-region-gl-size texture)
+                          (texture-gl-size texture)))
+             (w (rect-width region))
              (h (rect-height region))
              (border-x1 (rect-left region))
              (border-y1 (rect-bottom region))
@@ -420,12 +442,12 @@ LEFT-MARGIN, and RIGHT-MARGIN arguments may be used."
              (fill-y1 (+ border-y1 bottom-margin))
              (fill-x2 (- border-x2 right-margin))
              (fill-y2 (- border-y2 top-margin))
-             (tw (texture-width texture))
-             (th (texture-width texture))
-             (border-s1 (rect-left texture-region))
-             (border-t1 (rect-bottom texture-region))
-             (border-s2 (rect-right texture-region))
-             (border-t2 (rect-top texture-region))
+             (tw (f32vector-ref texsize 0))
+             (th (f32vector-ref texsize 1))
+             (border-s1 (rect-left texcoords))
+             (border-t1 (rect-bottom texcoords))
+             (border-s2 (rect-right texcoords))
+             (border-t2 (rect-top texcoords))
              (fill-s1 (+ border-s1 (/ left-margin tw)))
              (fill-t1 (+ border-t1 (/ bottom-margin th)))
              (fill-s2 (- border-s2 (/ right-margin tw)))
@@ -440,7 +462,7 @@ LEFT-MARGIN, and RIGHT-MARGIN arguments may be used."
           (set-rect-width! trect (- s2 s1))
           (set-rect-height! trect (- t2 t1))
           (draw-sprite texture rect
-                       #:texture-region trect
+                       #:texcoords trect
                        #:scale scale
                        #:rotation rotation
                        #:blend-mode blend-mode
index 1f69059..022c103 100644 (file)
@@ -682,7 +682,7 @@ stored in textures (@pxref{Textures}) and can be used to draw sprites
 via the @code{draw-sprite} procedure.
 
 @deffn {Procedure} draw-sprite @var{texture} @var{region} @
-       [#:scale] [#:rotation] [#:blend-mode alpha] [#:texture-region] @
+       [#:scale] [#:rotation] [#:blend-mode alpha] [#:texcoords] @
        [#:shader]
 
 @end deffn