setting members as parameters

I am making a menu system for an engine and I am wondering if there is a way to tell what to do when enter is pressed for certain menu items. I'm using allegro library which handles all the screen displays. here's a bit of code to help define my question(below next paragraph explaining my question):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
class Menu
{
public:
	Menu(int x1, int y1, int Selections, const char * n, ...);
	void destroyMenu_bitmaps();
	void displayMenu_bitmaps();
	int x, y, numSelections, selected, menuW, menuH;
//bitmap array, 0 is unselected bitmap and 1 is selected, which make 1 selection together, so can have max of 10 selections to choose from in the menu
	BITMAP * menubmp[20];
};
//this allows the user to type in as many bitmaps as they want to be included in the list, and display it at x1,y1 and define how many selections there are
Menu::Menu(int x1, int y1, int Selections, const char * n, ...)
{
	va_list ap;  
	va_start(ap, n);
	const char * p;
	x = x1;
	y = y1;
	menuH = 0;
	menuW = 0;
	selected = 1;
	numSelections = Selections; 
	menubmp[0] = load_bitmap(n,NULL);
	for (int m = 1 ; m < (numSelections * 2) ; m++)
	{
		p = va_arg(ap, char *);
		menubmp[m] = load_bitmap(p,NULL);
	}
	va_end(ap);
}
void Menu :: destroyMenu_bitmaps()
	//destroy_bitmap (menubmp);

void Menu :: displayMenu_bitmaps()
{
	///////////////Get Screen////////////
	//code to get current screen, create menubuffer
	BITMAP * menuBuffer;
	for (int n = 1 ; n < (numSelections*2 ) ; n+=2)
		menuH += menubmp[n]->h;

	menuW += menubmp[0]->w;
	menuBuffer = create_bitmap (menuW, menuH);

	//////////////create menu//////////// 
	menubmp[numSelections*2] = create_bitmap(menuW, menuH);
	rectfill(menubmp[numSelections*2],0,0,menuW,menuH,makecol(255,139,3));
	for (int n = 0 ; n < (numSelections*2 ) ; n+=2)
	{ 
		draw_sprite(menubmp[numSelections*2], menubmp[n], 0 , n/2 * menubmp[n]->h);
	}

	int yDraw = 0;
	////////////display menu/////////////
	while (!key[KEY_Y])
	{	
		draw_sprite(menuBuffer, menubmp[numSelections*2], 0 , 0);
		yDraw = 0;
		for (int l = 1; l<selected ; l++)
			yDraw += menubmp[l*2]->h;   
		draw_sprite(menuBuffer, menubmp[selected*2 - 1], 0, yDraw);
		//////////interact with menu/////////
		//navigate w keypress
		if (key[KEY_UP])
		{
			selected--;
			rest(100);
		}
		else if (key[KEY_DOWN])
		{
			selected++;
			rest(100);
		}
		if (selected > numSelections )
			selected = 1;
		else if(selected < 1)
			selected = (numSelections);
		
		/////////menu functionality//////////
		if (key[KEY_ENTER])
		{
			switch (selected)
			{
			case 0:
				break;
			case 1:
				//convert characters of itemFunction into tasks might have to specify w each menu

				player->speed += 1;
				break;
			and so on
			}
		}
		//vsync();
		acquire_screen();
		stretch_sprite(menuBuffer, player_image[0], 100, 125, 150, 192);
		blit(menuBuffer, screen, 0, 0, x, y, menuW, menuH);
		release_screen();
	}
}


so the piece of code basically puts a picture of your bitmaps in a column and there is a piece of code:
1
2
3
4
5
6
7
8
9
10
11
if (key[KEY_ENTER])
		{
			switch (selected)
			{
			case 0:
  
				break;
			case 1:

				player->speed += 1;
				break;

so i have separate class for my player class and i want to increase the speed like above but i dont want to do it inside of the definition of the class and its functions, is there a way i can pass it when i create a new Menu as a parameter? for example,when enter is pressed and selection is 1 i need to increase player.speed , and if selection is 2 i need to create a new Menu with other options as well. Maybe I have been siting in front of this computer too long tonight.. I can't seem to figure it out. I need it to be separate because there will be items in the menu that need to create and display another menu with other operations for each selection in the menu.. If its hard to understand my question (i am not too detail oriented) let me know and I will be happy to explain further. Thanks!!!!
Last edited on
The problem, I think, is that you're trying to define too much behavior in code.
Keep the class structure simple: a menu is a collection of menu items. Each menu item can do one of two things; change internal state, or call a new menu. If the menu item can call a new menu, this other menu should be allocated (preferably dynamically) inside the menu item object. When a menu item is activated, this will performed the action the menu item was constructed for.

That's simple and generic. Now the menu structure and the class structure are separate, and you can move the latter former elsewhere. For example, a script:
Menu 0:
Exit
Accelerate: speed++
Become snafucated: call Menu 1

Menu 1:
...

It doesn't have to be a script. If you don't want users to modify that particular menu, you could build a hard-coded tree structure at run time.
Last edited on
ok thank you, really, that helps a lot. Ill try to mess around with it tonight.

But what if the variable I want it to increment is not in that class but a different one, and the value I want to change is different for each menu I create so it can't be defined in the menu class definitions?

thanks again!
Last edited on
You'll need a centralized class that can access most of the program's state. The menu items will call a member function of this class.
Topic archived. No new replies allowed.