Skip to content

Commit

Permalink
Improve Sprite unloading
Browse files Browse the repository at this point in the history
  • Loading branch information
zyperpl committed Dec 14, 2023
1 parent beeac24 commit 4a0b8a7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/asteroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Particle create_asteroid_particle(const Vector2 &position, unsigned char alpha =
return Particle::create(pos, vel, color);
}

[[nodiscard]] Asteroid Asteroid::create(const Vector2 &position, int size)
[[nodiscard]] Asteroid Asteroid::create(const Vector2 &position, uint8_t size)
{
assert(size >= 0 && size < 3);

Expand Down
8 changes: 4 additions & 4 deletions src/asteroid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class Asteroid
public:
Vector2 position{};
Vector2 velocity{};
int size{ 2 };
int max_life { 1 };
int life{ max_life };
uint8_t size{ 2 };
uint8_t max_life { 1 };
uint8_t life{ max_life };
mutable Sprite sprite{ "resources/asteroid.aseprite" };
Mask mask{};

[[nodiscard]] static Asteroid create(const Vector2 &position, int size);
[[nodiscard]] static Asteroid create(const Vector2 &position, uint8_t size);

bool update();
void die();
Expand Down
32 changes: 28 additions & 4 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ Game &Game::get() noexcept
void Game::init()
{
player = std::make_unique<Player>();
bullets = std::make_unique<ObjectCircularBuffer<Bullet, 64>>();
asteroids = std::make_unique<ObjectCircularBuffer<Asteroid, 128>>();
particles = std::make_unique<ObjectCircularBuffer<Particle, 1024>>();
pickables = std::make_unique<ObjectCircularBuffer<Pickable, 64>>();
bullets = std::make_unique<ObjectCircularBuffer<Bullet, 128>>();
asteroids = std::make_unique<ObjectCircularBuffer<Asteroid, 1024>>();
particles = std::make_unique<ObjectCircularBuffer<Particle, 4096>>();
pickables = std::make_unique<ObjectCircularBuffer<Pickable, 1024>>();

SetTraceLogLevel(LOG_TRACE);

TraceLog(LOG_TRACE, "Size of Asteroid buffer: %zukB", sizeof(Asteroid) * asteroids->capacity / 1024);
TraceLog(LOG_TRACE, "Size of Bullet buffer: %zukB", sizeof(Bullet) * bullets->capacity / 1024);
TraceLog(LOG_TRACE, "Size of Particle buffer: %zukB", sizeof(Particle) * particles->capacity / 1024);
TraceLog(LOG_TRACE, "Size of Pickable buffer: %zukB", sizeof(Pickable) * pickables->capacity / 1024);

for (size_t i = 0; i < NUMBER_OF_ASTEROIDS; i++)
{
Expand Down Expand Up @@ -82,6 +89,23 @@ void Game::update()
}
}

#if defined(DEBUG)

if (IsKeyDown(KEY_LEFT_SHIFT))
{
if (IsKeyPressed(KEY_F1))
{
for (size_t i = 0; i < NUMBER_OF_ASTEROIDS; i++)
{
const Vector2 position = { static_cast<float>(GetRandomValue(0, width)),
static_cast<float>(GetRandomValue(0, height)) };
asteroids->push(Asteroid::create(position, 2));
}
}
}

#endif

frame++;
}

Expand Down
8 changes: 4 additions & 4 deletions src/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class Game
static Game &get() noexcept;

std::unique_ptr<Player> player;
std::unique_ptr<ObjectCircularBuffer<Bullet, 64>> bullets;
std::unique_ptr<ObjectCircularBuffer<Asteroid, 128>> asteroids;
std::unique_ptr<ObjectCircularBuffer<Particle, 1024>> particles;
std::unique_ptr<ObjectCircularBuffer<Pickable, 64>> pickables;
std::unique_ptr<ObjectCircularBuffer<Bullet, 128>> bullets;
std::unique_ptr<ObjectCircularBuffer<Asteroid, 1024>> asteroids;
std::unique_ptr<ObjectCircularBuffer<Particle, 4096>> particles;
std::unique_ptr<ObjectCircularBuffer<Pickable, 1024>> pickables;

static constexpr int width = 640;
static constexpr int height = 360;
Expand Down
2 changes: 1 addition & 1 deletion src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void Player::handle_input()
bullet_velocity.x += velocity.x * 0.5f;
bullet_velocity.y += velocity.y * 0.5f;

BulletType bullet_type = BulletType::Normal;
BulletType bullet_type = BulletType::Homing;

if (bullet_type == BulletType::Normal)
{
Expand Down
6 changes: 5 additions & 1 deletion src/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ Sprite::Sprite(const std::string &file_path, std::string tag_name)
Sprite::~Sprite()
{
file_path_usage_count[path] -= 1;
assert(file_path_usage_count[path] >= 0);
if (file_path_usage_count[path] == 0)
{
TraceLog(LOG_TRACE, "Sprite(%s) unloaded", path.data());
sprite_cache.erase(path);
}
}

void Sprite::load_texture_with_animation()
Expand Down

0 comments on commit 4a0b8a7

Please sign in to comment.