fix buildWithDataOf() does not copy over sprite animation metadata

This commit is contained in:
wgroeneveld 2019-12-12 13:14:45 +01:00
parent 6999264e20
commit ec18d62171
4 changed files with 28 additions and 19 deletions

View File

@ -73,17 +73,7 @@ public:
explicit Sprite(const void *imageData, int imageSize, int x, int y, SpriteSize size); explicit Sprite(const void *imageData, int imageSize, int x, int y, SpriteSize size);
virtual ~Sprite() {} virtual ~Sprite() {}
void makeAnimated(int numberOfFrames, int animationDelay) { void makeAnimated(int beginFrame, int numberOfFrames, int animationDelay);
this->numberOfFrames = numberOfFrames;
this->animationDelay = animationDelay;
animate();
}
void makeAnimated(int beginFrame, int numberOfFrames, int animationDelay) {
setBeginFrame(beginFrame);
animateToFrame(beginFrame);
makeAnimated(numberOfFrames, animationDelay);
animate();
}
void setBeginFrame(int frame) { this->beginFrame = frame; } void setBeginFrame(int frame) { this->beginFrame = frame; }
void animateToFrame(int frame) { this->currentFrame = frame; } void animateToFrame(int frame) { this->currentFrame = frame; }
void animate() { this->animating = true; } void animate() { this->animating = true; }
@ -113,6 +103,8 @@ public:
u32 getDy() { return dy; } u32 getDy() { return dy; }
u32 getWidth() { return w; } u32 getWidth() { return w; }
u32 getHeight() { return h; } u32 getHeight() { return h; }
u32 getAnimationDelay() { return animationDelay; }
u32 getNumberOfFrames() { return numberOfFrames; }
u32 getCurrentFrame() { return currentFrame; } u32 getCurrentFrame() { return currentFrame; }
bool isAnimating() { return animating; }; bool isAnimating() { return animating; };
bool isOffScreen(); bool isOffScreen();

View File

@ -80,11 +80,7 @@ template<typename T> std::unique_ptr<T> SpriteBuilder<T>::buildWithDataOf(const
template<typename T> void SpriteBuilder<T>::setProperties(T* s) { template<typename T> void SpriteBuilder<T>::setProperties(T* s) {
s->setVelocity(this->dx, this->dy); s->setVelocity(this->dx, this->dy);
if(this->numberOfFrames > 0) { if(this->numberOfFrames > 0) {
if(this->beginFrame > 0) { s->makeAnimated(this->beginFrame, this->numberOfFrames, this->animationDelay);
s->makeAnimated(this->beginFrame, this->numberOfFrames, this->animationDelay);
} else {
s->makeAnimated(this->numberOfFrames, this->animationDelay);
}
} }
s->setStayWithinBounds(stayWithinBounds); s->setStayWithinBounds(stayWithinBounds);

View File

@ -10,6 +10,10 @@
Sprite::Sprite(const Sprite &other) : Sprite(nullptr, 0, other.x, other.y, other.spriteSize) { Sprite::Sprite(const Sprite &other) : Sprite(nullptr, 0, other.x, other.y, other.spriteSize) {
tileIndex = other.tileIndex; tileIndex = other.tileIndex;
animationDelay = other.animationDelay;
numberOfFrames = other.numberOfFrames;
currentFrame = other.currentFrame;
animationCounter = other.animationCounter;
} }
Sprite::Sprite(const void *imageData, int imageSize, int x, int y, SpriteSize size) Sprite::Sprite(const void *imageData, int imageSize, int x, int y, SpriteSize size)
@ -55,6 +59,14 @@ void Sprite::syncVelocity() {
oam->attr1 = (oam->attr1 & ~ATTR1_X_MASK) | (x & ATTR1_X_MASK); oam->attr1 = (oam->attr1 & ~ATTR1_X_MASK) | (x & ATTR1_X_MASK);
} }
void Sprite::makeAnimated(int beginFrame, int numberOfFrames, int animationDelay) {
setBeginFrame(beginFrame);
animateToFrame(beginFrame);
this->numberOfFrames = numberOfFrames;
this->animationDelay = animationDelay;
animate();
}
void Sprite::syncAnimation() { void Sprite::syncAnimation() {
int newTileIndex = this->tileIndex + (currentFrame * (this->animation_offset * 2)); int newTileIndex = this->tileIndex + (currentFrame * (this->animation_offset * 2));
oam->attr2 &= OAM_TILE_OFFSET_CLEAR; oam->attr2 &= OAM_TILE_OFFSET_CLEAR;

View File

@ -207,7 +207,7 @@ protected:
TEST_F(SpriteSuite, Sync_Animation_Updates_OAM_To_Next_Frame) { TEST_F(SpriteSuite, Sync_Animation_Updates_OAM_To_Next_Frame) {
s = new SpriteWithStubOam(SIZE_16_32); s = new SpriteWithStubOam(SIZE_16_32);
s->makeAnimated(2, 0); s->makeAnimated(0, 2, 0);
auto oam = s->buildOamForTesting(208); // should start at 224 (11100000) after a frame update auto oam = s->buildOamForTesting(208); // should start at 224 (11100000) after a frame update
s->update(); s->update();
@ -216,9 +216,18 @@ TEST_F(SpriteSuite, Sync_Animation_Updates_OAM_To_Next_Frame) {
ASSERT_EQ(std::string("0000000011100000"), attr2); ASSERT_EQ(std::string("0000000011100000"), attr2);
} }
TEST_F(SpriteSuite, Animated_Sprite_Copy_In_Constructor_Takes_Over_Animation_Properties) {
s->makeAnimated(0, 2, 5);
Sprite copy(*s);
ASSERT_EQ(0, copy.getCurrentFrame());
ASSERT_EQ(5, copy.getAnimationDelay());
ASSERT_EQ(2, copy.getNumberOfFrames());
}
TEST_F(SpriteSuite, Animated_Sprite_Increases_Current_Frame_After_Delay) { TEST_F(SpriteSuite, Animated_Sprite_Increases_Current_Frame_After_Delay) {
s->makeAnimated(2, 5); s->makeAnimated(0, 2, 5);
ASSERT_EQ(0, s->getCurrentFrame()); ASSERT_EQ(0, s->getCurrentFrame());
s->update(); // 1 times s->update(); // 1 times
@ -232,7 +241,7 @@ TEST_F(SpriteSuite, Animated_Sprite_Increases_Current_Frame_After_Delay) {
} }
TEST_F(SpriteSuite, Animated_Sprite_Resets_Current_Frame_After_Hitting_Max) { TEST_F(SpriteSuite, Animated_Sprite_Resets_Current_Frame_After_Hitting_Max) {
s->makeAnimated(2, 0); s->makeAnimated(0, 2, 0);
s->update(); s->update();
ASSERT_EQ(1, s->getCurrentFrame()); ASSERT_EQ(1, s->getCurrentFrame());
s->update(); s->update();