Jump to content

  • Log In with Google      Sign In   
  • Create Account

[SOLVED] Change function call order for a Layer (draw) system


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 GlenDC   Members   -  Reputation: 545

Like
0Likes
Like

Posted 27 September 2012 - 12:21 PM

I would like to design a system, which allows me to execute my functions in "layers". So a function of layer 3 should always be executed from a function of layer 4 but before a function of layer 2.
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


Sponsor:

#2 return0   Members   -  Reputation: 444

Like
2Likes
Like

Posted 27 September 2012 - 03:46 PM

Why not just some sets of functors implementing () or some invoke interface?

#3 GlenDC   Members   -  Reputation: 545

Like
0Likes
Like

Posted 27 September 2012 - 05:09 PM

Why not just some sets of functors implementing () or some invoke interface?


And how exactly would you do this? I worked with an interface and a set of functors, isn't really working out. Maybe i'm doing it wrong or maybe it's just a wrong idea to go down that road for this specific concept. So if you think it is possible to do with that, please provide me some example on how to do such a thing correctly and legal.

#4 Hodgman   Moderators   -  Reputation: 31800

Like
1Likes
Like

Posted 27 September 2012 - 09:50 PM

Theres two ways to approach this kind of system
a) make a generic message buffering system that you can use to store/sort/call any function.
b) make a buffering system specific to your renderer.

In my engine, I've got both -- I use (a) for general buffering of gameplay commands, but use (b) when it comes to drawing things, because I rarely add new basic drawing functions (new drawing functions are usually a composition of the basic commands).

For my drawing commands, I keep the system as simple as possible, and it boils down to something like:
struct DrawCommand
{
  enum Type
  {
    Foo,
    Bar
  };
  Type type;
  int Size();
}
struct DrawFoo : public DrawCommand
{
  static int Size() { return sizeof(DrawFoo); }
  DrawFoo() { type = Foo; }
  int foo;
}
struct DrawBar : public DrawCommand
{
  static int Size() { return sizeof(DrawBar); }
  DrawBar() { type = Bar; }
  float x, y, z;
}
int DrawCommand::Size()
{
  switch(type)
  {
    case Foo: return DrawFoo::Size();
    case Bar: return DrawBar::Size();
  }
  return 0;
}


....

std::vector<char> bytes(1024);
int pointer = 0;
struct DrawItem
{
  int byteOffset;
  int layer;
};
std::vector<DrawItem> items;
...
void Push( DrawCommand& cmd, int layer )
{
  char* out = &bytes[pointer];
  DrawItem item = { pointer, layer };
  items.push_back( item );
  int size = cmd.Size();
  pointer += size;
  assert( pointer <= bytes.size() );
  memcpy( out, &cmd, size );
}


...
std::stable_sort( items.begin(), items.end(), sortByLayer() );
foreach( items as item )
{
  DrawCommand& cmd = *(DrawCommand*)&bytes[item.byteOffset];
  switch( cmd.type )
    ...
}

Edited by Hodgman, 27 September 2012 - 10:11 PM.


#5 wqking   Members   -  Reputation: 756

Like
1Likes
Like

Posted 27 September 2012 - 10:04 PM

I've ever used a similar approach for rendering in OpenGL on iPhone as Hodgman did, and I also called it "command".
In brief, any function calling doesn't really perform the function, but store it as a command.
Then in your render phase (or any phase related), there is central place to sort all commands, then loop over and render all commands, depending on their type.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#6 GlenDC   Members   -  Reputation: 545

Like
0Likes
Like

Posted 28 September 2012 - 08:20 AM

Theres two ways to approach this kind of system
a) make a generic message buffering system that you can use to store/sort/call any function.
b) make a buffering system specific to your renderer.


Thank you. You gave me a great idea, gonna do it slightly else, but it's based on your idea!
I'll let you know if I succesfully implemented everything.

------------------------------------------------------------------------------

[UPDATE]
Ok after 4 days, I finally managed to get my layer system up and running. It's stable and everything works just like I had planned. I would like to thank you all for your help and inspiration. I wrote a blog post about my final implantation which you can read here. Thanks everyone!

(this topic is solved)

Edited by GlenDC, 29 September 2012 - 03:54 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS