Skip to content

Commit

Permalink
SpriteHandler.h:
Browse files Browse the repository at this point in the history
* Splitting up the functions get_curr_frame() into two variants get_curr_sim_frame() and get_curr_local_frame() because we need to be specific sometimes.
* Fixing bug in clone_sprite() due to using sim_frame index rather than the actual local frame_id when copying individual frames.
  • Loading branch information
razterizer committed Dec 4, 2024
1 parent 7040733 commit 16fcdf8
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions SpriteHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ class BitmapSprite : public Sprite
std::optional<Color> bg_color, std::optional<Color> bg_color_replace,
std::optional<int> mat, std::optional<int> mat_replace)
{
auto* texture = get_curr_frame(sim_frame);
auto* texture = get_curr_sim_frame(sim_frame);
if (texture == nullptr)
return false;
std::vector<RC> points;
Expand Down Expand Up @@ -578,9 +578,14 @@ class BitmapSprite : public Sprite
flip_lr(anim_frame);
}

drawing::Texture* get_curr_frame(int sim_frame) const
drawing::Texture* get_curr_sim_frame(int sim_frame) const
{
int frame_id = func_calc_anim_frame(sim_frame);
return get_curr_local_frame(frame_id);
}

drawing::Texture* get_curr_local_frame(int frame_id) const
{
if (frame_id >= stlutils::sizeI(texture_frames))
{
std::cerr << "ERROR in BitmapSprite::get_curr_frame() : Incorrect frame id: " + std::to_string(frame_id) + " for sprite \"" + name + "\"! Sprite only has " + std::to_string(texture_frames.size()) + " frames." << std::endl;
Expand All @@ -597,7 +602,7 @@ class BitmapSprite : public Sprite
template<int NR, int NC>
bool draw(ScreenHandler<NR, NC>& sh, int sim_frame)
{
auto* texture = get_curr_frame(sim_frame);
auto* texture = get_curr_sim_frame(sim_frame);
if (texture == nullptr)
return false;

Expand All @@ -622,7 +627,7 @@ class BitmapSprite : public Sprite

virtual bool_vector calc_curr_mask(int sim_frame, const std::vector<int>& mask_materials) override
{
const auto* texture = get_curr_frame(sim_frame);
const auto* texture = get_curr_sim_frame(sim_frame);
if (texture == nullptr)
return {};
const auto num_mats = stlutils::sizeI(texture->materials);
Expand All @@ -636,7 +641,7 @@ class BitmapSprite : public Sprite

virtual bool is_opaque(int sim_frame, const RC& pt) const override
{
const auto* texture = get_curr_frame(sim_frame);
const auto* texture = get_curr_sim_frame(sim_frame);
if (texture == nullptr)
return false;

Expand All @@ -648,7 +653,7 @@ class BitmapSprite : public Sprite

virtual std::vector<RC> get_opaque_points(int sim_frame) const override
{
const auto* texture = get_curr_frame(sim_frame);
const auto* texture = get_curr_sim_frame(sim_frame);
if (texture == nullptr)
return {};

Expand Down Expand Up @@ -812,9 +817,14 @@ class VectorSprite : public Sprite
*frame_dst = frame;
}

VectorFrame* get_curr_frame(int sim_frame) const
VectorFrame* get_curr_sim_frame(int sim_frame) const
{
int frame_id = func_calc_anim_frame(sim_frame);
return get_curr_local_frame(frame_id);
}

VectorFrame* get_curr_local_frame(int frame_id) const
{
if (frame_id >= stlutils::sizeI(vector_frames))
{
std::cerr << "ERROR in VectorSprite::get_curr_frame() : Incorrect frame id: " + std::to_string(frame_id) + " for sprite \"" + name + "\"! Sprite only has " + std::to_string(vector_frames.size()) + " frames." << std::endl;
Expand All @@ -841,7 +851,7 @@ class VectorSprite : public Sprite
template<int NR, int NC>
bool draw(ScreenHandler<NR, NC>& sh, int sim_frame)
{
auto* vector_frame = get_curr_frame(sim_frame);
auto* vector_frame = get_curr_sim_frame(sim_frame);
if (vector_frame == nullptr)
return false;

Expand Down Expand Up @@ -891,7 +901,7 @@ class VectorSprite : public Sprite

virtual AABB<int> calc_curr_AABB(int sim_frame) const override
{
auto* vector_frame = get_curr_frame(sim_frame);
auto* vector_frame = get_curr_sim_frame(sim_frame);
if (vector_frame == nullptr)
return {};

Expand All @@ -912,7 +922,7 @@ class VectorSprite : public Sprite

virtual bool_vector calc_curr_mask(int sim_frame, const std::vector<int>& mask_materials) override
{
const auto* vector_frame = get_curr_frame(sim_frame);
const auto* vector_frame = get_curr_sim_frame(sim_frame);
if (vector_frame == nullptr)
return {};

Expand All @@ -939,7 +949,7 @@ class VectorSprite : public Sprite

virtual bool is_opaque(int sim_frame, const RC& pos) const override
{
const auto* vector_frame = get_curr_frame(sim_frame);
const auto* vector_frame = get_curr_sim_frame(sim_frame);
if (vector_frame == nullptr)
return false;

Expand All @@ -962,7 +972,7 @@ class VectorSprite : public Sprite

virtual std::vector<RC> get_opaque_points(int sim_frame) const override
{
const auto* vector_frame = get_curr_frame(sim_frame);
const auto* vector_frame = get_curr_sim_frame(sim_frame);
if (vector_frame == nullptr)
return {};

Expand Down Expand Up @@ -1050,7 +1060,7 @@ class SpriteHandler
sprite_dst_bitmap->init(size.r, size.c);
for (int frame_id = 0; frame_id < sprite_src_bitmap->num_frames(); ++frame_id)
{
auto* texture = sprite_src_bitmap->get_curr_frame(frame_id);
auto* texture = sprite_src_bitmap->get_curr_local_frame(frame_id);
if (texture != nullptr)
sprite_dst_bitmap->set_frame(frame_id, *texture);
}
Expand All @@ -1063,7 +1073,7 @@ class SpriteHandler
sprite_dst_vector->set_rotation(sprite_src_vector->get_rotation());
for (int frame_id = 0; frame_id < sprite_src_vector->num_frames(); ++frame_id)
{
auto* frame = sprite_src_vector->get_curr_frame(frame_id);
auto* frame = sprite_src_vector->get_curr_local_frame(frame_id);
if (frame != nullptr)
sprite_dst_vector->set_frame(frame_id, *frame);
}
Expand Down

0 comments on commit 16fcdf8

Please sign in to comment.