# Reflect an image with SDL?

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

## 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 on other sites
The simple answer is to use a library like SDL_gfx.

##### 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 on other sites
Thanks to both of you. Very much =).

• 37
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631360
• Total Posts
2999556
×