Created
January 21, 2019 04:04
-
-
Save cleak/289d0d9312ab08b24b0ee688c8215ddb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef void (UpdateFn)(void*); | |
struct CompareUpdateFns { | |
bool operator()(const std::pair<UpdateFn*, void*>& a, | |
const std::pair<UpdateFn*, void*>& b) const { | |
if (a.first == b.first) { | |
return a.second < b.second; | |
} | |
return a.first < b.first; | |
} | |
}; | |
class SceneObject { | |
public: | |
virtual ~SceneObject() {} | |
virtual void Update() {} | |
}; | |
struct SceneObject_VTable { | |
void* dtor; | |
UpdateFn* update; | |
}; | |
UpdateFn* GetUpdateFn(SceneObject* obj) { | |
SceneObject_VTable** vtable_ptr = (SceneObject_VTable**)obj; | |
return (*vtable_ptr)->update; | |
} | |
class SceneManager { | |
public: | |
void Add(SceneObject* obj) { | |
update_fns_.insert({ GetUpdateFn(obj), obj }); | |
} | |
void UpdateAll() { | |
for (auto& update : update_fns_) { | |
update.first(update.second); | |
} | |
} | |
private: | |
std::set<std::pair<UpdateFn*, void*>, CompareUpdateFns> update_fns_; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment