summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson@vistahigherlearning.com>2019-05-23 07:45:52 -0400
committerDavid Thompson <dthompson@vistahigherlearning.com>2019-05-23 07:45:52 -0400
commitc93bdb1cfb5d4776ea1ff28f94a715f26dcebccb (patch)
treed94b638ea72236dc133bc078f2af7efd53b460fb
parentf26106ad280cbc586b4470ba1f2229a2b4f6d0f9 (diff)
node-2d: Rewrite sprite nodes.
-rw-r--r--starling/node-2d.scm88
1 files 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 @@
<sprite>
texture
+ blend-mode
+ tint
- <animated-sprite>
+ <atlas-sprite>
atlas
+ index
+
+ <animated-sprite>
animations
frame-duration
current-animation
@@ -87,6 +92,7 @@
change-animation
<sprite-batch>
+ batch
<filled-rect>
region
@@ -422,27 +428,67 @@
;;;
+;;; Base Sprite
+;;;
+
+(define-class <base-sprite> (<node-2d>)
+ (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 <base-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 <sprite> (<node-2d>)
+(define-class <sprite> (<base-sprite>)
(texture #:accessor texture #:init-keyword #:texture))
-(define-method (render (sprite <sprite>) alpha)
- (let ((tex (asset-ref (texture sprite))))
- (draw-sprite* tex (texture-gl-rect tex) (world-matrix sprite))))
+
+;;;
+;;; Texture Atlas Sprite
+;;;
+
+(define-class <atlas-sprite> (<base-sprite>)
+ (atlas #:accessor atlas #:init-keyword #:atlas)
+ (index #:accessor index #:init-keyword #:index))
+
+(define-method (texture (sprite <atlas-sprite>))
+ (texture-atlas-ref (asset-ref (atlas sprite)) (index sprite)))
;;;
;;; Animated Sprite
;;;
-(define-class <animated-sprite> (<sprite>)
- (atlas #:accessor atlas #:init-keyword #:atlas)
+(define-class <animated-sprite> (<atlas-sprite>)
(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 <animated-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 <animated-sprite>) name)
@@ -467,10 +512,21 @@
;;; Sprite Batch
;;;
-(define-class <sprite-batch> (<node-2d>))
-
-(define-method (render* (batch <sprite-batch>) alpha)
- (with-batched-sprites (next-method)))
+(define-class <sprite-batch> (<node-2d>)
+ (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 <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))))
;;;