Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Turning a bitmap

This topic is 5312 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this bitmap of a car.I want it to turn around, but i dont want to create 50 bitmaps just for that. I''d need to know how to turn a bitmap by x degrees. Im using directdraw by the way.

Share this post

Link to post
Share on other sites
Directdraw has horrible support for rotating, so if you want hardware rotation you''re gonna have to move to Direct3D or OpenGL. However, you can do it yourself using trig. If you don''t know it I suggest you google for trigonometry and such, because it is really useful and you might as well learn it.

"Hey, what about those baggy pants you kids are wearin'' these days? Aren''t they hip and cool and poppin'' fresh?"-Peter Griffin
"Everytime I see an M followed by a dollar sign I just say "oh god" and go to the next post."-Neurokaotix
the D programming language|google|msdn|XLib reference manual

Share this post

Link to post
Share on other sites
If u dont have hardware support for bitmap rotation then u have to do it in software . Itz not hard .. but will be a lil slower.

the idea is

if u want to rotate a bitmap by say 60 degree.. then generate an matrix with rotates a vertex by -60.

cos(-60) -sin(-60)
sin(-60) cos(-60)

[ dont forget to convert angles to radians ]

now calculate the centre position of the target bitmap.. call them (Cx,Cy).

now go trough each pixel of the target bitmap and calculate

x = Tx - Cx
y = Ty - Cy

where Tx and Ty are the X and Y ordinates of the pixels counting forom the top left corner.

now multiply the above inverse rotation matrix with this (x,y)

this will year a new vertex (Sx,Sy)

now calculate

Gx = Sx + Cx;
Gy = Sy + Cy;

now check whether Gx and Gy is a valid pixel location in the source bitmap.. if it is.. then fetch that pixel and copy it to (Tx,Ty) of the target bitmap. If (Gx,Gy) is not valid and write 0 or whatever u feel appropriate in (Tx,Ty)

However, your source bitmap size must be same as ur target bitmap size otherwise this will not work !

Done !

[edited by - browny on January 4, 2004 3:11:31 PM]

Share this post

Link to post
Share on other sites
Ok heres the code: [.SOURCE]

lpddsrotate = DD_Create_Surface(32, 32, DDSCAPS_SYSTEMMEMORY);

DD_Fill_Surface(lpddsrotate, 0);

lpddsrotate = car.images[0];

int cx = car.width/2;
int cy = car.height/2;

for(int a = 0; a < car.width;a++)
for(int b = 0; b < car.height; b++)
int x = a - cx;
int y = b - cy;

int sx = ((x * cos(-90)) - (y * sin(-90)));
int sy = ((x * sin(-90)) + (y * cos(-90)));

int gx = sx + cx;
int gy = sy + cy;

if(gx <= car.width && gy <= car.height && gx >= 0 && gy >= 0)
Surface_Buffer = DD_Lock_Surface(lpddsrotate, &memory_pitch);
UCHAR pixel = Surface_Buffer[gx+gy*memory_pitch];
Draw_Pixel(a,b, pixel, Surface_Buffer, memory_pitch);
DD_Unlock_Surface(lpddsrotate, Surface_Buffer);
Surface_Buffer = DD_Lock_Surface(lpddsrotate, &memory_pitch);
Draw_Pixel(a,b, 0, Surface_Buffer, memory_pitch);
DD_Unlock_Surface(lpddsrotate, Surface_Buffer);

car.images[0] = lpddsrotate;

The bitmap gets screwed up. Anyone know why?

Share this post

Link to post
Share on other sites
According to the Window''s Guru''s book it says:

*NOTE* This is using DirectDraw7

"To perform a rotation operation on a bitmap, you must have hardware acceleration that supports it (very rare) and then set up a DDBLTFX structure, as follows:

DDBLTFX ddbltfx; // this holds our data

// initialize the structure


// set rotation angle, note that each unit is in 1/100

// of a degree rotation

ddbltfx.dwRotationAngle = angle; // each unit is

Then you make the call to BLT() as you normally would, but you add the flag DDBLT_ROTATIONANGLE to the flags parameter and add the ddbltfx paramater like this:

// blt to destination surface

if( FAILED( lpddsback->BLT( &dest_rect, lpdds_image, &source_rect, (DDBLT_WAIT | DDBLTKEYSRC | DDBLT_ROTATIONANGLE), &ddbltfx ) ) )
return 0;

hope it helps

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!