Skip to content

Commit

Permalink
Fixed removal during iteration in resource managers
Browse files Browse the repository at this point in the history
  • Loading branch information
nemene21 committed Jul 4, 2024
1 parent 3e8855f commit 110126a
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 85 deletions.
130 changes: 65 additions & 65 deletions build/Makefile

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/framework/components/collider_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,20 @@ void resolve_collision(Vector2 direction, ColliderComponent *coll1, ColliderComp
}
}
// Collider rectangle init
ColliderComponent::ColliderComponent(Entity *entity, float width, float height):
ColliderComponent::ColliderComponent(Entity *entity, Vector2 pos, float width, float height):
Component(CompType::COLLIDER, entity),
shape {nullptr},
is_rectangle {true},
is_circle {false},
collision_direction {0, 0},
position {0, 0}
position {pos}
{
shape = new Rectangle{0, 0, width, height};
update_shape_position();
}

// Collider circle init (crashes due to unimplemented circles)
ColliderComponent::ColliderComponent(Entity *entity, float radius):
ColliderComponent::ColliderComponent(Entity *entity, Vector2 pos, float radius):
Component(CompType::COLLIDER, entity),
shape {nullptr},
is_rectangle {false},
Expand Down
6 changes: 3 additions & 3 deletions src/framework/components/collider_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class ColliderComponent: public Component {
void *shape;
Vector2 position;

ColliderComponent(Vector2 pos);
ColliderComponent(Entity *entity, float width, float height);
ColliderComponent(Entity *entity, float radius);
ColliderComponent(Vector2 pos={0, 0});
ColliderComponent(Entity *entity, Vector2 pos, float width, float height);
ColliderComponent(Entity *entity, Vector2 pos, float radius);

/// @brief Sets the layers
/// @param new_layers Set of layers
Expand Down
12 changes: 7 additions & 5 deletions src/framework/entities/tilemap_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ void Tilemap::set_tile(int x, int y, int type) {
changed_chunks.insert({chunk_pos.first + 1, chunk_pos.second - 1});

// Build tile collider
ColliderComponent collider = ColliderComponent(this, tilesize.x, tilesize.y);
collider.position = {x * tilesize.x, y * tilesize.y};
ColliderComponent collider = ColliderComponent(this, {x * tilesize.x, y * tilesize.y}, tilesize.x, tilesize.y);
collider.set_layer((int)ColliderIndex::TILEMAP, true);
collider.process(0);

Expand Down Expand Up @@ -197,8 +196,7 @@ void Tilemap::build_chunk(std::pair<int, int> chunk_pos) {
bitmap += get_tile(pos.first + .5f, pos.second + .5f) == -1 ? "." : "#";

// Tile's collider, position and texture position data
ColliderComponent collider = ColliderComponent(this, tilesize.x, tilesize.y);
collider.position = {pos.first, pos.second};
ColliderComponent collider = ColliderComponent(this, {pos.first, pos.second}, tilesize.x, tilesize.y);

TileData data {{pos.first, pos.second}, {0, 0}};

Expand Down Expand Up @@ -263,7 +261,11 @@ void Tilemap::build_chunk(std::pair<int, int> chunk_pos) {
}
}

void Tilemap::process(float delta) {}
void Tilemap::process(float delta) {
if (IsKeyPressed(KEY_ENTER)) {
save("test.json");
}
}

// Draws the tilemap (spatial partitioning and camera culling at play)
void Tilemap::draw(float delta) {
Expand Down
11 changes: 7 additions & 4 deletions src/framework/objects/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,22 @@ void AudioManager::unload_sfx(std::string name) {

// Unloads all sounds and tracks that aren't being referanced
void AudioManager::unload_unused() {
for (auto &sound_pair: sound_map) {
std::vector<std::string> to_unload {};

for (auto &sound_pair: sound_map) {
if (sound_pair.second.use_count() == 1) {
unload_sfx(sound_pair.first);
to_unload.push_back(sound_pair.first);
}
}
for (auto to: to_unload) unload_sfx(to);
to_unload.clear();

for (auto &music_pair: music_map) {

if (music_pair.second.use_count() == 1 && !IsMusicStreamPlaying(*music_pair.second.get())) {
unload_track(music_pair.first);
to_unload.push_back(music_pair.first);
}
}
for (auto to: to_unload) unload_track(to);
}
// Ticks down a timer which calls "unload_unused()" when it hits 0 every "tick" seconds
void AudioManager::unload_check() {
Expand Down
12 changes: 8 additions & 4 deletions src/framework/objects/drawables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ void TextureManager::unload(std::string name) {

// Unloads all textures that aren't being referanced
void TextureManager::unload_unused() {
for (auto& texture_pair: texture_map) {
std::vector<std::string> to_unload {};

for (auto& texture_pair: texture_map) {
if (texture_pair.second.use_count() == 1) {
unload(texture_pair.first);
to_unload.push_back(texture_pair.first);
}
}
for (auto to: to_unload) unload(to);
}
// Ticks down a timer which calls "unload_unused()" when it hits 0 every "tick" seconds
void TextureManager::unload_check() {
Expand Down Expand Up @@ -96,12 +98,14 @@ void ShaderManager::unload(std::string name) {

// Unloads all shaders which aren't being referanced
void ShaderManager::unload_unused() {
for (auto& texture_pair: shader_map) {
std::vector<std::string> to_unload {};

for (auto& texture_pair: shader_map) {
if (texture_pair.second.use_count() == 1) {
unload(texture_pair.first);
to_unload.push_back(texture_pair.first);
}
}
for (auto to: to_unload) unload(to);
}

// Ticks down timer which will call "unload_unused()" after "tick" seconds
Expand Down
2 changes: 1 addition & 1 deletion src/framework/objects/particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ void ParticleDataManager::unload(std::string name) {
// Unloads all particle data that isn't being referanced
void ParticleDataManager::unload_unused() {
std::vector<std::string> to_unload {};

for (auto& texture_pair: particle_data_map) {

if (texture_pair.second.use_count() == 1) {
to_unload.push_back(texture_pair.first);
}
Expand Down

0 comments on commit 110126a

Please sign in to comment.