From 8906ff2e2755f6f8a2e52f5252afbcbea248c947 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 1 Jan 2017 12:03:58 +0100 Subject: instance_destroy that actually works; no more shared_ptr --- main.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 10 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index a10231e..b279d78 100644 --- a/main.cpp +++ b/main.cpp @@ -13,9 +13,10 @@ using namespace std; -static shared_ptr 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(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 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::iterator &it){ + bool incremented=false; + set 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::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::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(0,0); + applyObjectChanges(); while(window.isOpen()){ eventCycle(); } - for(shared_ptr p : global.objects){ - p->destroy(); + for(set::iterator it=global.objects.begin(); + it!=global.objects.end(); + applyObjectChanges(it)){ + instance_destroy(*it); } } -- cgit v1.2.3-70-g09d2