From e68f6d4bf5e976a14918d1e7d902737be4a5e453 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 27 Jun 2013 22:36:50 -0400 Subject: Update particles example to use sprite batching. --- examples/particles.scm | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/examples/particles.scm b/examples/particles.scm index 39e2ae7..90bd010 100644 --- a/examples/particles.scm +++ b/examples/particles.scm @@ -26,29 +26,6 @@ (position particle-position set-particle-position!) (velocity particle-velocity set-particle-velocity!)) -(define (draw-particle particle) - (let* ((texture (sprite-texture (particle-sprite particle))) - (p (particle-position particle)) - (x (vx p)) - (y (vy p)) - (x2 (+ x (texture-width texture))) - (y2 (+ y (texture-height texture)))) - (gl-texture-coordinates 0 0) - (gl-vertex x y) - (gl-texture-coordinates 1 0) - (gl-vertex x2 y) - (gl-texture-coordinates 1 1) - (gl-vertex x2 y2) - (gl-texture-coordinates 0 1) - (gl-vertex x y2))) - -(define (draw-particles particles) - (let ((texture (sprite-texture (particle-sprite (car particles))))) - (with-gl-bind-texture (texture-target texture-2d) (texture-id texture) - (gl-begin (begin-mode quads) - (gl-color 1 1 1) - (for-each (lambda (p) (draw-particle p)) particles))))) - (define (update-particle! particle) (set-particle-position! particle (v+ (particle-position particle) @@ -65,15 +42,30 @@ (define stars (load-sprite "stars.png" #:anchor #(0 0))) (define particle-image (load-texture "bullet.png")) -(define num-particles 1800) +(define particle-width (texture-width particle-image)) +(define particle-height (texture-height particle-image)) +(define particle-count 2000) (define particles - (list-tabulate num-particles + (list-tabulate particle-count (lambda (n) (make-particle (make-sprite particle-image) (vector (random window-width) (random window-height)) (vector (* (random:normal) 1) - (* (random:normal) 1)))))) + (* (random:normal) 1)))))) +(define batch (make-sprite-batch (* particle-count 4))) + +(define (draw-particles particles) + (with-sprite-batch batch + (for-each + (lambda (p) + (let* ((pos (particle-position p)) + (x (vx pos)) + (y (vy pos))) + (sprite-batch-draw batch particle-image x y 0 0 + particle-width particle-height + 1 1 0))) + particles))) (set-render-callback (lambda () (render))) (set-update-callback (lambda () (update))) -- cgit v1.2.3