Skip to content

Commit

Permalink
Reduce flushing of game objects
Browse files Browse the repository at this point in the history
  • Loading branch information
tobbi committed Jul 6, 2023
1 parent d78c56b commit 61170fb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/supertux/game_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ GameObject::after_editor_set()
m_previous_type = -1;
}

void
GameObject::remove_me()
{
m_scheduled_for_removal = true;
GameObjectManager::s_needs_flushing_gameobjects_removed = true;
}

int
GameObject::type_id_to_value(const std::string& id) const
{
Expand Down
2 changes: 1 addition & 1 deletion src/supertux/game_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class GameObject
virtual void on_flip(float height) {}

/** schedules this object to be removed at the end of the frame */
virtual void remove_me() { m_scheduled_for_removal = true; }
virtual void remove_me();

/** returns true if the object is not scheduled to be removed yet */
bool is_valid() const { return !m_scheduled_for_removal; }
Expand Down
28 changes: 26 additions & 2 deletions src/supertux/game_object_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include "supertux/game_object_factory.hpp"

bool GameObjectManager::s_draw_solids_only = false;
bool GameObjectManager::s_needs_flushing_gameobjects_added = false;
bool GameObjectManager::s_needs_flushing_gameobjects_removed = false;
bool GameObjectManager::s_needs_flushing_tilemaps = false;

GameObjectManager::GameObjectManager(bool undo_tracking) :
m_initialized(false),
Expand Down Expand Up @@ -134,6 +137,7 @@ GameObjectManager::add_object(std::unique_ptr<GameObject> object)

GameObject& tmp = *object;
m_gameobjects_new.push_back(std::move(object));
s_needs_flushing_gameobjects_added = true;
return tmp;
}

Expand Down Expand Up @@ -182,6 +186,12 @@ GameObjectManager::draw(DrawingContext& context)
void
GameObjectManager::flush_game_objects()
{
if(!s_needs_flushing_gameobjects_added &&
!s_needs_flushing_gameobjects_removed &&
!s_needs_flushing_tilemaps)
return;

if(s_needs_flushing_gameobjects_removed)
{ // cleanup marked objects
m_gameobjects.erase(
std::remove_if(m_gameobjects.begin(), m_gameobjects.end(),
Expand All @@ -196,8 +206,10 @@ GameObjectManager::flush_game_objects()
}
}),
m_gameobjects.end());
s_needs_flushing_gameobjects_removed = false;
}

if(s_needs_flushing_gameobjects_added)
{ // add newly created objects
// Objects might add new objects in finish_construction(), so we
// loop until no new objects show up.
Expand All @@ -213,8 +225,14 @@ GameObjectManager::flush_game_objects()
}
}
}
s_needs_flushing_gameobjects_added = false;
}

if(s_needs_flushing_tilemaps)
{
update_tilemaps();
s_needs_flushing_tilemaps = false;
}
update_tilemaps();

m_initialized = true;
}
Expand Down Expand Up @@ -381,7 +399,12 @@ GameObjectManager::this_before_object_add(GameObject& object)
}

{ // by_type_index
m_objects_by_type_index[std::type_index(typeid(object))].push_back(&object);
auto typeIdx = std::type_index(typeid(object));
m_objects_by_type_index[typeIdx].push_back(&object);
if(typeIdx == typeid(TileMap))
{
s_needs_flushing_tilemaps = true;
}
}

save_object_change(object, true);
Expand Down Expand Up @@ -409,6 +432,7 @@ GameObjectManager::this_before_object_remove(GameObject& object)
auto it = std::find(vec.begin(), vec.end(), &object);
assert(it != vec.end());
vec.erase(it);
s_needs_flushing_gameobjects_removed = true;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/supertux/game_object_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class GameObjectManager
{
public:
static bool s_draw_solids_only;
static bool s_needs_flushing_gameobjects_added;
static bool s_needs_flushing_gameobjects_removed;
static bool s_needs_flushing_tilemaps;

private:
struct NameResolveRequest
Expand Down

0 comments on commit 61170fb

Please sign in to comment.