Skip to content

Commit

Permalink
SpriteHandler.h:
Browse files Browse the repository at this point in the history
* Making Sprite::calc_curr_AABB() const.
* Adding function Sprite::calc_centroid().
* Making get_curr_frame() functions const.
* Splitting VectorSprite::calc_seg_world_pos() into a function that generates two Vec2 and another that converts them to a pair of RC positions.
* Simplified debug drawing.
* Adding debug drawing of sprite position and sprite (frame) centroid.
  • Loading branch information
razterizer committed Oct 24, 2024
1 parent 1683d95 commit 7544639
Showing 1 changed file with 51 additions and 18 deletions.
69 changes: 51 additions & 18 deletions SpriteHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "ScreenHandler.h"
#include "Drawing.h"
#include "AABB.h"
#include "Vec2.h"
#include <map>
#include <memory>

Expand All @@ -30,7 +31,9 @@ class Sprite
virtual ~Sprite() = default;
Sprite(const std::string& a_name) : name(a_name) {}

virtual AABB<int> calc_curr_AABB(int /*sim_frame*/) = 0;
virtual AABB<int> calc_curr_AABB(int /*sim_frame*/) const = 0;

virtual RC calc_centroid(int /*sim_frame*/) const = 0;
};

class BitmapSprite : public Sprite
Expand Down Expand Up @@ -225,7 +228,7 @@ class BitmapSprite : public Sprite
set_sprite_data(texture->materials, bb, mat...);
}

const drawing::Texture& get_curr_frame(int sim_frame)
const drawing::Texture& get_curr_frame(int sim_frame) const
{
int frame_id = func_calc_anim_frame(sim_frame);
if (frame_id >= texture_frames.size())
Expand All @@ -246,10 +249,15 @@ class BitmapSprite : public Sprite

}

virtual AABB<int> calc_curr_AABB(int /*sim_frame*/) override
virtual AABB<int> calc_curr_AABB(int /*sim_frame*/) const override
{
return { pos.r, pos.c, size.r, size.c };
}

virtual RC calc_centroid(int /*sim_frame*/) const override
{
return { pos.r + size.r / 2, pos.c + size.c / 2 };
}
};

class VectorSprite : public Sprite
Expand Down Expand Up @@ -278,7 +286,7 @@ class VectorSprite : public Sprite
return vector_frames[anim_frame].get();
}

std::pair<RC, RC> calc_seg_world_pos(const LineSeg& line_seg) const
std::pair<Vec2, Vec2> calc_seg_world_pos_flt(const LineSeg& line_seg) const
{
const auto aspect_ratio = 1.5f;
auto rr0 = static_cast<float>(line_seg.pos[0].r);
Expand All @@ -287,15 +295,21 @@ class VectorSprite : public Sprite
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 };
auto r0 = pos.r + (C*rr0 - S*cc0);
auto c0 = pos.c + (S*rr0 + C*cc0)*aspect_ratio;
auto r1 = pos.r + (C*rr1 - S*cc1);
auto c1 = pos.c + (S*rr1 + C*cc1)*aspect_ratio;
Vec2 p0 { r0, c0 };
Vec2 p1 { r1, c1 };
return { p0, p1 };
}

std::pair<RC, RC> calc_seg_world_pos(const LineSeg& line_seg) const
{
auto [v0, v1] = calc_seg_world_pos_flt(line_seg);
return { v0, v1 };
}

public:
VectorSprite(const std::string& a_name) : Sprite(a_name) {}

Expand All @@ -310,7 +324,7 @@ class VectorSprite : public Sprite
line_seg.mat = mat;
}

const VectorFrame& get_curr_frame(int sim_frame)
const VectorFrame& get_curr_frame(int sim_frame) const
{
int frame_id = func_calc_anim_frame(sim_frame);
if (frame_id >= vector_frames.size())
Expand All @@ -335,7 +349,7 @@ class VectorSprite : public Sprite
}
}

virtual AABB<int> calc_curr_AABB(int sim_frame) override
virtual AABB<int> calc_curr_AABB(int sim_frame) const override
{
auto& vector_frame = get_curr_frame(sim_frame);

Expand All @@ -348,6 +362,23 @@ class VectorSprite : public Sprite
}
return aabb;
}

virtual RC calc_centroid(int sim_frame) const override
{
auto& vector_frame = get_curr_frame(sim_frame);

Vec2 centroid;
int ctr = 0;
for (const auto& line_seg : vector_frame.line_segments)
{
auto [p0, p1] = calc_seg_world_pos_flt(line_seg);
centroid += p0;
centroid += p1;
ctr += 2;
}
centroid /= static_cast<float>(ctr);
return centroid;
}
};

// /////////////////////////////////////
Expand Down Expand Up @@ -421,12 +452,14 @@ class SpriteHandler
const auto& sprite = sprite_pair.second;
if (sprite->enabled && sprite->layer_id == layer_id)
{
AABB<int> aabb;
if (auto* bitmap_sprite = dynamic_cast<BitmapSprite*>(sprite.get()); bitmap_sprite != nullptr)
aabb = bitmap_sprite->calc_curr_AABB(sim_frame);
else if (auto* vector_sprite = dynamic_cast<VectorSprite*>(sprite.get()); vector_sprite != nullptr)
aabb = vector_sprite->calc_curr_AABB(sim_frame);

auto pos = sprite->pos;
sh.write_buffer("O", pos.r, pos.c, Color::DarkGray);

auto centroid = sprite->calc_centroid(sim_frame);
sh.write_buffer("x", centroid.r, centroid.c, Color::DarkYellow);

auto aabb = sprite->calc_curr_AABB(sim_frame);

auto rec = aabb.to_rectangle();
drawing::draw_box_outline(sh, rec, drawing::OutlineType::Line, { Color::LightGray, Color::Transparent2 });
}
Expand Down

0 comments on commit 7544639

Please sign in to comment.