• [SFML] Object being removed from vector unintetionally
    0 replies, posted
I have a problem with an object "disappearing" from the game world. I've narrowed it down to some odd memory manipulation that's causing it to be removed from a vector of all the currently active objects, but I can't find what would do this. I've tried very hard in the debugger, but I still can't find anything, so I'm trying one more time before totally ripping all of my hard-worked code and starting again. Object: [php] void Object::Update() { //float etime = CalcAverageTick(app->GetRenderWindow()->GetFrameTime()); cx = GetSprite().GetCenter().x; cy = GetSprite().GetCenter().y; std::vector<Object*> * list = app->GetCollides(this); int i; bool movex = true; bool movey = true; // = new Object(app); float ox; float oy; //std::cout << "Started checking..."; //if(list == NULL) //{ //std::cout << "List is null!\n"; //} for(i = 0; i < list->size();i++) { std::cout << "In loop!..."; //Object * list->at(i) = list->at(i); ox = list->at(i)->GetSprite().GetCenter().x; oy = list->at(i)->GetSprite().GetCenter().y; if(list->at(i)->GetDensity() > this->GetDensity())//more dense { std::cout << "It is more dense."; if(this->velocity.x > 0)// going right { if(ox > cx)//gonna hit muthafucker { movex = false; } } if(this->velocity.x < 0)//going left { if(ox < cx) { movex = false; } } if(this->velocity.y > 0)//going up { if(oy > cy) { movey = false; } } if(this->velocity.y < 0)//going down { if(oy < cy) { movey = false; } } } //list->at(i) = 0; //delete list->at(i); } if(movex == true) { this->Object::spr.SetX(this->Object::spr.GetPosition().x + (this->velocity.x)); } if(movey == true) { this->Object::spr.SetY(this->Object::spr.GetPosition().y + (this->velocity.y)); } //[TESTCODE] if(this->is_visible == false) { std::cout << "hiding"; } OnCollide(); //delete list; list = 0; } bool Object::IsKeyDown(sf::Key::Code keycode) { if(input->IsKeyDown(keycode)) { return true; } else { return false; } } void Object::OnCollide() { int i; std::vector<Object*> * vec = app->GetCollides(this); for(i = 0; i < vec->size(); i++) { if(vec->at(i))//is it not null { if(vec->at(i) != this) { this->AddVelocity( -(vec->at(i)->GetFriction()), -(vec->at(i)->GetFriction()));//Controls velocity through friction if(vec->at(i)->GetName() == "ent_cool") { //TESTCODE //Add one for each interaction you want } } } else { //Nothing is colliding return; } } delete vec; //std::cout << "Updated Object::Update!\n"; //vec = NULL; } [/php] (I'm posting stripped-down versions, bear with me) Application: [php] bool Application::IsColliding(Object * obj1, Object * obj2) { if(collision.PixelPerfectTest(obj1->GetSprite(),obj2->GetSprite())) { return true; } else { return false; } } std::vector<Object*> * Application::GetCollides(Object * obj) { unsigned int i = 0; std::vector<Object*> * ObjectCollides = new std::vector<Object*>; //ObjectCollides = new std::vector<Object*>; for(i = 0; i < objmap.size(); i++) //placeholder [TESTCODE] { try { if(objmap.empty() != true)//is the whole thing empty? { if(objmap.at(i))//is it empty? { if(objmap.at(i) != obj) { if(collision.PixelPerfectTest(obj->GetSprite(),objmap.at(i)->GetSprite())) { ObjectCollides->push_back(objmap.at(i)); std::cout << "found something\n"; } } else { //std::cout << "Was the object itself!\n"; } continue; } else //objmap's done { break; } } else //empty, also due to stupidty { break; } } catch(...) { GetLogger()->AddToFile("Some error was encountered."); std::cout << "Some Error was encountered.\n"; } } return ObjectCollides; //delete ObjectCollides; } void Application::AddObject(Object * obj) { objmap.push_back(obj); } bool Application::ObjAtPos(float x, float y,Object * checked) { /*//[TODO]: Make this more efficent //Right now, it creates an object //Fix this faggotry at once Object checker(x,y,this,"data/cool.tga"); //checker.SetVisible(false); //checker.GetSprite().Resize(xsize,ysize); std::vector<Object*> * vec0 = this->GetCollides(&checker); if(vec0->empty() == false) { vec = vec0; std::cout << "Not Empty.\n"; return false; } if(vec0->empty() == true) { std::cout << "Empty.\n"; return true; }*/ //Object checker(x,y,this,"data/cool.tga"); //checker.SetVisible(false); int i = 0; sf::IntRect rect; for(i=0;i < objmap.size();i++) { if(objmap.at(i) != checked) { rect = collision.GetAABB(objmap.at(i)->GetSprite()); if(rect.Contains(x,y)) { std::cout << "It does contain!\n"; return true; } } } return false; } void Application::Update() { //this->logger.AddToFile("Updated."); sf::Event * event_u = new sf::Event; //std::cout << "Cool.\n"; //sf::Event & event_u = u_event while(GetRenderWindow()->GetEvent(*event_u)) { //this->logger.AddToFile("Getting Event!\n"); if(event_u->Type == sf::Event::Closed) { GetRenderWindow()->Close(); //this->logger.AddToFile("Render Window closing."); } } //this->logger.AddToFile("Done Getting Event\n"); //update all object unsigned int i; for(i = 0; i < objmap.size(); i++) { objmap.at(i)->Update(); std::cout << objmap.at(i)->GetName(); //std::cout << "Updated an object!\n"; } //std::cout << "Done Updating Objects\n"; //this->logger.AddToFile("Done updating objects.\n"); //std::cout << "Updated objects!\n" ; //if(i == objmap.size()) //{ //Draw(); //return; //} Draw(); //std::cout << "Drawing!\n"; this->logger.WriteFile(); //std::cout << "Returning!\n"; return; delete event_u; } void Application::Draw() { GetRenderWindow()->Clear(); unsigned int i; for(i = 0; i < objmap.size(); i++) { if(objmap.at(i)->Visible() == true) { GetRenderWindow()->Draw(objmap.at(i)->GetSprite()); } else { continue; } } //if(i = objmap.size()) //{ //break; //} GetRenderWindow()->Display(); return; } [/php] That's all I can think of that helps. I've worked on this long and hard, and I'm rather worn. [editline] Please do not complain about "code-slap"; I can't see how else to do this.
Sorry, you need to Log In to post a reply to this thread.