Advertisement

How do I pass a function as an arguement?

Started by October 15, 2000 10:18 AM
1 comment, last by OGAPO 24 years, 1 month ago
For a game I''m working on, I have lots of elmenents which will possibly be drawing to the screen (console, particle engine, stats, slide-in items box, etc) but i don''t want them to all be running at the same time unless of course the user has turned them on, also some require multiple instances (it''s a different cintrol i won''t go into right now). so, the way i figured it, I thought if I made one drawing function with a linked list set up sort of like a rendering queue, then i could have instances of drawing objects put themselves in the queue to be drawn each frame, that way I could also disable all of them (by not rendering the queue at all) quite easily for cut scenes and the like. the only problem is i don''t know how to reference a function in a variable. It can be done though, right? after all a function is just a jump to a memory address. Anyway, if anyone could help me out here or if you have a better way to do this altogether I''m all ears. =) Thanks, O.G.A.P.O.
Brought to you by: [email=ogapo@ithink.net]O.G.A.P.O.[/email] +----------------------------------------+| Surgeon General's Warning - || OGAPO can cause serious mental damage || if taken in large doses. |+----------------------------------------+/* Never underestimate the power of stupid people in large groups */
This is how you use function pointers : void (*Func)(int param1, int param2, int etc);

Nate Miller
http://nate.scuzzy.net
Advertisement
This sounds like a textbook example for C++.
Have your structs in the list be c++ objects (instances of classes).
You define a base class with a virtual function for drawing them,
removing self from que etc. Then you can easily traverse the que and ask evry object there to draw themselves without having to know exactly what type of object they are(thanks to virtual function).
Something like:

// A class is basicly like a struct that has functions
class DrawableObject {
public:
DrawableObject* Next; // Link to next obj in que
virtual int DrawToScreen();
// ...
}

class BallObject : public DrawableObject {
// This class now inherits evrything from DrawableObject
int DrawToScreen(); // Now we implement drawing a ball
// ...
}

int BallObject::DrawToScreen() {
// Draw a ball
}

class BoxObject : public DrawableObject {
// This class now inherits evrything from DrawableObject
int DrawToScreen();
}

int BoxObject::DrawToScreen() {
// Draw a box
}

Now for you list, you declare pointers of type DrawableObject*
to manipulate list. And when you are stepping through the list and an object to draw you simply do like:
DrawableObject* p;DrawableObject* q;q = p;q = new BallObject;q->next = new BoxObject;q->next->next = NULL;q = p; // start of listwhile(q != NULL) { p->DrawToScreen(); q = q->next;}

I''m sure I''ve Made some c++ errors but I hope you get the point.
Of course you can get the same functionality in c with a simple struct containing a function pointer

This topic is closed to new replies.

Advertisement