In my first iterative algorithm, I do this
for (auto &widget : controls.getWidgets())
{
if (!widget->visible) continue;
widget->draw();
for (auto &widget_component : widget->components)
{
if (!widget_component->visible) continue;
widget_component->draw();
for (auto &ww : widget_component->components)
{
if (!ww->visible) continue;
ww->draw();
}
}
}
Now, I see the flaw in this algorithm because of the repetition of code.
I try to write a recursive function call to handle this thing.
void swcApplication::recursiveDisplay(swcWidget *next)
{
if (next == nullptr) return;
if ((next->parent != nullptr) &&
!next->parent->visible) return;
if (next->visible)
next->draw();
for (auto &i : next->components)
{
recursiveDisplay(i);
}
}
void display()
{
for (auto &widget : controls.getWidgets())
{
recursiveDisplay(widget);
}
}
Display first the parent before all other children, if parent is not visible, then don't draw its children
Is the above phrase satisfy by this algorithm (As far as I can see, it is)? Is this optimal? I don't know what drawbacks might occur here because I just write in a few seconds.
If you didn't find anything wrong here, now, how can I put it back in iterative way? I know iteration is better.
Update
I didn't use any z
attribute here and instead I go for painter's algorithm.
The flaw I am referring in my iteration
version is that, it is limited to draw widgets depends on how deep I will code for iteration.