//Improved, almost fixed. However the rotated image now has a speckled black dots spaced in a grid-like pattern on it. I think the "double" is "rounding out" some of the values.
SDL_Surface* Surface::RotateSurface(SDL_Surface* Surface, double Angle) {
if(!Surface) return 0;
SDL_Surface* _ret = SDL_CreateRGBSurface(Surface->flags, Surface->w, Surface->h, Surface->format->BitsPerPixel,
Surface->format->Rmask, Surface->format->Gmask, Surface->format->Bmask, Surface->format->Amask);
double CX = Surface->w / 2, CY = Surface->h / 2; //Center coordinates of image, or close enough.
double X, Y, X2, Y2;
for(Uint32 y = 0; y < Surface->h; y++) {
for(Uint32 x = 0; x < Surface->w; x++) {
X = (double)x - CX;
Y = (double)y - CY;
X2 = (X * cos(Angle) - Y * sin(Angle));
Y2 = (X * sin(Angle) + Y * cos(Angle));
X2 += CX;
Y2 += CY;
if( (int)X2 >= (int)Surface->w || (int)X2 < 0 || (int)Y2 >= Surface->h || (int)Y2 < 0) SetPixel32(_ret, x, y, SDL_MapRGB(Surface->format, 255, 0, 255));
X = (double)x - CX;
Y = (double)y - CY;
X2 = (X * cos(Angle) + Y * sin(Angle));
Y2 = (-X * sin(Angle) + Y * cos(Angle));
X2 += CX;
Y2 += CY;
if( (int)X2 >= 0 && (int)X2 < Surface->w && (int)Y2 >= 0 && (int)Y2 < Surface->h) SetPixel32(_ret, (Uint32)X2, (Uint32)Y2, GetPixel32(Surface, x, y));
}
}
return _ret;
} |
Uint32 Surface::GetPixel32(SDL_Surface* Surface, int X, int Y) {
SDL_LockSurface(Surface);
Uint32* Pixels = (Uint32 *)Surface->pixels; //Convert pixel to Uint32 Pointer
SDL_UnlockSurface(Surface);
return Pixels[Y * (Surface->pitch / 4) + X]; //Returns Uint32 color of pixel at x, y;
}
void Surface::SetPixel32(SDL_Surface* Surface, int X, int Y, Uint32 Color) {
SDL_LockSurface(Surface);
Uint32* Pixels = (Uint32 *)Surface->pixels; //Converts pixels to Uint32
Pixels[ Y * (Surface->pitch / 4) + X ] = Color; //Sets pixel color
SDL_UnlockSurface(Surface);
} |
SDL_Surface* Surface::ScaleSurface(SDL_Surface* Surface, Uint32 Width, Uint32 Height)
{
if(!Surface || !Width || !Height)
return 0;
SDL_Surface* _ret = SDL_CreateRGBSurface(Surface->flags, Width, Height, Surface->format->BitsPerPixel,
Surface->format->Rmask, Surface->format->Gmask, Surface->format->Bmask, Surface->format->Amask);
double _stretch_factor_x = (double)(Width) / (double)(Surface->w);
double _stretch_factor_y = (double)(Height) / (double)(Surface->h);
for(Uint32 y = 0; y < Surface->h; y++) {
for(Uint32 x = 0; x < Surface->w; x++) {
for(Uint32 o_y = 0; o_y < _stretch_factor_y; ++o_y) {
for(Uint32 o_x = 0; o_x < _stretch_factor_x; ++o_x) {
SetPixel32(_ret, (Uint32)(_stretch_factor_x * x) + o_x,
(Uint32)(_stretch_factor_y * y) + o_y, GetPixel32(Surface, x, y));
}
}
}
}
return _ret;
}
|
double CX = Surface->pitch / 2double CX = Surface->w / 2(Uint32)X < 0 can never be anything other than false because (Uint32)X is an unsigned integer.
for(Uint32 y = 0; y < Surface->h; y++) {
for(Uint32 x = 0; x < Surface->w; x++) {
X = (double)x - CX;
Y = (double)y - CY;
X2 = (X * cos(Angle) - Y * sin(Angle));
Y2 = (X * sin(Angle) + Y * cos(Angle));
X2 += CX;
Y2 += CY;
if( (int)X2 >= (int)Surface->w || (int)X2 < 0 || (int)Y2 >= Surface->h || (int)Y2 < 0) SetPixel32(_ret, x, y, SDL_MapRGB(Surface->format, 255, 0, 255));
else SetPixel32(_ret, x, y, GetPixel32(Surface, X2, Y2));
}
} |