• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
GlenDC

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

5 posts in this topic

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:[list=1]
[*]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.
[/list]
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:
[CODE]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){}
};[/CODE]
An example of a private draw function, saved in the list by the public function and based on the GenericFunction class:
[CODE]struct dLine : public GenericFunction
{ virtual void operator() (float x0, float y0, float x1, float y1,float lineWidth = 1.0f, bool DoscaleOperations = true); };[/CODE]

The vector in the map doesn't contain a pointer to these functions, but a wrapper class, called Command:
(header)
[CODE]
class Command
{
public:
Command(GenericFunction * f,UINT nArgs, ...);
~Command();
void Execute();
private:
boost::shared_ptr<GenericFunction> _f_;
std::vector<boost::any> _vaArguments_;
};
[/CODE]
(implementation)
[CODE]
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;
}
}
[/CODE]
The map that contains everything:
[CODE]std::map<int,std::vector<boost::shared_ptr<Command> > > * _mPaintFunctionList_;[/CODE]
I reset this every tick, this because parameters have maybe changed:
[CODE]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();
}[/CODE]
When I want to paint my draw functions I call all the functions within that map:
[CODE]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();
}
}
}
}[/CODE]
An example on how I push such a function via a public function:
[CODE]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;
}[/CODE]


That's basicly how the implementation is. It has the following problems:[list]
[*]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 )
[/list]
Concerns:[list]
[*]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
[/list]
0

Share this post


Link to post
Share on other sites
[quote name='return0' timestamp='1348782368' post='4984504']
Why not just some sets of functors implementing () or some invoke interface?
[/quote]

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.
0

Share this post


Link to post
Share on other sites
Theres two ways to approach this kind of system
a) make a generic message buffering system that you can use to store/sort/call [i]any[/i] 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 ([i]new drawing functions are usually a composition of the basic commands[/i]).

For my drawing commands, I keep the system as simple as possible, and it boils down to something like:
[code]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 )
...
}[/code] Edited by Hodgman
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1348804225' post='4984583']
Theres two ways to approach this kind of system
a) make a generic message buffering system that you can use to store/sort/call [i]any[/i] function.
b) make a buffering system specific to your renderer.
[/quote]

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.

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

[size=5][b][UPDATE][/b][/size]
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 [url="http://www.glendc.com/?p=355"]here[/url]. Thanks everyone!

(this topic is solved) Edited by GlenDC
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0