Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » blitting sprites

Credits go to Alex Hanson-White, Jonny Cook, Miles Lombardi, Onewing, and Richard Phipps for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2   3   4 
blitting sprites
Money
Member #6,730
December 2005
avatar

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
Member #4,055
November 2003

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.

The face of a child can say it all, especially the mouth part of the face.

Money
Member #6,730
December 2005
avatar

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
Member #6,152
August 2005
avatar

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.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Alex Hanson-White
Member #6,657
December 2005
avatar

edit

Money
Member #6,730
December 2005
avatar

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
Member #5,876
May 2005

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
Member #6,730
December 2005
avatar

//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
Member #5,876
May 2005

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
Member #6,152
August 2005
avatar

Do this:

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

[EDIT]

Quote:

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

Nor do I.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Money
Member #6,730
December 2005
avatar

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
Member #4,055
November 2003

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.

The face of a child can say it all, especially the mouth part of the face.

Richard Phipps
Member #1,632
November 2001
avatar

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
Member #6,730
December 2005
avatar

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
Member #4,055
November 2003

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.

The face of a child can say it all, especially the mouth part of the face.

Money
Member #6,730
December 2005
avatar

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
Member #4,055
November 2003

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).

The face of a child can say it all, especially the mouth part of the face.

Onewing
Member #6,152
August 2005
avatar

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.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Money
Member #6,730
December 2005
avatar

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
Member #6,152
August 2005
avatar

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?

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Money
Member #6,730
December 2005
avatar

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
Member #794
November 2000
avatar

Attach the picture.

Richard Phipps
Member #1,632
November 2001
avatar

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

Onewing
Member #6,152
August 2005
avatar

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

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Money
Member #6,730
December 2005
avatar

[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

 1   2   3   4 


Go to: