Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » GUI, GUI and more GUI...

This thread is locked; no one can reply to it. rss feed Print
GUI, GUI and more GUI...
jaime barrachina
Member #6,780
January 2006
avatar

So, nearing the end of my map editor project, I'm met with a problem when i have to "slap on top" of the project the GUI.

There's the map on the left, in a bitmap (typical d_bitmap_proc) an there's the tiles on the right (at the moment, no another d_bitmap_proc), which the user presses and then he "paints" them on the map. In earlier versions, this worked out fine by clearing the screen, painting all the possible 256 tiles and then finding out he tile the user selected using the mouse_x / y the moment he clicked. But now the allegro gui does resicing and its quite "dirty "to do it, cause it's not 32 x 32 tiles anymore.

So far I've thought of the folowing solutions:

- Even if it's dirty, and evil, calculate which tile is the user pressing, even thought the rounding up of numbers may cause a pixel or 2 to be in the wrong tile.

- Make a dialog with 256 d_icon_proc's. Not my favorite cause of the ridiculous amount of work for something so simple, and also cause several icons may be pressed at the same time and there fore it would need extra work to make sure its the tile we want.

- Make some kind of universal d_icon_proc, using a "For" somewere for the 256 cases. The problem here is that first I'm not sure i could do something like that, and even if it could be done, i have no idea how to do it.

I'd love to hear any suggestions and/or explanations on how to do it, cause I'm sure there's been many map editors since the world came to life and most of them must have solved this one way or another.
Please bear with me, its my first C++ and allegro project and it's been less than 4 dais since i started with GUI's so don't assume i know what you're talking about, cause i might disappoint you :P

Cheers and thxs!!!

"Under the sword lifted high There is hell making you tremble: But go ahead, And you have the land of bliss. - Miyamoto Musahshi"
"When all else fails, read the manual - Dad"

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

Make a dialog with 256 d_icon_proc's. Not my favorite cause of the ridiculous amount of work for something so simple, and also cause several icons may be pressed at the same time and there fore it would need extra work to make sure its the tile we want.

DIALOG* list_of_icons = malloc(sizeof(DIALOG) * 257);
for(int i = 0; i < 256; i++)
{
    // Initialize what you need to here.
}
list_of_icons[256] = { NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, NULL, NULL };
// Use the dialog now.
free(list_of_icons);
// I don't actually remember the proper way to initialize a DIALOG.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Johan Halmén
Member #1,550
September 2001

In my a-maze game I have a maze editor made with allegro gui. I think I had 16 icon_procs that chose the tile I wanted to place out. But I had four or five pages of these procs. The pages could be selected with icon buttons that looked like those tabs at the top of the opera browser. I think the map was a modified bitmap_proc. When the mouse was placed over the bitmap, the mouse sprite was changed into the chosen tile. And clicking on the bitmap caused the proc to calculate which tile was clicked and then the tile was blitted to the bitmap at that coordinate. This would be almost equal to your first option.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

jaime barrachina
Member #6,780
January 2006
avatar

Txs for the replies, I'm going to try for now CGamesPlay's suggestion, I just didn't know if you could put "for's" and such in a Dialog. I'll post again if I manage anything, in the mean time I'm open to any other suggestions you may have :)

Cheers and txs again!!

"Under the sword lifted high There is hell making you tremble: But go ahead, And you have the land of bliss. - Miyamoto Musahshi"
"When all else fails, read the manual - Dad"

CGamesPlay
Member #2,559
July 2002
avatar

Yep. Just make sure you malloc 1 more than the number of DIALOGs that you need, and set the last one to all NULLs or Os.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

jaime barrachina
Member #6,780
January 2006
avatar

Hummm sorry for being so thick, but i cant get the thing to work, for this code:

1DIALOG* list_of_icons = malloc(sizeof(DIALOG) * 257);
2int xl=774, yl=15;
3for(int i = 0; i < 256; i++)
4{
5 xl += 32;
6 if (xl == 1024)
7 {
8 xl=774;
9 yl=+32;
10 }
11}
12list_of_icons[256] = { xl, yl, 32, 32, 0, 0, 0, 0, NULL, NULL, NULL };
13 
14 
15//And somewere at the end of the program I call:
16free(list_of_icons);

I get the folowing errors:

Compilador: Default compiler
Building Makefile: "C:\Documents and Settings\JAIME\Desktop\editor99\Makefile.win"
Ejecutando make clean
rm -f Main3.o Bproject.exe

g++.exe -c Main3.cpp -o Main3.o -I"C:/Program Files/devcpp/include/c++" -I"C:/Program Files/devcpp/include/c++/mingw32" -I"C:/Program Files/devcpp/include/c++/backward" -I"C:/Program Files/devcpp/include" -lalleg

In file included from Main3.cpp:1:
Encabezado.h:398: invalid conversion from `void*' to `DIALOG*'

Encabezado.h:400: parse error before `for'
Encabezado.h:400: parse error before `;' token
Encabezado.h:400: syntax error before `++' token
Encabezado.h:406: ISO C++ forbids declaration of `yl' with no type
Encabezado.h:406: redefinition of `int yl'
Encabezado.h:399: `int yl' previously defined here
Encabezado.h:407: parse error before `}' token
Encabezado.h:409: ISO C++ forbids declaration of `list_of_icons' with no type
Encabezado.h:409: conflicting types for `int list_of_icons[256]'
Encabezado.h:398: previous declaration as `DIALOG*list_of_icons'
Encabezado.h:409: warning: initialization to non-pointer type `int' from NULL
Encabezado.h:409: warning: argument to non-pointer type `int' from NULL
Encabezado.h:409: warning: initialization to non-pointer type `int' from NULL
Encabezado.h:409: warning: argument to non-pointer type `int' from NULL
Encabezado.h:409: warning: initialization to non-pointer type `int' from NULL
Encabezado.h:409: warning: argument to non-pointer type `int' from NULL

make.exe: *** [Main3.o] Error 1

Ejecución Terminada

I'm just running around in the dark, bumping off walls, so if you can tell me what's wrong, or give a more specific example, I'll make you a statue...

Cheers and hope someone can help!

"Under the sword lifted high There is hell making you tremble: But go ahead, And you have the land of bliss. - Miyamoto Musahshi"
"When all else fails, read the manual - Dad"

BAF
Member #2,981
December 2002
avatar

Most of those errors are self explanitory - redeclarations and such. As far as the line 398 error, add (DIALOG *) before the malloc().

Wetimer
Member #1,622
November 2001

Have you not used malloc before? Do you normally use c++? If so, you can use new instead.

<code>if(Windows.State = Crash) Computer.halt();</code>

jaime barrachina
Member #6,780
January 2006
avatar

Well, I've started learning how to program 2 months ago and I chose C++ as my language, Allegro as my library and making a 2D game as my project. Having no experience whith programing at all (except html, but that doesn't count), It's not too easy. So yes, my answer is that I haven't used malloc before xD

Winston Ewert said:

... If so, you can use new instead.

New? Please tell me what you mean.

Right now I have the folowing code:

1DIALOG* list_of_icons = (DIALOG *) malloc(sizeof(DIALOG) * 257);
2 
3int iconos(void)
4{
5 int xl = 774, yl = 15, u;
6 for(u = 0; u < 256; u++)
7 {
8 xl += 32;
9 if (xl == 1024) { xl=774; yl=+32; }
10
11 BITMAP* Dibu = (BITMAP*)data<u>.dat; //Not sure if this works it doesn't give errors anyhow
12 }
13 list_of_icons[256] = { xl, yl, 32, 32, 0, 0, 0, 0, Dibu, NULL, NULL }; //Error line
14 return 0;
15}

And I get a "parse error before "{" token" in the error line. Can anyone tell me why, and also, if the "BITMAP* Dibu... " line would work.

Cheers and txs!!

"Under the sword lifted high There is hell making you tremble: But go ahead, And you have the land of bliss. - Miyamoto Musahshi"
"When all else fails, read the manual - Dad"

miran
Member #2,407
June 2002

Quote:

New? Please tell me what you mean.

Instead of DIALOG* list_of_icons = (DIALOG *) malloc(sizeof(DIALOG) * 257); you can do DIALOG* list_of_icons = new DIALOG[257];

Quote:

And I get a "parse error before "{" token" in the error line.

First of all, you should create the DIALOG array inside a function. Declare it globally if you want, but allocate memory inside a function. Like this:

//global DIALOG array
DIALOG *list_of_icons = 0;

// some function
void create_icons() {
   list_of_icons = new DIALOG[257];
}

I didn't know your code even compiles.
Secondly, the error you get is because you can't initialize a dynamically created object like that. You must initialize each struct member by hand, like

list_of_icons[256].x = x1;
list_of_icons[256].y = y1;

and so on.

Quote:

Can anyone tell me why, and also, if the "BITMAP* Dibu... " line would work.

It's valid syntax but the code doesn't do anything.

Quote:

Cheers and txs!!

Texas? ???

--
sig used to be here

Johan Halmén
Member #1,550
September 2001

Quote:

I'm just running around in the dark, bumping off walls

Congratulations, you've made your first Pong.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

jaime barrachina
Member #6,780
January 2006
avatar

;D My first pong, i feel so proud xD
And by the way, what i meant with txs is thanks ::)

Well, anyhow, I've come up with the folowing piece of code (or rather almost copied it from this forum...):

1 
2DIALOG *list_of_icons = 0; //Somewere at the top of the code page
3 
4void iconos(void)
5{
6 list_of_icons = new DIALOG[257];
7 int xl = 774, yl = 15, u;
8 for(u = 0; u < 256; u++)
9 {
10 xl += 32;
11 if (xl == 1024) { xl=774; yl=+32; }
12
13 list_of_icons[256].dp = (BITMAP*)data<u>.dat; // O.O Does this work?
14 list_of_icons[256].x = xl;
15 list_of_icons[256].y = yl;
16
17 }
18 do_dialog(list_of_icons, -1); //Hum, the program doesnt like this line, while I
19 //don't call the dialog, everiting is fine, the moment
20 //I call it, it compiles but automaticaly crashes the program.
21}

Almost there... but It doesn't let me call it! I promise to keep quiet a good while afther this, but please, please (*begs on his knees) someone tell me what's wrong, and, more importantly, how to fix it (or is that less important in fact...?).

"Under the sword lifted high There is hell making you tremble: But go ahead, And you have the land of bliss. - Miyamoto Musahshi"
"When all else fails, read the manual - Dad"

Johan Halmén
Member #1,550
September 2001

You've changed it. I'm not sure if it helps if anyone writes this stuff for you. miran wrote a bad example, which you didn't understand. You should get a hang of the basics of Allegro's gui first. Anyway, this would be heading in the correct direction:

1DIALOG *list_of_icons = 0; //Somewere at the top of the code page
2 
3void iconos(void)
4{
5 list_of_icons = new DIALOG[257];
6 int xl = 774, yl = 15, u;
7 for(u = 0; u < 256; u++)
8 {
9 xl += 32;
10 if (xl == 1024) { xl=774; yl=+32; }
11
12 list_of_icons<u>.proc = (void*)d_bitmap_proc; // not sure about the void
13 list_of_icons<u>.w =32;
14 list_of_icons<u>.h = 32;
15 list_of_icons<u>.dp = data<u>.dat; // Both are void pointers!
16 list_of_icons<u>.x = xl;
17 list_of_icons<u>.y = yl;
18
19 }
20 list_of_icons[256] = { NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, NULL, NULL };
21 do_dialog(list_of_icons, -1);
22}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

GullRaDriel
Member #3,861
September 2003
avatar

OK I must put my tip here :

Any allegro dialog should end by a NULL line who mean... end of dialog.

But correct me if i'm wrong.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Tobias Dammers
Member #2,604
August 2002
avatar

More specifically, the proc member of the struct must be NULL.
Anyway, I think this is pretty advanced stuff - a map editor isn't the easiest thing to code, and allegro GUI has never been known to be easy. Especially when it comes to dynamically allocated arrays.
So; may I suggest something easier? You can still code a tile-mapped game alright, but maybe you can use an existing tool for creating the maps (Mappy comes to mind, but you can also use normal bitmaps, or even text files if all else fails).
Anyway, good luck.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

jaime barrachina
Member #6,780
January 2006
avatar

Well, this is about it, thanks everyone for their awesome help, I really don't know if I could make anything work without the splendid people from Allegro c.c. In the end the code looks like this:

1void iconos(void)
2{
3 list_of_icons = new DIALOG[258];
4 set_dialog_color (list_of_icons, makecol(0, 0, 0), makecol(242, 242, 242));
5 int xl = 774, yl = 15, u=0;
6
7 list_of_icons[0].proc = d_clear_proc;
8 list_of_icons[0].fg = 0;
9 list_of_icons[0].bg = 0;
10 list_of_icons[0].dp = NULL;
11
12
13 for(u = 1; u < 257; u++)
14 {
15 xl += 32;
16 if (xl == 1024) { xl=774; yl=+32; }
17
18 list_of_icons<u>.proc = d_icon_proc; // not sure about the void
19 list_of_icons<u>.x = xl;
20 list_of_icons<u>.y = yl;
21 list_of_icons<u>.w = 32;
22 list_of_icons<u>.h = 32;
23 list_of_icons<u>.fg = 0;
24 list_of_icons<u>.bg = 0;
25 list_of_icons<u>.dp = data<u>.dat; // Both are void pointers!
26 }
27 list_of_icons[257].proc = NULL;
28 list_of_icons[257].x = 0;
29 list_of_icons[257].y = 0;
30 list_of_icons[257].w = 0;
31 list_of_icons[257].h = 0;
32 list_of_icons[257].fg = 0;
33 list_of_icons[257].bg = 0;
34 list_of_icons[257].key = 0;
35 list_of_icons[257].flags = 0;
36 list_of_icons[257].d1 = 0;
37 list_of_icons[257].d2 = 0;
38 list_of_icons[257].dp = NULL;
39 list_of_icons[257].dp2 = NULL;
40 list_of_icons[257].dp3 = NULL;
41}

And though I'm having problems with my do_dialog(list_of_icons,-1) (It just doesn't show the dialog O.O ) I think the icons function should work fine now.

Thank you again everyone!! The map editor is almost done ;D

[EDIT] Yust to say everything works OK now. Thanks again everyone!

"Under the sword lifted high There is hell making you tremble: But go ahead, And you have the land of bliss. - Miyamoto Musahshi"
"When all else fails, read the manual - Dad"

Go to: