Skip to content

Commit

Permalink
SpriteHandler.h:
Browse files Browse the repository at this point in the history
* Added virtual function is_opaque() in Sprite. This allows you to do some cool effects.
  • Loading branch information
razterizer committed Nov 16, 2024
1 parent bd48d83 commit 0246f0b
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions SpriteHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Sprite
virtual bool_vector calc_curr_mask(int sim_frame, const std::vector<int>& mask_materials) = 0;

virtual bool calc_cm() const = 0;

virtual bool is_opaque(int sim_frame, const RC& pt) const = 0;
};

// /////////////////////////////////////////////////
Expand Down Expand Up @@ -495,6 +497,18 @@ class BitmapSprite : public Sprite
}

virtual bool calc_cm() const override { return true; }

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

int r = pt.r - pos.r;
int c = pt.c - pos.c;
auto textel = (*texture)(r, c);
return !(textel.bg_color == Color::Transparent || textel.bg_color == Color::Transparent2);
}
};

// /////////////////////////////////////////////////
Expand Down Expand Up @@ -765,6 +779,29 @@ class VectorSprite : public Sprite
}

virtual bool calc_cm() const override { return false; }

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

auto aabb = calc_curr_AABB(sim_frame);
if (!aabb.contains(pos))
return false;

std::vector<RC> points;
for (const auto& line_seg : vector_frame->line_segments)
{
points.clear();
auto [p0, p1] = calc_seg_world_pos_round(line_seg);
bresenham::plot_line(p0, p1, points);
for (const auto& pt : points)
if (pt == pos)
return true;
}
return false;
}
};

// /////////////////////////////////////////////////
Expand Down

0 comments on commit 0246f0b

Please sign in to comment.