Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » help with alegro api

Credits go to for helping out!
This thread is locked; no one can reply to it. rss feed Print
help with alegro api
Money
Member #6,730
December 2005
avatar

Posted on 12-29-2005 4:43 PM
hey, umm

int do_menu(MENU *menu, int x, int y);

i can't get it to work..i tried

MENU* aMenu;

//then

do_menu(aMenu, 200, 200);

//but the program crashed..so then i looked into MENU

//its a struct,

//typedef struct MENU

// char *text; - the text to display for the menu item
int (*proc)(void); - called when the menu item is clicked
struct MENU *child; - nested child menu
int flags; - disabled or checked state
void *dp; - pointer to any data you need

//sodo i need to define aMenu or what? i need help

BAF
Member #2,981
December 2002
avatar

Yep.

int myfunc()
{
    // whatever
}

MENU mymenu[] = {
  { "Menu item name", myfunc, NULL /* can be a child menu item */, 0, NULL },
  { NULL, NULL, NULL, 0, NULL}
};

do_menu(mymenu, 200, 200);

Money
Member #6,730
December 2005
avatar

ohhh, ok , thanks ...that made me understand the allegro api like a lot better

Fladimir da Gorf
Member #1,565
October 2001
avatar

The crash was because the pointer was null (eg. not set). You need an array of MENU objects instead of an empty pointer (as in BAF's code)

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)

BAF
Member #2,981
December 2002
avatar

You're welcome. Welcome to Allegro.cc, by the way... I forgot to say that in my post in your other thread.

Money
Member #6,730
December 2005
avatar

btw, im using c++ not c

can u give it to me in c form?

because this is what i had

1#include <allegro.h>
2 
3int main(void)
4{
5 allegro_init();
6 install_keyboard();
7 
8 MENU mymenu[]
9{
10 "Menu item name", myfunc, NULL /* can be a child menu item */, 0, NULL }, NULL, NULL, NULL, 0, NULL}
11};
12 
13 
14 
15 acquire_screen();
16 
17 do_menu(mymenu, 200, 200);
18 
19 release_screen();
20 
21 readkey;
22 
23 return 0;
24}
25 
26END_OF_MAIN();

/errors

Project : Win32 Application
Compiler : GNU GCC Compiler (called directly)
Directory : C:\Documents and Settings\Lavontae\Desktop\
--------------------------------------------------------------------------------
Switching to target: default
Compiling: main.cpp
main.cpp: In function `int _mangled_main()':
main.cpp:8: error: expected primary-expression before "mymenu"
main.cpp:8: error: expected `;' before "mymenu"
main.cpp:10: error: expected primary-expression before ',' token
main.cpp:10: error: expected `;' before '}' token
main.cpp: At global scope:
main.cpp:11: error: expected declaration before '}' token
Process terminated with status 1 (0 minutes, 2 seconds)

Neil Walker
Member #210
April 2000
avatar

you missed the = sign off the array definition.

also, when you use some of the GUI stuff, you have to make explicit casts when using C++.

btw, if you are looking at gui stuff, you could always look in the library section at 'masking', it's a c++ based GUI that produces something a lot prettier.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Money
Member #6,730
December 2005
avatar

ummm..

1#include <allegro.h>
2 
3int main(void)
4{
5 allegro_init();
6 install_keyboard();
7 MENU mymenu[];
8 
9 
10 
11 
12 acquire_screen();
13 
14 do_menu(mymenu, 200, 200);
15 
16 release_screen();
17 
18 readkey;
19 
20 return 0;
21}
22 
23END_OF_MAIN();
24 
25MENU mymenu[] = {
26 { "Menu item name", main, NULL /* can be a child menu item */, 0, NULL },
27 { NULL, NULL, NULL, 0, NULL}
28};

/errors
Project : Win32 Application
Compiler : GNU GCC Compiler (called directly)
Directory : C:\Documents and Settings\Lavontae\Desktop\
--------------------------------------------------------------------------------
Switching to target: default
Compiling: main.cpp
main.cpp: In function `int _mangled_main()':
main.cpp:7: error: storage size of `mymenu' isn't known
main.cpp:18: warning: statement is a reference, not call, to function `readkey'
Process terminated with status 1 (0 minutes, 1 seconds)

BAF
Member #2,981
December 2002
avatar

Why are you trying to call main from your menu? And you cant define your menu as nothing in main then expect it to work. And readkey should be readkey().

Money
Member #6,730
December 2005
avatar

what do u mean define my menu as nothing?

is this right?

1#include <allegro.h>
2 
3int main(void)
4{
5 allegro_init();
6 install_keyboard();
7 
8 
9 
10 
11 acquire_screen();
12 
13 do_menu(mymenu, 200, 200);
14 
15 release_screen();
16 
17 readkey();
18 
19 return 0;
20}
21 
22END_OF_MAIN();
23 
24MENU mymenu[] = {
25 { "Menu item name", myFunc, NULL /* can be a child menu item */, 0, NULL },
26 { NULL, NULL, NULL, 0, NULL}
27};
28 
29void myFunc()
30{
31}

make yourself clearer please

Neil Walker
Member #210
April 2000
avatar

http://www.allegro.cc/resource/resource-view.php?id=63

Is Miran's MasKinG (put random letters capitalised) and will probably suite you better as it is c++, easy to use, and has lots of simple examples.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Evert
Member #794
November 2000
avatar

If you haven't seen it yet, Allegro comes with a couple of example programmes that you can learn from (in allegro/examples/). For instance exgui.c (attached; I didn't know posts had a size limit...)

It's not the shortest possible programme that shows you how to do menus or use the GUI (which, by the way, is not great for use inside an actual game because it's quite ugly unless you use your own dialog routines), but it is intended to show you how things work and explain things.

One more suggestion, if I may:

Quote:

u

Please take the extra two keystrokes and write it out as `you'. For me, `u' is a sound that doesn't even exist in English and seeing it where `you' is intended interrupts the flow of the text. In other words, it makes it harder for me to read and I find it annoying.

Money
Member #6,730
December 2005
avatar

thanks neil, thats great...i don't possibly see how these people code games in C, i have to have classes

C to me is unorganized like h**l

IonBlade
Member #3,521
May 2003
avatar

Try this site: http://www.cprogramming.com/tutorial.html

It's where I learned most of the basics. I remember how nonsensical it all seemed, and I thought I'd never get past the boring stuff.. but because I couldn't give up the chance to make cool games, I kept at it. I suggest you do the same.

By the way, if you're wondering whether to do the C tutorials or C++ tutorials, it might not matter. I started on C++ and I still understand+write plain C code. Starting in plain C might introduce another learning curve if you start C++ later on, though.

------

"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

Money
Member #6,730
December 2005
avatar

umm, sorry to burst your bubble ION, but i know c++, when i said classes i mean OO classes,

IonBlade
Member #3,521
May 2003
avatar

Quote:

when i said classes i mean OO classes

Oh, whoops ;)

Quote:

sorry to burst your bubble ION, but i know c++

You may know C++, but do you how to use it to create games from scratch? IE, create a main loop and set different functions for input, drawing, and logic?

------

"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

BAF
Member #2,981
December 2002
avatar

Quote:

Is Miran's MasKinG (put random letters capitalised) and will probably suite you better as it is c++, easy to use, and has lots of simple examples.

Its MASkinG. Easy to remember. M and A are his initials, Skin for skinnable, and G for GUI. ;)

razor
Member #2,256
April 2002
avatar

Quote:

umm, sorry to burst your bubble ION, but i know c++

Don't be so quick to attack people, remember they are trying to help you (for free at that!). A few points that I don't think have been mentioned to you:

  • When you have a question do the following:

  • Make sure it is a question.

  • If it is about allegro, check the docs

  • If it's not in the docs, check the examples

  • If it's not there (almost everything is there FYI)

  • When Posting:

  • [list ordered]
  • Use a good title in the proper section

  • Be specific, include source if applicable

  • Don't include too much, no one wants to read a bunch of crap to get to the problem

  • </ol>
    [/list]
    Anyways, back on topic, don't give up with the allegro GUI so quickly, while it isn't the greatest thing ever it's good to know how it works because if you can figure this out then you can figure out the rest of allegro on your own.
    About your code, make sure mymenu is defined before you use it. This is something you should know from all C/C++ programming you have done. Remember allegro is just a library, IE a bunch of functions, nothing more. While some of them work together they still function under the rules of C/C++.

    Whoooo Oregon State University

    Money
    Member #6,730
    December 2005
    avatar

    alright, im sorry. here is my source, i redid it. everytime i run it , it crashes.
    what am i doing wrong?

    1#include <allegro.h>
    2 
    3 
    4MENU mymenu[] = {
    5 { "Menu item name", NULL, NULL /* can be a child menu item */, 0, NULL },
    6 { NULL, NULL, NULL, 0, NULL}
    7};
    8 
    9 
    10 
    11 
    12int main(void)
    13{
    14 allegro_init();
    15 install_keyboard();
    16 
    17 
    18 
    19 
    20 acquire_screen();
    21 
    22 do_menu(mymenu, 200, 200);
    23 
    24 release_screen();
    25 
    26 readkey();
    27 
    28 return 0;
    29}
    30 
    31END_OF_MAIN();

    Onewing
    Member #6,152
    August 2005
    avatar

    Don't you need to: set_gfx_mode???

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

    Money
    Member #6,730
    December 2005
    avatar

    ok, i got it to work.

    it was just some technical difficulties with the compiler , thanks :)

    Onewing
    Member #6,152
    August 2005
    avatar

    Quote:

    The color depth of the graphic mode has to be specified before calling this function with set_color_depth().

    Also, you can use allegro_message() or, for a more acurate method, a debugger to find out where the program is crashing and then you can say more than "it crashes".

    When I'm at work, I sometimes get a call from somebody saying "their computer doesn't work." That's a very vague statement and usually, it's something stupid. My first question is always, "is your monitor turned on?" (yes, sometimes that is the problem). So, with more details, it's easier to solve your problem. :)

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

    Evert
    Member #794
    November 2000
    avatar

    Quote:

    Also, you can use allegro_message() [...] to find out where the program is crashing and then you can say more than "it crashes".

    Note that you should not use allegro_message() after you've called set_gfx_mode(), unless you call set_gfx_mode(GFX_TEXT,...) again first.

    BAF
    Member #2,981
    December 2002
    avatar

    SS said:

    Quote:

    The color depth of the graphic mode has to be specified before calling this function with set_color_depth().

    You don't have to set the color depth (defaults to 8), but if you are going to set it, you must set it before setting the gfx mode.

    Quote:

    ok, i got it to work.

    it was just some technical difficulties with the compiler , thanks :)

    Nah, the only problem was not calling set_gfx_mode. :)

    Go to: