Skip to content

Commit

Permalink
Sprite is now a baseclass to new class BitmapSprite. Paving the way f…
Browse files Browse the repository at this point in the history
…or new sprite class VectorSprite.
  • Loading branch information
razterizer committed Oct 22, 2024
1 parent 78b4cc4 commit b33e891
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
10 changes: 5 additions & 5 deletions Examples/SpriteHandler_examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace sprite_handler

// //////////////////////////////

auto* sprite0 = sprh.create_sprite("spaceship");
auto* sprite0 = sprh.create_bitmap_sprite("spaceship");
sprite0->layer_id = 4;
sprite0->init(4, 5);
sprite0->create_frame(0);
Expand Down Expand Up @@ -66,7 +66,7 @@ namespace sprite_handler

// ///////////////////////////////////////////////////////////

auto* sprite1 = sprh.create_sprite("alien");
auto* sprite1 = sprh.create_bitmap_sprite("alien");
sprite1->layer_id = 5;
sprite1->init(2, 3);
sprite1->create_frame(0);
Expand Down Expand Up @@ -101,7 +101,7 @@ namespace sprite_handler
std::array<std::tuple<Sprite*, float, float>, 16> asteroids;
for (int a_idx = 0; a_idx < asteroids.size(); ++a_idx)
{
auto* sprite2 = sprh.create_sprite("asteroid" + std::to_string(a_idx));
auto* sprite2 = sprh.create_bitmap_sprite("asteroid" + std::to_string(a_idx));
sprite2->pos.r = rnd::rand_int(0, sh.num_rows()-1);
sprite2->pos.c = rnd::rand_int(0, sh.num_cols()-1);
sprite2->layer_id = rnd::rand_select<int>({ 1, 3 });
Expand All @@ -121,7 +121,7 @@ namespace sprite_handler

for (int s_idx = 0; s_idx < 20; ++s_idx)
{
auto* sprite3 = sprh.create_sprite("star" + std::to_string(s_idx));
auto* sprite3 = sprh.create_bitmap_sprite("star" + std::to_string(s_idx));
sprite3->pos.r = rnd::rand_int(0, sh.num_rows()-1);
sprite3->pos.c = rnd::rand_int(0, sh.num_cols()-1);
sprite3->layer_id = 0;
Expand All @@ -145,7 +145,7 @@ namespace sprite_handler
};
}

auto* sprite4 = sprh.create_sprite("background");
auto* sprite4 = sprh.create_bitmap_sprite("background");
sprite4->layer_id = 2;
sprite4->init(sh.num_rows(), sh.num_cols());
sprite4->create_frame(0);
Expand Down
70 changes: 41 additions & 29 deletions SpriteHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@


class Sprite
{
protected:
RC size { 0, 0 };
int area = 0;
std::string name;

public:
RC pos { 0, 0 };

int layer_id = 0; // 0 is the bottom layer.
bool enabled = true;

std::function<int(int)> func_frame_to_texture = [](int anim_frame) -> int { return 0; };

virtual ~Sprite() = default;
Sprite(const std::string& a_name) : name(a_name) {}

// Initialize the sprite's dimensions (NR and NC)
void init(int NR, int NC)
{
size = { NR, NC };
area = NR * NC;
}
};

class BitmapSprite : public Sprite
{
// Helper function for setting any vector data
template<typename T, typename... Args>
Expand Down Expand Up @@ -50,11 +76,8 @@ class Sprite
}
}
}

RC size { 0, 0 };
int area = 0;

std::vector<std::unique_ptr<drawing::Texture>> texture_frames;
std::string name;

drawing::Texture* fetch_frame(int anim_frame)
{
Expand All @@ -64,19 +87,7 @@ class Sprite
}

public:
RC pos { 0, 0 };

int layer_id = 0; // 0 is the bottom layer.
bool enabled = true;

Sprite(const std::string& a_name) : name(a_name) {}

// Initialize the sprite's dimensions (NR and NC)
void init(int NR, int NC)
{
size = { NR, NC };
area = NR * NC;
}
BitmapSprite(const std::string& a_name) : Sprite(a_name) {}

void create_frame(int anim_frame)
{
Expand Down Expand Up @@ -211,8 +222,6 @@ class Sprite
set_sprite_data(texture->materials, bb, mat...);
}

std::function<int(int)> func_frame_to_texture = [](int anim_frame) -> int { return 0; };

const drawing::Texture& get_curr_frame_texture(int anim_frame)
{
int tex_id = func_frame_to_texture(anim_frame);
Expand All @@ -232,11 +241,11 @@ class SpriteHandler
SpriteHandler() = default;
~SpriteHandler() = default;

Sprite* create_sprite(const std::string& sprite_name)
BitmapSprite* create_bitmap_sprite(const std::string& sprite_name)
{
m_sprites[sprite_name] = std::make_unique<Sprite>(sprite_name);
m_sprites[sprite_name] = std::make_unique<BitmapSprite>(sprite_name);
// We simply assume that it was successfully created.
return m_sprites[sprite_name].get();
return static_cast<BitmapSprite*>(m_sprites[sprite_name].get());
}

Sprite* fetch_sprite(const std::string& sprite_name)
Expand All @@ -262,13 +271,16 @@ class SpriteHandler
const auto& sprite = sprite_pair.second;
if (sprite->enabled && sprite->layer_id == layer_id)
{
auto& texture = sprite->get_curr_frame_texture(anim_frame);

drawing::draw_box_textured(sh,
sprite->pos.r - 1, sprite->pos.c - 1,
texture.size.r + 2, texture.size.c + 2,
drawing::SolarDirection::Zenith,
texture);
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);
}
}
}
}
Expand Down

0 comments on commit b33e891

Please sign in to comment.