Merge pull request #3 from tjorim/better-sprite-animation

Sprite animation now allows beginFrame and add relative background scroll
This commit is contained in:
Wouter Groeneveld 2019-08-05 08:40:07 +02:00 committed by GitHub
commit 05bee78dd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 16 deletions

3
.gitignore vendored
View File

@ -122,3 +122,6 @@ CTestTestfile.cmake
*.exe *.exe
*.out *.out
*.app *.app
#VisualStudio code
.vscode

View File

@ -25,9 +25,10 @@ public:
const int getCharBlock() { return charBlockIndex; } const int getCharBlock() { return charBlockIndex; }
void useMapScreenBlock(int block) { screenBlockIndex = block; } void useMapScreenBlock(int block) { screenBlockIndex = block; }
void scroll(int x, int y); void scroll(int x, int y);
void scrollSpeed(int dx, int dy);
Background(int bgIndex, const void *data, int size, const void* map, int mapSize) : data(data), bgIndex(bgIndex), size(size), map(map), Background(int bgIndex, const void *data, int size, const void* map, int mapSize) : data(data), bgIndex(bgIndex), size(size), map(map),
screenBlockIndex(0), charBlockIndex(0), mapSize(mapSize) {} screenBlockIndex(0), charBlockIndex(0), mapSize(mapSize) {}
virtual void persist(); virtual void persist();
void updateMap(const void* map); void updateMap(const void* map);
void clearMap(); void clearMap();

View File

@ -58,7 +58,7 @@ protected:
bool stayWithinBounds; bool stayWithinBounds;
u32 imageSize, tileIndex; u32 imageSize, tileIndex;
SpriteSize spriteSize; SpriteSize spriteSize;
u32 animationDelay, amountOfFrames, currentFrame, animationCounter; u32 animationDelay, numberOfFrames, beginFrame, currentFrame, animationCounter;
bool animating; bool animating;
std::unique_ptr<OBJ_ATTR> oam; std::unique_ptr<OBJ_ATTR> oam;
@ -73,13 +73,20 @@ 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 amountOfFrames, int animationDelay) { void makeAnimated(int numberOfFrames, int animationDelay) {
this->amountOfFrames = amountOfFrames; this->numberOfFrames = numberOfFrames;
this->animationDelay = animationDelay; this->animationDelay = animationDelay;
animate(); animate();
} }
void animate() { this->animating = true; } void makeAnimated(int beginFrame, int numberOfFrames, int animationDelay) {
setBeginFrame(beginFrame);
animateToFrame(beginFrame);
makeAnimated(numberOfFrames, animationDelay);
animate();
}
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 stopAnimating() { this->animating = false; } void stopAnimating() { this->animating = false; }
void setStayWithinBounds(bool b) { stayWithinBounds = b; } void setStayWithinBounds(bool b) { stayWithinBounds = b; }
void setVelocity(int dx, int dy) { void setVelocity(int dx, int dy) {
@ -100,13 +107,14 @@ public:
GBAVector getPosAsVector() { return GBAVector(getPos()); } GBAVector getPosAsVector() { return GBAVector(getPos()); }
VECTOR getCenter() { return { x + w / 2, y + h / 2 }; } VECTOR getCenter() { return { x + w / 2, y + h / 2 }; }
VECTOR getVelocity() { return { dx, dy}; } VECTOR getVelocity() { return { dx, dy}; }
u32 getX() { return x; }
u32 getY() { return y; }
u32 getDx() { return dx; } u32 getDx() { return dx; }
u32 getDy() { return dy; } u32 getDy() { return dy; }
u32 getX() { return x; }
u32 getHeight() { return h; }
u32 getWidth() { return w; } u32 getWidth() { return w; }
u32 getY() { return y; } u32 getHeight() { return h; }
u32 getCurrentFrame() { return currentFrame; } u32 getCurrentFrame() { return currentFrame; }
bool isAnimating() { return animating; };
bool isOffScreen(); bool isOffScreen();
friend class SpriteManager; friend class SpriteManager;

View File

@ -13,12 +13,12 @@ private:
bool stayWithinBounds = false; bool stayWithinBounds = false;
const void *imageData; const void *imageData;
u32 x, y, dx, dy; u32 x, y, dx, dy;
u32 numberOfFrames, animationDelay; u32 beginFrame, numberOfFrames, animationDelay;
SpriteSize size; SpriteSize size;
void setProperties(T* sprite); void setProperties(T* sprite);
void reset() { void reset() {
imageSize = x = y = dx = dy = numberOfFrames = animationDelay = 0; imageSize = x = y = dx = dy = beginFrame = numberOfFrames = animationDelay = 0;
imageData = nullptr; imageData = nullptr;
stayWithinBounds = false; stayWithinBounds = false;
size = SIZE_16_16; size = SIZE_16_16;
@ -51,9 +51,16 @@ public:
this->size = size; this->size = size;
return *this; return *this;
} }
SpriteBuilder& withAnimated(int numberOfFrames, int delay) { SpriteBuilder& withAnimated(int numberOfFrames, int animationDelay) {
this->beginFrame = 0;
this->numberOfFrames = numberOfFrames; this->numberOfFrames = numberOfFrames;
this->animationDelay = delay; this->animationDelay = animationDelay;
return *this;
}
SpriteBuilder& withAnimated(int beginFrame, int numberOfFrames, int animationDelay) {
this->beginFrame = beginFrame;
this->numberOfFrames = numberOfFrames;
this->animationDelay = animationDelay;
return *this; return *this;
} }
T build(); T build();
@ -73,7 +80,11 @@ 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) {
s->makeAnimated(this->numberOfFrames, this->animationDelay); if(this->beginFrame > 0) {
s->makeAnimated(this->beginFrame, this->numberOfFrames, this->animationDelay);
} else {
s->makeAnimated(this->numberOfFrames, this->animationDelay);
}
} }
s->setStayWithinBounds(stayWithinBounds); s->setStayWithinBounds(stayWithinBounds);

View File

@ -78,4 +78,9 @@ void Background::buildRegister() {
void Background::scroll(int x, int y) { void Background::scroll(int x, int y) {
REG_BG_OFS[bgIndex].x = x; REG_BG_OFS[bgIndex].x = x;
REG_BG_OFS[bgIndex].y = y; REG_BG_OFS[bgIndex].y = y;
}
void Background::scrollSpeed(int dx, int dy) {
REG_BG_OFS[bgIndex].x += dx;
REG_BG_OFS[bgIndex].y += dy;
} }

View File

@ -14,7 +14,7 @@ Sprite::Sprite(const Sprite &other) : Sprite(nullptr, 0, other.x, other.y, other
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)
: x(x), y(y), data(imageData), imageSize(imageSize), spriteSize(size), : x(x), y(y), data(imageData), imageSize(imageSize), spriteSize(size),
animationDelay(0), amountOfFrames(0), currentFrame(0), animationCounter(0) { animationDelay(0), numberOfFrames(0), beginFrame(0), currentFrame(0), animationCounter(0) {
setAttributesBasedOnSize(size); setAttributesBasedOnSize(size);
} }
@ -86,8 +86,11 @@ void Sprite::updateAnimation() {
animationCounter++; animationCounter++;
if(animationCounter > animationDelay) { if(animationCounter > animationDelay) {
currentFrame++; currentFrame++;
if(currentFrame > (amountOfFrames - 1)) { if(currentFrame > (numberOfFrames - 1) + beginFrame) {
currentFrame = 0; currentFrame = beginFrame;
}
if(currentFrame < beginFrame) {
currentFrame = beginFrame;
} }
animationCounter = 0; animationCounter = 0;