blitting sprites
Money

hey, i have a .bmp, its a..well a bunch of sprites, i got a suggestion to blit the sprite frame onto the screen, but it still didn't work, i only need one static image of a sprite, not all of its movements. here is my source.

1#include <allegro.h>
2 
3enum { SUCCESS, FAILURE };
4 
5int main(void)
6{
7 
8//initialization
9 allegro_init();
10 set_color_depth(16);
11 set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
12 install_keyboard();
13 
14 //bitmaps
15 BITMAP *bmp;
16 PALETTE palette;
17 bmp = load_bitmap( "sprite.bmp", palette );
18 if(!bmp)
19 {
20 allegro_message("Error: could not load bitmap");
21 }
22 
23 
24 
25//main game loop
26while ( !key[KEY_ESC] )
27{
28 acquire_screen();
29
30 blit(bmp, screen, 0, 0, 0, 0, 20, 20);
31 
32 
33 
34 release_screen();
35 
36 
37}
38 
39 
40 
41return SUCCESS;
42 
43}
44 
45END_OF_MAIN();

download attachment, its a screenshot of the output.

Jonny Cook

Check the documentation for blit. You can use it to draw only parts of a bitmap.

[edit]
A couple things:
You don't have to acquire the bitmap before you draw to it.
You can just pass NULL as the pallet when you are using 15 or higher color depth.
Are you sure that's the output? Because your code only draw 20 pixels of the image, and that's a lot more.

Money

well, i have

while ( !key[KEY_ESC] )
{
acquire_screen();

blit(bmp, screen, 0, 0, 0, 0, 20, 20);
blit(screen , screen, 0, 0, 90, 90, 70,70);

release_screen();

}

and it still didn't work. when you said palette did you mean like

Palette palette;
palette = 155, 0, 0; //red color? or something, i always use makecol

Onewing

Does your sprite have a white background? Typically, you'll want to use the masking color (rgb(255,0,255)) as the background instead. This way, when you blit the image, anything with that pinkish color will not overwrite what is behind the image. Note, you will have to use masked_blit instead of just plain ole blit.

For just picking out a certain portion of the image, yes, check the documentation of blit.

Alex Hanson-White

edit

Money

no that still didn't work

while ( !key[KEY_ESC] )
{
acquire_screen();

masked_blit(bmp, screen, 0, 0, 0, 0, 20, 20);
masked_blit(screen , screen, 0, 0, 90, 90, 70,70);

release_screen();

}

download attachment..it is a screenshot

Miles Lombardi

Quote:

You can just pass NULL as the pallet when you are using 15 or higher color depth.

As he said just use NULL, it'll stop the complications of it using a palette.
Blitting the screen to the screen won't do anything, apart from waste time.
And one other thing, it might be wise to rest(30); at the end of your step, otherwise your wasting a lot of processor power. Well all of it in fact.

Money
//main game loop
while ( !key[KEY_ESC] )
{
    acquire_screen();
    
 

    release_screen();
    rest(30);


}

ok, so i don't use blit, then what do i do? i just want static image, so then i can animate it from input(keyboard).

Miles Lombardi

I don't use acquire_screen() and released_screen() used to annoy me because they get very spread out over the entire loop and eventually i got rid of them and i can't remember why now, but i think acquire_screen is only needed when your using primitives, rather than bitmaps.

But I don't understand why this wouldn't work (standard blitting code):

blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h);

You only need to worry about masked_blit when you want transparency (say for your moving character).

Just put that in between acquire and release screen and i don't see why it shouldn't work :S.

And oh btw, I have to ask why these dimensions:

masked_blit(screen , screen, 0, 0, 90, 90, 70,70);

It seems a weird area of the screen to copy.

Onewing

Do this:

bmp = load_bitmap( "sprite.bmp", NULL);

[EDIT]

Quote:

I don't use acquire_screen() and released_screen()

Nor do I.

Money

hmm, still didn't work, all of the sprites still showed up.

1#include <allegro.h>
2 
3enum { SUCCESS, FAILURE };
4 
5int main(void)
6{
7 
8//initialization
9 allegro_init();
10 set_color_depth(16);
11 set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
12 install_keyboard();
13 
14 //bitmaps
15 BITMAP *bmp;
16 PALETTE palette;
17 bmp = load_bitmap( "sprite.bmp", NULL );
18 if(!bmp)
19 {
20 allegro_message("Error: could not load bitmap");
21 }
22 
23 
24 
25//main game loop
26while ( !key[KEY_ESC] )
27{
28 acquire_screen();
29
30 blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h);
31 
32 release_screen();
33 rest(30);
34 
35 
36}
37 
38 
39 
40return SUCCESS;
41 
42}
43 
44END_OF_MAIN();

Jonny Cook
1int main() {
2 allegro_init();
3 install_keyboard();
4 set_color_depth(16);
5 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
6 
7 BITMAP *big = load_bitmap("sprite.bmp", NULL); // Pass NULL to use default palette
8
9 blit(big, screen, 32, 0, 0, 0, 32, 32);
10 
11 /*
12 ***********
13 *.#.......*
14 *.........*
15 *.........*
16 *.........*
17 ***********
18 The # would be the part of the bitmaps that is actually drawn.
19 */
20
21 while (!keypressed()) ;
22
23 return 0;
24}
25END_OF_MAIN()

That would blit a 32x32 section of sprite.bmp.

Richard Phipps

Money: I have a selection of game creation articles I wrote some time ago. They deal with creating a gauntlet style game using simple C and allegro commands. Maybe that will help you understand how things work?

http://www.reflectedgames.com/create.html

Money

ok, it worked, jonny, thanks :)

now only the top head was shown, so i think i need to put the width and height greater, um, you said to get rid of the white background too use masked_blit on the sprite?

Jonny Cook

Not sure why that would happen... but I haven't tested it so it could be something wrong. I was just giving an example of how to blit a portion of a bitmap to the screen. Oh, and I forgot to destroy the image.

[edit]
Yeah, use masked_blit. It takes the same arguments, but just skips all the pink (255, 0, 255) pixels.

Money

alright, well i guess i need to do 255, 255, 255 because the sprites background is white.

is there an exact way to get the coordinates of the first frame, rather than to keep testing coordinates to see if the frame is there?

last thing, if i wanted to animate it, i just load another frame and just blit it and delete the last frame correct?

sorry for all of thesse questions, but this is the stuff i can't find in the api, documents , or examples, their using c, and its kinda difficult for me. in the example files its different.

edit: richard, thanks a lot! :) thats really useful

Jonny Cook
Quote:

alright, well i guess i need to do 255, 255, 255 because the sprites background is white.

I'm afraid you'll have to change the background to pink. Allegro has the it hardcoded in.

Quote:

is there an exact way to get the coordinates of the first frame, rather than to keep testing coordinates to see if the frame is there?

If you have all the frames the same size, you can do something like this:

int src_x = image_w * frame;

Assuming you have them all in one line, src_y should be 0.

Quote:

last thing, if i wanted to animate it, i just load another frame and just blit it and delete the last frame correct?

You shouldn't do any loading or deleting in the main loop. Load the big image at the beggining, and then do something like this:

int src_x = sprite_w * frame;
int src_y = 0;
blit(big, buffer, src_x, src_y, sprite_x, sprite_y, sprite_w, sprite_h);

That would work only if all your frames are the same size.
[edit]
You'll also have to make sure the images are right next to each other, and that the first frame starts at the very corner (0, 0).

Onewing
Quote:

alright, well i guess i need to do 255, 255, 255 because the sprites background is white.

I don't think you can do that. I would just open up the picture itself and fill the background with pink (255,0,255).

Quote:

is there an exact way to get the coordinates of the first frame, rather than to keep testing coordinates to see if the frame is there?

Again, open up the picture and set it up to where the images can be found at a given length in an x,y direction.

Quote:

last thing, if i wanted to animate it, i just load another frame and just blit it and delete the last frame correct?

Shouldn't have to delete it, if you have your design right.

Money

hmm i opened the sprite up in ms paint, and changed that backgound to pink(255, 0, 255), when i recompiled and ran the .exe , the pink background showed up, i changed it to black , it was perfect, but thats not very smart when i load like other bitmaps in ,like buildings and stuff, because you'll e able to tell there is a black background stuck to the sprite

i used

while( !key[KEY_ESC] )
    {

    acquire_screen();
    masked_blit(big, screen, 32, 0, 640/2, 480/2, 40, 90);

    release_screen();

    }

what could be wrong, im doing everything right.

[edit]
ok, i guess thats it, i guess i'll have to figure it out someonehow :( but thanks !

Onewing
Quote:

and changed that backgound to pink(255, 0, 255)

Are you sure? If you are off, even by 1 (e.g. 254,0,255), then the pink will show up.

Quote:

when i recompiled and ran the .exe

No need to recompile, you are changing the bitmap, not the code. ;)

Are you blitting the sprite to 'big' somewhere or is 'big' the loaded picture?

Money

nope, i have tried like 3 different pinks, but it still shows up, here is the code:

1#include <allegro.h>
2 
3int main() {
4 allegro_init();
5 install_keyboard();
6 set_color_depth(16);
7 set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
8 
9 BITMAP *big = load_bitmap("sprite.bmp", NULL); // Pass NULL to use default pallete
10 
11 while( !key[KEY_ESC] )
12 {
13 
14 acquire_screen();
15 masked_blit(big, screen, 32, 0, 640/2, 480/2, 40, 90);
16 
17 release_screen();
18 
19 }
20 
21 
22 
23 return 0;
24}
25END_OF_MAIN()

i don't possibly see what i could be doing wrong :|

Evert

Attach the picture.

Richard Phipps

Can you attach the sprite to a post so we can have a look?

Onewing
Quote:

nope, i have tried like 3 different pinks, but it still shows up, here is the code:

If you are trying different pinks, then that's not going to work. Do this:

1) Open PAINT
2) Double click the pink color in the color selections available
3) Click "Define Custom Color"
4) Make sure Red says "255", Green says "0", blue says "255"
5) Fill the defined color to the background

Money

[edit]

alright, i got it to work, turns out it was ms paint messing up, i downloaded neopaint and edited the palette to 255, 0, 255, and used that as the background, so i guess i stick with neopaint :)

thanks all of you for helping me out, i have not yet seen a post this long for one problem, sorry about the credits i had did that earlier, on like the 1st page, but thank you all so much, you've been such a big help

Credit goes to all who replied on this subject...except to craig, no credit goes to craig whatsoever

Richard Phipps

First: That's a jpeg file (so the original colours are lost)
Second: Even still, that looks like violet not purple. I get about 204,153,209 when looking at that 'pink'. It's nowhere near 255,0,255 which is what you need.

Attach the original BMP file please.

Jonny Cook

That's definitely the wrong pink.
[edit]
Don't use JPEGs. Use BMPs, because JPEG compression messes with the colors.

Money

ok, this one is with 255, 0, 255 pink

Jonny Cook

Dude, that's purple.

Richard Phipps

PSP looks at the 'pink' and says:

R: 160
G: 64
B: 192

Do you understand what we are asking? ???

Money

man, i put 255, 0, 255 as the rbg, and ad to custom, thats what it said, and thats what the color was, and thats why i tried other pinks, doens't work

Jonny Cook

Look at the image I attached. That's the pink you should use. Open mspaint, use the eye drop tool, and fill in your background with that exact color.

Onewing
Quote:

man, i put 255, 0, 255 as the rbg

You do realize it's rgb, right?

Money

ok, for the 3rd time, i'll try another pink and its still won't work, i'll post the pic too

Richard Phipps

Download Jonny's image, rename it to sprite.bmp, copy it to your code folder, run your program and see if that works ok.

Money

ok, this is pink- Pink 255-192-203

shows grey over here though

great: now i can't upload any files, wtf, is going on???

richard, i got a blank black screen...

Richard Phipps

Just try what I suggested. It should work.

Jonny Cook

That color is grey, yes. The color you want is 255, 0, 255. You've got the first number right.

Money

johnny cook: sigh, when you said "dude, thats purple", that was 255,0, 255

Onewing

Attach the sprite.bmp, not a screenshot.

[edit]-that probably won't work because it'll be too big of file size, you'll have to make an image link.

Jonny Cook

I'm afraid you are wrong. The exact color in the image was RGB(160, 64, 192). At least that's the color in the image you posted.

[edit]
If the BMPs small it will be okay.

Money

but i put in 255, 0, 255 johnny

i tried that sprite, but all i got was a black screen of death

is there anythin else other than ms paint, its actin really strange, colors are all screwed

Richard Phipps

No it wasn't..
http://www.retroremakes.com/forum/images/smilies/coderpunch.gif

Christopher Bludau

What colordepth is your programm running in?

I'm just asking because allegro docs quote this:

allegro manual said:

For 256-color bitmaps this is zero, and for truecolor bitmaps it is bright pink (maximum red and blue, zero green).

Money

color death is 16

[edit]
color depth is 16[/edit]

Onewing

And what type of bitmap are you saving sprite.bmp as?

[edit]

Quote:

[edit]
color depth is 16[/edit]

Coincedentally, 'p' is the farthest letter from 'a'. ;D

Jonny Cook

Have you tried using the bitmap I posted yet?

Money

umm, i dunno, i think it was the true color,...or maybe it was 256..:|

johnny cook: for the 4th time yes, and all i got was a black screen

Richard Phipps

Attach the original sprite.bmp you want to use. I will recolour the background to the correct colour and reattach it to my post.

Christopher Bludau

just try your program with jonny's bitmap.
Then tell us what happened.

[EDIT]
Ok. you did.

Money

here is the orignial sprite.bmp

Alex Hanson-White

sounds like when you're saving the file it is converting the colors. make sure you are saving it correctly.
What program are you useing to save the image?

Evert

Also make sure that you entered the 255,0,255 as RGB and not in some other colourspace (don't know if Paintbrush can do that or not).

Money

microsoft paint, i save it as 24 bit bmp

Christopher Bludau

The image you attached was a 8 bpp image. ???

Money

ok, attached screenshot*, now i told you, its something with ms paint or something

Richard Phipps

Try this

EDIT: Too big to attach as a 24-bit bmp.
I'll clip it..

Fladimir da Gorf

All this trouble while you could just go and save the image as an RGBA PNG and you wouldn't have to worry about pinks, your images would look nicer etc ;) Though you can't do that in Paint anyways.

Alex Hanson-White

you shoudl save the file as 16 bit not 24.. because your allegro program is running in 16 bit, so it converts the colors when it runs(because you saved it as a different depth).
or try defineing the graphics mode to 24 bit instead in your allegro code

Richard Phipps

Try this section of your sprite.bmp file:

Money

255-0-255 in 16 bit bmp

Richard Phipps

Did the sprite I changed work for you?

Money

richard: that worked perfectly, how come i couldn't get it to work, what color was that(rgb)

Christopher Bludau

it has to be 255-0-255 in richards image.

probably your ms paint is messing soemthing up.

Richard Phipps

It's R: 255, G: 0, B: 255 saved as a 24-bit Bmp file.
MS Paint is obviously not saving your image right for one reason or another.

Glad it worked! Now I'm off to bed.

Money

yea...i said that like 4 times too

is there another program other than ms paint?

Neil Walker

Hello,
Check out this, which is basically what Alex is saying: http://www.allegro.cc/manual/api/loading-image-files/set_color_conversion

You are setting 16bit graphics mode, but your picture is 8bit paletted, so on loading the image, the system converts all colours to RGB.

What you need to do is save the image as hi/true colour not 8bit and make sure the background/mask colour is magenta (255,0,255).

Fladimir da Gorf

Money, that's clearly not the color you want. How do you get the color and use it? If you've picked the pink from the default palette, you should choose the lighter one, just below the one you've chosen.

Money

what is true color mode? 256? 16, 24? i tried all of them, but it won't work with mine :'(

none of those color conversions work

Matthew Leverton

If your paint program is operating in 8-bits, then it is quite likely that even if you select (255,0,255) that it will select a different color depending on the palette.

If your image is 16-bit and you select (255,0,255) and save it as a BMP file, it will work with Allegro if you set_color_depth(16).

Summary:

  • Use a 16-bit image

  • Select (255,0,255) as the pink color

  • Save it as a BMP

  • set_color_depth(16) BEFORE setting the graphics

  • masked_blit will now be transparent

Just keep reading that and following it till you get it right.

PS: Truecolor is 24/32 bit and High color is 15/16 bit.

Money

what bmp did you want me to save it as? my color depth is 16 in my code, so i saved the sprite as a 16 bit bmp and when i ran the exe it was all distored and green

Christopher Bludau
Quote:

what bmp did you want me to save it as?

Quote:

Use a 16-bit image

So you should save it as 16 bit bitmap.
It was just a general advice.

Money

i did everything you said,

but it just showed up as the sprite, but with the background behind it :(

anyone want to remote assistance? please :|

Jonny Cook

Okay, zip your entire project and post it so we can see exactly what is going on.

Christopher Bludau

I guess it has nothing to do with his project, but his paint program. see his attached screenshot of his paint.

It works in 8 bit modus.
I guess it is because the image he is working with is a 8 bpp image.
When he opens it, paint goes to 8 bit modus.

So he can not set the right color.

Money

i'm using codeblocks so you won't be able to open the project, only the source

Jonny Cook

Yeah, so the BMP was only 256 colors (8 bit). I opened it in MSPaint and saved it in 24 bit colors. Then everything worked fine. Just make sure you don't convert the images back to 8 bit. What paint program do you use?

Money

i use ms paint, i opened the gohan file, changed the color saved as 24 bmp, and it still didn't work .... sigh

Jonny Cook

That is reallllly strange. I guess this wasn't really your fault then... perhaps a bug in your version of MSPaint? I doubt that's the case, but who knows.

Does the version I attached work?

Money

no it doesn't work :(

Jonny Cook

Okay... well that's beyond me. It works fine on my computer. Does it work for anyone else?

Money

i sent it to one guy, said he needed allegro42.dll or something

Jonny Cook

Well put it in the archive too. You can usually find it in your compiler bin folder. If it's not there then just search for it.

zer0

Sounds to me like Money is fucking with all of you; no one can be that stupid. Just look at how many posts he has made showing incredible incompetence ::)

and its alleg42.dll

edit: i got his attached code to work by simply changing the color. The bug isn't in MSPaint and I'm sure your program works fine johnny; the only bug that exists is in Money's brain

Money

craig...no one likes you

[edit]
why would anyone waste like..hours trying to joke someone....thats just dumb..and second these people don't HAVE to help me, but they did anyway cause their good people, why am i even wasting my time saying this to you craig..in short, craig your a pisshead

you can download it from here

www.freewebs.com/avont29/sprite.rar

Jonny Cook

Just want to double check one more time: did you download the version I uploaded and run it as is? Like, without opening the images or anything?

Money

well i did open the image, to see it, the color i mean

Christopher Bludau

Please try this:

    set_color_conversion(COLORCONV_TOTAL); /* this is the new line */
    set_color_depth(16);
    set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);

Enter the new line in your code and tell us what happens.

Money

no, that didn't work either :(

we can remote assistance, where you connect and control my computer and see what the prob is

my contacts are

[edit]
..deleted

Chris Katko

You might want to remove your contacts and just give them to anyone who PMs you for them. Otherwise, you might end up getting a bunch of spam mail.

Evert

About the sprite... make sure you save it in Paint as a 16 bit image and not as a 16 colour image. The former could be called something like `64k colours' or `high colours' and the latter just `16 colours'.
Also, what's your desktop resolution? Have you tried other programmes, such as PaintShopPro, NeoPaint or the GIMP? Try either or all of those and see if they work.

EDIT: yeah, I meant to say desktop colourdepth, not resolution...

miran

Maybe I'm missing something (I admit I haven't read the entire thread), but to me it looks like Money is running Windows in 8bpp mode, that's why Paint refuses to work in anything else...

Money: Right click on your desktop, select "properties", then the the "settings" tab and in the "color quality" list select "32 bit" or something. Then click "apply" or "OK".

Jonny Cook
Quote:

well i did open the image, to see it, the color i mean

No, you were supposed to run the program... but okay.

Avenger
Quote:

Maybe I'm missing something (I admit I haven't read the entire thread), but to me it looks like Money is running Windows in 8bpp mode, that's why Paint refuses to work in anything else...

The problem is you can't tell from his screenshot. Because he must have been using mspaint to make that image, and since mspaint is (as one possibility) broken.

My guess is that Money is either using Windows in 8-bit mode, which I consider very unlikely, or that his mspaint is broken, which also sounds unlikely. But in the Microsoft world, nothing is impossible, except for the good stuff.

Money

there is no way i can run in 8 bit mode

download attachment

HoHo

What attachment ::)

Money

alright, i got it to work, turns out it was ms paint messing up, i downloaded neopaint and edited the palette to 255, 0, 255, and used that as the background, so i guess i stick with neopaint

thanks all of you for helping me out, i have not yet seen a post this long for one problem, sorry about the credits i had did that earlier, on like the 1st page, but thank you all so much, you've been such a big help

Credit goes to all who replied on this subject...except to craig, no credit goes to craig whatsoever

zer0

;D boohoo

Thread #556463. Printed from Allegro.cc