Skip to content

Commit

Permalink
SpriteHandler.h:
Browse files Browse the repository at this point in the history
* Refactoring drawing code.
* Adding drawing code for VectorSprite.
  • Loading branch information
razterizer committed Oct 23, 2024
1 parent 92504e6 commit a3ce077
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions SpriteHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,34 @@ class BitmapSprite : public Sprite
throw std::invalid_argument("ERROR: Incorrect frame id: " + std::to_string(frame_id) + " for sprite \"" + name + "\"! Sprite only has " + std::to_string(texture_frames.size()) + " frames.");
return *texture_frames[frame_id];
}

template<int NR, int NC>
void draw(ScreenHandler<NR, NC>& sh, int sim_frame)
{
auto& texture = get_curr_frame_texture(sim_frame);

drawing::draw_box_textured(sh,
pos.r - 1, pos.c - 1,
texture.size.r + 2, texture.size.c + 2,
drawing::SolarDirection::Zenith,
texture);

}
};

class VectorSprite : public Sprite
{
struct LineSeg
{
std::array<RC, 2> line_seg;
std::array<RC, 2> pos;
char ch = 0;
styles::Style style;
int mat = 0;
};

struct VectorFrame
{
std::vector<LineSeg> m_line_segments;
std::vector<LineSeg> line_segments;
};

std::vector<std::unique_ptr<VectorFrame>> vector_frames;
Expand All @@ -258,6 +271,19 @@ class VectorSprite : public Sprite
throw std::invalid_argument("ERROR: Incorrect frame id: " + std::to_string(frame_id) + " for sprite \"" + name + "\"! Sprite only has " + std::to_string(vector_frames.size()) + " frames.");
return *vector_frames[frame_id];
}

template<int NR, int NC>
void draw(ScreenHandler<NR, NC>& sh, int sim_frame)
{
auto& vector_frame = get_curr_frame_vector(sim_frame);

for (const auto& line_seg : vector_frame.line_segments)
{
const auto& p0 = line_seg.pos[0];
const auto& p1 = line_seg.pos[1];
bresenham::plot_line(sh, p0, p1, std::string(1, line_seg.ch), line_seg.style.fg_color, line_seg.style.bg_color);
}
}
};

// /////////////////////////////////////
Expand Down Expand Up @@ -286,7 +312,7 @@ class SpriteHandler
}

template<int NR, int NC>
void draw(ScreenHandler<NR, NC>& sh, int anim_frame) const
void draw(ScreenHandler<NR, NC>& sh, int sim_frame) const
{
int max_layer_id = 0;
for (const auto& sprite_pair : m_sprites)
Expand All @@ -301,15 +327,9 @@ class SpriteHandler
if (sprite->enabled && sprite->layer_id == layer_id)
{
if (auto* bitmap_sprite = dynamic_cast<BitmapSprite*>(sprite.get()); bitmap_sprite != nullptr)
{
auto& texture = bitmap_sprite->get_curr_frame_texture(anim_frame);

drawing::draw_box_textured(sh,
bitmap_sprite->pos.r - 1, bitmap_sprite->pos.c - 1,
texture.size.r + 2, texture.size.c + 2,
drawing::SolarDirection::Zenith,
texture);
}
bitmap_sprite->draw(sh, sim_frame);
else if (auto* vector_sprite = dynamic_cast<VectorSprite*>(sprite.get()); vector_sprite != nullptr)
vector_sprite->draw(sh, sim_frame);
}
}
}
Expand Down

0 comments on commit a3ce077

Please sign in to comment.