Attaching bitmaps to region of other bitmaps and how to get its screen coords.

So basically I am trying to figure out how to relate two, or more, bitmaps together. So when one bitmap is moved and/or rotated, the other bitmap moves with it, but that I can still access the second bitmap separately.

I want to be able to have a spaceship. This spaceship should have turrets that can move independently. Now, I've managed to make this happen by creating a ship bitmap, a turret bitmap, and a tempship bitmap. I then put the ship bitmap on the tempship bitmap. Then I put the rotated turret on the tempship bitmap based on the spot I want. Then I rotate the tempship bitmap. The problem is that then I have no way of indicating which spot the turret is at when the whole thing is rotated.

I'll list my questions now.

1. Am I doing the right thing by copying multiple bitmaps to a final bitmap to associate various moving bitmaps together?

2. How do I find a specific area of a bitmap on the screen after it has been rotated so I can create new bitmaps at this origin?

//Create ship bitmap
shipImage = al_load_bitmap("spaceship.bmp");
al_convert_mask_to_alpha(shipImage, al_map_rgb(255,0,255));

//Create turret bitmap
turretImage = al_load_bitmap("turret.bmp");;
al_convert_mask_to_alpha(turretImage, al_map_rgb(255,0,255));

//Create temp bitmap
tempImage = al_create_bitmap(shipImageHeight * 1.2, shipImageWidth * 1.2);

//Combine ship and turret
al_draw_bitmap(shipImage, tempImageWidthDiff, tempImageHeightDiff, 0);
al_draw_rotated_bitmap(turretImage, turretImageWidth / 2, turretImageHeight / 2, 20 + tempImageWidthDiff, 100 + tempImageHeightDiff, ConvertDegreeToRadian(turretAngle), 0);
al_convert_mask_to_alpha(tempImage, al_map_rgb(255,0,255));

al_draw_scaled_rotated_bitmap(tempImage, tempImageWidth / 2, tempImageHeight / 2, WIDTH / 2, HEIGHT / 2, zoom, zoom, ConvertDegreeToRadian(shipAngle), 0);

Thanks for any help you can provide to my newbie game programmer self!

Edgar Reynaldo

Sorry it took me so long to reply, but I wanted to make a working prototype, and I just kept adding more features...

I made a demo program that shows how you can pin bitmaps to another. Currently they are pinned by a pivot point on the parent (the center, for now) and extended by a pivot arm. They also have a rotation of their own in addition to the rotation of the parents. Changes cascade down from parent to child. All you have to do is set the pivot arm angle and length and the personal rotation of each bitmap.

Of interest are PinnedBitmaps.hpp and .cpp. All they do is store the relative position of a bitmap on a parent bitmap. You can layer them as deep as you need to.

The turrets are pinned to the ships by specified pivot arms.

Drawing is taken care of by an allegro transform for each pinned bitmap, including the parent. All bitmaps are actually drawn at 0,0.

Here's a fully working demo 7zip archive with exe and source and data :

Here's a screenie :

Here are the basic controls :
Left/Right/Up/Down - Turn Left / Turn Right / Move Forward / Move Backward
A,D Strafe Left / Strafe Right
Q,W Rotate Player turrets left and right
F Fire Player cannons
G Fire Enemy cannons

There is no collision detection implemented at the moment, didn't have time for that yet, but I believe it could be done easily with a reverse transform and a bitmap alpha test. I have to work on that.

Graphics are shamelessly ripped off from the internet. I'm a programmer, not a pixel artist. Copyright somebody else. :P Graphics line up directly facing right, as that is 0 degrees, and makes alignment of everything else easier.


Holy crap!

This is way beyond what I was hoping for, thanks so much. Now I just have to somehow wrap my mind around what you wrote. Transformation confuse the heck out of me still. :) I'm also a bit baffled by the line:

al_translate_transform(&transform , -al_get_bitmap_width(bitmap)/2.0 , -al_get_bitmap_height(bitmap)/2.0);

What does the - in front of the function al_get_bitmap_width() mean? I'm not sure I've seen or read about this before and a quick google search didn't bring up anything. I thought maybe it was literally to subtract the return value but what would it subtract it from? Other than that most of the code looks fairly familiar and I'll have to go through it and pick the pieces apart to understand it. Really need to read up more on transformations, though, as they seem fairly mysterious to me! Thanks so much for the help! I was getting worried I asked a weird question, but I haven't been able to find any discussion about this type of thing, even though it seems like it should be a commonly used thing.


P.S. One other thing, I tried to compile what you had and it gave me two errors on the following line. Probably just messed something up but looking at the error it looks like a function return type error?

if (!al_init_font_addon()) {return 3;}
PinnedBitmapTest.cpp|40|error: could not convert 'al_init_font_addon()' from 'void' to 'bool'|
PinnedBitmapTest.cpp|40|error: in argument to unary !|

P.S.S. Looks like you are using 5.1 and I am using 5.0.10 which might be the problem. Should I upgrade, or is it better to stick to stable for learning?

Thomas Fjellstrom
koekhaos2 said:

I thought maybe it was literally to subtract the return value but what would it subtract it from?

It's the unary negate operator. Pretend theres a 0 on the left hand side.

Edgar Reynaldo
koekhaos2 said:

if (!al_init_font_addon()) {return 3;}

PinnedBitmapTest.cpp|40|error: could not convert 'al_init_font_addon()' from 'void' to 'bool'|
PinnedBitmapTest.cpp|40|error: in argument to unary !|

In Allegro 5.0.X, al_init_font_addon returns void. In 5.1.X, the same function returns a bool parameter for consistency with the other al_init functions. Just change that line to :

if you are compiling for 5.0.X.


Should I upgrade, or is it better to stick to stable for learning?

Both versions are 'stable', it's just that one is under development and subject to change at any time. 5.1.8+ has more features, like the video addon and shaders, but 5.0.10 should be fine.

Arthur Kalliokoski

It's the unary negate operator. Pretend theres a 0 on the left hand side.

To expand on that,

al_translate_transform(&transform , al_get_bitmap_width(bitmap)/(-2.0) , al_get_bitmap_height(bitmap)/(-2.0));

would be an algebraic equivalent. Or maybe you're confused as to why it has to be negated at all, actually you don't move the camera, you move the world coordinates in the other direction, which is equivalent.


Thanks all! To be honest, a lot of these things are just so far beyond me still. I'm really new to programming, only started trying to learn in my little bits of spare time the past two years. Even then I've only been able to comprehend so much. My dream has been, since a child, to make my own video games so I have just been trying to dedicate my free time to trying just that. I think I'm a bit slower in picking up languages and especially math expressions. I've always had a hard time with math. However, I know I can get somewhere if I keep trying! I've just gotten the first bits of an automated random sky background generator started and so excited by the possibilities before me! I probably got into programming a little late, 33 now, so I'm sure I'll have a million more questions as I go, so please be patient with me. Thanks!


Thread #614615. Printed from