Here's some code. This is a new method for the Sprite class of the Overloaded Game Foundation; it will be distributed in a nicer way in the upcoming version 0.99.
- Code:
//-------------------------------------------------------------------------
/*! Draws a rotated sprite - Very limited, no flags, no clipping
/ But hey, it's sub-pixel and sub-texel accurate. :)
*///-----------------------------------------------------------------------
void SpriteImpl::DrawRotated( int a_X, int a_Y, int a_Rotation )
{
int ox[4], oy[4], rx[4], ry[4], u[4], v[4];
int x1[320], x2[320], u1[320], u2[320], v1[320], v2[320], h, p;
int cx = a_X + (m_Width >> 1), cy = a_Y + (m_Height >> 1);
ox[0] = ox[3] = -m_Width / 2, oy[0] = oy[1] = -m_Height / 2, u[0] = u[3] = v[0] = v[1] = 1 << 10;
ox[1] = ox[2] = m_Width / 2, u[1] = u[2] = v[2] = v[3] = 63 << 10, oy[2] = oy[3] = m_Height / 2;
float r = (float)a_Rotation * 3.141592f / 180.0f;
for ( int i = 0; i < 4; i++ )
{
rx[i] = (cx << 10) + (int)(ox[i] * 1024.0f * cos( r ) + oy[i] * 1024.0f * sin( r ));
ry[i] = (cy << 10) + (int)(ox[i] * 1024.0f * sin( r ) - oy[i] * 1024.0f * cos( r ));
}
int miny = ry[0], maxy = ry[0];
for ( i = 1; i < 4; i++ ) miny = ry[i] < miny?ry[i]:miny, maxy = ry[i] > maxy?ry[i]:maxy;
miny >>= 10, maxy >>= 10;
for ( i = miny; i < maxy; i++ ) x1[i] = 240 << 10, x2[i] = 0;
for ( i = 0; i < 4; i++ )
{
int xs = rx[i], ys = ry[i], us = u[i], vs = v[i];
int xe = rx[(i + 1) & 3], ye = ry[(i + 1) & 3];
int ue = u[(i + 1) & 3], ve =v[(i + 1) & 3];
if (ys > ye)
{
int t = xs; xs = xe; xe = t; t = ys, ys = ye; ye = t;
t = us; us = ue; ue = t; t = vs, vs = ve; ve = t;
}
if (h = (ye - ys) >> 2)
{
int dx = ((xe - xs) << 8) / h, du = ((ue - us) << 8) / h, dv = ((ve - vs) << 8) / h;
int fix = 1023 - (ys & 1023);
xs += (fix * dx) >> 10, us += (fix * du) >> 10, vs += (fix * dv) >> 10;
for ( int j = ys >> 10; j < ye >> 10; j++ )
{
if (xs < x1[j]) x1[j] = xs, u1[j] = us, v1[j] = vs;
if (xs > x2[j]) x2[j] = xs, u2[j] = us, v2[j] = vs;
xs += dx, us += du, vs += dv;
}
}
}
Pixel* dest = s_Dest->GetBuffer() + miny * 240;
for ( i = miny; i < maxy; i++, dest += 240 )
{
if (p = (x2[i] - x1[i]) >> 10)
{
int us = u1[i], vs = v1[i];
int du = (u2[i] - u1[i]) / p, dv = (v2[i] - v1[i]) / p;
int fix = 1023 - (x1[i] & 1023); us += (fix * du) >> 10, vs += (fix * dv) >> 10;
for ( int j = x1[i] >> 10; j < x2[i] >> 10; j++, us += du, vs += dv )
*(dest + j) = *(m_Bitmap + (us >> 10) + ((vs >> 10) << 6));
}
}
}
Some notes:
I know this code uses floats. They are used to rotate the four vertices of the rectangle of the original sprite. Of course this should be done using a nice precalculated fixed point sin / cos table, but I wanted a single function for this post.
The second part is basically a sub-pixel, sub-texel correct convex-textured-polygon filler. If you rotate the thing REALLY slowly you'll see what I mean.

This would look just as good on a 3D model.
One thing that needs to be optimized is the divide in the final scanline filler (final for loop): Since the sprite has no depth, it also has a constant z-slope and thus constant delta's for u and v. The division should thus give the same result all the time; calculating it once should suffice. That should be done on the longest scanline however, wich is kinda tricky to determine (well, actually, I was kinda lazy). That being said, this code is pretty fast despite of that.
One thing to note: The code assumes 64 pixel wide sprites. The final rasterizing loop uses a << 6 instead of '* 64' for some extra speed. A 70 * 70 sprite would thus require the multiplication; this would slow down the drawing of every single pixel and it would thus have a great impact on the overall speed.
Another thing to note: To avoid some precision problems at the edges of the polygon, I ignore a 1-pixel border around the sprite. Wich reminds me that this code does no alpha checking.

That should be easy to add.

There's no clipping either btw.
Have fun,
- Jacco.