There are however some things the system has to be able to handle:
- The layer order should be respected, not the call order for the user. The call order only is taken into account for functions within the same layer.
- All the draw functions return void, BUT they all have different parameters. This means that not only the data type's of the function is different, but also the amount of parameters.
I'll explain on how i tried to implement this concept, but as I said before, I really think and hope there are better ways to do things like this as basically the core thing it has to do is change the order in when specific functions get called with specific functions. How I achieve this doesn't really matter as long as it can't become a bottleneck or hostile to the memory while used for games heavy on resources.
I've been working on this problem now for almost 3 days, and I still have not solved. The way I did it, was by saving all functions in a vector within a map. So every layer Is represented by one vector and these vectors are saved in a map. I've made some big Changes to my draw functions and had to create some extra things to achieve this. I think there have to be way better ways to transom this concept into C++ code. If not, I'll sum up the sort of components I have and where I do what. Every functor draw object is based on the Generic Function class:
Class Generic Function
{
protected:
GenericFunction() {}
public:
virtual void operator() (){}
virtual void operator() (boost::any){}
virtual void operator() (boost::any,boost::any){}
virtual void operator() (boost::any,boost::any,boost::any){}
virtual void operator() (boost::any,boost::any,boost::any,boost::any){}
virtual void operator() (boost::any,boost::any,boost::any,boost::any,boost::any){}
virtual void operator() (boost::any,boost::any,boost::any,boost::any,boost::any,boost::any){}
virtual void operator() (boost::any,boost::any,boost::any,boost::any,boost::any,boost::any,boost::any){}
virtual void operator() (boost::any,boost::any,boost::any,boost::any,boost::any,boost::any,boost::any,boost::any){}
};
An example of a private draw function, saved in the list by the public function and based on the GenericFunction class:
struct dLine : public GenericFunction
{ virtual void operator() (float x0, float y0, float x1, float y1,float lineWidth = 1.0f, bool DoscaleOperations = true); };
The vector in the map doesn't contain a pointer to these functions, but a wrapper class, called Command:
(header)
class Command
{
public:
Command(GenericFunction * f,UINT nArgs, ...);
~Command();
void Execute();
private:
boost::shared_ptr<GenericFunction> _f_;
std::vector<boost::any> _vaArguments_;
};
(implementation)
Command::Command(GenericFunction * f,UINT nArgs, ...) ctor
: _f_(f)
{
boost::any * it;
va_list args;
va_start(args,it);
for(unsigned int i = 0 ; i < nArgs ; ++i, ++it)
_vaArguments_.push_back(&it);
va_end(args);
}
Command::~Command()
{
_f_.reset();
_vaArguments_.clear();
}
void Command::Execute()
{
auto it = _vaArguments_.begin();
switch(_vaArguments_.size())
{
case 0:
boost::bind<void>(*(_f_.get()))();
break;
case 1:
boost::bind<void>(*(_f_.get()),_1)(*(it++));
break;
case 2:
boost::bind<void>(*(_f_.get()),_1,_2)(*(it++),*(it++));
break;
case 3:
boost::bind<void>(*(_f_.get()),_1,_2,_3)(*(it++),*(it++),*(it++));
break;
case 4:
boost::bind<void>(*(_f_.get()),_1,_2,_3,_4)(*(it++),*(it++),*(it++),*(it++));
break;
case 5:
boost::bind<void>(*(_f_.get()),_1,_2,_3,_4,_5)(*(it++),*(it++),*(it++),*(it++),*(it++));
break;
case 6:
boost::bind<void>(*(_f_.get()),_1,_2,_3,_4,_5,_6)(*(it++),*(it++),*(it++),*(it++),*(it++),*(it++));
break;
case 7:
boost::bind<void>(*(_f_.get()),_1,_2,_3,_4,_5,_6,_7)(*(it++),*(it++),*(it++),*(it++),*(it++),*(it++),*(it++));
break;
case 8:
boost::bind<void>(*(_f_.get()),_1,_2,_3,_4,_5,_6,_7,_8)(*(it++),*(it++),*(it++),*(it++),*(it++),*(it++),*(it++),*(it++));
break;
}
}
The map that contains everything:
std::map<int,std::vector<boost::shared_ptr<Command> > > * _mPaintFunctionList_;
I reset this every tick, this because parameters have maybe changed:
if(_mPaintFunctionList_ != nullptr && _mPaintFunctionList_->size() != 0)
{
for ( auto it = _mPaintFunctionList_->begin() ; it != _mPaintFunctionList_->end() ; ++it )
{
for ( auto iit = (*it).second.begin() ; iit != (*it).second.end() ; ++iit )
{
(*iit).reset();
}
(*it).second.clear();
}
_mPaintFunctionList_->clear();
}
When I want to paint my draw functions I call all the functions within that map:
if(_mPaintFunctionList_ != nullptr && _mPaintFunctionList_->size() != 0)
{
for ( auto it = _mPaintFunctionList_->begin() ; it != _mPaintFunctionList_->end() ; ++it )
{
if((*it).second.size() != 0)
{
for ( auto iit = (*it).second.begin() ; iit != (*it).second.end() ; ++iit )
{
(*iit).get()->Execute();
}
}
}
}
An example on how I push such a function via a public function:
bool WinEngine::DrawLine(float x0, float y0, float x1, float y1, float lineWidth, UINT layer, bool DoscaleOperations)
{
if(_bIsPainting_ | _bIsDoubleBuffering_)
{
(*_mPaintFunctionList_)[layer].push_back(
boost::shared_ptr<Command>(new Command(new dLine(),6,x0,y0,x1,y1,lineWidth,DoscaleOperations))
);
return true;
}
_bPaintError_ = true;
return false;
}
That's basicly how the implementation is. It has the following problems:
- The functions don't get executed, so something is wrong there.
- have alot of functions who get added, ( you can't fix this problem, as I didnt share all my code ) while i don't see where they can be ever added to it ( I don't think I have ever called them )
Concerns:
- I think this takes alot of memory ( even way to much )on the stack and heap, for something as trivial als executing a function
- Maybe this can become a bottleneck when I have a lot of functions in a game where I have a lot of action and things that need to be on screen