• Advertisement
Sign in to follow this  

Reflect an image with SDL?

This topic is 3671 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

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
Advertisement
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
Sign in to follow this  

  • Advertisement