Functions in Array?

Started by
7 comments, last by user0 14 years, 9 months ago
Hello im sure this has been asked somewhere before but I lack the proper vocabulary to find a good solution. My drawing code is getting messy with all the ifs then a function to draw something. It came across my mind that I could have a array or stack or whatever of functions that would hold all the functions that need to be done then my drawing code could just call those functions. To me that seems alot faster and easier but I dont know where to begin in something like that. What kind of array holds im guessing pointers to functions then how would i pull them out and call them?
Advertisement
what language are you using? Assuming C++ why not move the drawing to the class that represents the entity? Then your draw function loops over the list and calls render on each thing. If you wish to use the array of functions approach I would suggest functors. That is a class that overloads operator() you can call it just like you would any other function. From an array I believe it would be arr[2](args);
While you can take this approach, it would be more typical to hold an array of objects (more exactly, smart_pointers to objects), and inherit each object from an abstract base class that defines the function you want to call. Assuming you are using C++, something along the following lines:
class IRendererable{public:	virtual ~IRenderable() {} // virtual constructor needed for deleting through IRenderable pointer		virtual void render() = 0; // pure virtual, must be implemented by subclasses};// can use raw pointers if you really want to avoid boost/std::tr1std::vector< boost::smart_ptr<IRenderable> > renderables;

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

In my game engine i've created an std::vector<object*>

each object contains a render function call.

When I want to add a new item to be rendered I can just push it into the array.

When I render I use a for loop and just call arrayOfObjects->render();

Sorry, yes I am using c++.

Well I guess I have some learning todo on pointers and objects. I think I understand howto do it now. I think a vector of object will work for me too I just need to learn objects.

Thank you swiftcoder for the code I will try to make that make sense lol.

Is this the correct way of drawing was I correct in my assumption that this is better?
Quote:Original post by jeff8j
My drawing code is getting messy with all the ifs then a function to draw something.
Why exactly are you doing those conditionals?
The solutions presented so far are for solving those nauseating switch/elsif chains:

if      (entity->type == "box")    draw_box(entity);else if (entity->type == "circle") draw_circle(entity);else if (entity->type == "sky")    draw_sky(entity);// etc
But there are other things you could be doing instead and we might be able to give you better answers if we knew what that was [smile]
Quote:Original post by jeff8j
I think a vector of object will work for me too

No, it has to be a vector of pointers to objects, otherwise you won't get polymorphic behavior.

Well, pointers in vectors are actually objects too (everything in memory is an object in C++ except functions IIRC), but that terminology is seldomly used, and I don't think you meant that :)
As a general rule, I would recommend avoiding approaches that involve:

* A separate function for every type of thing you want to draw, which as you noted creates a vast number of functions or if-else statements
* Every object having its own drawing code, which makes it very hard to adapt the code if you decide you want to e.g. use a different rendering system or modify every drawn object

My own project uses an Animation class in conjunction with an ImageManager class. The ImageManager loads image files and has all the logic related to drawing any given image at a given location; the Animation class martials image files and decides which one to display on each frame. The flow looks like this:

* Instantiate a new Animation instance with a specific name
* Animation instance calls ImageManager.loadImages(name)
* ImageManager looks up the image files in the named directory, loads them, and returns them
* Animation.draw() selects one of those images and calls ImageManager.draw(image)

I'm using SDL for loading and displaying images, but all I'd need to change if I wanted to switch to, say, OpenGL, would be the ImageManager class, since I'd need different functions for loading and drawing images.
Jetblade: an open-source 2D platforming game in the style of Metroid and Castlevania, with procedurally-generated levels
Thank you all I have alot to learn I will be busy over the next few days.

This topic is closed to new replies.

Advertisement