| Posted by
| Will Sayin
USA (17 posts) Bio
|
| Message
| I tried to think of a way to phrase my problem but realized that the only way I could was to first show you what I have done already:
I have programmed a console-mouse class:
class MOUSE
{
class MOUSE
{
public:
MOUSE()
{
//sets all pointers to NULL, etc.
}
MOUSE update();
MOUSE getLoc( short &x, short &y );
bool isat( short x, short y );
bool blClick();
bool brClick();
private:
short mX; //mouse X location
short mY; //mouse Y location
bool lClick;
bool rClick;
};
I now want to add "buttons" to the class, as in coordinates on the screen that when clicked, perform a certain function.
struct BUTTONS
{
public:
void setfunc(
private:
short TLX, TLY, BRX, BRY; //Top Left X, Top Left Y, Bottom Left X, Bottom Left Y
short defcolor, overcolor; //default color, color when the mouse is over it.
void (*function);
....
This is the way I was thinking of doing it. Add a linked list of button structures. In add a loop that transverses the linked list of buttons and checks to see if the mouse coordinates are within any of the button's coordinates. If it is, it does the function pointed to by the function pointer in the BUTTONS structure. This is where my problem arises, I have not been able to figure out how to set what the pointer points to through another function. (Since I know that sentence doesn't really make sense, let me explain) The way I would do this without using function pointers is as follows:
struct BUTTON
{
unsigned int tlX, tlY, brX, brY;
}
struct MOUSE
{
bool click()
unsigned int X, Y;
}
void anotherFoo();
void foo()
{
MOUSE mouse;
BUTTON button;
if( mouse.click() )
{
if( mouse.X > button.tlX && mouse.X < button.brX &&
mouse.Y > button.tlY && mouse.Y < button.brY )
{
anotherFoo()
}
}
}
However, with function-pointers, I can do it like this:
struct BUTTON
{
(*void)func;
unsigned int tlX, tlY, brX, brY;
BUTTON *pNext;
};
struct MOUSE
{
unsigned int x, y;
BUTTON buttons;
.... //all the functions to add/remove buttons from the linked list
void update();
bool click();
};
void foo()
{
MOUSE mouse;
while( 1 )
{
mouse.update();
}
}
void MOUSE::update()
{
//loop through button linked list and if the mouse is over any of the buttons, to the function pointed to by the button.
}
Now that may seem a lot more verbose than my previous example, but with a crapload of buttons, it requires much less code.
Now for my question (Finally!):
I want to create a function that will set the function in the button structure.
IE:
void setFunc( address-of-the-function )
{
(*void)func = address-of-the-function;
}
What do I put in for address-of-the-function?
For each instance of a button I would want a different function, so I would need a function to set that function. If I wanted to use a normal int data-type, I would write:
void setInt( int number )
{
buttonNum = number;
}
But I am not using an int, I am using a pointer to a function, so what would I put there?
Is this even possible?
Thanks for reading such a giant post,
Will |
Will Sayin
Developer, Legends of the Darkstone
www.darkstonemud.com:5432 | | Top |
|