Skip to content

Commit

Permalink
SpriteHandler.h:
Browse files Browse the repository at this point in the history
* Added support for sprite rotation in VectorSprite.
  • Loading branch information
razterizer committed Oct 24, 2024
1 parent 9a9d89b commit d705169
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions SpriteHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ class VectorSprite : public Sprite

std::vector<std::unique_ptr<VectorFrame>> vector_frames;

float rot_rad = 0.f;

VectorFrame* fetch_frame(int anim_frame)
{
while (vector_frames.size() <= anim_frame)
Expand Down Expand Up @@ -290,6 +292,11 @@ class VectorSprite : public Sprite
return *vector_frames[frame_id];
}

void set_rotation(float rot_deg)
{
rot_rad = math::deg2rad(rot_deg);
}

template<int NR, int NC>
void draw(ScreenHandler<NR, NC>& sh, int sim_frame)
{
Expand All @@ -298,10 +305,16 @@ class VectorSprite : public Sprite
for (const auto& line_seg : vector_frame.line_segments)
{
const auto aspect_ratio = 1.5f;
auto r0 = pos.r + math::roundI(static_cast<float>(line_seg.pos[0].r));
auto c0 = pos.c + math::roundI(static_cast<float>(line_seg.pos[0].c)*aspect_ratio);
auto r1 = pos.r + math::roundI(static_cast<float>(line_seg.pos[1].r));
auto c1 = pos.c + math::roundI(static_cast<float>(line_seg.pos[1].c)*aspect_ratio);
auto rr0 = static_cast<float>(line_seg.pos[0].r);
auto cc0 = static_cast<float>(line_seg.pos[0].c);
auto rr1 = static_cast<float>(line_seg.pos[1].r);
auto cc1 = static_cast<float>(line_seg.pos[1].c);
float C = std::cos(rot_rad);
float S = std::sin(rot_rad);
auto r0 = pos.r + math::roundI(C*rr0 - S*cc0);
auto c0 = pos.c + math::roundI((S*rr0 + C*cc0)*aspect_ratio);
auto r1 = pos.r + math::roundI(C*rr1 - S*cc1);
auto c1 = pos.c + math::roundI((S*rr1 + C*cc1)*aspect_ratio);
RC p0 { r0, c0 };
RC p1 { r1, c1 };
bresenham::plot_line(sh, p0, p1, std::string(1, line_seg.ch), line_seg.style.fg_color, line_seg.style.bg_color);
Expand Down

0 comments on commit d705169

Please sign in to comment.