Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem with image rountine

This thread is locked; no one can reply to it. rss feed Print
Problem with image rountine
van le
Member #14,721
November 2012

hello guys, my name is van. I have a problem with using image in allegro.
i try to load a image, and use function al_draw_tinted_bitmap to color the tank. But when i render. Nothing happen. Someone can help me. Here is the code

m_bImage = al_load_bitmap("Tankz_sprite.png"); // the entry image
int h = al_get_bitmap_height(m_bImage);
int w = al_get_bitmap_width(m_bImage);

ALLEGRO_BITMAP* tempImage;

tempImage = al_create_bitmap(w/5, h);
m_tankBody->image[0] = al_create_bitmap(w/5, h);
m_tankBody->image[1] = al_create_bitmap(w/5, h);
m_tankBarrel->image = al_create_bitmap(w/5, h);

//al_draw_bitmap_region(m_bImage, 40.0f, 40.0f
al_set_target_bitmap(m_tankBody->image[0]);
al_draw_bitmap_region(m_bImage, 0.0f, 0.0f, 40.0f, 40.0f, 40.0f, 40.0f, 0);
al_set_target_bitmap(m_tankBody->image[1]);
al_draw_bitmap_region(m_bImage, 40.0f, 40.0f, 40.0f, 40.0f, 80.0f, 80.0f, 0);
al_set_target_bitmap(tempImage);
al_draw_bitmap_region(m_bImage, 80.0f, 80.0f, 40.0f, 40.0f, 120.0f, 120.0f, 0);

al_set_target_bitmap(m_tankBody->image[0]);
al_draw_tinted_bitmap(tempImage,al_map_rgb(0, 255,0), 40.0f, 40.0f,0);
al_set_target_bitmap(m_tankBody->image[1]);
al_draw_tinted_bitmap(tempImage,al_map_rgb(0,255,0), 40.0f, 40.0f, 0);

al_set_target_bitmap(tempImage);
al_draw_bitmap_region(m_bImage,160.0f, 160.f, 40.0f, 40.0f, 200.0f, 200.0f, 0);
al_set_target_bitmap(m_tankBarrel->image);
al_draw_bitmap_region(m_bImage, 120.0f, 120.0f, 40.0f, 40.0f, 160.0f, 160.0f, 0);
al_draw_tinted_bitmap(tempImage,al_map_rgb(125,255,0), 40.0f, 40.0f, 0);

al_set_target_bitmap(al_get_backbuffer(display));

Kris Asick
Member #1,424
July 2001

It's difficult to tell what you're trying to do but I think the problem is that you're misunderstanding the purpose of al_set_target_bitmap().

In terms of working with graphics, you always have a "source" bitmap and a "target" bitmap. The source is where you want to draw from, and the target is where you want to draw to. For instance...

al_set_target_bitmap(tempImage);
al_draw_bitmap(m_bImage,0.0f,0.0f,0);

...will draw the entire contents of m_bImage into the upper-left corner of tempImage. This is because you set tempImage as the target, and m_bImage is the source.

You have to set your target first if you don't intend to draw to the screen but onto another bitmap, then make your draw calls. You only need to call al_set_target_bitmap() once for each set of al_draw_bitmap() calls following. When you're ready to draw to the screen again, call al_set_target_backbuffer() so that all following al_draw_bitmap() calls will draw to the screen. (Though you still need to cal al_flip_display() when you're done drawing to the screen for anything to show up.)

That said, nowhere in the code you've provided do you actually draw anything to the screen.

Lastly, please use <code> and </code> tags when showing your code.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

van le
Member #14,721
November 2012

ok let me be clear.

#SelectExpand
1 ALLEGRO_BITMAP* image = NULL; 2 ALLEGRO_BITMAP* image2 = NULL; 3 ALLEGRO_BITMAP* image3 = NULL; 4 5 image = al_load_bitmap("Tankz_sprite.png"); 6 int w = al_get_bitmap_width(image); 7 int h = al_get_bitmap_height(image); 8 image2 = al_create_bitmap(w/5,h); 9 image3 = al_create_bitmap(w/5,h); 10 11 al_set_target_bitmap(image3); 12 al_draw_bitmap_region(image, 80.0f, 80.0f, 40.0f, 40.0f, 0.0f, 0.0f, 0); 13 al_set_target_bitmap(image2); 14 al_draw_bitmap_region(image, 0.0f, 0.0f, 40.0f, 40.0f, 0.0f, 0.0f, 0); 15 al_draw_tinted_bitmap(image3, al_map_rgba(0,255,0,0.2), 0.0f, 0.0f, 0); 16 al_set_target_bitmap(al_get_backbuffer(display));

so what i want to do here is.

image is a bitmap hold the entry sprite
image2 is the bitmap use to draw
image3 is the bitmap that make up image2 ( i mean color image2 )with color al_map_rgba(0,255,0,0.2).

u can see my sprite :). I think i could understand the code through the tutorial

http://fixbyproximity.com/2011/09/2d-game-dev-part-6-3-rotating-scaling-and-tinting-bitmaps/

but it not work. it just render the tank with no color

thankyou for reading :)

Trent Gamblin
Member #261
April 2000
avatar

You're mixing color respresentations up.

van le said:

al_map_rgba(0,255,0,0.2)

al_map_rgba takes 4 values, all between 0 and 255. Either use it that way or use all floats between 0 and 1 with al_map_rgba_f.

I also think you're not handling the fact that Allegro uses premultiplied alpha in its images. You probably want:

float alpha = 0.2f;
al_map_rgba_f(0.0f*alpha, 1.0f*alpha, 0.0f*alpha, alpha);

Now obviously you don't need the 0*alpha, you can just put 0, and you can precalculate 1.0f*alpha and just put 'alpha' there too... but when drawing with colors you need to multiply r,g,b by alpha (only when they're floats though, if they're 0-255 values then multiple r,g,b by alpha/255.0f.)

van le
Member #14,721
November 2012

ok thankyou all you guys, i just misunderstand the source x, source y, destinate x y of al_draw_bitmap_region. I fixed it. Thankyou :)

Go to: