From 8e203fbd095c993d3394c0118497c1828202669f Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sat, 30 Jan 2021 15:34:06 -0500 Subject: node-2d: Rewrite sprite classes. --- starling/node-2d.scm | 65 ++++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/starling/node-2d.scm b/starling/node-2d.scm index 71ff25f..521c3bb 100644 --- a/starling/node-2d.scm +++ b/starling/node-2d.scm @@ -416,10 +416,11 @@ ;;; -;;; Base Sprite +;;; Sprite ;;; -(define-class () +(define-class () + (texture #:accessor texture #:init-keyword #:texture #:asset? #t) (batch #:accessor batch #:init-keyword #:batch #:init-form #f) @@ -430,17 +431,9 @@ #:init-keyword #:blend-mode #:init-form 'alpha)) -(define-generic texture) - -(define-method (texcoords (sprite )) - (texture-gl-tex-rect (texture sprite))) - -(define-method (source-rect (sprite )) - (texture-gl-rect (texture sprite))) - -(define-method (render (sprite ) alpha) +(define-method (render (sprite ) alpha) (let* ((tex (texture sprite)) - (rect (source-rect sprite)) + (rect (texture-gl-rect tex)) (batch (batch sprite)) (tint (tint sprite)) (matrix (world-matrix sprite))) @@ -450,37 +443,31 @@ #:texture-region tex) (draw-sprite* tex rect matrix #:tint tint - #:texcoords (texcoords sprite))))) - - -;;; -;;; Static Sprite -;;; - -(define-class () - (texture #:getter texture #:init-keyword #:texture #:asset? #t) - (texcoords #:init-keyword #:texcoords #:init-form #f) - (source-rect #:init-keyword #:source-rect #:init-form #f)) - -(define-method (texcoords (sprite )) - (or (slot-ref sprite 'texcoords) - (next-method))) - -(define-method (source-rect (sprite )) - (or (slot-ref sprite 'source-rect) - (next-method))) + #:texcoords (texture-gl-tex-rect tex))))) ;;; ;;; Texture Atlas Sprite ;;; -(define-class () +(define-class () (atlas #:accessor atlas #:init-keyword #:atlas #:asset? #t) (index #:accessor index #:init-keyword #:index)) -(define-method (texture (sprite )) - (texture-atlas-ref (atlas sprite) (index sprite))) +(define-method (sync-texture (sprite )) + (set! (texture sprite) (texture-atlas-ref (atlas sprite) (index sprite)))) + +(define-method (initialize (sprite ) initargs) + (next-method) + (sync-texture sprite)) + +(define-method ((setter atlas) (sprite ) atlas) + (next-method) + (sync-texture sprite)) + +(define-method ((setter index) (sprite ) i) + (next-method) + (sync-texture sprite)) ;;; @@ -492,13 +479,18 @@ (frame-duration #:getter frame-duration #:init-keyword #:frame-duration #:init-form 250)) -(define-class () +(define-class () + (atlas #:accessor atlas #:init-keyword #:atlas #:asset? #t) (animations #:accessor animations #:init-keyword #:animations) (current-animation #:accessor current-animation #:init-keyword #:default-animation #:init-form 'default) (start-time #:accessor start-time #:init-form 0)) +(define-method (initialize (sprite ) initargs) + (next-method) + (update sprite 0)) + (define-method (on-enter (sprite )) (update sprite 0)) @@ -510,8 +502,7 @@ (time (mod (- (elapsed-time) (start-time sprite)) anim-duration)) (frame (vector-ref frames (inexact->exact (floor (/ time frame-duration)))))) - (set! (index sprite) frame) - (next-method))) + (set! (texture sprite) (texture-atlas-ref (atlas sprite) frame)))) (define-method (change-animation (sprite ) name) (set! (current-animation sprite) name) -- cgit v1.2.3