From 318a25c1f850ed3ad62bfcee82a13d80ccd86e8e Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 2 Aug 2013 20:24:03 -0400 Subject: Refactor pack-sprite-vertices. --- 2d/sprite.scm | 88 ++++++++++++++++++++++++----------------------------------- 1 file changed, 36 insertions(+), 52 deletions(-) (limited to '2d/sprite.scm') diff --git a/2d/sprite.scm b/2d/sprite.scm index b5c3fec..e2a4e4a 100644 --- a/2d/sprite.scm +++ b/2d/sprite.scm @@ -58,45 +58,46 @@ (define r-offset (packed-struct-offset sprite-vertex r)) (define s-offset (packed-struct-offset sprite-vertex s)) + + (define (pack-sprite-vertices vertices offset x y width height origin-x origin-y - scale-x scale-y rotation u v u2 v2 color) - (let* ((color (rgba->gl-color color)) - (local-x1 (* (- origin-x) scale-x)) - (local-y1 (* (- origin-y) scale-y)) - (local-x2 (* (- width origin-x) scale-x)) - (local-y2 (* (- height origin-y) scale-y)) - (r (vector-ref color 0)) - (g (vector-ref color 1)) - (b (vector-ref color 2)) - (a (vector-ref color 3))) + scale-x scale-y rotation s1 t1 s2 t2 color) + (define (pack-sprite x1 y1 x2 y2 x3 y3 x4 y4) + (let* ((color (rgba->gl-color color)) + (r (vector-ref color 0)) + (g (vector-ref color 1)) + (b (vector-ref color 2)) + (a (vector-ref color 3))) + ;; Vertices go counter clockwise, starting from the top-left + ;; corner. + (pack vertices offset sprite-vertex + x1 y1 + r g b a + s1 t1) + (pack vertices (+ offset 1) sprite-vertex + x2 y2 + r g b a + s1 t2) + (pack vertices (+ offset 2) sprite-vertex + x3 y3 + r g b a + s2 t2) + (pack vertices (+ offset 3) sprite-vertex + x4 y4 + r g b a + s2 t1))) + + (let ((local-x1 (* (- origin-x) scale-x)) + (local-y1 (* (- origin-y) scale-y)) + (local-x2 (* (- width origin-x) scale-x)) + (local-y2 (* (- height origin-y) scale-y))) (if (= rotation 0) (begin (let ((x1 (+ x local-x1)) (y1 (+ y local-y1)) - (x2 (+ x local-x1)) - (y2 (+ y local-y2)) - (x3 (+ x local-x2)) - (y3 (+ y local-y2)) - (x4 (+ x local-x2)) - (y4 (+ y local-y1))) - ;; Vertices go counter clockwise, starting from the top-left - ;; corner. - (pack vertices offset sprite-vertex - x1 y1 - r g b a - u v) - (pack vertices (+ offset 1) sprite-vertex - x2 y2 - r g b a - u v2) - (pack vertices (+ offset 2) sprite-vertex - x3 y3 - r g b a - u2 v2) - (pack vertices (+ offset 3) sprite-vertex - x4 y4 - r g b a - u2 v))) + (x2 (+ x local-x2)) + (y2 (+ y local-y2))) + (pack-sprite x1 y1 x1 y2 x2 y2 x2 y1))) (begin (let* ((sin (sin-degrees rotation)) (cos (cos-degrees rotation)) @@ -108,24 +109,7 @@ (y3 (+ y (* sin local-x2) (* cos local-y2))) (x4 (+ x1 (- x3 x2))) (y4 (- y3 (- y2 y1)))) - ;; Vertices go counter clockwise, starting from the top-left - ;; corner. - (pack vertices offset sprite-vertex - x1 y1 - r g b a - u v) - (pack vertices (+ offset 1) sprite-vertex - x2 y2 - r g b a - u v2) - (pack vertices (+ offset 2) sprite-vertex - x3 y3 - r g b a - u2 v2) - (pack vertices (+ offset 3) sprite-vertex - x4 y4 - r g b a - u2 v)))))) + (pack-sprite x1 y1 x2 y2 x3 y3 x4 y4)))))) (define (draw-sprite-vertices texture vertices size) (let ((pointer-type (tex-coord-pointer-type float))) -- cgit v1.2.3