From df2872c456185090f3b4a7ceba408d576526ba32 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Wed, 17 Jan 2018 13:33:10 -0500 Subject: render: sprite: Fix bug in rendering texture regions. The texture-rect procedure contains the pixel location of the texture region, so if you use that rect to render a sprite the result is a sprite offset by its location in the greater texture, which is very very very wrong. * chickadee/render/sprite.scm (draw-sprite): Dynamically configure rendering rect based on the texture region's width and height. --- chickadee/render/sprite.scm | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/chickadee/render/sprite.scm b/chickadee/render/sprite.scm index c284ff4..78a2ce5 100644 --- a/chickadee/render/sprite.scm +++ b/chickadee/render/sprite.scm @@ -341,7 +341,8 @@ void main (void) { (define %default-scale (vec2 1.0 1.0)) (define draw-sprite - (let ((matrix (make-null-matrix4))) + (let ((matrix (make-null-matrix4)) + (%rect (make-rect 0.0 0.0 0.0 0.0))) (lambda* (texture position #:key @@ -349,7 +350,12 @@ void main (void) { (scale %default-scale) (rotation 0.0) (blend-mode 'alpha) - (rect (texture-rect texture)) + ;; Default to an area that is the same size of the + ;; texture. 99% of the time that's what you want. + (rect (let ((r (texture-rect texture))) + (set-rect-width! %rect (rect-width r)) + (set-rect-height! %rect (rect-height r)) + %rect)) (shader (force default-shader))) "Draw TEXTURE at POSITION. @@ -368,7 +374,7 @@ rendered entirely." #:position position #:rotation rotation #:scale scale) - (draw-sprite* texture (texture-rect texture) matrix + (draw-sprite* texture rect matrix #:blend-mode blend-mode #:shader shader)))) -- cgit v1.2.3