Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How do I make the background color 0, etc

This thread is locked; no one can reply to it. rss feed Print
 1   2 
How do I make the background color 0, etc
SonShadowCat
Member #1,548
September 2001
avatar

Ok, now I have this background which has some terrain with a building on it. I want the background to be 0 so my pixel doesnt collide with it and destroy it.

One more question, why doesnt this code work?

1 if( key[KEY_SPACE])
2 {
3 for (int a=0; a<10; a++)
4 {
5 bomb[a]->Move();
6 bomb[a]->Collision(foreground);
7 
8 blit( bomb[a]->picture, foreground, 0, 0, bomb[a]->x, bomb[a]->y, 1, 1);
9 }
10 
11 delete[] bomb;
12 
13 for (int a=0; a<10; a++)
14 {
15 bomb[a]=new Bomb(a);
16 }
17 }

It should blit the pixels to the screen, delete the array and then draw a new set of pixels right?

ty, sorry for being such an idiot -.-

ReyBrujo
Moderator
January 2001
avatar

Quote:

blit( bomb[a]->picture, foreground, 0, 0, bomb[a]->x, bomb[a]->y, 1, 1);

Hmm... you are blitting a picture which is 1x1? If picture is a color (int), you should use putpixel(foreground, bomb[a]->x, bomb[a]->y, bomb[a]->picture). If it is a bitmap... hmm... a 1x1 bitmap is a waste of memory.

RB

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

SonShadowCat
Member #1,548
September 2001
avatar

For a program this trivial im not worried about saving memory.

Plucky
Member #1,346
May 2001
avatar

I see no reason to delete array of bomb[] in your code.

What you're doing is creating the bombs (elsewhere), moving it one frame, check for collision, displaying it, blowing the bomb class variables away, creating a new set that gets initialized to the starting point (if initialized at all), moving it one frame, check for collision, etc.

The bombs won't move because your replacing the bombs with a fresh set of bombs still at the starting line every frame.

What you seem to want to do is erase graphically where the bombs used to be. Just store their previous locations and in each frame, draw over their previous locations with the background and then copy the new locations into the previous location variables.

SonShadowCat
Member #1,548
September 2001
avatar

I changed my code into this

1 if( key[KEY_SPACE])
2 {
3 for (int a=0; a<10; a++)
4 {
5 bomb[a]->Move();
6 check=bomb[a]->Collision(foreground);
7 
8 blit( bomb[a]->picture, foreground, 0, 0, bomb[a]->x, bomb[a]->y, 1, 1);
9 }
10 
11 if( check)
12 {
13 delete[] bomb;
14 
15 for (int a=0; a<10; a++)
16 {
17 bomb[a]=new Bomb(a);
18 }
19 }
20 }

I think instead of deleting and creating bombs, ill just move them...

edit: What I really want to know now is how to make the background in my foreground BITMAP transparent so that the background BITMAP shows through

Thomas Fjellstrom
Member #476
June 2000
avatar

1 if( key[KEY_SPACE])
2 {
3 for (int a=0; a<10; a++)
4 {
5 bomb[a]->Move();
6 check=bomb[a]->Collision(foreground);
7
8 blit( bomb[a]->picture, foreground, 0, 0, bomb[a]->x, bomb[a]->y, 1, 1);
9 }
10
11 if( check)
12 {
13 delete[] bomb;
14
15 for (int a=0; a<10; a++)
16 {
17 bomb[a]=new Bomb(a);
18 }
19 }
20 }

ouch... when you delete the bomb array, you never reallocate it. so when you try to assign pointers to the places in the bomb array you'll get a memory error. (invalid pointer)

Just don't delete the bomb array, and instead:

      if( check) 
      { 
        for (int a=0; a<10; a++) 
        { 
          delete bomb[a];
          bomb[a]=new Bomb(a); 
        } 
      }

or even just make a method to reinitialize the bombs:

    if( key[KEY_SPACE]) 
    { 
      for (int a=0; a<10; a++) 
      { 
        bomb[a]->Move(); 
        check=bomb[a]->Collision(foreground); 
 
        blit( bomb[a]->picture, foreground, 0, 0, bomb[a]->x, bomb[a]->y, 1, 1); 
        bomb[a]->reinit();
      }
    }

and as for making your bitmap transparent, do you mean translucent? or just seeing through areas that are marked with a special color?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Plucky
Member #1,346
May 2001
avatar

Quote:

What I really want to know now is how to make the background in my foreground BITMAP transparent so that the background BITMAP shows through

But you already know how to do this from your code from other threads. In 8 bit color, the "transparent" color is 0 (index 0). A masked_blit() of foreground bitmap to background bitmap would not write over the background wherever a "transparent" color code exist in the foreground bitmap.

Perhaps you're not asking the right question?

[edit]
Thomas, I don't see a difference between your code and that of sonshadowcat. Sonshadowcat already initializes the class variables in his constructor routine (look at his old code in a previous thread).

SonShadowCat
Member #1,548
September 2001
avatar

I know transparent color is 0 but how do I make the white parts of my BITMAP transparent?

Ive tried reading through every pixel in the bitmap and checking to see if it was white, if it was I made it 0

Did I do it wrong?
Plz help

Thomas Fjellstrom
Member #476
June 2000
avatar

It depends on the color depth. in 8 bit mode, color 0 is transparent, while makecol(255,0,255) is transparent in the other color depths.

I think the best way is to call 'bitmap_mask_color(..)'

ie:

int mask_color = bitmap_mask_color(bmp);
int white = makecol(255,255,255); /* could be different if your color palette has more than one 'white' color... */

for(i=0; i<bmp->h; i++) {
 for(j=0; j<bmp->w; j++) {
  if(getpixel(bmp, j, i) == white)
   putpixel(bmp, j, i, mask_color);
 }
}

mind you this is quite slow... but shouldnt be mush of a problem if you only use it on small bitmaps, and then only once in a while.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

SonShadowCat
Member #1,548
September 2001
avatar

Ok, it works, the white part is transparent
But there is one problem remaining...when I press space the program crashes ><

I should really pay more attention to my code

Thomas Fjellstrom
Member #476
June 2000
avatar

what is the code you are currently using for the space key event?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

SonShadowCat
Member #1,548
September 2001
avatar

1if( key[KEY_SPACE])
2 {
3 for (int a=0; a<10; a++)
4 {
5 bomb[a]->Move();
6 check=bomb[a]->Collision(foreground);
7
8 blit( bomb[a]->picture, foreground, 0, 0, bomb[a]->x, bomb[a]->y, 1, 1);
9 }
10
11 if( check)
12 {
13 for (int a=0; a<10; a++)
14 {
15 delete bomb[a];
16 bomb[a]=new Bomb(a);
17 }
18 }
19 }

It was something like this, I kinda changed my code and forgot what it was -.-

Thomas Fjellstrom
Member #476
June 2000
avatar

does it crash if 'check' is 0 ?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

23yrold3yrold
Member #1,134
March 2001
avatar

I assume check is supposed to be checked for every bomb, but it'll only check the last with that code. You may want to move the if(check) bit into the for loop, wouldn't you?

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Thomas Fjellstrom
Member #476
June 2000
avatar

um. There is a for loop there. so if check is not 0 it will recreate all the bombs.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

SonShadowCat
Member #1,548
September 2001
avatar

I changed my code to the following:

1 if( key[KEY_SPACE])
2 {
3 for (int a=0; a<10; a++)
4 {
5 bomb[a]->Move();
6 check=bomb[a]->Collision(foreground);
7 
8 if( check)
9 {
10 delete bomb[a];
11 
12 bomb[a]=new Bomb(a);
13 }
14
15 blit( bomb[a]->picture, foreground, 0, 0, bomb[a]->x, bomb[a]->y, 1, 1);
16 }
17 }

It still crashes when I press space

23yrold3yrold
Member #1,134
March 2001
avatar

Yes, but if check is 1 for the first nine bombs and 0 for the last, then none of them will be deleted.

Arg, ssc beat me .....

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Thomas Fjellstrom
Member #476
June 2000
avatar

Is bomb[n]->picture a valid bitmap?

[edit:] Chris: I didn't notice he started asigning something to check... [/edit]

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

23yrold3yrold
Member #1,134
March 2001
avatar

Especially if you just created one. Try moving the if(check) under the blit just for kicks.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

SonShadowCat
Member #1,548
September 2001
avatar

yes bomb[a]->picture is a valid bitmap, I moved the if statement( not like it was gonna change anything)

hmmmm...what could possibly be the problem...

Im gonna post my entire code, maybe that'll help

1#include "allegro.h"
2#define BACKGROUND 0
3#define FOREGROUND 1
4 
5class Bomb
6{
7 public:
8 
9 BITMAP *picture; // The bitmap holding the image
10 int x;
11 int y;
12 int trans;
13 bool move;
14 
15 Bomb(int a)
16 {
17 picture=create_bitmap(1,1);
18 putpixel( picture, 0, 0, makecol( 255, 0, 0));
19 x=50*a;
20 y=0;
21 move=true;
22 }
23 
24 bool Collision(BITMAP *buffer)
25 {
26 trans=getpixel( buffer, x, y+1);
27 if (trans!=0)
28 {
29 clear_bitmap( picture);
30 move=false;
31 
32 circlefill( buffer, x, y+1, 9, 0);
33 
34 return true;
35 }
36 else
37 {
38 return false;
39 }
40 }
41 
42 void Move()
43 {
44 if (move==true)
45 {
46 y+=2;
47 }
48 else
49 {}
50 }
51};
52 
53int main()
54{
55 allegro_init();
56 install_keyboard();
57 install_timer();
58 
59 set_color_depth(8);
60 set_gfx_mode( GFX_AUTODETECT || GFX_HW_VRAM_BLIT_MASKED, 800, 600, 0, 0);
61 
62 BITMAP *buffer;
63 BITMAP *background;
64 BITMAP *foreground;
65 
66 DATAFILE *images=load_datafile( "c:/C++/Allegro/Terrain/image.dat");
67 
68 Bomb* bomb[10];
69 
70 int trans;
71 bool check;
72 
73 buffer=create_bitmap( 800, 600);
74 background=create_bitmap( 800, 600);
75 foreground=create_bitmap( 800, 600);
76 
77 clear_bitmap(buffer);
78 clear_bitmap(background);
79 clear_bitmap(foreground);
80 
81 blit( (BITMAP*)images[BACKGROUND].dat, background, 0, 0, 0, 0, 800, 600);
82 blit( (BITMAP*)images[FOREGROUND].dat, foreground, 0, 0, 0, 0, 800, 600);
83 
84 int mask_color = bitmap_mask_color(foreground);
85 int white = makecol(255,255,255);
86
87 for(int i=0; i<foreground->h; i++)
88 {
89 for(int j=0; j<foreground->w; j++)
90 {
91 if(trans=getpixel(foreground, j, i) == white)
92 {
93 putpixel(foreground, j, i, mask_color);
94 }
95 }
96 }
97 
98 while (!key[KEY_ESC])
99 {
100 blit( background, buffer, 0, 0, 0, 0, 800, 600);
101 masked_blit( foreground, buffer, 0, 0, 0, 0, 800, 600);
102 
103 
104 if( key[KEY_SPACE])
105 {
106 for (int a=0; a<10; a++)
107 {
108 bomb[a]->Move();
109 check=bomb[a]->Collision(foreground);
110
111 blit( bomb[a]->picture, foreground, 0, 0, bomb[a]->x, bomb[a]->y, 1, 1);
112 
113 if( check)
114 {
115 delete bomb[a];
116 
117 bomb[a]=new Bomb(a);
118 }
119 }
120 }
121 
122 vsync();
123 masked_blit( buffer, screen, 0, 0, 0, 0, 800, 600);
124 }
125 
126 return 1;
127}
128END_OF_MAIN();

Time to goto sleep, goodnight

Thomas Fjellstrom
Member #476
June 2000
avatar

I may be blind, but there isn't one place where you initilize the bombs...

for (int a=0; a<10; a++) {
 bomb[a]=new Bomb(a); 
}

add that right after where you:

Bomb* bomb[10];

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

23yrold3yrold
Member #1,134
March 2001
avatar

Try some commenting out. Does it crash if you comment out check = bomb[a]->Collision(foreground)?

EDIT: Dang, Tom beat me to it ;) I went and made sure you had enough elements, but I missed that. :-[

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

topaz22
Member #2,049
March 2002
avatar

it's crashing because you're trying to call a method of an object that hasn't been created, right!

there's many ways to fix this, try thomas' idea.

SonShadowCat
Member #1,548
September 2001
avatar

...I was pretty sure I had intialized them...
Anyway it works now! thx alot

gotta pay more attention to my code ><

Thomas Fjellstrom
Member #476
June 2000
avatar

no problem. asking other people is usually the way to see mistakes like that. Or waiting a few days (or months) to forget about most of the code, then usually I find stuff like that. :)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

 1   2 


Go to: