summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--starling/node-2d.scm65
1 files 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 <base-sprite> (<node-2d>)
+(define-class <sprite> (<node-2d>)
+ (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 <base-sprite>))
- (texture-gl-tex-rect (texture sprite)))
-
-(define-method (source-rect (sprite <base-sprite>))
- (texture-gl-rect (texture sprite)))
-
-(define-method (render (sprite <base-sprite>) alpha)
+(define-method (render (sprite <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 <sprite> (<base-sprite>)
- (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 <sprite>))
- (or (slot-ref sprite 'texcoords)
- (next-method)))
-
-(define-method (source-rect (sprite <sprite>))
- (or (slot-ref sprite 'source-rect)
- (next-method)))
+ #:texcoords (texture-gl-tex-rect tex)))))
;;;
;;; Texture Atlas Sprite
;;;
-(define-class <atlas-sprite> (<base-sprite>)
+(define-class <atlas-sprite> (<sprite>)
(atlas #:accessor atlas #:init-keyword #:atlas #:asset? #t)
(index #:accessor index #:init-keyword #:index))
-(define-method (texture (sprite <atlas-sprite>))
- (texture-atlas-ref (atlas sprite) (index sprite)))
+(define-method (sync-texture (sprite <atlas-sprite>))
+ (set! (texture sprite) (texture-atlas-ref (atlas sprite) (index sprite))))
+
+(define-method (initialize (sprite <atlas-sprite>) initargs)
+ (next-method)
+ (sync-texture sprite))
+
+(define-method ((setter atlas) (sprite <atlas-sprite>) atlas)
+ (next-method)
+ (sync-texture sprite))
+
+(define-method ((setter index) (sprite <atlas-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 <animated-sprite> (<atlas-sprite>)
+(define-class <animated-sprite> (<sprite>)
+ (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 <animated-sprite>) initargs)
+ (next-method)
+ (update sprite 0))
+
(define-method (on-enter (sprite <animated-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 <animated-sprite>) name)
(set! (current-animation sprite) name)