From 2950dfa6e1d140a90ad19e1f1c0b6f737f3efe7f Mon Sep 17 00:00:00 2001 From: David Thompson Date: Tue, 17 Oct 2017 21:44:31 -0400 Subject: render: sprite: Add support for drawing texture regions. * 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 | 2 +- chickadee/render/sprite.scm | 78 +++++++++++++++++++++++++++++---------------- doc/api.texi | 2 +- 3 files changed, 52 insertions(+), 30 deletions(-) diff --git a/chickadee/render/font.scm b/chickadee/render/font.scm index 8f89b95..fab1977 100644 --- a/chickadee/render/font.scm +++ b/chickadee/render/font.scm @@ -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) diff --git a/chickadee/render/sprite.scm b/chickadee/render/sprite.scm index 5c4ecb4..b07a969 100644 --- a/chickadee/render/sprite.scm +++ b/chickadee/render/sprite.scm @@ -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)))))) ;;; @@ -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 diff --git a/doc/api.texi b/doc/api.texi index 1f69059..022c103 100644 --- a/doc/api.texi +++ b/doc/api.texi @@ -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 -- cgit v1.2.3