Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef int Fixed;
- // Fixed
- // 24.8 fixed point
- #define itofix(x) ((x) << 8)
- #define fixtoi(x) ((x) / 256)
- #define fixmul(x, y) ((x) * (y) / 256)
- #define fixdiv(x, y) (((x) << 8) / (y))
- #define fixsin(x) fixcos((x) - 64)
- Fixed fixcos(Fixed angle)
- {
- 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 };
- return cosLUT[angle & 0xff];
- }
- void rotate(int x, int y, Fixed ca, Fixed sa, SDL_Rect *out)
- {
- out->x = fixtoi(fixmul(itofix(x), ca) + fixmul(itofix(y), sa));
- out->y = fixtoi(fixmul(itofix(x), -sa) + fixmul(itofix(y), ca));
- }
- void custom_rotosprite(SDL_Surface *source, SDL_Rect sr, Fixed angle, SDL_Surface *screen)
- {
- // sr is the center of the sprite
- SDL_LockSurface(source);
- SDL_LockSurface(screen);
- // Rotated vertices
- SDL_Rect upleft, upright, downleft, downright;
- // Final rectangle, the rectangle containing all of the four rotated points
- // x,y = top-left corner
- // w,h = bottom-right corner
- SDL_Rect fr;
- Fixed dX = fixcos(angle), dY = fixsin(angle);
- rotate(-source->w / 2, -source->h / 2, dX, dY, &upleft);
- rotate(source->w / 2, -source->h / 2, dX, dY, &upright);
- rotate(-source->w / 2, source->h / 2, dX, dY, &downleft);
- rotate(source->w / 2, source->h / 2, dX, dY, &downright);
- fr.x = min(min(min(upleft.x, upright.x), downleft.x), downright.x) + sr.x;
- fr.y = min(min(min(upleft.y, upright.y), downleft.y), downright.y) + sr.y;
- fr.w = max(max(max(upleft.x, upright.x), downleft.x), downright.x) + sr.x;
- fr.h = max(max(max(upleft.y, upright.y), downleft.y), downright.y) + sr.y;
- // Current pixel
- SDL_Rect cp;
- // Current de-rotated pixel
- SDL_Rect cdrp;
- for(cp.y = fr.y; cp.y < fr.h; cp.y++)
- {
- for(cp.x = fr.x; cp.x < fr.w; cp.x++)
- {
- // de-rotate the point into sprite space
- rotate(cp.x - sr.x, cp.y - sr.y, dX, -dY, &cdrp);
- if(abs(cdrp.x) < source->w / 2 && abs(cdrp.y) < source->h / 2)
- {
- Uint32 currentPixel = nSDL_GetPixel(source, cdrp.x + source->w / 2, cdrp.y + source->h / 2);
- if(currentPixel != source->format->colorkey)
- nSDL_SetPixel(screen, cp.x, cp.y, currentPixel);
- }
- }
- }
- SDL_UnlockSurface(screen);
- SDL_UnlockSurface(source);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement