Skip to content

Commit

Permalink
Merge pull request #3053 from Ghabry/fix/spritesheet
Browse files Browse the repository at this point in the history
Picture: Consider spritesheet when calculating the origin
  • Loading branch information
fdelapena authored Jul 28, 2023
2 parents 4cacbbb + e7c6e41 commit 23ac5cf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/game_pictures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ void Game_Pictures::Picture::ApplyOrigin(bool is_move) {
y = data.current_y;
}

double width = sprite->GetWidth();
double height = sprite->GetHeight();
double width = sprite->GetFrameWidth();
double height = sprite->GetFrameHeight();

switch (origin) {
case 1:
Expand Down
28 changes: 28 additions & 0 deletions src/sprite_picture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,31 @@ void Sprite_Picture::Draw(Bitmap& dst) {

Sprite::Draw(dst);
}

int Sprite_Picture::GetFrameWidth() const {
const auto& pic = Main_Data::game_pictures->GetPicture(pic_id);
const auto& data = pic.data;

auto& bitmap = GetBitmap();
assert(bitmap);

if (feature_spritesheet && pic.NumSpriteSheetFrames() > 1) {
return bitmap->GetWidth() / data.spritesheet_cols;
} else {
return bitmap->GetWidth();
}
}

int Sprite_Picture::GetFrameHeight() const {
const auto& pic = Main_Data::game_pictures->GetPicture(pic_id);
const auto& data = pic.data;

auto& bitmap = GetBitmap();
assert(bitmap);

if (feature_spritesheet && pic.NumSpriteSheetFrames() > 1) {
return bitmap->GetHeight() / data.spritesheet_rows;
} else {
return bitmap->GetHeight();
}
}
6 changes: 6 additions & 0 deletions src/sprite_picture.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class Sprite_Picture : public Sprite {

void OnPictureShow();

/** @return Width of a single spritesheet frame or the entire width if the picture has no spritesheet */
int GetFrameWidth() const;

/** @return Height of a single spritesheet frame or the entire width if the picture has no spritesheet */
int GetFrameHeight() const;

private:
int last_spritesheet_frame = -1;
const int pic_id = 0;
Expand Down

0 comments on commit 23ac5cf

Please sign in to comment.