Advertisement
matrefeytontias

Custom rotosprite

May 17th, 2014
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. typedef int Fixed;
  2.  
  3. // Fixed
  4. // 24.8 fixed point
  5. #define itofix(x) ((x) << 8)
  6. #define fixtoi(x) ((x) / 256)
  7. #define fixmul(x, y) ((x) * (y) / 256)
  8. #define fixdiv(x, y) (((x) << 8) / (y))
  9.  
  10. #define fixsin(x) fixcos((x) - 64)
  11.  
  12. Fixed fixcos(Fixed angle)
  13. {
  14.     static Fixed cosLUT[] = { 256, 255, 255, 255, 254, 254, 253, 252, 251, 249, 248, 246, 244, 243, 241, 238, 236, 234, 231, 228, 225, 222, 219, 216, 212, 209, 205, 201, 197, 193, 189, 185, 181, 176, 171, 167, 162, 157, 152, 147, 142, 136, 131, 126, 120, 115, 109, 103, 97, 92, 86, 80, 74, 68, 62, 56, 49, 43, 37, 31, 25, 18, 12, 6, 0, -6, -12, -18, -25, -31, -37, -43, -49, -56, -62, -68, -74, -80, -86, -92, -97, -103, -109, -115, -120, -126, -131, -136, -142, -147, -152, -157, -162, -167, -171, -176, -181, -185, -189, -193, -197, -201, -205, -209, -212, -216, -219, -222, -225, -228, -231, -234, -236, -238, -241, -243, -244, -246, -248, -249, -251, -252, -253, -254, -254, -255, -255, -255, -256, -255, -255, -255, -254, -254, -253, -252, -251, -249, -248, -246, -244, -243, -241, -238, -236, -234, -231, -228, -225, -222, -219, -216, -212, -209, -205, -201, -197, -193, -189, -185, -181, -176, -171, -167, -162, -157, -152, -147, -142, -136, -131, -126, -120, -115, -109, -103, -97, -92, -86, -80, -74, -68, -62, -56, -49, -43, -37, -31, -25, -18, -12, -6, 0, 6, 12, 18, 25, 31, 37, 43, 49, 56, 62, 68, 74, 80, 86, 92, 97, 103, 109, 115, 120, 126, 131, 136, 142, 147, 152, 157, 162, 167, 171, 176, 181, 185, 189, 193, 197, 201, 205, 209, 212, 216, 219, 222, 225, 228, 231, 234, 236, 238, 241, 243, 244, 246, 248, 249, 251, 252, 253, 254, 254, 255, 255, 255 };
  15.     return cosLUT[angle & 0xff];
  16. }
  17.  
  18. void rotate(int x, int y, Fixed ca, Fixed sa, SDL_Rect *out)
  19. {
  20.     out->x = fixtoi(fixmul(itofix(x), ca) + fixmul(itofix(y), sa));
  21.     out->y = fixtoi(fixmul(itofix(x), -sa) + fixmul(itofix(y), ca));
  22. }
  23.  
  24. void custom_rotosprite(SDL_Surface *source, SDL_Rect sr, Fixed angle, SDL_Surface *screen)
  25. {
  26.     // sr is the center of the sprite
  27.     SDL_LockSurface(source);
  28.     SDL_LockSurface(screen);
  29.    
  30.     // Rotated vertices
  31.     SDL_Rect upleft, upright, downleft, downright;
  32.     // Final rectangle, the rectangle containing all of the four rotated points
  33.     // x,y = top-left corner
  34.     // w,h = bottom-right corner
  35.     SDL_Rect fr;
  36.    
  37.     Fixed dX = fixcos(angle), dY = fixsin(angle);
  38.    
  39.     rotate(-source->w / 2, -source->h / 2, dX, dY, &upleft);
  40.     rotate(source->w / 2, -source->h / 2, dX, dY, &upright);
  41.     rotate(-source->w / 2, source->h / 2, dX, dY, &downleft);
  42.     rotate(source->w / 2, source->h / 2, dX, dY, &downright);
  43.    
  44.     fr.x = min(min(min(upleft.x, upright.x), downleft.x), downright.x) + sr.x;
  45.     fr.y = min(min(min(upleft.y, upright.y), downleft.y), downright.y) + sr.y;
  46.     fr.w = max(max(max(upleft.x, upright.x), downleft.x), downright.x) + sr.x;
  47.     fr.h = max(max(max(upleft.y, upright.y), downleft.y), downright.y) + sr.y;
  48.    
  49.     // Current pixel
  50.     SDL_Rect cp;
  51.     // Current de-rotated pixel
  52.     SDL_Rect cdrp;
  53.    
  54.     for(cp.y = fr.y; cp.y < fr.h; cp.y++)
  55.     {
  56.         for(cp.x = fr.x; cp.x < fr.w; cp.x++)
  57.         {
  58.             // de-rotate the point into sprite space
  59.             rotate(cp.x - sr.x, cp.y - sr.y, dX, -dY, &cdrp);
  60.             if(abs(cdrp.x) < source->w / 2 && abs(cdrp.y) < source->h / 2)
  61.             {
  62.                 Uint32 currentPixel = nSDL_GetPixel(source, cdrp.x + source->w / 2, cdrp.y + source->h / 2);
  63.                 if(currentPixel != source->format->colorkey)
  64.                     nSDL_SetPixel(screen, cp.x, cp.y, currentPixel);
  65.             }
  66.         }
  67.     }
  68.    
  69.     SDL_UnlockSurface(screen);
  70.     SDL_UnlockSurface(source);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement