summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2020-12-05 15:08:52 -0500
committerDavid Thompson <dthompson2@worcester.edu>2020-12-05 15:08:52 -0500
commit8a11b291ec148441efacc96a2f9f187e4098af20 (patch)
tree467753af72d6e466d2c9a03f7d479d452ab714ba
parenta75936cdff67adfd34fc163ab446b87844f1dcfe (diff)
render: Allow for rendering points with minimal allocation.
-rw-r--r--doc/api.texi8
-rw-r--r--sdl2/render.scm51
2 files changed, 33 insertions, 26 deletions
diff --git a/doc/api.texi b/doc/api.texi
index ee26d9b..de24b07 100644
--- a/doc/api.texi
+++ b/doc/api.texi
@@ -1155,7 +1155,9 @@ current rendering target of @var{renderer}.
@deffn {Procedure} render-draw-lines renderer points
Draw a series of connected lines from @var{points} on the current
-rendering target of @var{renderer}.
+rendering target of @var{renderer}. Optionally, for best performance,
+@var{points} may instead be a bytevector packed with signed 32 bit
+integers, 2 per point (x and y).
@end deffn
@deffn {Procedure} render-draw-point renderer x y
@@ -1165,7 +1167,9 @@ of @var{renderer}.
@deffn {Procedure} render-draw-points renderer points
Draw a multiple points from @var{points} on the current rendering
-target of @var{renderer}.
+target of @var{renderer}. Optionally, for best performance,
+@var{points} may instead be a bytevector packed with signed 32 bit
+integers, 2 per point (x and y).
@end deffn
@deffn {Procedure} render-draw-rect renderer rectangle
diff --git a/sdl2/render.scm b/sdl2/render.scm
index 2320d71..0f5119c 100644
--- a/sdl2/render.scm
+++ b/sdl2/render.scm
@@ -218,35 +218,38 @@ disable it."
(hashq-set! *pointer-cache* bv ptr)
ptr)))
+(define (build-point-bv points)
+ (let ((bv (make-s32vector (* (length points) 2))))
+ (let loop ((points points)
+ (i 0))
+ (match points
+ (() bv)
+ (((x y) . rest)
+ (s32vector-set! bv (* i 2) x)
+ (s32vector-set! bv (+ (* i 1) 1) y)
+ (loop rest (+ i 1)))))))
+
(define (render-draw-lines renderer points)
"Draw lines connecting POINTS on RENDERER."
- (define (fill-bv bv l n)
- (match l
- (() bv)
- (((x y) . r)
- (s32vector-set! bv n x)
- (s32vector-set! bv (+ n 1) y)
- (fill-bv bv r (+ 2 n)))))
- (let* ((count (length points))
- (bv (fill-bv (make-s32vector (* count 2)) points 0)))
- (ffi:sdl-render-draw-lines (unwrap-renderer renderer)
- (bytevector->pointer bv)
- count)))
+ (if (bytevector? points)
+ (ffi:sdl-render-draw-lines (unwrap-renderer renderer)
+ (bytevector->pointer/cached points)
+ (/ (bytevector-length points) 8))
+ (let ((bv (build-point-bv points)))
+ (ffi:sdl-render-draw-lines (unwrap-renderer renderer)
+ (bytevector->pointer bv)
+ (length points)))))
(define (render-draw-points renderer points)
"Draw POINTS on RENDERER."
- (define (fill-bv bv l n)
- (match l
- (() bv)
- (((x y) . r)
- (s32vector-set! bv n x)
- (s32vector-set! bv (+ n 1) y)
- (fill-bv bv r (+ 2 n)))))
- (let* ((count (length points))
- (bv (fill-bv (make-s32vector (* count 2)) points 0)))
- (ffi:sdl-render-draw-points (unwrap-renderer renderer)
- (bytevector->pointer bv)
- count)))
+ (if (bytevector? points)
+ (ffi:sdl-render-draw-points (unwrap-renderer renderer)
+ (bytevector->pointer/cached points)
+ (/ (bytevector-length points) 8))
+ (let ((bv (build-point-bv points)))
+ (ffi:sdl-render-draw-points (unwrap-renderer renderer)
+ (bytevector->pointer bv)
+ (length points)))))
(define (build-rect-bv rects)
(let* ((count (length rects))