• 11
• 9
• 10
• 9
• 10
• entries
11
26
• views
5838

# A little bugger

313 views

This one got me for a little while. My renderer has a list of entities that get rendered when a Draw function is called on the renderer.
void lxRender::RenderScene(){  std::list::iterator elI = mElementList.begin();  while(!(elI == mElementList.end()))  {    (*elI)->Draw(mScreen);    elI++;  }  SDL_Flip(mScreen);}

In an effort to program better I decided to change the way visibility works. Before, I was having the graphic element handle visibility by just exiting the Draw function right away if the flag wasn't set. Moving it to the renderer, I simply added a condition to see if the element was visible.
void lxRender::RenderScene(){  std::list::iterator elI = mElementList.begin();  while(!(elI == mElementList.end()))  {    if((*elI)->Visible)    {      (*elI)->Draw(mScreen);      elI++;    }  }  SDL_Flip(mScreen);}

Everything worked until I made an element invisible. The program froze. This one got me for a bit then I felt so dumb. Can you see the problem?

If it is invisible, the iterator never increments =D

Also, why dont you create a list of visible items that is updated with each item that is visible. That way its not necessarily O(n).

Hey, good idea! And... you win this picture: [totally]

ZOMG I FOUND A HUGE ERROR! Although you were right, I found another major flaw. I wasn't calling clear() on the list! I wondered for weeks why I was getting so much lag! (Because this function is called around 60xSecond!)

At what unholy hour of the night did you code this?