Reflect an image with SDL?

Started by
2 comments, last by Aztral 16 years, 2 months ago
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.
Advertisement
The simple answer is to use a library like SDL_gfx.
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;}
Thanks to both of you. Very much =).

This topic is closed to new replies.

Advertisement