From c93bdb1cfb5d4776ea1ff28f94a715f26dcebccb Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 23 May 2019 07:45:52 -0400 Subject: node-2d: Rewrite sprite nodes. --- starling/node-2d.scm | 88 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 16 deletions(-) diff --git a/starling/node-2d.scm b/starling/node-2d.scm index edb8f46..a309bc9 100644 --- a/starling/node-2d.scm +++ b/starling/node-2d.scm @@ -77,9 +77,14 @@ texture + blend-mode + tint - + atlas + index + + animations frame-duration current-animation @@ -87,6 +92,7 @@ change-animation + batch region @@ -420,29 +426,69 @@ (vec2-copy! (position node) (last-position node)) (next-method)) + +;;; +;;; Base Sprite +;;; + +(define-class () + (batch #:accessor batch + #:init-keyword #:batch + #:init-form #f) + (tint #:accessor tint + #:init-keyword #:tint + #:init-form white) + (blend-mode #:accessor blend-mode + #:init-keyword #:blend-mode + #:init-form 'alpha)) + +(define-generic texture) + +(define-method (render (sprite ) alpha) + (let* ((tex (asset-ref (texture sprite))) + (rect (texture-gl-rect tex)) + (batch (batch sprite)) + (tint (tint sprite)) + (matrix (world-matrix sprite))) + (if batch + (sprite-batch-add* batch rect matrix + #:tint tint + #:texture-region tex) + (draw-sprite* tex rect matrix #:tint tint)))) + ;;; ;;; Static Sprite ;;; -(define-class () +(define-class () (texture #:accessor texture #:init-keyword #:texture)) -(define-method (render (sprite ) alpha) - (let ((tex (asset-ref (texture sprite)))) - (draw-sprite* tex (texture-gl-rect tex) (world-matrix sprite)))) + +;;; +;;; Texture Atlas Sprite +;;; + +(define-class () + (atlas #:accessor atlas #:init-keyword #:atlas) + (index #:accessor index #:init-keyword #:index)) + +(define-method (texture (sprite )) + (texture-atlas-ref (asset-ref (atlas sprite)) (index sprite))) ;;; ;;; Animated Sprite ;;; -(define-class () - (atlas #:accessor atlas #:init-keyword #:atlas) +(define-class () (animations #:accessor animations #:init-keyword #:animations) - (frame-duration #:accessor frame-duration #:init-keyword #:frame-duration) + (frame-duration #:accessor frame-duration + #:init-keyword #:frame-duration + #:init-form 500) (current-animation #:accessor current-animation - #:init-keyword #:current-animation) + #:init-keyword #:default-animation + #:init-form 'default) (start-time #:accessor start-time #:init-form 0)) (define-method (on-enter (sprite )) @@ -453,9 +499,8 @@ (frame-duration (frame-duration sprite)) (anim-duration (* frame-duration (vector-length anim))) (time (modulo (- (elapsed-time) (start-time sprite)) anim-duration)) - (frame (vector-ref anim (floor (/ time frame-duration)))) - (texture-region (texture-atlas-ref (asset-ref (atlas sprite)) frame))) - (set! (texture sprite) texture-region) + (frame (vector-ref anim (floor (/ time frame-duration))))) + (set! (index sprite) frame) (next-method))) (define-method (change-animation (sprite ) name) @@ -467,10 +512,21 @@ ;;; Sprite Batch ;;; -(define-class ()) - -(define-method (render* (batch ) alpha) - (with-batched-sprites (next-method))) +(define-class () + (batch #:accessor batch #:init-keyword #:batch) + (blend-mode #:accessor blend-mode + #:init-keyword #:blend-mode + #:init-form 'alpha) + (clear-after-draw? #:accessor clear-after-draw? + #:init-keyword #:clear-after-draw? + #:init-form #t)) + +(define-method (render (sprite-batch ) alpha) + (let ((batch (batch sprite-batch))) + (draw-sprite-batch* batch (world-matrix sprite-batch) + #:blend-mode (blend-mode sprite-batch)) + (when (clear-after-draw? sprite-batch) + (sprite-batch-clear! batch)))) ;;; -- cgit v1.2.3