summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2017-01-10 19:40:31 -0500
committerDavid Thompson <dthompson2@worcester.edu>2017-01-10 19:43:01 -0500
commitfade3fb9bff62cb319cff5d635521f828ddd8ea5 (patch)
tree1ae848287c7c2c46eeca421f72e4f747e6c1c39d
parent6e03a586d8f61cc3cd44a8f9e6d3cbcdd18cbb4e (diff)
render: sprite: Change draw-sprite to use rects.
-rw-r--r--chickadee/render/sprite.scm79
1 files changed, 47 insertions, 32 deletions
diff --git a/chickadee/render/sprite.scm b/chickadee/render/sprite.scm
index b23130a..11b8724 100644
--- a/chickadee/render/sprite.scm
+++ b/chickadee/render/sprite.scm
@@ -20,6 +20,7 @@
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-11)
#:use-module (chickadee math matrix)
+ #:use-module (chickadee math rect)
#:use-module (chickadee math vector)
#:use-module (chickadee render)
#:use-module (chickadee render shader)
@@ -68,14 +69,12 @@ void main (void) {
(force texcoord-buffer))))
(tmp-matrix (make-null-matrix4))
(mvp (make-null-matrix4)))
- (lambda (texture position center width height
- scale rotation blend-mode shader
- s1 t1 s2 t2)
+ (lambda (texture dimensions scale rotation blend-mode shader texture-rect)
(with-mapped-vertex-buffer (force vertex-buffer)
- (let* ((x1 (- (vx center)))
- (y1 (- (vy center)))
- (x2 (+ x1 width))
- (y2 (+ y1 height))
+ (let* ((x1 0)
+ (y1 0)
+ (x2 (rect-width dimensions))
+ (y2 (rect-height dimensions))
(bv (vertex-buffer-data (force vertex-buffer))))
(f32vector-set! bv 0 x1)
(f32vector-set! bv 1 y1)
@@ -86,7 +85,11 @@ void main (void) {
(f32vector-set! bv 6 x1)
(f32vector-set! bv 7 y2)))
(with-mapped-vertex-buffer (force texcoord-buffer)
- (let ((bv (vertex-buffer-data (force texcoord-buffer))))
+ (let ((s1 (rect-left texture-rect))
+ (t1 (rect-bottom texture-rect))
+ (s2 (rect-right texture-rect))
+ (t2 (rect-top texture-rect))
+ (bv (vertex-buffer-data (force texcoord-buffer))))
(f32vector-set! bv 0 s1)
(f32vector-set! bv 1 t1)
(f32vector-set! bv 2 s2)
@@ -102,7 +105,7 @@ void main (void) {
(when scale
(matrix4-scale! tmp-matrix scale)
(matrix4-mult! mvp mvp tmp-matrix))
- (matrix4-translate! tmp-matrix position)
+ (matrix4-translate! tmp-matrix (rect-x dimensions) (rect-y dimensions))
(matrix4-mult! mvp mvp tmp-matrix)
(matrix4-mult! mvp mvp (current-projection))
(with-blend-mode blend-mode
@@ -171,8 +174,8 @@ void main (void) {
(define sprite-batch-add!
(let ((tmp-matrix (make-null-matrix4))
(matrix (make-null-matrix4)))
- (lambda (batch texture position center width height
- scale rotation blend-mode shader s1 t1 s2 t2)
+ (lambda (batch texture dimensions scale rotation blend-mode
+ shader texture-rect)
;; Expand the buffers when necessary.
(when (sprite-batch-full? batch)
(double-sprite-batch-size! batch))
@@ -193,10 +196,14 @@ void main (void) {
(indices (vertex-buffer-data (sprite-batch-index-buffer batch)))
(vertices (vertex-buffer-data (sprite-batch-position-buffer batch)))
(texcoords (vertex-buffer-data (sprite-batch-texture-buffer batch)))
- (local-x1 (- (vx center)))
- (local-y1 (- (vy center)))
- (local-x2 (+ local-x1 width))
- (local-y2 (+ local-y1 height)))
+ (local-x1 0)
+ (local-y1 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)))
(matrix4-identity! matrix)
(when rotation
(matrix4-rotate-z! tmp-matrix rotation)
@@ -204,7 +211,7 @@ void main (void) {
(when scale
(matrix4-scale! tmp-matrix scale)
(matrix4-mult! matrix matrix tmp-matrix))
- (matrix4-translate! tmp-matrix position)
+ (matrix4-translate! tmp-matrix (rect-x dimensions) (rect-y dimensions))
(matrix4-mult! matrix matrix tmp-matrix)
(let-values (((world-x1 world-y1)
(transform matrix local-x1 local-y1))
@@ -248,12 +255,12 @@ void main (void) {
(define *batch?* #f)
(define %batch (delay (make-sprite-batch 256)))
-(define (draw-sprite-batched texture position center width height
+(define (draw-sprite-batched texture dimensions
scale rotation blend-mode shader
- s1 t1 s2 t2)
- (sprite-batch-add! (force %batch) texture position center width height
+ texture-rect)
+ (sprite-batch-add! (force %batch) texture dimensions
scale rotation blend-mode shader
- s1 t1 s2 t2))
+ texture-rect))
(define-syntax-rule (with-batched-sprites body ...)
(dynamic-wind
@@ -266,17 +273,25 @@ void main (void) {
(lambda ()
(set! *batch?* #f))))
-(define* (draw-sprite texture position #:key
- (center (vector2 0 0))
- (width (texture-width texture))
- (height (texture-height texture))
+(define draw-sprite
+ (let ((rect (make-rect 0.0 0.0 0.0 0.0))
+ (default-texture-rect (make-rect 0.0 0.0 1.0 1.0)))
+ (lambda* (texture dimensions #:key
scale rotation (blend-mode 'alpha)
- (s1 0.0) (t1 0.0) (s2 1.0) (t2 1.0)
+ texture-rect
(shader (force default-shader)))
- (if *batch?*
- (draw-sprite-batched texture position center width height
- scale rotation blend-mode shader
- s1 t1 s2 t2 )
- (draw-sprite-unbatched texture position center width height
- scale rotation blend-mode shader
- s1 t1 s2 t2)))
+ (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)))
+ (if *batch?*
+ (draw-sprite-batched texture dimensions
+ scale rotation blend-mode shader
+ texture-rect)
+ (draw-sprite-unbatched texture dimensions
+ scale rotation blend-mode shader
+ texture-rect))))))