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?
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 -.-
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
For a program this trivial im not worried about saving memory.
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.
I changed my code into this
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
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:
and as for making your bitmap transparent, do you mean translucent? or just seeing through areas that are marked with a special color?
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).
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
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.
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
what is the code you are currently using for the space key event?
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 | 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 -.-
does it crash if 'check' is 0 ?
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?
um. There is a for loop there. so if check is not 0 it will recreate all the bombs.
I changed my code to the following:
It still crashes when I press space
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 .....
Is bomb[n]->picture a valid bitmap?
[edit:] Chris: I didn't notice he started asigning something to check... [/edit]
Especially if you just created one. Try moving the if(check) under the blit just for kicks.
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 | |
5 | class 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 | |
53 | int 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 | } |
128 | END_OF_MAIN(); |
Time to goto sleep, goodnight
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];
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.
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.
...I was pretty sure I had intialized them...
Anyway it works now! thx alot
gotta pay more attention to my code ><
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.
wait months?...
I can barely wait an hour
well.. if you spend WEEKS trying to fix a bug you can't find... you'd lock it away for a while.
If I spend weeks trying to find a bug I would post it here ^^
duh. thats one of the options I posted above, either get someone to see it, or lock it away for eternity
you should have included both options in one post to eliminate the possibility of confusion
I did. look at the message just above the one where you post: 'wait months?'
Stop spamming you two.
I need to raise my post count! ^^
Must be as hated as on GameDev
They're probably wondering where you took off to.
Oh the irony...
I bet if we compared post counts for a week, we'd see I post less than anyone ....
whats your ratio then? mine is at 1.16 posts a day, it used to be at 0.7ish.
Never you mind.
I dont see any irony...
Maybe I should pay the fools at GameDev a visit...nahhhhh
I'm dumb!. But mine is going up...
p.s. Chris, you must love your premiere
Huh? That's Stephen Limbaugh, the ignorant Missouri judge that said video games weren't free speech (note the sig).
ah. it looked like a one of the premieres (assumed it was yours)... Oh well.
premier==CommieRussian?
no.. not quite sure if youre talking about our Prime Minister.
I think this is going to be off-topic....
And your problem, SonShadowCat, you solve?
Whaddaya mean, "going to be"?
Hey! I can drink 20 gallons of whole milk in an hour! What's your record?