From 06ed730cd1f74ac5d473dcd8456013b24e66b58e Mon Sep 17 00:00:00 2001 From: GriffinR Date: Sun, 24 Dec 2023 23:44:04 -0500 Subject: [PATCH] Support vertical spritsheets for overworld graphics --- include/core/events.h | 9 ++++++++- include/project.h | 8 -------- src/core/events.cpp | 35 ++++++++++++++++++++++++++--------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/include/core/events.h b/include/core/events.h index e0f1d1b9..8be1bbfc 100644 --- a/include/core/events.h +++ b/include/core/events.h @@ -42,6 +42,13 @@ public: virtual void visitSign(SignEvent *) = 0; }; +struct EventGraphics +{ + QImage spritesheet; + int spriteWidth; + int spriteHeight; + bool inanimate; +}; /// @@ -258,7 +265,7 @@ public: public: void setFrameFromMovement(QString movement); - void setPixmapFromSpritesheet(QImage, int, int, bool); + void setPixmapFromSpritesheet(EventGraphics * gfx); protected: diff --git a/include/project.h b/include/project.h index cf9fcd73..5c2be7cb 100644 --- a/include/project.h +++ b/include/project.h @@ -18,14 +18,6 @@ #include #include -struct EventGraphics -{ - QImage spritesheet; - int spriteWidth; - int spriteHeight; - bool inanimate; -}; - // The displayed name of the special map value used by warps with multiple potential destinations static QString DYNAMIC_MAP_NAME = "Dynamic"; diff --git a/src/core/events.cpp b/src/core/events.cpp index 7f160481..bd74ec94 100644 --- a/src/core/events.cpp +++ b/src/core/events.cpp @@ -288,22 +288,39 @@ void ObjectEvent::loadPixmap(Project *project) { this->usingSprite = false; } else { this->setFrameFromMovement(project->facingDirections.value(this->movement)); - this->setPixmapFromSpritesheet(eventGfx->spritesheet, eventGfx->spriteWidth, eventGfx->spriteHeight, eventGfx->inanimate); + this->setPixmapFromSpritesheet(eventGfx); } } -void ObjectEvent::setPixmapFromSpritesheet(QImage spritesheet, int spriteWidth, int spriteHeight, bool inanimate) +void ObjectEvent::setPixmapFromSpritesheet(EventGraphics * gfx) { - int frame = inanimate ? 0 : this->frame; - QImage img = spritesheet.copy(frame * spriteWidth % spritesheet.width(), 0, spriteWidth, spriteHeight); - if (this->hFlip && !inanimate) { - img = img.transformed(QTransform().scale(-1, 1)); + QImage img; + if (gfx->inanimate) { + img = gfx->spritesheet.copy(0, 0, gfx->spriteWidth, gfx->spriteHeight); + } else { + int x = 0; + int y = 0; + + // Get frame's position in spritesheet. + // Assume horizontal layout. If position would exceed sheet width, try vertical layout. + if ((this->frame + 1) * gfx->spriteWidth <= gfx->spritesheet.width()) { + x = this->frame * gfx->spriteWidth; + } else if ((this->frame + 1) * gfx->spriteHeight <= gfx->spritesheet.height()) { + y = this->frame * gfx->spriteHeight; + } + + img = gfx->spritesheet.copy(x, y, gfx->spriteWidth, gfx->spriteHeight); + + // Right-facing sprite is just the left-facing sprite mirrored + if (this->hFlip) { + img = img.transformed(QTransform().scale(-1, 1)); + } } // Set first palette color fully transparent. img.setColor(0, qRgba(0, 0, 0, 0)); pixmap = QPixmap::fromImage(img); - this->spriteWidth = spriteWidth; - this->spriteHeight = spriteHeight; + this->spriteWidth = gfx->spriteWidth; + this->spriteHeight = gfx->spriteHeight; this->usingSprite = true; } @@ -435,7 +452,7 @@ void CloneObjectEvent::loadPixmap(Project *project) { this->usingSprite = false; } else { this->setFrameFromMovement(project->facingDirections.value(this->movement)); - this->setPixmapFromSpritesheet(eventGfx->spritesheet, eventGfx->spriteWidth, eventGfx->spriteHeight, eventGfx->inanimate); + this->setPixmapFromSpritesheet(eventGfx); } }