EnttPong/src/libs/SDL2_gfx/SDL2_rotozoom.c
/*!
\brief Rotates a 8/16/24/32 bit surface in increments of 90 degrees.

Specialized 90 degree rotator which rotates a 'src' surface in 90 degree 
increments clockwise returning a new surface. Faster than rotozoomer since
no scanning or interpolation takes place. Input surface must be 8/16/24/32 bit.
(code contributed by J. Schiller, improved by C. Allport and A. Schiffler)

\param src Source surface to rotate.
\param numClockwiseTurns Number of clockwise 90 degree turns to apply to the source.

\returns The new, rotated surface; or NULL for surfaces with incorrect input format.
*/
\brief Rotates a 8/16/24/32 bit surface in increments of 90 degrees.
Specialized 90 degree rotator which rotates a 'src' surface in 90 degree
increments clockwise returning a new surface. Faster than rotozoomer since
no scanning or interpolation takes place. Input surface must be 8/16/24/32 bit.
(code contributed by J. Schiller, improved by C. Allport and A. Schiffler)

\param src Source surface to rotate.
\param numClockwiseTurns Number of clockwise 90 degree turns to apply to the source.

\returns The new, rotated surface; or NULL for surfaces with incorrect input format.
\ brief Rotates a 8 / 16 / 24 / 32 bit surface in increments of 90 degrees . Specialized 90 degree rotator which rotates a ' src ' surface in 90 degree increments clockwise returning a new surface . Faster than rotozoomer since no scanning or interpolation takes place . Input surface must be 8 / 16 / 24 / 32 bit . ( code contributed by J . Schiller improved by C . Allport and A . Schiffler ) \ param src Source surface to rotate . \ param numClockwiseTurns Number of clockwise 90 degree turns to apply to the source . \ returns The new rotated surface ; or NULL for surfaces with incorrect input format .
SDL_Surface* rotateSurface90Degrees(SDL_Surface* src, int numClockwiseTurns) 
{
	int row, col, newWidth, newHeight;
	int bpp, bpr;
	SDL_Surface* dst;
	Uint8* srcBuf;
	Uint8* dstBuf;
	int normalizedClockwiseTurns;
	if (!src || 
	    !src->format) {
		SDL_SetError("NULL source surface or source surface format");
	    return NULL; 
	}
	if ((src->format->BitsPerPixel % 8) != 0) {
		SDL_SetError("Invalid source surface bit depth");
	    return NULL; 
	}
	normalizedClockwiseTurns = (numClockwiseTurns % 4);
	if (normalizedClockwiseTurns < 0) {
		normalizedClockwiseTurns += 4;
	}
	if (normalizedClockwiseTurns % 2) {
		newWidth = src->h;
		newHeight = src->w;
	} else {
		newWidth = src->w;
		newHeight = src->h;
	}
	dst = SDL_CreateRGBSurface( src->flags, newWidth, newHeight, src->format->BitsPerPixel,
		src->format->Rmask,
		src->format->Gmask, 
		src->format->Bmask, 
		src->format->Amask);
	if(!dst) {
		SDL_SetError("Could not create destination surface"); 
		return NULL;
	}
	if (SDL_MUSTLOCK(src)) {
		SDL_LockSurface(src);
	}
	if (SDL_MUSTLOCK(dst)) {
		SDL_LockSurface(dst);
	}
	bpp = src->format->BitsPerPixel / 8;
	switch(normalizedClockwiseTurns) {
	case 0: 
		{
			if (src->pitch == dst->pitch) {
				memcpy(dst->pixels, src->pixels, (src->h * src->pitch));
			}
			else
			{
				srcBuf = (Uint8*)(src->pixels);
				dstBuf = (Uint8*)(dst->pixels);
				bpr = src->w * bpp;
				for (row = 0; row < src->h; row++) {
					memcpy(dstBuf, srcBuf, bpr);
					srcBuf += src->pitch;
					dstBuf += dst->pitch;
				}
			}
		}
		break;
	case 1: 
		{
			for (row = 0; row < src->h; ++row) {
				srcBuf = (Uint8*)(src->pixels) + (row * src->pitch);
				dstBuf = (Uint8*)(dst->pixels) + (dst->w - row - 1) * bpp;
				for (col = 0; col < src->w; ++col) {
					memcpy (dstBuf, srcBuf, bpp);
					srcBuf += bpp;
					dstBuf += dst->pitch;
				} 
			} 
		}
		break;
	case 2: 
		{
			for (row = 0; row < src->h; ++row) {
				srcBuf = (Uint8*)(src->pixels) + (row * src->pitch);
				dstBuf = (Uint8*)(dst->pixels) + ((dst->h - row - 1) * dst->pitch) + (dst->w - 1) * bpp;
				for (col = 0; col < src->w; ++col) {
					memcpy (dstBuf, srcBuf, bpp);
					srcBuf += bpp;
					dstBuf -= bpp;
				} 
			} 
		}
		break;
	case 3: 
		{
			for (row = 0; row < src->h; ++row) {
				srcBuf = (Uint8*)(src->pixels) + (row * src->pitch);
				dstBuf = (Uint8*)(dst->pixels) + (row * bpp) + ((dst->h - 1) * dst->pitch);
				for (col = 0; col < src->w; ++col) {
					memcpy (dstBuf, srcBuf, bpp);
					srcBuf += bpp;
					dstBuf -= dst->pitch;
				} 
			} 
		}
		break;
	} 
	if (SDL_MUSTLOCK(src)) {
		SDL_UnlockSurface(src);
	}
	if (SDL_MUSTLOCK(dst)) {
		SDL_UnlockSurface(dst);
	}
	return dst;
}
SDL_Surface * rotateSurface90Degrees ( SDL_Surface * src , int numClockwiseTurns ) { int row , col , newWidth , newHeight ; int bpp , bpr ; SDL_Surface * dst ; Uint8 * srcBuf ; Uint8 * dstBuf ; int normalizedClockwiseTurns ; if ( ! src || ! src -> format ) { SDL_SetError ( " " ) ; return NULL ; } if ( ( src -> format -> BitsPerPixel % 8 ) != 0 ) { SDL_SetError ( " " ) ; return NULL ; } normalizedClockwiseTurns = ( numClockwiseTurns % 4 ) ; if ( normalizedClockwiseTurns < 0 ) { normalizedClockwiseTurns += 4 ; } if ( normalizedClockwiseTurns % 2 ) { newWidth = src -> h ; newHeight = src -> w ; } else { newWidth = src -> w ; newHeight = src -> h ; } dst = SDL_CreateRGBSurface ( src -> flags , newWidth , newHeight , src -> format -> BitsPerPixel , src -> format -> Rmask , src -> format -> Gmask , src -> format -> Bmask , src -> format -> Amask ) ; if ( ! dst ) { SDL_SetError ( " " ) ; return NULL ; } if ( SDL_MUSTLOCK ( src ) ) { SDL_LockSurface ( src ) ; } if ( SDL_MUSTLOCK ( dst ) ) { SDL_LockSurface ( dst ) ; } bpp = src -> format -> BitsPerPixel / 8 ; switch ( normalizedClockwiseTurns ) { case 0 : { if ( src -> pitch == dst -> pitch ) { memcpy ( dst -> pixels , src -> pixels , ( src -> h * src -> pitch ) ) ; } else { srcBuf = ( Uint8 * ) ( src -> pixels ) ; dstBuf = ( Uint8 * ) ( dst -> pixels ) ; bpr = src -> w * bpp ; for ( row = 0 ; row < src -> h ; row ++ ) { memcpy ( dstBuf , srcBuf , bpr ) ; srcBuf += src -> pitch ; dstBuf += dst -> pitch ; } } } break ; case 1 : { for ( row = 0 ; row < src -> h ; ++ row ) { srcBuf = ( Uint8 * ) ( src -> pixels ) + ( row * src -> pitch ) ; dstBuf = ( Uint8 * ) ( dst -> pixels ) + ( dst -> w - row - 1 ) * bpp ; for ( col = 0 ; col < src -> w ; ++ col ) { memcpy ( dstBuf , srcBuf , bpp ) ; srcBuf += bpp ; dstBuf += dst -> pitch ; } } } break ; case 2 : { for ( row = 0 ; row < src -> h ; ++ row ) { srcBuf = ( Uint8 * ) ( src -> pixels ) + ( row * src -> pitch ) ; dstBuf = ( Uint8 * ) ( dst -> pixels ) + ( ( dst -> h - row - 1 ) * dst -> pitch ) + ( dst -> w - 1 ) * bpp ; for ( col = 0 ; col < src -> w ; ++ col ) { memcpy ( dstBuf , srcBuf , bpp ) ; srcBuf += bpp ; dstBuf -= bpp ; } } } break ; case 3 : { for ( row = 0 ; row < src -> h ; ++ row ) { srcBuf = ( Uint8 * ) ( src -> pixels ) + ( row * src -> pitch ) ; dstBuf = ( Uint8 * ) ( dst -> pixels ) + ( row * bpp ) + ( ( dst -> h - 1 ) * dst -> pitch ) ; for ( col = 0 ; col < src -> w ; ++ col ) { memcpy ( dstBuf , srcBuf , bpp ) ; srcBuf += bpp ; dstBuf -= dst -> pitch ; } } } break ; } if ( SDL_MUSTLOCK ( src ) ) { SDL_UnlockSurface ( src ) ; } if ( SDL_MUSTLOCK ( dst ) ) { SDL_UnlockSurface ( dst ) ; } return dst ; }
\brief Rotates a 8/16/24/32 bit surface in increments of 90 degrees.
\ brief Rotates a 8 / 16 / 24 / 32 bit surface in increments of 90 degrees .
/* Has to be a valid surface pointer and be a Nbit surface where n is divisible by 8 */ /* normalize numClockwiseTurns */ /* If turns are even, our new width/height will be the same as the source surface */ /* Calculate byte-per-pixel */ /* Make a copy of the surface */ /* Unfortunately SDL_BlitSurface cannot be used to make a copy of the surface
			since it does not preserve alpha. */ /* If the pitch is the same for both surfaces, the memory can be copied all at once. */ /* If the pitch differs, copy each row separately */ /* rotate clockwise */ /* rotated 90 degrees clockwise */ /* rotated 180 degrees clockwise */ /* rotated 270 degrees clockwise */ /* end switch */