summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2016-02-27 11:15:55 -0500
committerDavid Thompson <dthompson2@worcester.edu>2016-02-27 11:15:55 -0500
commit3636e7b29ad8e00c05489cdb2ab4ffa180c33f5a (patch)
treedee35beb123c24a4734635248c6bd03fa9328401
parentc09f5ceefcddaa2b4d567901c04a27a86cd11969 (diff)
render: sprite-batch: Clean up sprite-batch-add!
* sly/render/sprite-batch.scm (sprite-batch-add!): Take better advantage of local variables to reduce lookups, add a comment, and reformat.
-rw-r--r--sly/render/sprite-batch.scm168
1 files changed, 63 insertions, 105 deletions
diff --git a/sly/render/sprite-batch.scm b/sly/render/sprite-batch.scm
index 1f591c2..8b59cb3 100644
--- a/sly/render/sprite-batch.scm
+++ b/sly/render/sprite-batch.scm
@@ -91,114 +91,72 @@
(sprite-batch-flush! batch context)
(sprite-batch-begin! batch))
+ ;; Establish the texture to use if this is the first sprite in the
+ ;; current batch.
(when (texture-null? (sprite-batch-texture batch))
(set-sprite-batch-texture! batch texture))
- (let* ((size (sprite-batch-size batch))
- (index-offset (* size 6))
- (index-vertex-offset (* size 4))
- (vertex-offset (* size 12)) ; 4 vertices, 3 floats per vertex
- (texture-offset (* size 8))
- (index-buffer (vertex-buffer-data
- (sprite-batch-index-buffer batch)))
- (pos-buffer (vertex-buffer-data
- (sprite-batch-position-buffer batch)))
- (tex-buffer (vertex-buffer-data
- (sprite-batch-texture-buffer batch))))
-
- ;; Add indices.
- (u32vector-set! index-buffer
- index-offset
- index-vertex-offset)
- (u32vector-set! index-buffer
- (+ index-offset 1)
- (+ index-vertex-offset 3))
- (u32vector-set! index-buffer
- (+ index-offset 2)
- (+ index-vertex-offset 2))
- (u32vector-set! index-buffer
- (+ index-offset 3)
- index-vertex-offset)
- (u32vector-set! index-buffer
- (+ index-offset 4)
- (+ index-vertex-offset 2))
- (u32vector-set! index-buffer
- (+ index-offset 5)
- (+ index-vertex-offset 1))
-
- ;; Add vertices.
- ;; Bottom-left
- (f32vector-set! pos-buffer
- vertex-offset
- (rect-left rect))
- (f32vector-set! pos-buffer
- (+ vertex-offset 1)
- (rect-bottom rect))
- (f32vector-set! pos-buffer
- (+ vertex-offset 2)
- 0)
- ;; Bottom-right
- (f32vector-set! pos-buffer
- (+ vertex-offset 3)
- (rect-right rect))
- (f32vector-set! pos-buffer
- (+ vertex-offset 4)
- (rect-bottom rect))
- (f32vector-set! pos-buffer
- (+ vertex-offset 5)
- 0)
- ;; Top-right
- (f32vector-set! pos-buffer
- (+ vertex-offset 6)
- (rect-right rect))
- (f32vector-set! pos-buffer
- (+ vertex-offset 7)
- (rect-top rect))
- (f32vector-set! pos-buffer
- (+ vertex-offset 8)
- 0)
- ;; Top-left
- (f32vector-set! pos-buffer
- (+ vertex-offset 9)
- (rect-left rect))
- (f32vector-set! pos-buffer
- (+ vertex-offset 10)
- (rect-top rect))
- (f32vector-set! pos-buffer
- (+ vertex-offset 11)
- 0)
-
- ;; Add texture coordinates.
- ;; Bottom-left
- (f32vector-set! tex-buffer
- texture-offset
- (texture-s1 texture))
- (f32vector-set! tex-buffer
- (+ texture-offset 1)
- (texture-t1 texture))
- ;; Bottom-right
- (f32vector-set! tex-buffer
- (+ texture-offset 2)
- (texture-s2 texture))
- (f32vector-set! tex-buffer
- (+ texture-offset 3)
- (texture-t1 texture))
- ;; Top-right
- (f32vector-set! tex-buffer
- (+ texture-offset 4)
- (texture-s2 texture))
- (f32vector-set! tex-buffer
- (+ texture-offset 5)
- (texture-t2 texture))
- ;; Top-left
- (f32vector-set! tex-buffer
- (+ texture-offset 6)
- (texture-s1 texture))
- (f32vector-set! tex-buffer
- (+ texture-offset 7)
- (texture-t2 texture))
-
- (set-sprite-batch-size! batch (1+ size))))
+ (let ((size (sprite-batch-size batch)))
+ (let ((index-offset (* size 6))
+ (index-vertex-offset (* size 4))
+ (vertex-offset (* size 12)) ; 4 vertices, 3 floats per vertex
+ (texture-offset (* size 8))
+ (index-buffer (vertex-buffer-data
+ (sprite-batch-index-buffer batch)))
+ (pos-buffer (vertex-buffer-data
+ (sprite-batch-position-buffer batch)))
+ (tex-buffer (vertex-buffer-data
+ (sprite-batch-texture-buffer batch)))
+ (s1 (texture-s1 texture))
+ (t1 (texture-t1 texture))
+ (s2 (texture-s2 texture))
+ (t2 (texture-t2 texture))
+ (top (rect-top rect))
+ (bottom (rect-bottom rect))
+ (left (rect-left rect))
+ (right (rect-right rect)))
+
+ ;; Add indices.
+ (u32vector-set! index-buffer index-offset index-vertex-offset)
+ (u32vector-set! index-buffer (+ index-offset 1) (+ index-vertex-offset 3))
+ (u32vector-set! index-buffer (+ index-offset 2) (+ index-vertex-offset 2))
+ (u32vector-set! index-buffer (+ index-offset 3) index-vertex-offset)
+ (u32vector-set! index-buffer (+ index-offset 4) (+ index-vertex-offset 2))
+ (u32vector-set! index-buffer (+ index-offset 5) (+ index-vertex-offset 1))
+
+ ;; Add vertices.
+ ;; Bottom-left
+ (f32vector-set! pos-buffer vertex-offset left)
+ (f32vector-set! pos-buffer (+ vertex-offset 1) bottom)
+ (f32vector-set! pos-buffer (+ vertex-offset 2) 0.0)
+ ;; Bottom-right
+ (f32vector-set! pos-buffer (+ vertex-offset 3) right)
+ (f32vector-set! pos-buffer (+ vertex-offset 4) bottom)
+ (f32vector-set! pos-buffer (+ vertex-offset 5) 0.0)
+ ;; Top-right
+ (f32vector-set! pos-buffer (+ vertex-offset 6) right)
+ (f32vector-set! pos-buffer (+ vertex-offset 7) top)
+ (f32vector-set! pos-buffer (+ vertex-offset 8) 0.0)
+ ;; Top-left
+ (f32vector-set! pos-buffer (+ vertex-offset 9) left)
+ (f32vector-set! pos-buffer (+ vertex-offset 10) top)
+ (f32vector-set! pos-buffer (+ vertex-offset 11) 0.0)
+
+ ;; Add texture coordinates.
+ ;; Bottom-left
+ (f32vector-set! tex-buffer texture-offset s1)
+ (f32vector-set! tex-buffer (+ texture-offset 1) t1)
+ ;; Bottom-right
+ (f32vector-set! tex-buffer (+ texture-offset 2) s2)
+ (f32vector-set! tex-buffer (+ texture-offset 3) t1)
+ ;; Top-right
+ (f32vector-set! tex-buffer (+ texture-offset 4) s2)
+ (f32vector-set! tex-buffer (+ texture-offset 5) t2)
+ ;; Top-left
+ (f32vector-set! tex-buffer (+ texture-offset 6) s1)
+ (f32vector-set! tex-buffer (+ texture-offset 7) t2)
+
+ (set-sprite-batch-size! batch (1+ size)))))
(define (sprite-batch-reset! batch)
"Reset BATCH to size 0."