Bitmaps get confused
James Stanley

I have some code that creates some images for buttons on a menu screen. What seems to happen is one of the buttons I make gets given the same memory position as the menu background. Here's the relevant code:

menu = load_bitmap("menu.tga", NULL);

play[1] = create_bitmap(300, 40);
play[2] = create_bitmap(300, 40);//THIS IS THE LINE
 
rectfill(play[1], 0, 0, 300, 40, pink);
rectfill(play[2], 0, 0, 300, 40, darkblue);
  
textout_centre_ex(play[1], myfont, "Play", 150, 0, darkblue, -1);
textout_centre_ex(play[2], myfont, "Play", 150, 0, pink, -1);

mask_image(play[1], pink); //I wrote mask_image.
mask_image(play[2], pink);

On the line marked THIS IS THE LINE, if I comment the whole line out, the code compiles fine, and all the drawing functions performed on play[2] get drawn to my menu background, if I don't comment it out, my menu background turns into the picture of a button. These aren't the final buttons, they look horrible, they're just for testing. I was wondering if anybody knew why this was happening or knew how to fix it, or has ever experienced it.

EDIT:
I know the title is bad, I should have written it better.

count
Quote:

These aren't the final buttons, they look horrible, they're just for testing.

We can't see them. ;)

From your code we can just see, that you create two bitmaps with a size of 300*40.
Then fill them to a color.
draw a text to them.

Then you call your function which we can't see.

Actually we don't know what you are doing.

The problem is probably in your function. So show it ;D

James Stanley

Sure, here's my menu loop. There are actually more buttons than that, but I thought that was adequate. 1 is for Not Selected, 2 is for Selected.

1 while(!key[KEY_ESC]) {
2 draw_image(menu, canvas, 0, 0);
3 draw_image(play[1], canvas, 160, 100);
4 draw_image(help[1], canvas, 160, 160);
5 draw_image(credits[1], canvas, 160, 220);
6 draw_image(quit[1], canvas, 160, 280);
7 flip_to_screen(canvas);
8 }
9 
10void draw_image(BITMAP *src, BITMAP *dest, int x, int y) {
11 blit(src, dest, 0, 0, x, y, src->w, src->h);
12}
13 
14void flip_to_screen(BITMAP *bmp) {
15 blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h);
16}

I wrote those other functions to go in my useful.cpp file. It's to reduce the amount of code I have to type. I know I haven't got the selection started at all yet, I'm having trouble with drawing the things, never mind selecting them. Never had this before, though...

EDIT:
Oohh... you mean the mask_image function:

void mask_image(BITMAP *bmp, int maskcol) {
  int x, y;

  for (y = 0; y < bmp->h+1; y++) {
     for (x = 0; x < bmp->w+1; x++) {
      if (getpixel(bmp, x, y) == maskcol) putpixel(bmp, x, y, bitmap_mask_color(bmp));
    }
  }
}

It's just so that it's easier for me to load bitmaps with pink as the background and stuff like that. I dunno... ;)

EDIT2:
Attached an image of what happens WITH the line commented (used to say without).

count

edit

Quote:

What seems to happen is one of the buttons I make gets given the same memory position as the menu background.

What do you mean with this?
What actualy happens?
Is the image displayed at the wrong position?
Or is the bitmap currupted?

Kauhiz

What does the mask_image function look like/do?

James Stanley

Masks the image with the colour specified. It's to simplify the process of getting invisible. It doesn't seem to work for this project. It's worked in my others.

You can see what happens in the attachment to my previous post. Whenever I draw something to play[2], it goes to menu.

count
Quote:

You can see what happens in the attachment to my previous post. Whenever I draw something to play[2], it goes to menu.

Ah, missed that. sorry.

Does this only happen with "play"?
Please show me the whole button loop.

James Stanley

Which? The menu loop? That's it. The bit that says while(!key[KEY_ESC])
make_buttons looks like this:

1void make_buttons(void) {
2 play[1] = create_bitmap(300, 40);
3 //play[2] = create_bitmap(300, 40);
4 help[1] = create_bitmap(300, 40);
5 help[2] = create_bitmap(300, 40);
6 credits[1] = create_bitmap(300, 40);
7 credits[2] = create_bitmap(300, 40);
8 quit[1] = create_bitmap(300, 40);
9 quit[2] = create_bitmap(300, 40);
10 
11 rectfill(play[1], 0, 0, 300, 40, pink);
12 rectfill(play[2], 0, 0, 300, 40, darkblue);
13 rectfill(help[1], 0, 0, 300, 40, pink);
14 rectfill(help[2], 0, 0, 300, 40, darkblue);
15 rectfill(credits[1], 0, 0, 300, 40, pink);
16 rectfill(credits[2], 0, 0, 300, 40, darkblue);
17 rectfill(quit[1], 0, 0, 300, 40, pink);
18 rectfill(quit[2], 0, 0, 300, 40, darkblue);
19 
20 textout_centre_ex(play[1], myfont, "Play", 150, 0, darkblue, -1);
21 textout_centre_ex(play[2], myfont, "Play", 150, 0, pink, -1);
22 textout_centre_ex(help[1], myfont, "Help", 150, 0, darkblue, -1);
23 textout_centre_ex(help[2], myfont, "Help", 150, 0, pink, -1);
24 textout_centre_ex(credits[1], myfont, "Credits", 150, 0, darkblue, -1);
25 textout_centre_ex(credits[2], myfont, "Credits", 150, 0, pink, -1);
26 textout_centre_ex(quit[1], myfont, "Exit", 150, 0, darkblue, -1);
27 textout_centre_ex(quit[2], myfont, "Exit", 150, 0, pink, -1);
28 
29 mask_image(play[1], pink);
30 mask_image(play[2], pink);
31 mask_image(help[1], pink);
32 mask_image(help[2], pink);
33 mask_image(credits[1], pink);
34 mask_image(credits[2], pink);
35 mask_image(quit[1], pink);
36 mask_image(quit[2], pink);
37}

Kauhiz

Hey, wait a sec!

Quote:

play[1] = create_bitmap(300, 40);
play[2] = create_bitmap(300, 40);//THIS IS THE LINE

If you declare that as BITMAP *play[2], then it should be

play[0] = create_bitmap(300, 40);
play[1] = create_bitmap(300, 40);//THIS IS THE LINE

That would explain it...

James Stanley

Oh. Really? Whoops. :-[:-[:-[:-[:-[:-[:-[:-[
That's fixed it. I'm embarassed now.

count

Oh, yes! It's an array overflow. ::)
applys to all the other buttons too, btw.

Thread #586715. Printed from Allegro.cc