Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void rotate(int x, int y, int cx, int cy, Fixed angle, Rect *out)
- {
- x -= cx;
- y -= cy;
- out->x = fixtoi(fixmul(itofix(x), fixcos(angle)) + fixmul(itofix(y), fixsin(angle))) + cx;
- out->y = fixtoi(fixmul(itofix(x), -fixsin(angle)) + fixmul(itofix(y), fixcos(angle))) + cy;
- }
- void getBoundingBox(int x, int y, int w, int h, int cx, int cy, Fixed angle, Rect *out)
- {
- Rect tl, tr, bl, br;
- rotate(x, y, cx, cy, angle, &tl);
- rotate(x + w, y, cx, cy, angle, &tr);
- rotate(x, y + h, cx, cy, angle, &bl);
- rotate(x + w, y + h, cx, cy, angle, &br);
- out->x = min(min(min(tl.x, tr.x), bl.x), br.x);
- out->y = min(min(min(tl.y, tr.y), bl.y), br.y);
- out->w = max(max(max(tl.x, tr.x), bl.x), br.x) - out->x;
- out->h = max(max(max(tl.y, tr.y), bl.y), br.y) - out->y;
- }
- void drawSpriteRotated(const unsigned short* source, const Rect* sr, const Rect* rc, Fixed angle, int flash, unsigned short flashColor)
- {
- Rect defaultRect = { source[0] / 2, source[1] / 2, 0, 0 };
- Rect fr;
- unsigned short currentPixel;
- Fixed dX = fixcos(angle), dY = fixsin(angle);
- if(rc == NULL)
- rc = &defaultRect;
- getBoundingBox(-rc->x, -rc->y, source[0], source[1], 0, 0, angle, &fr);
- fr.x += sr->x;
- fr.y += sr->y;
- fr.w += fr.x;
- fr.h += fr.y;
- Rect cp, lsp, cdrp;
- // Feed fixed-point to get fixed-point
- rotate(itofix(fr.x - sr->x), itofix(fr.y - sr->y), 0, 0, -angle, &lsp);
- for(cp.y = fr.y; cp.y <= fr.h; cp.y++)
- {
- cdrp.x = lsp.x;
- cdrp.y = lsp.y;
- for(cp.x = fr.x; cp.x <= fr.w; cp.x++)
- {
- if(cp.x >= 0 && cp.x < 320 && cp.y >= 0 && cp.y < 240)
- {
- currentPixel = getPixel(source, fixtoi(cdrp.x) + rc->x, fixtoi(cdrp.y) + rc->y);
- if(currentPixel != source[2])
- {
- setPixelUnsafe(cp.x, cp.y, flash ? flashColor : currentPixel);
- }
- }
- cdrp.x += dX;
- cdrp.y += dY;
- }
- lsp.x -= dY;
- lsp.y += dX;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement