So I've been thinking alot about simple/convenient ways to implement simple 2D animation using a bare-bones graphics library I'm using for school (Animation is way out of the scope of the class, I'm just experimenting.)
I thought of using a custom function passed to the class to allow the "user" of the code to just write their own code to do whatever they want to the sprite, then return control to the sprite's Update() function. Here's what I have so far:
// SpriteStuff.h
typedef bool (AnimationStep*)(Sprite* sprite) ANIMATIONSTEP
class Sprite {
//...
ANIMATIONSTEP m_currentAnimation;
void BeginAnimation(ANIMATIONSTEP step);
void EndAnimation();
void Update();
//...
};
void Sprite::BeginAnimation(ANIMATIONSTEP step) {
if (step != NULL)
m_currentAnimation = step;
}
void Sprite::EndAnimation() {
m_currentAnimation = NULL;
}
void Sprite::Update() {
//...
if (m_currentAnimation != NULL)
if (!m_currentAnimation(this)) EndAnimation();
//...
}
// (off in some other code...)
void DoAnimateThing() {
mySprite->BeginAnimation(GeneralFallAnimationStep);
}
bool GeneralFallAnimationStep(Sprite* sprite){
return sprite->y++ > SOME_LIMIT_SOMEWHERE;
}
// Could possibly end it early...
void CancelAnimationOnThing() {
mySprite->EndAnimation();
}
It seems like a pretty simple design that will do what I need, but almost too simple. I'm doing something terribly wrong, huh?
EDIT: So I've gotten some good input on this, but now I'm curious... Any suggestions how controlling the speed of these animations? Can this be done well in the Sprite class, or should that be handled strictly by the ANIMATIONSTEP
?