Difference between revisions of "Vertically invert a surface in SDL"
Jump to navigation
Jump to search
| Line 8: | Line 8: | ||
Uint8 *a, *b; | Uint8 *a, *b; | ||
Uint16 pitch; | Uint16 pitch; | ||
| + | |||
| + | if( SDL_MUSTLOCK(surface) ) { | ||
| + | if( SDL_LockSurface(surface) ) { | ||
| + | return -2; | ||
| + | } | ||
| + | } | ||
pitch = surface->pitch * sizeof(Uint8); | pitch = surface->pitch * sizeof(Uint8); | ||
| Line 20: | Line 26: | ||
a = (Uint8*)surface->pixels; | a = (Uint8*)surface->pixels; | ||
b = a + pitch * (surface->h - 1); | b = a + pitch * (surface->h - 1); | ||
| + | |||
while( a < b ) { | while( a < b ) { | ||
| + | /* memcpy(dest, src, len) */ | ||
memcpy(line, b, pitch); | memcpy(line, b, pitch); | ||
memcpy(b, a, pitch); | memcpy(b, a, pitch); | ||
| Line 31: | Line 39: | ||
free(line); | free(line); | ||
| + | |||
| + | if( SDL_MUSTLOCK(surface) ) { | ||
| + | SDL_UnlockSurface(surface); | ||
| + | } | ||
return 1; | return 1; | ||
} | } | ||
</nowiki> | </nowiki> | ||
Revision as of 17:52, 30 May 2005
This code flips an SDL_Surface upside-down by using a buffer the size of one line of the image to swap the first and last successively until the middle is reached.
int invert_surface_vertical(SDL_Surface *surface)
{
/* buffer big enough for one line of this image */
Uint8 *line;
Uint8 *a, *b;
Uint16 pitch;
if( SDL_MUSTLOCK(surface) ) {
if( SDL_LockSurface(surface) ) {
return -2;
}
}
pitch = surface->pitch * sizeof(Uint8);
line = (Uint8*)malloc(pitch);
if( line == NULL )
return -1;
/* let us begin swapping the first and last lines
of an ever-narrowing area of the image */
a = (Uint8*)surface->pixels;
b = a + pitch * (surface->h - 1);
while( a < b ) {
/* memcpy(dest, src, len) */
memcpy(line, b, pitch);
memcpy(b, a, pitch);
memcpy(a, line, pitch);
a += pitch;
b -= pitch;
}
free(line);
if( SDL_MUSTLOCK(surface) ) {
SDL_UnlockSurface(surface);
}
return 1;
}