SDL  2.0
SDL_rotate.h File Reference
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define MIN(a, b)   (((a) < (b)) ? (a) : (b))
 

Functions

SDL_SurfaceSDLgfx_rotateSurface (SDL_Surface *src, double angle, int centerx, int centery, int smooth, int flipx, int flipy, int dstwidth, int dstheight, double cangle, double sangle)
 
void SDLgfx_rotozoomSurfaceSizeTrig (int width, int height, double angle, int *dstwidth, int *dstheight, double *cangle, double *sangle)
 

Macro Definition Documentation

◆ MIN

#define MIN (   a,
  b 
)    (((a) < (b)) ? (a) : (b))

Definition at line 26 of file SDL_rotate.h.

Function Documentation

◆ SDLgfx_rotateSurface()

SDL_Surface* SDLgfx_rotateSurface ( SDL_Surface src,
double  angle,
int  centerx,
int  centery,
int  smooth,
int  flipx,
int  flipy,
int  dstwidth,
int  dstheight,
double  cangle,
double  sangle 
)

Definition at line 415 of file SDL_rotate.c.

416 {
417  SDL_Surface *rz_dst;
418  int is8bit, angle90;
419  int i;
420  SDL_BlendMode blendmode;
421  Uint32 colorkey = 0;
422  int colorKeyAvailable = SDL_FALSE;
423  double sangleinv, cangleinv;
424 
425  /* Sanity check */
426  if (src == NULL)
427  return NULL;
428 
429  if (SDL_HasColorKey(src)) {
430  if (SDL_GetColorKey(src, &colorkey) == 0) {
431  colorKeyAvailable = SDL_TRUE;
432  }
433  }
434 
435  /* This function requires a 32-bit surface or 8-bit surface with a colorkey */
436  is8bit = src->format->BitsPerPixel == 8 && colorKeyAvailable;
437  if (!(is8bit || (src->format->BitsPerPixel == 32 && src->format->Amask)))
438  return NULL;
439 
440  /* Calculate target factors from sin/cos and zoom */
441  sangleinv = sangle*65536.0;
442  cangleinv = cangle*65536.0;
443 
444  /* Alloc space to completely contain the rotated surface */
445  rz_dst = NULL;
446  if (is8bit) {
447  /* Target surface is 8 bit */
448  rz_dst = SDL_CreateRGBSurface(0, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
449  if (rz_dst != NULL) {
450  for (i = 0; i < src->format->palette->ncolors; i++) {
451  rz_dst->format->palette->colors[i] = src->format->palette->colors[i];
452  }
453  rz_dst->format->palette->ncolors = src->format->palette->ncolors;
454  }
455  } else {
456  /* Target surface is 32 bit with source RGBA ordering */
457  rz_dst = SDL_CreateRGBSurface(0, dstwidth, dstheight + GUARD_ROWS, 32,
458  src->format->Rmask, src->format->Gmask,
459  src->format->Bmask, src->format->Amask);
460  }
461 
462  /* Check target */
463  if (rz_dst == NULL)
464  return NULL;
465 
466  /* Adjust for guard rows */
467  rz_dst->h = dstheight;
468 
469  SDL_GetSurfaceBlendMode(src, &blendmode);
470 
471  if (colorKeyAvailable == SDL_TRUE) {
472  /* If available, the colorkey will be used to discard the pixels that are outside of the rotated area. */
473  SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey);
474  SDL_FillRect(rz_dst, NULL, colorkey);
475  } else if (blendmode == SDL_BLENDMODE_NONE) {
476  blendmode = SDL_BLENDMODE_BLEND;
477  } else if (blendmode == SDL_BLENDMODE_MOD) {
478  /* Without a colorkey, the target texture has to be white for the MOD blend mode so
479  * that the pixels outside the rotated area don't affect the destination surface.
480  */
481  colorkey = SDL_MapRGBA(rz_dst->format, 255, 255, 255, 0);
482  SDL_FillRect(rz_dst, NULL, colorkey);
483  /* Setting a white colorkey for the destination surface makes the final blit discard
484  * all pixels outside of the rotated area. This doesn't interfere with anything because
485  * white pixels are already a no-op and the MOD blend mode does not interact with alpha.
486  */
487  SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey);
488  }
489 
490  SDL_SetSurfaceBlendMode(rz_dst, blendmode);
491 
492  /* Lock source surface */
493  if (SDL_MUSTLOCK(src)) {
495  }
496 
497  /* check if the rotation is a multiple of 90 degrees so we can take a fast path and also somewhat reduce
498  * the off-by-one problem in _transformSurfaceRGBA that expresses itself when the rotation is near
499  * multiples of 90 degrees.
500  */
501  angle90 = (int)(angle/90);
502  if (angle90 == angle/90) {
503  angle90 %= 4;
504  if (angle90 < 0) angle90 += 4; /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
505  } else {
506  angle90 = -1;
507  }
508 
509  if (is8bit) {
510  /* Call the 8-bit transformation routine to do the rotation */
511  if(angle90 >= 0) {
512  transformSurfaceY90(src, rz_dst, angle90, flipx, flipy);
513  } else {
514  transformSurfaceY(src, rz_dst, centerx, centery, (int)sangleinv, (int)cangleinv,
515  flipx, flipy);
516  }
517  } else {
518  /* Call the 32-bit transformation routine to do the rotation */
519  if (angle90 >= 0) {
520  transformSurfaceRGBA90(src, rz_dst, angle90, flipx, flipy);
521  } else {
522  _transformSurfaceRGBA(src, rz_dst, centerx, centery, (int)sangleinv, (int)cangleinv,
523  flipx, flipy, smooth);
524  }
525  }
526 
527  /* Unlock source surface */
528  if (SDL_MUSTLOCK(src)) {
530  }
531 
532  /* Return rotated surface */
533  return rz_dst;
534 }

References _transformSurfaceRGBA(), SDL_Palette::colors, SDL_Surface::format, GUARD_ROWS, SDL_Surface::h, i, SDL_Palette::ncolors, NULL, SDL_PixelFormat::palette, SDL_BLENDMODE_BLEND, SDL_BLENDMODE_MOD, SDL_BLENDMODE_NONE, SDL_CreateRGBSurface, SDL_FALSE, SDL_FillRect, SDL_GetColorKey, SDL_GetSurfaceBlendMode, SDL_HasColorKey, SDL_LockSurface, SDL_MapRGBA, SDL_MUSTLOCK, SDL_SetColorKey, SDL_SetSurfaceBlendMode, SDL_TRUE, SDL_UnlockSurface, transformSurfaceRGBA90(), transformSurfaceY(), and transformSurfaceY90().

Referenced by SW_RenderCopyEx().

◆ SDLgfx_rotozoomSurfaceSizeTrig()

void SDLgfx_rotozoomSurfaceSizeTrig ( int  width,
int  height,
double  angle,
int *  dstwidth,
int *  dstheight,
double *  cangle,
double *  sangle 
)

Definition at line 106 of file SDL_rotate.c.

109 {
110  /* The trig code below gets the wrong size (due to FP inaccuracy?) when angle is a multiple of 90 degrees */
111  int angle90 = (int)(angle/90);
112  if(angle90 == angle/90) { /* if the angle is a multiple of 90 degrees */
113  angle90 %= 4;
114  if(angle90 < 0) angle90 += 4; /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
115  if(angle90 & 1) {
116  *dstwidth = height;
117  *dstheight = width;
118  *cangle = 0;
119  *sangle = angle90 == 1 ? -1 : 1; /* reversed because our rotations are clockwise */
120  } else {
121  *dstwidth = width;
122  *dstheight = height;
123  *cangle = angle90 == 0 ? 1 : -1;
124  *sangle = 0;
125  }
126  } else {
127  double x, y, cx, cy, sx, sy;
128  double radangle;
129  int dstwidthhalf, dstheighthalf;
130  /*
131  * Determine destination width and height by rotating a centered source box
132  */
133  radangle = angle * (M_PI / -180.0); /* reverse the angle because our rotations are clockwise */
134  *sangle = SDL_sin(radangle);
135  *cangle = SDL_cos(radangle);
136  x = (double)(width / 2);
137  y = (double)(height / 2);
138  cx = *cangle * x;
139  cy = *cangle * y;
140  sx = *sangle * x;
141  sy = *sangle * y;
142 
143  dstwidthhalf = MAX((int)
144  SDL_ceil(MAX(MAX(MAX(SDL_fabs(cx + sy), SDL_fabs(cx - sy)), SDL_fabs(-cx + sy)), SDL_fabs(-cx - sy))), 1);
145  dstheighthalf = MAX((int)
146  SDL_ceil(MAX(MAX(MAX(SDL_fabs(sx + cy), SDL_fabs(sx - cy)), SDL_fabs(-sx + cy)), SDL_fabs(-sx - cy))), 1);
147  *dstwidth = 2 * dstwidthhalf;
148  *dstheight = 2 * dstheighthalf;
149  }
150 }

References MAX, SDL_ceil, SDL_cos, SDL_fabs, and SDL_sin.

Referenced by SW_RenderCopyEx().

SDL_UnlockSurface
#define SDL_UnlockSurface
Definition: SDL_dynapi_overrides.h:449
SDL_Palette::ncolors
int ncolors
Definition: SDL_pixels.h:306
SDL_MapRGBA
#define SDL_MapRGBA
Definition: SDL_dynapi_overrides.h:287
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:70
SDL_fabs
#define SDL_fabs
Definition: SDL_dynapi_overrides.h:430
SDL_ceil
#define SDL_ceil
Definition: SDL_dynapi_overrides.h:426
NULL
#define NULL
Definition: begin_code.h:164
width
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
SDL_BLENDMODE_BLEND
@ SDL_BLENDMODE_BLEND
Definition: SDL_blendmode.h:44
MAX
#define MAX(a, b)
Definition: SDL_rotate.c:65
transformSurfaceRGBA90
static void transformSurfaceRGBA90(SDL_Surface *src, SDL_Surface *dst, int angle, int flipx, int flipy)
Definition: SDL_rotate.c:197
SDL_MUSTLOCK
#define SDL_MUSTLOCK(S)
Definition: SDL_surface.h:61
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
SDL_GetSurfaceBlendMode
#define SDL_GetSurfaceBlendMode
Definition: SDL_dynapi_overrides.h:460
GUARD_ROWS
#define GUARD_ROWS
Definition: SDL_rotate.c:77
SDL_Palette::colors
SDL_Color * colors
Definition: SDL_pixels.h:307
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
SDL_BLENDMODE_NONE
@ SDL_BLENDMODE_NONE
Definition: SDL_blendmode.h:42
height
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
SDL_cos
#define SDL_cos
Definition: SDL_dynapi_overrides.h:428
_transformSurfaceRGBA
static void _transformSurfaceRGBA(SDL_Surface *src, SDL_Surface *dst, int cx, int cy, int isin, int icos, int flipx, int flipy, int smooth)
Definition: SDL_rotate.c:230
SDL_PixelFormat::palette
SDL_Palette * palette
Definition: SDL_pixels.h:318
SDL_SetColorKey
#define SDL_SetColorKey
Definition: SDL_dynapi_overrides.h:453
SDL_GetColorKey
#define SDL_GetColorKey
Definition: SDL_dynapi_overrides.h:454
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
transformSurfaceY
static void transformSurfaceY(SDL_Surface *src, SDL_Surface *dst, int cx, int cy, int isin, int icos, int flipx, int flipy)
Definition: SDL_rotate.c:344
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
SDL_LockSurface
#define SDL_LockSurface
Definition: SDL_dynapi_overrides.h:448
SDL_CreateRGBSurface
#define SDL_CreateRGBSurface
Definition: SDL_dynapi_overrides.h:444
SDL_Surface::h
int h
Definition: SDL_surface.h:73
transformSurfaceY90
static void transformSurfaceY90(SDL_Surface *src, SDL_Surface *dst, int angle, int flipx, int flipy)
Definition: SDL_rotate.c:203
src
GLenum src
Definition: SDL_opengl_glext.h:1737
SDL_SetSurfaceBlendMode
#define SDL_SetSurfaceBlendMode
Definition: SDL_dynapi_overrides.h:459
angle
GLfloat angle
Definition: SDL_opengl_glext.h:6097
SDL_FillRect
#define SDL_FillRect
Definition: SDL_dynapi_overrides.h:466
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
SDL_sin
#define SDL_sin
Definition: SDL_dynapi_overrides.h:435
SDL_Surface::format
SDL_PixelFormat * format
Definition: SDL_surface.h:72
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_BLENDMODE_MOD
@ SDL_BLENDMODE_MOD
Definition: SDL_blendmode.h:50
SDL_HasColorKey
#define SDL_HasColorKey
Definition: SDL_dynapi_overrides.h:699
SDL_BlendMode
SDL_BlendMode
The blend mode used in SDL_RenderCopy() and drawing operations.
Definition: SDL_blendmode.h:41