summaryrefslogtreecommitdiff
path: root/2d
diff options
context:
space:
mode:
authorDavid Thompson <dthompson@member.fsf.org>2013-09-15 21:00:07 -0400
committerDavid Thompson <dthompson@member.fsf.org>2013-09-15 21:00:07 -0400
commit878110e63e6b81367d12b7018a1ae6b8338b1afb (patch)
tree58797e7f6b9c75389a98b2dc00f6b511f9dbcb32 /2d
parent6ff97f3185245a9eea3fe8726f7ceef068069997 (diff)
Animate sprites during game updates instead of on render.
Frame rate is independent of the update rate, so the old way didn't make sense.
Diffstat (limited to '2d')
-rw-r--r--2d/sprite.scm28
1 files changed, 26 insertions, 2 deletions
diff --git a/2d/sprite.scm b/2d/sprite.scm
index 11730b8..d25a535 100644
--- a/2d/sprite.scm
+++ b/2d/sprite.scm
@@ -28,6 +28,7 @@
#:use-module (figl gl)
#:use-module (figl contrib packed-struct)
#:use-module ((sdl sdl) #:prefix SDL:)
+ #:use-module (2d agenda)
#:use-module (2d animation)
#:use-module (2d color)
#:use-module (2d helpers)
@@ -187,6 +188,10 @@ 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 (animated-sprite? sprite)
+ "Return #t if SPRITE has an animation as its drawable object."
+ (animation? (sprite-drawable sprite)))
+
(define (sprite-animation-texture sprite)
(animator-texture (sprite-animator sprite)))
@@ -240,8 +245,7 @@ sprite."
bound."
(when (sprite-dirty? sprite)
(update-sprite-vertices! sprite))
- (when (animation? (sprite-drawable sprite))
- (update-sprite-animator! sprite))
+ (register-animated-sprite-maybe sprite)
(if *sprite-batch*
(draw-sprite-batched sprite)
(draw-sprite-vertices (sprite-texture sprite)
@@ -254,6 +258,7 @@ bound."
(pos (sprite-position sprite))
(scale (sprite-scale sprite))
(anchor (sprite-anchor-vector sprite)))
+ (register-animated-sprite-maybe sprite)
(%sprite-batch-draw *sprite-batch*
texture
(vx pos)
@@ -271,6 +276,24 @@ bound."
(texture-t2 texture)
(sprite-color sprite))))
+;; A hash table for all of the animated sprites that have been drawn
+;; since the last game update. It is cleared after every game update.
+(define animated-sprites (make-hash-table))
+
+(define (register-animated-sprite-maybe sprite)
+ (when (animated-sprite? sprite)
+ (hash-set! animated-sprites sprite sprite)))
+
+(define (update-animated-sprites!)
+ "Update all animators for sprites that have been drawn this frame."
+ (hash-for-each (lambda (key val)
+ (update-sprite-animator! val))
+ animated-sprites)
+ (hash-clear! animated-sprites))
+
+;; Update animated sprites upon every update.
+(agenda-schedule-interval update-animated-sprites!)
+
(export make-sprite
sprite?
sprite-drawable
@@ -287,6 +310,7 @@ bound."
set-sprite-anchor!
sprite-vertices
set-sprite-vertices!
+ animated-sprite?
load-sprite
draw-sprite)