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 23 of file SDL_rotate.h.

Referenced by SW_RenderCopyEx().

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 413 of file SDL_rotate.c.

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

Referenced by SW_RenderCopyEx().

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

◆ SDLgfx_rotozoomSurfaceSizeTrig()

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

Definition at line 104 of file SDL_rotate.c.

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

Referenced by SW_RenderCopyEx().

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