From 5e3e8f8d7311e60f132b898a644ec0bd7afafc24 Mon Sep 17 00:00:00 2001 From: Ghabry Date: Mon, 15 Apr 2024 18:49:43 +0200 Subject: [PATCH] Sprite_Character/AirshipShadow: Move functions that are only used by Sprite from Update to Draw. This is similiar how Pictures do it since years. Avoids some unnecessary function calls when Player is frame-skipping as less draw related code is processed. --- src/sprite_airshipshadow.cpp | 20 ++++++++++++------- src/sprite_airshipshadow.h | 1 + src/sprite_character.cpp | 38 +++++++++++++++++++++--------------- src/sprite_character.h | 2 ++ tests/game_character.cpp | 14 ------------- 5 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/sprite_airshipshadow.cpp b/src/sprite_airshipshadow.cpp index ca57f231d19..ce51f8230f1 100644 --- a/src/sprite_airshipshadow.cpp +++ b/src/sprite_airshipshadow.cpp @@ -54,6 +54,19 @@ void Sprite_AirshipShadow::RecreateShadow() { GetBitmap()->Blit(0, 0, *system, Rect(128+16,32,16,16), opacity); } +void Sprite_AirshipShadow::Draw(Bitmap &dst) { + Game_Vehicle* airship = Game_Map::GetVehicle(Game_Vehicle::Airship); + const int altitude = airship->GetAltitude(); + const int max_altitude = TILE_SIZE; + const double opacity = (double)altitude / max_altitude; + SetOpacity(opacity * 255); + + SetX(Main_Data::game_player->GetScreenX() + x_offset); + SetY(Main_Data::game_player->GetScreenY() + y_offset + Main_Data::game_player->GetJumpHeight()); + + Sprite::Draw(dst); +} + void Sprite_AirshipShadow::Update() { if (!Main_Data::game_player->InAirship()) { SetVisible(false); @@ -63,13 +76,6 @@ void Sprite_AirshipShadow::Update() { Game_Vehicle* airship = Game_Map::GetVehicle(Game_Vehicle::Airship); - const int altitude = airship->GetAltitude(); - const int max_altitude = TILE_SIZE; - const double opacity = (double)altitude / max_altitude; - SetOpacity(opacity * 255); - - SetX(Main_Data::game_player->GetScreenX() + x_offset); - SetY(Main_Data::game_player->GetScreenY() + y_offset + Main_Data::game_player->GetJumpHeight()); // Synchronized with airship priority SetZ(airship->GetScreenZ(x_offset, y_offset) - 1); } diff --git a/src/sprite_airshipshadow.h b/src/sprite_airshipshadow.h index 2d5b89ea351..f7322686466 100644 --- a/src/sprite_airshipshadow.h +++ b/src/sprite_airshipshadow.h @@ -38,6 +38,7 @@ class Sprite_AirshipShadow : public Sprite { }; Sprite_AirshipShadow(int x_offset = 0, int y_offset = 0); + void Draw(Bitmap& dst) override; void Update(); void RecreateShadow(); diff --git a/src/sprite_character.cpp b/src/sprite_character.cpp index 86218e04a91..f0dccc49754 100644 --- a/src/sprite_character.cpp +++ b/src/sprite_character.cpp @@ -34,6 +34,27 @@ Sprite_Character::Sprite_Character(Game_Character* character, int x_offset, int Update(); } +void Sprite_Character::Draw(Bitmap &dst) { + if (UsesCharset()) { + int row = character->GetFacing(); + auto frame = character->GetAnimFrame(); + if (frame >= lcf::rpg::EventPage::Frame_middle2) frame = lcf::rpg::EventPage::Frame_middle; + SetSrcRect({frame * chara_width, row * chara_height, chara_width, chara_height}); + } + + SetFlashEffect(character->GetFlashColor()); + + SetOpacity(character->GetOpacity()); + + SetX(character->GetScreenX() + x_offset); + SetY(character->GetScreenY() + y_offset); + + int bush_split = 4 - character->GetBushDepth(); + SetBushDepth(bush_split > 3 ? 0 : GetHeight() / bush_split); + + Sprite::Draw(dst); +} + void Sprite_Character::Update() { if (tile_id != character->GetTileId() || character_name != character->GetSpriteName() || @@ -63,29 +84,14 @@ void Sprite_Character::Update() { } } - if (UsesCharset()) { - int row = character->GetFacing(); - auto frame = character->GetAnimFrame(); - if (frame >= lcf::rpg::EventPage::Frame_middle2) frame = lcf::rpg::EventPage::Frame_middle; - SetSrcRect({frame * chara_width, row * chara_height, chara_width, chara_height}); - } - - SetFlashEffect(character->GetFlashColor()); - - SetOpacity(character->GetOpacity()); SetVisible(character->IsVisible()); - - SetX(character->GetScreenX() + x_offset); - SetY(character->GetScreenY() + y_offset); SetZ(character->GetScreenZ(x_offset, y_offset)); - - int bush_split = 4 - character->GetBushDepth(); - SetBushDepth(bush_split > 3 ? 0 : GetHeight() / bush_split); } Game_Character* Sprite_Character::GetCharacter() { return character; } + void Sprite_Character::SetCharacter(Game_Character* new_character) { character = new_character; } diff --git a/src/sprite_character.h b/src/sprite_character.h index 5c820c0a69a..bb8f9187df5 100644 --- a/src/sprite_character.h +++ b/src/sprite_character.h @@ -47,6 +47,8 @@ class Sprite_Character : public Sprite { */ Sprite_Character(Game_Character* character, int x_offset = 0, int y_offset = 0); + void Draw(Bitmap& dst) override; + /** * Updates sprite state. */ diff --git a/tests/game_character.cpp b/tests/game_character.cpp index 99c6bc9bb73..09f0ab281fb 100644 --- a/tests/game_character.cpp +++ b/tests/game_character.cpp @@ -77,20 +77,6 @@ static_assert(Game_Character::IsDirectionFixedAnimationType(lcf::rpg::EventPage: static_assert(!Game_Character::IsDirectionFixedAnimationType(lcf::rpg::EventPage::AnimType_spin), "DirFixedBroken"); static_assert(!Game_Character::IsDirectionFixedAnimationType(lcf::rpg::EventPage::AnimType_step_frame_fix), "DirFixedBroken"); -#if 0 - - /** @return the direction we would need to face the hero. */ - int GetDirectionToHero(); - - /** @return the direction we would need to face away from hero. */ - int GetDirectionAwayHero(); - - virtual Drawable::Z_t GetScreenZ(bool apply_shift = false) const; - - int DistanceXfromPlayer() const; - int DistanceYfromPlayer() const; -#endif - TEST_SUITE_BEGIN("Game_Character"); static void testInit(Game_Character& ch, int max_stop_count = 0) {