Sign in to follow this  
Aztral

Reflect an image with SDL?

Recommended Posts

Frankly, I'm not sure if I should be posting this here or in the beginners forum, but I thought I'd just pick one as it kind of applies to both. I'm just wondering if there is any way to reflect an image over it's y-axis using SDL? Thanks and sorry if I'm posting in the wrong place.

Share this post


Link to post
Share on other sites
I have some code here that does what you want. I ripped it out of a larger project and modified it to make it more general, but I haven't tested it, so use at your own risk :)


void flipHorizontally( SDL_Surface*& image )
{
// error checking
if(!image)
return;

// create a copy of the image
SDL_Surface* flipped_image = SDL_CreateRGBSurface( SDL_SWSURFACE, image->w, image->h, image->format->BitsPerPixel,
image->format->Rmask, image->format->Gmask, image->format->Bmask, image->format->Amask );

// loop through pixels
for( int y=0; y<image->h; y++ )
{
for( int x=0; x<image->w; x++ )
{
// copy pixels, but reverse the x pixels!
putpixel( flipped_image, x, y, getpixel(image, image->w - x - 1, y) );
}
}

// free original and assign flipped to it
SDL_FreeSurface( image );
image = flipped_image;
}

void flipVertically( SDL_Surface*& image )
{
// error checking
if(!image)
return;

// create a copy of the image
SDL_Surface* flipped_image = SDL_CreateRGBSurface( SDL_SWSURFACE, image->w, image->h, image->format->BitsPerPixel,
image->format->Rmask, image->format->Gmask, image->format->Bmask, image->format->Amask );

// loop through pixels
for( int y=0; y<image->h; y++ )
{
for( int x=0; x<image->w; x++ )
{
// copy pixels, but reverse the y pixels!
putpixel( flipped_image, x, y, getpixel(image, x, image->h - y - 1) );
}
}

// free original and assign flipped to it
SDL_FreeSurface( image );
image = flipped_image;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this