Skip to content

Commit

Permalink
Sprite_Character/AirshipShadow: Move functions that are only used by …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
Ghabry committed Apr 15, 2024
1 parent c338202 commit 89b4537
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 37 deletions.
20 changes: 13 additions & 7 deletions src/sprite_airshipshadow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
1 change: 1 addition & 0 deletions src/sprite_airshipshadow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
38 changes: 22 additions & 16 deletions src/sprite_character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() ||
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/sprite_character.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
14 changes: 0 additions & 14 deletions tests/game_character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 89b4537

Please sign in to comment.