#include #include #include #include #include #include #include "object_base.h" #include "global.h" #include "library.h" #include "build/objects.h" #include "build/sprites.h" using namespace std; __attribute__((unused)) static Object* makeobjectbyname(int x,int y,const char *name){ #define X(objname) if(strcmp(name,#objname)==0)return new objname(x,y); OBJECT_CLASSES_XLIST #undef X return nullptr; //no object with that name } 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)){ if(event.type==sf::Event::Closed){ window.close(); return; } else if(event.type==sf::Event::KeyPressed){ if(event.key.code==sf::Keyboard::Escape){ window.close(); return; } } } for(set::iterator it=global.objects.begin(); it!=global.objects.end(); applyObjectChanges(it)){ (*it)->step(); } window.clear(sf::Color::White); for(set::iterator it=global.objects.begin(); it!=global.objects.end(); applyObjectChanges(it)){ (*it)->draw(); } window.display(); } int main(void){ struct timeval tv; gettimeofday(&tv,nullptr); srand(tv.tv_sec*1000000+tv.tv_usec); window.create(sf::VideoMode(640,480),GAME_NAME); window.setVerticalSyncEnabled(true); instance_create(0,0); applyObjectChanges(); while(window.isOpen()){ eventCycle(); } for(set::iterator it=global.objects.begin(); it!=global.objects.end(); applyObjectChanges(it)){ instance_destroy(*it); } } /*static Fl_Window_draw *window; Fl_Window_draw::Fl_Window_draw(int w,int h,const char *title=0) :Fl_Window(w,h,title){} Fl_Window_draw::Fl_Window_draw(int x,int y,int w,int h,const char *label=nullptr) :Fl_Window(x,y,w,h,label){} void Fl_Window_draw::draw(void){ fl_rectf(0,0,w(),h(),FL_WHITE); fl_color(FL_BLACK); int i; for(i=0;i<(int)global.objects.size();i++){ global.objects[i]->draw(); } } void stepcallback(void*){ int i; for(i=0;i<(int)global.objects.size();i++){ global.objects[i]->step(); } window->redraw(); Fl::repeat_timeout(1.0/30,stepcallback); } int main(int argc,char **argv){ struct timeval tv; gettimeofday(&tv,nullptr); srand(tv.tv_sec*1000000+tv.tv_usec); window=new Fl_Window_draw(640,480,GAME_NAME); global.objects.push_back(makeobjectbyname(0,0,"obj_control")); global.objects[0]->create(); window->end(); Fl::visual(FL_RGB); window->show(argc,argv); Fl::add_timeout(1.0/30,stepcallback); int ret=Fl::run(); for(shared_ptr p : global.objects){ p->destroy(); } return ret; }*/