In my C# game engine, I used to handle ordered drawing by adding/removing action to a manager object, which sorted the actions by priority, then executed them.
Here is a very simplified example:
class DrawManager
{
public List<Tuple<Action, int>> DrawActions = new List<Tuple<Action, int>>();
void Draw() { foreach (var tuple in DrawActions) tuple.Item1(); }
}
class Example
{
DrawManager manager;
Example()
{
manager.DrawActions.Add(new Tuple<Action, int>(DrawBackground, 0));
manager.DrawActions.Add(new Tuple<Action, int>(DrawForeground, 100));
}
~Example()
{
manager.DrawActions.Remove(manager.DrawActions.Find(x => x.Item1 == DrawBackground));
manager.DrawActions.Remove(manager.DrawActions.Find(x => x.Item1 == DrawForeground));
}
void DrawBackground() { /* draw something */ }
void DrawForeground() { /* draw something */ }
}
By adding some helper methods, the code becomes very elegant and easy to use in my engine.
I've moved to C++ recently, and I can't find any easy way to achieve the same result.
I tried using std::function, but in order to remove the method on object destruction, I had to store the draw method in a pointer owned by the caller, then wrap it into a lambda and pass it in. Very inelegant and time consuming.
Is there any way to get code similar to the one shown in the C# example?