diff options
-rw-r--r-- | chickadee/render/sprite.scm | 110 |
1 files changed, 59 insertions, 51 deletions
diff --git a/chickadee/render/sprite.scm b/chickadee/render/sprite.scm index 8dabd07..2288edf 100644 --- a/chickadee/render/sprite.scm +++ b/chickadee/render/sprite.scm @@ -72,12 +72,12 @@ void main (void) { (tmp-matrix (make-null-matrix4)) (mvp (make-null-matrix4)) (position (vec2 0 0))) - (lambda (texture dimensions scale rotation blend-mode shader texture-rect) + (lambda (texture region scale rotation blend-mode shader texture-region) (with-mapped-vertex-buffer (force vertex-buffer) (let* ((x1 0) (y1 0) - (x2 (rect-width dimensions)) - (y2 (rect-height dimensions)) + (x2 (rect-width region)) + (y2 (rect-height region)) (bv (vertex-buffer-data (force vertex-buffer)))) (f32vector-set! bv 0 x1) (f32vector-set! bv 1 y1) @@ -88,10 +88,10 @@ void main (void) { (f32vector-set! bv 6 x1) (f32vector-set! bv 7 y2))) (with-mapped-vertex-buffer (force texcoord-buffer) - (let ((s1 (rect-left texture-rect)) - (t1 (rect-bottom texture-rect)) - (s2 (rect-right texture-rect)) - (t2 (rect-top texture-rect)) + (let ((s1 (rect-left texture-region)) + (t1 (rect-bottom texture-region)) + (s2 (rect-right texture-region)) + (t2 (rect-top texture-region)) (bv (vertex-buffer-data (force texcoord-buffer)))) (f32vector-set! bv 0 s1) (f32vector-set! bv 1 t1) @@ -108,8 +108,8 @@ void main (void) { (when scale (matrix4-scale! tmp-matrix scale) (matrix4-mult! mvp mvp tmp-matrix)) - (set-vec2-x! position (rect-x dimensions)) - (set-vec2-y! position (rect-y dimensions)) + (set-vec2-x! position (rect-x region)) + (set-vec2-y! position (rect-y region)) (matrix4-translate! tmp-matrix position) (matrix4-mult! mvp mvp tmp-matrix) (matrix4-mult! mvp mvp (current-projection)) @@ -213,8 +213,8 @@ void main (void) { (u32vector-set! offset-bv 0 offset)) (define (offset) (u32vector-ref offset-bv 0)) - (lambda (batch texture dimensions scale rotation blend-mode - shader texture-rect) + (lambda (batch texture region scale rotation blend-mode + shader texture-region) ;; Expand the buffers when necessary. (when (sprite-batch-full? batch) (double-sprite-batch-size! batch)) @@ -237,12 +237,12 @@ void main (void) { (texcoords (vertex-buffer-data (sprite-batch-texture-buffer batch))) (local-x1 0.0) (local-y1 0.0) - (local-x2 (rect-width dimensions)) - (local-y2 (rect-height dimensions)) - (s1 (rect-left texture-rect)) - (t1 (rect-bottom texture-rect)) - (s2 (rect-right texture-rect)) - (t2 (rect-top texture-rect))) + (local-x2 (rect-width region)) + (local-y2 (rect-height region)) + (s1 (rect-left texture-region)) + (t1 (rect-bottom texture-region)) + (s2 (rect-right texture-region)) + (t2 (rect-top texture-region))) (matrix4-identity! matrix) (when rotation (matrix4-rotate-z! tmp-matrix rotation) @@ -250,8 +250,8 @@ void main (void) { (when scale (matrix4-scale! tmp-matrix scale) (matrix4-mult! matrix matrix tmp-matrix)) - (set-vec2-x! position (rect-x dimensions)) - (set-vec2-y! position (rect-y dimensions)) + (set-vec2-x! position (rect-x region)) + (set-vec2-y! position (rect-y region)) (matrix4-translate! tmp-matrix position) (matrix4-mult! matrix matrix tmp-matrix) (set-vec2-x! world1 local-x1) @@ -307,14 +307,15 @@ void main (void) { (define *batch?* #f) (define %batch (delay (make-sprite-batch 256))) -(define (draw-sprite-batched texture dimensions +(define (draw-sprite-batched texture region scale rotation blend-mode shader - texture-rect) - (sprite-batch-add! (force %batch) texture dimensions + texture-region) + (sprite-batch-add! (force %batch) texture region scale rotation blend-mode shader - texture-rect)) + texture-region)) (define-syntax-rule (with-batched-sprites body ...) + "Use batched rendering for all draw-sprite calls within BODY." (if *batch?* (begin body ...) (dynamic-wind @@ -327,29 +328,36 @@ void main (void) { (lambda () (set! *batch?* #f))))) -(define %default-texture-rect (make-rect 0.0 0.0 1.0 1.0)) +(define %default-texture-region (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 dimensions #:key + (lambda* (texture region #:key scale rotation (blend-mode 'alpha) - texture-rect + texture-region (shader (force default-shader))) - (let ((dimensions (if (vec2? dimensions) - (begin - (rect-move-vec2! rect dimensions) - (set-rect-width! rect (texture-width texture)) - (set-rect-height! rect (texture-height texture)) - rect) - dimensions)) - (texture-rect (or texture-rect %default-texture-rect))) + "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))) (if *batch?* - (draw-sprite-batched texture dimensions + (draw-sprite-batched texture region scale rotation blend-mode shader - texture-rect) - (draw-sprite-unbatched texture dimensions + texture-region) + (draw-sprite-unbatched texture region scale rotation blend-mode shader - texture-rect)))))) + texture-region)))))) ;;; @@ -359,10 +367,10 @@ void main (void) { (define draw-nine-patch (let ((rect (make-rect 0.0 0.0 0.0 0.0)) (trect (make-rect 0.0 0.0 0.0 0.0))) - (lambda* (texture dimensions #:key (margin 0) + (lambda* (texture region #:key (margin 0) (top-margin margin) (bottom-margin margin) (left-margin margin) (right-margin margin) - (texture-rect %default-texture-rect) + (texture-region %default-texture-region) scale rotation (blend-mode 'alpha) (shader (force default-shader))) "Draw a \"nine patch\" sprite. A nine patch sprite renders @@ -377,22 +385,22 @@ 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 dimensions)) - (h (rect-height dimensions)) - (border-x1 (rect-left dimensions)) - (border-y1 (rect-bottom dimensions)) - (border-x2 (rect-right dimensions)) - (border-y2 (rect-top dimensions)) + (let* ((w (rect-width region)) + (h (rect-height region)) + (border-x1 (rect-left region)) + (border-y1 (rect-bottom region)) + (border-x2 (rect-right region)) + (border-y2 (rect-top region)) (fill-x1 (+ border-x1 left-margin)) (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-rect)) - (border-t1 (rect-bottom texture-rect)) - (border-s2 (rect-right texture-rect)) - (border-t2 (rect-top texture-rect)) + (border-s1 (rect-left texture-region)) + (border-t1 (rect-bottom texture-region)) + (border-s2 (rect-right texture-region)) + (border-t2 (rect-top texture-region)) (fill-s1 (+ border-s1 (/ left-margin tw))) (fill-t1 (+ border-t1 (/ bottom-margin th))) (fill-s2 (- border-s2 (/ right-margin tw))) @@ -407,7 +415,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-rect trect + #:texture-region trect #:scale scale #:rotation rotation #:blend-mode blend-mode |