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

References _colorkey(), _transformSurfaceRGBA(), SDL_PixelFormat::Amask, SDL_PixelFormat::BitsPerPixel, blendMode, SDL_PixelFormat::Bmask, SDL_Palette::colors, SDL_Surface::flags, SDL_Surface::format, SDL_PixelFormat::Gmask, GUARD_ROWS, SDL_Surface::h, i, SDL_Palette::ncolors, NULL, SDL_PixelFormat::palette, SDL_PixelFormat::Rmask, SDL_ConvertSurfaceFormat, SDL_CreateRGBSurface, SDL_FillRect, SDL_FreeSurface, SDL_GetRGB, SDL_GetSurfaceAlphaMod, SDL_GetSurfaceBlendMode, SDL_GetSurfaceColorMod, SDL_LockSurface, SDL_MapRGB, SDL_MUSTLOCK, SDL_PIXELFORMAT_ARGB32, SDL_RLEACCEL, SDL_SetColorKey, SDL_SetSurfaceAlphaMod, SDL_SetSurfaceBlendMode, SDL_SetSurfaceColorMod, SDL_SWSURFACE, SDL_TRUE, SDL_UnlockSurface, transformSurfaceRGBA90(), transformSurfaceY(), and transformSurfaceY90().

Referenced by SW_RenderCopyEx().

415 {
416  SDL_Surface *rz_src;
417  SDL_Surface *rz_dst;
418  int is32bit, angle90;
419  int i;
420  Uint8 r = 0, g = 0, b = 0;
421  Uint32 colorkey = 0;
422  int colorKeyAvailable = 0;
423  double sangleinv, cangleinv;
424 
425  /*
426  * Sanity check
427  */
428  if (src == NULL)
429  return (NULL);
430 
431  if (src->flags & SDL_TRUE/* SDL_SRCCOLORKEY */)
432  {
433  colorkey = _colorkey(src);
434  SDL_GetRGB(colorkey, src->format, &r, &g, &b);
435  colorKeyAvailable = 1;
436  }
437  /*
438  * Determine if source surface is 32bit or 8bit
439  */
440  is32bit = (src->format->BitsPerPixel == 32);
441  if ((is32bit) || (src->format->BitsPerPixel == 8)) {
442  /*
443  * Use source surface 'as is'
444  */
445  rz_src = src;
446  } else {
448  if (rz_src == NULL) {
449  return NULL;
450  }
451  is32bit = 1;
452  }
453 
454  /* Determine target size */
455  /* _rotozoomSurfaceSizeTrig(rz_src->w, rz_src->h, angle, &dstwidth, &dstheight, &cangle, &sangle); */
456 
457  /*
458  * Calculate target factors from sin/cos and zoom
459  */
460  sangleinv = sangle*65536.0;
461  cangleinv = cangle*65536.0;
462 
463  /*
464  * Alloc space to completely contain the rotated surface
465  */
466  if (is32bit) {
467  /*
468  * Target surface is 32bit with source RGBA/ABGR ordering
469  */
470  rz_dst =
471  SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 32,
472  rz_src->format->Rmask, rz_src->format->Gmask,
473  rz_src->format->Bmask, rz_src->format->Amask);
474  } else {
475  /*
476  * Target surface is 8bit
477  */
478  rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
479  }
480 
481  /* Check target */
482  if (rz_dst == NULL)
483  return NULL;
484 
485  /* Adjust for guard rows */
486  rz_dst->h = dstheight;
487 
488  if (colorKeyAvailable == 1){
489  colorkey = SDL_MapRGB(rz_dst->format, r, g, b);
490 
491  SDL_FillRect(rz_dst, NULL, colorkey );
492  }
493 
494  /*
495  * Lock source surface
496  */
497  if (SDL_MUSTLOCK(rz_src)) {
498  SDL_LockSurface(rz_src);
499  }
500 
501  /* check if the rotation is a multiple of 90 degrees so we can take a fast path and also somewhat reduce
502  * the off-by-one problem in _transformSurfaceRGBA that expresses itself when the rotation is near
503  * multiples of 90 degrees.
504  */
505  angle90 = (int)(angle/90);
506  if (angle90 == angle/90) {
507  angle90 %= 4;
508  if (angle90 < 0) angle90 += 4; /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
509  } else {
510  angle90 = -1;
511  }
512 
513  /*
514  * Check which kind of surface we have
515  */
516  if (is32bit) {
517  /*
518  * Call the 32bit transformation routine to do the rotation (using alpha)
519  */
520  if (angle90 >= 0) {
521  transformSurfaceRGBA90(rz_src, rz_dst, angle90, flipx, flipy);
522  } else {
523  _transformSurfaceRGBA(rz_src, rz_dst, centerx, centery, (int) (sangleinv), (int) (cangleinv), flipx, flipy, smooth);
524  }
525  /*
526  * Turn on source-alpha support
527  */
528  /* SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255); */
529  SDL_SetColorKey(rz_dst, /* SDL_SRCCOLORKEY */ SDL_TRUE | SDL_RLEACCEL, _colorkey(rz_src));
530  } else {
531  /*
532  * Copy palette and colorkey info
533  */
534  for (i = 0; i < rz_src->format->palette->ncolors; i++) {
535  rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
536  }
537  rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;
538  /*
539  * Call the 8bit transformation routine to do the rotation
540  */
541  if(angle90 >= 0) {
542  transformSurfaceY90(rz_src, rz_dst, angle90, flipx, flipy);
543  } else {
544  transformSurfaceY(rz_src, rz_dst, centerx, centery, (int)(sangleinv), (int)(cangleinv), flipx, flipy);
545  }
546  SDL_SetColorKey(rz_dst, /* SDL_SRCCOLORKEY */ SDL_TRUE | SDL_RLEACCEL, _colorkey(rz_src));
547  }
548 
549  /* copy alpha mod, color mod, and blend mode */
550  {
552  Uint8 alphaMod, cr, cg, cb;
553  SDL_GetSurfaceAlphaMod(src, &alphaMod);
554  SDL_GetSurfaceBlendMode(src, &blendMode);
555  SDL_GetSurfaceColorMod(src, &cr, &cg, &cb);
556  SDL_SetSurfaceAlphaMod(rz_dst, alphaMod);
557  SDL_SetSurfaceBlendMode(rz_dst, blendMode);
558  SDL_SetSurfaceColorMod(rz_dst, cr, cg, cb);
559  }
560 
561  /*
562  * Unlock source surface
563  */
564  if (SDL_MUSTLOCK(rz_src)) {
565  SDL_UnlockSurface(rz_src);
566  }
567 
568  /*
569  * Cleanup temp surface
570  */
571  if (rz_src != src) {
572  SDL_FreeSurface(rz_src);
573  }
574 
575  /*
576  * Return destination surface
577  */
578  return (rz_dst);
579 }
#define SDL_GetRGB
#define SDL_ConvertSurfaceFormat
#define SDL_UnlockSurface
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2072
#define SDL_SWSURFACE
Definition: SDL_surface.h:52
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:200
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:159
Uint32 flags
Definition: SDL_surface.h:71
#define SDL_GetSurfaceBlendMode
static SDL_BlendMode blendMode
Definition: testdraw2.c:34
GLboolean GLboolean g
#define SDL_FreeSurface
#define SDL_SetSurfaceColorMod
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
Uint8 BitsPerPixel
Definition: SDL_pixels.h:317
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:347
#define SDL_SetColorKey
static void transformSurfaceY90(SDL_Surface *src, SDL_Surface *dst, int angle, int flipx, int flipy)
Definition: SDL_rotate.c:206
#define SDL_GetSurfaceAlphaMod
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:143
SDL_Color * colors
Definition: SDL_pixels.h:305
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_LockSurface
#define SDL_CreateRGBSurface
#define SDL_GetSurfaceColorMod
#define SDL_MUSTLOCK(S)
Definition: SDL_surface.h:61
#define SDL_SetSurfaceBlendMode
#define SDL_FillRect
#define SDL_MapRGB
GLfloat angle
static Uint32 _colorkey(SDL_Surface *src)
Definition: SDL_rotate.c:88
SDL_Palette * palette
Definition: SDL_pixels.h:316
GLenum src
GLboolean GLboolean GLboolean b
#define SDL_SetSurfaceAlphaMod
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
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:233

◆ SDLgfx_rotozoomSurfaceSizeTrig()

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

Definition at line 109 of file SDL_rotate.c.

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

Referenced by SW_RenderCopyEx().

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