[C] Same functions with different function pointers inside

Started by
3 comments, last by BeerNutts 14 years, 1 month ago
I try to explain my problem:

void MenuLeftClick()
{
	for( int i = MenuNumWindows-1; i >= 0; i-- )
	{
		for( int k = 0; k < MAX_MENU_ELEMENTS; k++ )
		{
			if( GUI_Elements[k].LeftClickFunc 
			   && GUI_Elements[k].parent_window == MenuWindowBuffer
			   && ItemSelect(&GUI_Elements[k]) )
			{	
				GUI_Elements[k].LeftClickFunc();
				return;
			}
		}

		if( WindowSelect( &GUI_Windows[MenuWindowBuffer]) )
			return;

		if( GUI_Windows[MenuWindowBuffer].WindowUnClickFunc )
			GUI_Windows[MenuWindowBuffer].WindowUnClickFunc();

		if( GUI_Windows[MenuWindowBuffer].type == MENU_WINDOW_DIALOG )
			return;
		
	}
}
void MenuLeftUp()
{	
	for( int i = MenuNumWindows-1; i >= 0; i-- )
	{
		for( int k = 0; k < MAX_MENU_ELEMENTS; k++ )
		{
			if( GUI_Elements[k].LeftUpFunc 
			   && GUI_Elements[k].parent_window == MenuWindowBuffer
			   && ItemSelect(&GUI_Elements[k]) )
			{	
				GUI_Elements[k].LeftUpFunc();
				return;
			}
		}

		if( WindowSelect( &GUI_Windows[MenuWindowBuffer]) )
			return;

		if( GUI_Windows[MenuWindowBuffer].WindowUnUpFunc )
			GUI_Windows[MenuWindowBuffer].WindowUnUpFunc();

		if( GUI_Windows[MenuWindowBuffer].type == MENU_WINDOW_DIALOG )
			return;
	}
}
As you can see the only difference is GUI_Elements[k].LeftClickFunc(); vs GUI_Elements[k].LeftUpFunc(); and GUI_Windows[MenuWindowBuffer].WindowUnClickFunc() vs GUI_Windows[MenuWindowBuffer].WindowUnUpFunc()</i> And I will need the same for right mouse button click too. Is there a way around this, or I can &#111;nly copy the code? The code (and the GUI) is just a day old, but you can critique it if you like, especially the names. Thanks in advance!
Advertisement
If it were C++, I'd refactor the code with a template function type! Alas, since C shouldn't sport such spiffy stuff, a macro might surely suffice! (Or a function pointer?)

Or maybe something like this:
int f() {	for( int i = MenuNumWindows-1; i >= 0; i-- )	{		for( int k = 0; k < MAX_MENU_ELEMENTS; k++ )			if( GUI_Elements[k].LeftClickFunc 			   && GUI_Elements[k].parent_window == MenuWindowBuffer			   && ItemSelect(&GUI_Elements[k]) )				return k;		/* ... */	}	return -1;}void MenuLeftClick() {	int k = f();	if ( k >= 0 )		GUI_Elements[k].LeftClickFunc();}void MenuLeftUp() {	int k = f();	if ( k >= 0 )		GUI_Elements[k].LeftUpFunc();}
How about:
void MenuMouseClick(int code)
{
/* ... */
GUI_Elements[k].ClickFuncs();
/* ... */
}

void MenuMouseUp(int code)
{
/* ... */
GUI_Elements[k].UpFuncs();
/* ... */
}

Where all you do is replace individually named function pointers with an array of function pointers, and pass in a code indicating which mouse button it is?
Thanks for the replies, I think I will go Zipster's way, because I realized that the mouse up and down 'engine' aren't similar, since mouse up has no effect outside the topmost window, while mouse down will make the newly clicked window active (if the topmost is not a dialog).
So I click functions can be in one array, and release functions in the other (if right button release has any effect in Windows at all).

I could have solved it, but my question was a bit theoretical.
So I can't really do it in C (only with some horrible MACRO), only in C++.
Even Easier would be:
void MouseClickHandle(int iMouseButton, BOOL bDownClick){    .    .    .    GUI_Elements[k].ClickFuncs[iMouseButton][bDownClick]();   .   .   .}


No need for multiple functions, just multiple parameters

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement