summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson@member.fsf.org>2013-09-15 10:45:59 -0400
committerDavid Thompson <dthompson@member.fsf.org>2013-09-15 10:45:59 -0400
commit0fae41c306562e19d5965c4edc639d3778449f58 (patch)
treebb3ef50a11beb327cca6624f5ce5eecc598ce680
parentd2dabbd1aa9f75a919682cdeebe1ed3f4c1d84cc (diff)
Change animation API.
This should have been broken into more commits, but here we are: Rename <animation-state> to <animator>. Better name, less verbose. Make animators mutable because it makes more sense than when animation-states where immutable. Update sprite module to reflect the animation API changes.
-rw-r--r--2d/animation.scm129
-rw-r--r--2d/sprite.scm40
2 files changed, 89 insertions, 80 deletions
diff --git a/2d/animation.scm b/2d/animation.scm
index fad4828..2c9e9f0 100644
--- a/2d/animation.scm
+++ b/2d/animation.scm
@@ -32,80 +32,91 @@
;; The <animation> type represents a vector of textures or texture
;; regions that are to be played in sequence and possibly looped.
(define-record-type <animation>
- (make-animation frames duration loop)
+ (make-animation frames frame-duration loop)
animation?
(frames animation-frames)
- (duration animation-duration)
+ (frame-duration animation-frame-duration)
(loop animation-loop?))
(define (animation-frame animation index)
- "Returns the frame for the given index."
+ "Return the texture for the given frame INDEX."
(vector-ref (animation-frames animation) index))
(define (animation-length animation)
- "Returns the number of frames in the animation"
+ "Return the number of frames in the ANIMATION."
(vector-length (animation-frames animation)))
+(define (animation-duration animation)
+ "Return the total duration of ANIMATION in ticks."
+ (* (animation-length animation)
+ (animation-frame-duration animation)))
+
(export make-animation
animation?
animation-frames
- animation-duration
+ animation-frame-duration
animation-loop?
animation-frame
- animation-length)
+ animation-length
+ animation-duration)
-;; The <animation-state> type encapsulates the state for playing an
+;; The <animator> type encapsulates the state for playing an
;; animation.
-(define-record-type <animation-state>
- (%make-animation-state animation frame-index frame-time playing)
- animation-state?
- (animation animation-state-animation)
- (frame-index animation-state-frame-index)
- (frame-time animation-state-frame-time)
- (playing animation-state-playing?))
-
-(define (make-animation-state animation)
+(define-record-type <animator>
+ (%make-animator animation frame time)
+ animator?
+ (animation animator-animation)
+ (frame animator-frame set-animator-frame!)
+ (time animator-time set-animator-time!))
+
+(define (make-animator animation)
"Creates a new animation state object."
- (%make-animation-state animation 0 0 #t))
-
-(define (tick-animation-state state)
- "Increments the frame time for the animation state and determines
-which frame to show. Returns a new animation state object when the
-animation is playing. Otherwise the state passed in is returned."
- (let ((frame-time (1+ (animation-state-frame-time state)))
- (frame-index (animation-state-frame-index state))
- (playing (animation-state-playing? state))
- (animation (animation-state-animation state)))
-
- ;; Return the same state object if the animation is not playing.
- (cond ((not playing)
- state)
- ;; Return a new state object with a reset frame-index and
- ;; frame-time if we've reached the end of the animation.
- ;; Stops playing the animation if the animation does not
- ;; loop.
- ((and playing (= frame-time (animation-duration animation)))
- (let* ((frame-index (modulo (1+ frame-index)
- (animation-length animation)))
- (frame-time 0)
- (playing (or (not (= frame-index 0))
- (animation-loop? animation))))
- (%make-animation-state animation frame-index frame-time playing)))
- ;; Return a new state object with an incremented frame index.
- (else
- (%make-animation-state animation frame-index frame-time playing)))))
-
-(define (animation-state-frame state)
- "Returns the texture or texture region for the state's animation at
-the current frame index."
- (animation-frame (animation-state-animation state)
- (animation-state-frame-index state)))
-
-(export make-animation-state
- animation-state?
- animation-state-animation
- animation-state-frame-index
- animation-state-frame-time
- animation-state-playing?
- animation-state-frame
- tick-animation-state)
+ (%make-animator animation 0 0))
+
+(define (animator-frame-complete? state)
+ (>= (animator-time state)
+ (animation-frame-duration (animator-animation state))))
+
+(define (animator-playing? state)
+ "Return true if animation STATE is not done playing the
+animation. This will always return #t if the animation loops."
+ (not (= (animator-frame state) -1)))
+
+(define (animator-next-frame state)
+ "Return the next frame index for the animation STATE. Return -1 when
+the animation is complete."
+ (let ((frame (1+ (animator-frame state)))
+ (animation (animator-animation state)))
+ (cond ((< frame (animation-length animation)) frame)
+ ((animation-loop? animation) 0)
+ (else -1))))
+
+(define (animator-texture state)
+ "Returns the texture for the animation at the current frame index."
+ (animation-frame (animator-animation state)
+ (animator-frame state)))
+
+(define (animator-next! state)
+ "Advance to the next animation frame for the given animation STATE."
+ (set-animator-time! state 0)
+ (set-animator-frame! state (animator-next-frame state)))
+
+(define (animator-update! state)
+ "Increments the frame time for the animation STATE and advances to
+the next frame in the animation if necessary."
+ (when (animator-playing? state)
+ (set-animator-time! state (1+ (animator-time state)))
+ (when (animator-frame-complete? state)
+ (animator-next! state))))
+
+(export make-animator
+ animator?
+ animator-animation
+ animator-frame
+ animator-time
+ animator-frame-complete?
+ animator-playing?
+ animator-next-frame
+ animator-texture
+ animator-next!
+ animator-update!)
diff --git a/2d/sprite.scm b/2d/sprite.scm
index 16d40b6..11730b8 100644
--- a/2d/sprite.scm
+++ b/2d/sprite.scm
@@ -142,8 +142,8 @@
;; texture-region, animation, etc.) with a given position, scale,
;; rotation, and color.
(define-record-type <sprite>
- (%make-sprite drawable position scale rotation color anchor vertices
- dirty animation-state)
+ (%make-sprite drawable position scale rotation color anchor
+ vertices dirty animator)
sprite?
(drawable sprite-drawable set-sprite-drawable!)
(position sprite-position %set-sprite-position!)
@@ -153,18 +153,18 @@
(anchor sprite-anchor %set-sprite-anchor!)
(vertices sprite-vertices set-sprite-vertices!)
(dirty sprite-dirty? set-sprite-dirty!)
- (animation-state sprite-animation-state set-sprite-animation-state!))
+ (animator sprite-animator))
(define* (make-sprite drawable #:optional #:key
(position (vector2 0 0)) (scale (vector2 1 1))
(rotation 0) (color white) (anchor 'center))
"Makes a new sprite object."
(let ((vertices (make-packed-array sprite-vertex 4))
- (animation-state (if (animation? drawable)
- (make-animation-state drawable)
- #f)))
+ (animator (if (animation? drawable)
+ (make-animator drawable)
+ #f)))
(%make-sprite drawable position scale rotation color anchor vertices
- #t animation-state)))
+ #t animator)))
(define-syntax-rule (dirty-sprite-setter setter private-setter)
"Defines a setter that calls the private version of the given
@@ -187,16 +187,16 @@ operation that requires a refresh of the vertex array should use this macro."
(make-sprite (load-texture filename) #:position position #:scale scale
#:rotation rotation #:color color #:anchor anchor))
-(define (sprite-animation-frame sprite)
- (animation-state-frame (sprite-animation-state sprite)))
+(define (sprite-animation-texture sprite)
+ (animator-texture (sprite-animator sprite)))
(define (sprite-texture sprite)
"Returns the texture for the sprite's drawable object."
- (let ((drawable (sprite-drawable sprite)))
- (cond ((texture? drawable)
- drawable)
- ((animation? drawable)
- (sprite-animation-frame sprite)))))
+ (let ((drawable (sprite-drawable sprite)))
+ (cond ((texture? drawable)
+ drawable)
+ ((animation? drawable)
+ (sprite-animation-texture sprite)))))
(define (sprite-anchor-vector sprite)
"Returns a vector of the coordinates for the center point of a
@@ -205,8 +205,8 @@ sprite."
(texture (sprite-texture sprite)))
(cond
((eq? anchor 'center)
- (vector2 (/ (texture-width texture) 2)
- (/ (texture-height texture) 2)))
+ (vector2 (/ (texture-width texture) 2)
+ (/ (texture-height texture) 2)))
(else anchor))))
(define (update-sprite-vertices! sprite)
@@ -232,10 +232,8 @@ sprite."
(texture-t2 texture)
(sprite-color sprite))))
-(define (update-sprite-animation-state! sprite)
- (set-sprite-animation-state!
- sprite
- (tick-animation-state (sprite-animation-state sprite))))
+(define (update-sprite-animator! sprite)
+ (animator-update! (sprite-animator sprite)))
(define (draw-sprite sprite)
"Renders a sprite. A sprite batch will be used if one is currently
@@ -243,7 +241,7 @@ bound."
(when (sprite-dirty? sprite)
(update-sprite-vertices! sprite))
(when (animation? (sprite-drawable sprite))
- (update-sprite-animation-state! sprite))
+ (update-sprite-animator! sprite))
(if *sprite-batch*
(draw-sprite-batched sprite)
(draw-sprite-vertices (sprite-texture sprite)