Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » a simple menu

This thread is locked; no one can reply to it. rss feed Print
a simple menu
Money
Member #6,730
December 2005
avatar

here is a simple menu you can use..

i got some help from red and miran

1#include <allegro.h>
2 
3int main(){
4 BITMAP *bmp;
5 allegro_init();
6 install_keyboard();
7 install_mouse();
8 set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
9 
10 bmp = create_bitmap(640, 480);
11 while ( !key[KEY_ESC] ) {
12 clear_bitmap(bmp);
13 
14 textout_ex( bmp, font, "New Game", 0, 0, makecol( 0, 155, 0), makecol( 0, 0, 0) );
15 textout_ex( bmp, font, "Load Game", 0, 10, makecol( 0, 155, 0),makecol(0, 0, 0) );
16 textout_ex( bmp, font, "Options", 0, 20, makecol( 0, 155, 0), makecol(0,0,0) );
17 textout_ex( bmp, font, "Quit", 0, 30, makecol(0, 155, 0), makecol(0, 0, 0) );
18 
19 show_mouse(bmp);
20 
21 if ( mouse_x >= 0 && mouse_x <= 50 && mouse_y >= 0 && mouse_y <= 10 )
22 {
23 
24 textout_ex( bmp, font, "New Game", 0, 0, makecol( 0, 255, 0), makecol( 0, 0, 0) );
25 
26 }
27 
28 if ( mouse_x >= 0 && mouse_x <= 90 && mouse_y >= 10 && mouse_y <= 20 )
29 {
30 textout_ex( bmp, font, "Load Game", 0, 10, makecol( 0, 255, 0), makecol(0,0,0) );
31 
32 }
33 
34 if ( mouse_x >= 0 && mouse_x <= 70 && mouse_y >= 20 && mouse_y <= 30 )
35 {
36 
37 textout_ex( bmp, font, "Options", 0, 20, makecol( 0, 255, 0), makecol(0,0,0) );
38 
39 }
40 
41 if ( mouse_x >= 0 && mouse_x <= 50 && mouse_y >=30 && mouse_y <= 40 )
42 {
43 
44 textout_ex( bmp, font, "Quit", 0, 30, makecol( 0, 255, 0), makecol(0,0,0) );
45 
46 }
47 
48 
49 
50 
51 blit(bmp, screen, 0, 0, 0, 0, 640, 480);
52 
53 
54 
55 
56 
57 
58 
59 }
60 
61 show_mouse(NULL);
62 destroy_bitmap(bmp);
63 allegro_exit();
64 return 0;
65}
66END_OF_MAIN()

miran
Member #2,407
June 2002

Uhm, sorry to burst your bubble, but that's pretty useless. A good starting point for when you're learning, but not something you could put a "production quality" label on...

--
sig used to be here

ReyBrujo
Moderator
January 2001
avatar

There, there miran, don't bite newcomers ;)

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

miran
Member #2,407
June 2002

Hehe... :)

--
sig used to be here

Onewing
Member #6,152
August 2005
avatar

I'm impressed with Money's post count:

Quote:

21 posts at an average of 26.07 posts per day.

???

------------
Solo-Games.org | My Tech Blog: The Digital Helm

tapir
Member #6,629
November 2005

It's not that useless, btw, money: place the show_cursor(...) just before blit(..)

BAF
Member #2,981
December 2002
avatar

Steven: that would be because he just registered today, and at his current rate of posting, he can post 26 posts per day. :P

Murat AYIK
Member #6,514
October 2005
avatar

With this speed he will learn to make World Of Warcraft 2 next month:)

_____________________________________________________
"The world doesn't care about what storms you sailed through, it is interested in whether you brought the ship to the dock or not!"

Richard Phipps
Member #1,632
November 2001
avatar

Money
Member #6,730
December 2005
avatar

i wasn't posting for it to be a top notch leet menu

just using it as a reference

and as for the world of warcraft 2 guy.....no...just no...that wasn't even funny. just stop

Murat AYIK
Member #6,514
October 2005
avatar

1. I didn't mean offense
2. yes, it was funny

_____________________________________________________
"The world doesn't care about what storms you sailed through, it is interested in whether you brought the ship to the dock or not!"

Onewing
Member #6,152
August 2005
avatar

Quote:

Steven: that would be because he just registered today, and at his current rate of posting, he can post 26 posts per day.

I just thought it was funny since his total was less than his average.

Quote:

Ignore them Money.

I try, but I'm just so greedy...

Quote:

that wasn't even funny

Made me chuckle.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

BAF
Member #2,981
December 2002
avatar

I chuckled. Its something that will be funny to you when you become a veteran of the forums. ;)

Avenger
Member #4,550
April 2004

Quote:

and as for the world of warcraft 2 guy.....no...just no...that wasn't even funny. just stop

To be honest, I would take it as a compliment, if it were directed at me.

Murat AYIK
Member #6,514
October 2005
avatar

Quote:

I just thought it was funny since his total was less than his average.

Looks like it predicts near future! I didn't know Matthew was a seer:o At least he coded one! (Matrix?)

Quote:

To be honest, I would take it as a compliment, if it were directed at me.

Partially right. It looks like Money is coding with four arms!

Am I wrong or is this the most polite day of A.cc? I wasn't greeted like Elvis! (I need to be loved, too:P )

_____________________________________________________
"The world doesn't care about what storms you sailed through, it is interested in whether you brought the ship to the dock or not!"

Onewing
Member #6,152
August 2005
avatar

Quote:

I wasn't greeted like Elvis!

Nor shall you be.

Quote:

Am I wrong or is this the most polite day of A.cc?

Is that better?

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Murat AYIK
Member #6,514
October 2005
avatar

I feel better, now:)

I guess misunderstandings are solved now so I can sleep(2AM here)

_____________________________________________________
"The world doesn't care about what storms you sailed through, it is interested in whether you brought the ship to the dock or not!"

Jonny Cook
Member #4,055
November 2003

Well, it is simple. You write nice clear code, so that's a plus. But you get a minus for clumping the logic and the drawing together, so you're still zero.
Keep it up though. Might I suggest you look at some source code? There are plenty of small games in the Depot that serve as excellent learning tools.

The face of a child can say it all, especially the mouth part of the face.

IonBlade
Member #3,521
May 2003
avatar

If you're actually new to allegro, you're doing well so far. Allegro was my first venture into graphics and hardware access, so it took me a long time to figure out the concepts.

------

"I've got to choose my words carefully so that I'm not misunderstood, but I think there are fewer developers around today who think about making a good game. As a creator, I find that sad..." - Hideo Kojima

Michael Faerber
Member #4,800
July 2004
avatar

I've programmed this for you. I hope it helps!

1#include <allegro.h>
2#include <string>
3#include <vector>
4 
5 
6volatile int speed_counter = 0;
7 
8void increment_speed_counter()
9{
10 speed_counter++;
11}
12END_OF_FUNCTION(increment_speed_counter);
13 
14 
15int main()
16{
17 BITMAP *bmp;
18 allegro_init();
19 install_keyboard();
20 install_mouse();
21 set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
22 
23 LOCK_VARIABLE(speed_counter);
24 LOCK_FUNCTION(increment_speed_counter);
25 
26 install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));
27
28 bmp = create_bitmap(640, 480);
29 
30 std::vector<std::string> items;
31 items.push_back("New Game");
32 items.push_back("Load Game");
33 items.push_back("Options");
34 items.push_back("Quit");
35 
36 int active_item = 0;
37 bool quit = false;
38 
39 while (!quit)
40 {
41 while (speed_counter > 0)
42 {
43 if (key[KEY_ESC])
44 quit = true;
45
46
47 while (keypressed())
48 {
49 int val = readkey();
50 if (val >> 8 == KEY_UP)
51 if (active_item == 0)
52 active_item = items.size() - 1;
53 else
54 active_item--;
55 if (val >> 8 == KEY_DOWN)
56 if (active_item == items.size() - 1)
57 active_item = 0;
58 else
59 active_item++;
60
61 if (val >> 8 == KEY_ENTER)
62 {
63 //if (active_item == 0)
64 // start_ubercool_game();
65 if (active_item == 3)
66 quit = true;
67 }
68 }
69 
70 speed_counter--;
71 }
72
73 for (int i = 0; i < items.size(); i++)
74 textout_centre_ex(bmp, font, items<i>.c_str(), SCREEN_W/2,
75 SCREEN_H/2 - (items.size()-1)*text_height(font) + i*text_height(font)*2,
76 active_item == i ? makecol(0, 255, 0) : makecol(0, 155, 0), -1);
77 
78 blit(bmp, screen, 0, 0, 0, 0, 640, 480);
79 clear_bitmap(bmp);
80 }
81 
82 destroy_bitmap(bmp);
83 allegro_exit();
84 return 0;
85}
86END_OF_MAIN()

Have fun with it, and try to understand what I have written! ;)

EDIT: Whoops, it's a bit messed up. I have attached the source code!

--
"The basic of informatics is Microsoft Office." - An informatics teacher in our school
"Do you know Linux?" "Linux? Isn't that something for visually impaired people?"

Rick
Member #3,572
June 2003
avatar

Here's another idea in C++. Note that it's not complete just something to give you an idea.

1#define MENU_NORMAL makecol(255,0,0)
2#define MENU_HIGHLIGHTED makecol(0,255,0)
3 
4class Menu
5{
6private:
7 int newGameColor, loadGameColor, optionsColor, quitColor;
8 Rect newGameLoc, loadGameLoc, optionLoc, quitLoc;
9public:
10 void init()
11 {
12 //setup the menu item locations and colors
13 }
14 void input()
15 {
16 if(newGameLoc.pointInRect(mouse_x, mouse_y)
17 {
18 newGameColor = MENU_HIGHLIGHTED;
19 if(mouse_b & 1)
20 {
21 //set things up to move onto the next game state of playing the game
22 }
23 }
24 //etc...
25 }
26 void logic()
27 {
28 //you can have some animations running in your menu here if you wish
29 }
30 void draw(BITMAP* buffer)
31 {
32 textout_ex( buffer, font, "New Game", newGameLoc.getLeft(), newGameLoc.getTop(), newGameColor, -1 );
33 //etc...
34 }
35};

[EDIT]

You could even go on to make some of those vars arrays with some defines etc.. I would also make the font a variable in case you wish to change font on highlight also.

[EDIT]
Another twist on it, just to see the evolution:

1#define MENU_NORMAL makecol(255,0,0)
2#define MENU_HIGHLIGHTED makecol(0,255,0)
3 
4#define MENU_NEW_GAME 0
5#define MENU_LOAD_GAME 1
6#define MENU_OPTIONS 2
7#define MENU_QUIT 3
8 
9#define MENU_COUNT 4
10 
11class Menu
12{
13private:
14 int menuColors[MENU_COUNT];
15 Rect menuLocations[MENU_COUNT];
16 char* menuNames[MENU_COUNT];
17 bool mouseOver[MENU_COUNT];
18public:
19 void init()
20 {
21 //setup the menu item locations and colors
22 }
23 void input()
24 {
25 for(i=0;i<MENU_COUNT;i++)
26 {
27 if(menuLocations<i>.pointInRect(mouse_x, mouse_y) && !mouseOver<i>)
28 {
29 menuColors<i> = MENU_HIGHLIGHTED;
30 mouseOver<i> = true;
31 
32 if(mouse_b & 1)
33 {
34 switch(i)
35 {
36 case MENU_NEW_GAME:
37 //set things up to move onto the next game state of playing the game
38 break;
39 //etc...
40 }
41 }
42 }
43 else if(!menuLocations<i>.pointInRect(mouse_x, mouse_y) && mouseOver<i>)
44 {
45 mouseOver<i> = false;
46 menuColors<i> = MENU_NORMAL;
47 }
48 }
49 }
50 void logic()
51 {
52 //you can have some animations running in your menu here if you wish
53 }
54 void draw(BITMAP* buffer)
55 {
56 for(i=0;i<MENU_COUNT;i++)
57 textout_ex( buffer, font, menuNames<i>, menuLocations<i>.getLeft(), menuLocations<i>.getTop(), menuColors<i>, -1 );
58 }
59};

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

int menuColors[MENU_COUNT];
Rect menuLocations[MENU_COUNT];
char* menuNames[MENU_COUNT];
bool mouseOver[MENU_COUNT];

Why not to make a class called MenuItem which contains the color, location, name and mouseOver and them making a list of those?

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

nonnus29
Member #2,606
August 2002
avatar

Rick
Member #3,572
June 2003
avatar

Quote:

Why not to make a class called MenuItem which contains the color, location, name and mouseOver and them making a list of those?

Actually I just did that in my notepad version. I just wanted to give him some ideas.

[EDIT]
I also added active and visible variables. Active is checked when handleing input and visible is checked when drawing.

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

Go to: