Reflect an image with SDL?
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.
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;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement