PDA

Ver la Versión Completa : rotar x grados un TBitmap


aguml
20-03-2015, 16:25:00
hola amigos, estoy intentando hacer un pequeño juego que muestre la hora analogica y pida que la metas en digital y si está bien o mal suene un sonido u otro y una señal visual. Lo tengo encarrilado pero con 12 minuteros cada uno girado en el bmp para cada hora y otros 12 horeros. La duda es que me gustaria saber como hacer lo mismo pero en vez de tener 12 y 12 imagenes solo tener 1 y 1 y rotar la imagen los grados necesarios . La imagen la cargo en un TBitmap y no se como rotarla ¿Me pueden ayudar?

ecfisa
20-03-2015, 18:56:36
Hola aguml.


#include <math>

void RotateBMP(Graphics::TBitmap *bmp, float rads, bool adjSize, TColor bkColor = clNone) {
float C,S;
Graphics::TBitmap *tmp = new Graphics::TBitmap;
int ofsX, ofsY;
TPoint pts[2];

C = cos(rads);
S = sin(rads);
tmp->TransparentColor = bmp->TransparentColor;
tmp->TransparentMode = bmp->TransparentMode;
tmp->Canvas->Brush->Color = bkColor;

if(adjSize) {
tmp->Width = (int)bmp->Width * abs(C) + bmp->Height * abs(S);
tmp->Height = (int)bmp->Width * abs(S) + bmp->Height * abs(C);
ofsX = (tmp->Width - bmp->Width * C + bmp->Height * S) / 2.0;
ofsY = (tmp->Height - bmp->Width * S - bmp->Height * C) / 2.0;
} else {
tmp->Width = bmp->Width;
tmp->Height = bmp->Height;
ofsX = (bmp->Width - bmp->Width * C + bmp->Height * S) / 2.0;
ofsY = (bmp->Height - bmp->Width * S - bmp->Height * C) / 2.0;
}
pts[0].x = floor(ofsX);
pts[0].y = floor(ofsY);
pts[1].x = floor(ofsX + bmp->Width * C);
pts[1].y = floor(ofsY + bmp->Width * S);
pts[2].x = floor(ofsX - bmp->Height * S);
pts[2].y = floor(ofsY + bmp->Height * C);
PlgBlt(tmp->Canvas->Handle, pts, bmp->Canvas->Handle, 0, 0,
bmp->Width, bmp->Height, 0, 0, 0);

bmp->Assign(tmp);
delete tmp;
}


Ejemplo de uso:

void __fastcall TForm1::btnRotateClick(TObject *Sender)
{
Graphics::TBitmap *bmp = new Graphics::TBitmap;
bmp->Assign(Image1->Picture->Bitmap);
RotateBMP(bmp, 0.5, false);
Image2->Picture->Bitmap->Assign(bmp);
delete bmp;
}


Salida:

http://s23.postimg.org/wsmzcnvkr/rotate.png

Saludos :)

aguml
20-03-2015, 22:34:14
mil gracias amigo!!! Mañana lo pruebo que acabo de llegar a casa y he pillado un catarron...

aguml
21-03-2015, 19:49:38
Gracias amigo, me vino genial y lo hice de las dos maneras, tirando de imagenes rotadas y con la funcion que me pusiste.