diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-01-01 12:03:58 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-01-01 12:03:58 +0100 |
commit | 8906ff2e2755f6f8a2e52f5252afbcbea248c947 (patch) | |
tree | e684823e99b9182badcb49dd6c2e35409fe40d02 /main.cpp | |
parent | a871e68ebd153d9ab4f9f9e36d7b099689e05c38 (diff) |
instance_destroy that actually works; no more shared_ptr
Diffstat (limited to 'main.cpp')
-rw-r--r-- | main.cpp | 58 |
1 files changed, 48 insertions, 10 deletions
@@ -13,9 +13,10 @@ using namespace std; -static shared_ptr<Object> makeobjectbyname(int x,int y,const char *name){ +__attribute__((unused)) +static Object* makeobjectbyname(int x,int y,const char *name){ -#define X(objname) if(strcmp(name,#objname)==0)return make_shared<objname>(x,y); +#define X(objname) if(strcmp(name,#objname)==0)return new objname(x,y); OBJECT_CLASSES_XLIST #undef X @@ -28,6 +29,37 @@ Global global; sf::RenderWindow window; +void applyObjectChanges(){ + set<Object*> seen; + for(Object *obj : global.objects_todelete){ + if(seen.insert(obj).second){ + delete obj; + global.objects.erase(obj); + } + } + global.objects_todelete.clear(); +} + +void applyObjectChanges(set<Object*>::iterator &it){ + bool incremented=false; + set<Object*> seen; + for(Object *obj : global.objects_todelete){ + if(seen.insert(obj).second){ + delete obj; + if(it!=global.objects.end()&&*it==obj){ + ++it; + incremented=true; + } + global.objects.erase(obj); + } + } + global.objects_todelete.clear(); + if(!incremented){ + ++it; + } +} + + void eventCycle(){ sf::Event event; while(window.pollEvent(event)){ @@ -42,14 +74,18 @@ void eventCycle(){ } } - for(int i=0;i<(int)global.objects.size();i++){ - global.objects[i]->step(); + for(set<Object*>::iterator it=global.objects.begin(); + it!=global.objects.end(); + applyObjectChanges(it)){ + (*it)->step(); } window.clear(sf::Color::White); - for(int i=0;i<(int)global.objects.size();i++){ - global.objects[i]->draw(); + for(set<Object*>::iterator it=global.objects.begin(); + it!=global.objects.end(); + applyObjectChanges(it)){ + (*it)->draw(); } window.display(); @@ -63,15 +99,17 @@ int main(void){ window.create(sf::VideoMode(640,480),GAME_NAME); window.setVerticalSyncEnabled(true); - global.objects.push_back(makeobjectbyname(0,0,"obj_control")); - global.objects[0]->create(); + instance_create<obj_control>(0,0); + applyObjectChanges(); while(window.isOpen()){ eventCycle(); } - for(shared_ptr<Object> p : global.objects){ - p->destroy(); + for(set<Object*>::iterator it=global.objects.begin(); + it!=global.objects.end(); + applyObjectChanges(it)){ + instance_destroy(*it); } } |