summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2017-01-12 20:07:37 -0500
committerDavid Thompson <dthompson2@worcester.edu>2017-01-12 20:07:37 -0500
commit027671d62f3453eca3fde5fb46d5270a075121d7 (patch)
treeba8cc45ff6bf40e6fa9e8d3fb771d5f436516c4a
parent2f4eab3c233b4b82521e45c424e3d96910ba6e22 (diff)
render: sprite: Implement dynamic sprite batch resizing.
-rw-r--r--chickadee/render/sprite.scm45
1 files changed, 36 insertions, 9 deletions
diff --git a/chickadee/render/sprite.scm b/chickadee/render/sprite.scm
index f85ae93..bc2ac1d 100644
--- a/chickadee/render/sprite.scm
+++ b/chickadee/render/sprite.scm
@@ -16,6 +16,7 @@
;;; <http://www.gnu.org/licenses/>.
(define-module (chickadee render sprite)
+ #:use-module (rnrs bytevectors)
#:use-module (srfi srfi-4)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-11)
@@ -126,25 +127,51 @@ void main (void) {
(blend-mode sprite-batch-blend-mode set-sprite-batch-blend-mode!)
(shader sprite-batch-shader set-sprite-batch-shader!)
(size sprite-batch-size set-sprite-batch-size!)
- (capacity sprite-batch-capacity)
- (index-buffer sprite-batch-index-buffer)
- (position-buffer sprite-batch-position-buffer)
- (texture-buffer sprite-batch-texture-buffer)
- (vertex-array sprite-batch-vertex-array))
+ (capacity sprite-batch-capacity set-sprite-batch-capacity!)
+ (index-buffer sprite-batch-index-buffer set-sprite-batch-index-buffer!)
+ (position-buffer sprite-batch-position-buffer set-sprite-batch-position-buffer!)
+ (texture-buffer sprite-batch-texture-buffer set-sprite-batch-texture-buffer!)
+ (vertex-array sprite-batch-vertex-array set-sprite-batch-vertex-array!))
-(define (make-sprite-batch capacity)
- "Make a sprite batch that can hold CAPACITY sprites."
+(define (init-sprite-batch batch capacity)
(let* ((index (make-streaming-vertex-buffer 'index (* capacity 6)))
(pos (make-streaming-vertex-buffer 'vec2 (* capacity 4)))
(tex (make-streaming-vertex-buffer 'vec2 (* capacity 4)))
(va (make-vertex-array index pos tex)))
- (%make-sprite-batch #f #f #f 0 capacity index pos tex va)))
+ (set-sprite-batch-capacity! batch capacity)
+ (set-sprite-batch-index-buffer! batch index)
+ (set-sprite-batch-position-buffer! batch pos)
+ (set-sprite-batch-texture-buffer! batch tex)
+ (set-sprite-batch-vertex-array! batch va)))
+
+(define (make-sprite-batch capacity)
+ "Make a sprite batch that can hold CAPACITY sprites."
+ (let ((batch (%make-sprite-batch #f #f #f 0 0 #f #f #f #f)))
+ (init-sprite-batch batch capacity)
+ batch))
(define (sprite-batch-full? batch)
(= (sprite-batch-capacity batch) (sprite-batch-size batch)))
(define (double-sprite-batch-size! batch)
- #f)
+ (let ((old-index (sprite-batch-index-buffer batch))
+ (old-verts (sprite-batch-position-buffer batch))
+ (old-tex (sprite-batch-texture-buffer batch)))
+ (unmap-vertex-buffer! old-index)
+ (unmap-vertex-buffer! old-verts)
+ (unmap-vertex-buffer! old-tex)
+ (init-sprite-batch batch (* (sprite-batch-capacity batch) 2))
+ (sprite-batch-begin! batch)
+ (let ((new-index (sprite-batch-index-buffer batch))
+ (new-verts (sprite-batch-position-buffer batch))
+ (new-tex (sprite-batch-texture-buffer batch)))
+ (define (copy from to)
+ (bytevector-copy! (vertex-buffer-data from) 0
+ (vertex-buffer-data to) 0
+ (bytevector-length (vertex-buffer-data from))))
+ (copy old-index new-index)
+ (copy old-verts new-verts)
+ (copy old-tex new-tex))))
(define (sprite-batch-reset! batch)
"Reset BATCH to size 0."