PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

m_mysprite->Draw( x, y, angle ) ... Sprite rotating stuff

Hi there,

i'm trying to do some Sprite rotation, i need also some clipping as i need to see sprites part coming in screen.
in fact i did my own spriting drawing (with angle passed by parameter) from a surface (32x32). it works well but even if i used FP it's too slow when the sprite number comes up to 50 :/

has someone work on similar stuff ? or do u know where i could find some code ?

thx
If done well, a rotated sprite should not be slower than a regular one. Well, hardly. :) I'll try to post some code tomorrow.
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.
While I'm at it, here's some more code, this time to scale a sprite:

Code:

void SpriteImpl::DrawScaled( int a_X, int a_Y, int a_Width, int a_Height )
{
int v = 0;
int du = (m_Pitch << 10) / a_Width;
int dv = (m_Height << 10) / a_Height;
Pixel* dest = s_Dest->GetBuffer() + a_X + a_Y * s_Dest->GetWidth();
Pixel* src = m_Bitmap;
for ( int y = 0; y < a_Height; y++ )
{
int u = 0;
int cv = (v >> 10) * m_Pitch;
for ( int x = 0; x < a_Width; x++ )
{
*(dest + x) = *(src + (u >> 10) + cv);
u += du;
}
v += dv;
dest += s_Dest->GetWidth();
}
}


No clipping again, but the sprite can have an arbitrary size.

- Jacco.
Since it would seem that only Jacco can make all the nifty sprite drawing code I present here a super blazing fast algorithm to draw the sprite completely transparent:

Code:

void SpriteImpl::DrawCompletelyTransparent( int a_X, int a_Y, int a_Width, int a_Height )
{
return;
}


This routine will also be available in the 0.99 release of course ;^)

R
Bummer, just as I finish writing my own (albiet simpler 90-180-270 degree rotations only), you go and post this beauty! :)

Cheers all the same.

Dan
hmmm If I look at the code we could even get it faster by losing the argument list, and making it INLINE or even defining it as following:

Code:

#define DrawCompletelyTransparent


I think it can't be done any faster than this :D

p.s. on request I'll post a completely transparent rotated sprite and completely transparent scaled sprite too.

well, actually, I was kinda lazy


hmmm what's happening when you're courageous ?!? :roll:

well i suppose that was a good idea for everybody to launch such a post ? we all know how to rotate sprites fast ... and how to draw completlytransparent sprites :P

Thanks Rhino :roll: & jacco :D

btw, rhino, can i ask you some sample code for:
Code:

Overloaded->CodeAutomaticWonderfulCompletlyTransparentGame();
euh euh euh...

That's a toughy!

I'll have to come back on that later! :?
I made a small mistake. The image gets even more stable if you change

Code:

int du = (u2[i] - u1[i]) / p, dv = (v2[i] - v1[i]) / p;


to

Code:

int w = (x2[i] - x1[i]) >> 2;
int du = ((u2[i] - u1[i]) << 8) / w, dv = ((v2[i] - v1[i]) << 8) / w;


Needless to say, that's because delta-u and delta-v needs to be based on the un-truncated length of a scanline.

- Jacco.
Reinier (Whino or Rhino, can't remember) asked if I could do a bilerped version. Here it is:

Code:

//-------------------------------------------------------------------------
/*! Draws a rotated sprite - Very limited, no flags, no clipping
/ But hey, it's sub-pixel and sub-texel accurate. :)
*///-----------------------------------------------------------------------
void SpriteImpl::DrawRotatedBilerped( 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 / 100) * 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 w = (x2[i] - x1[i]) >> 2;
int du = ((u2[i] - u1[i]) << 8) / w, dv = ((v2[i] - v1[i]) << 8) / w;
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 )
{
Pixel* addr = m_Bitmap + (us >> 10) + ((vs >> 10) << 6);
Pixel p1 = *addr, p2 = *(addr + 1), p3 = *(addr + 64), p4 = *(addr + 65);
int a = us & 1023, b = vs & 1023;
int f1 = a * b, f2 = (1023 - a) * b, f3 = a * (1023 - b);
int f4 = ((1 << 20) - 1) - (f1 + f2 + f3);
f1 >>= 15, f2 >>= 15, f3 >>= 15, f4 >>= 15;
p1 = ScaleColor( p1, f4 );
p2 = ScaleColor( p2, f3 );
p3 = ScaleColor( p3, f2 );
p4 = ScaleColor( p4, f1 );
*(dest + j) = (Pixel)(p1 + p2 + p3 + p4);
}
}
}
}


Won't be fast on a PPC. :)

- Jacco.
and that's only because of the floating points, right?

I did use sin/cos fixed-pt. table.

I'm wondering if the library will include that so I can remove mine.
and in mine i also use a FP x*cos(y)table which can avoid multiplications

Code:

int m_Icos[360][48];
int m_Isin[360][48];
int m_cos[360];
int m_sin[360];

void MyMathInit()
{
// FP COS/SIN TABLE CALCULATION
for ( int i=0; i<360; i++)
{
m_cos[i] = FP_FROM_FLOAT((float)cos( i*2*PI/360.0f ) );
m_sin[i] = FP_FROM_FLOAT((float)sin( i*2*PI/360.0f ) );
for ( int j=0; j<48; j++ )
{
m_Icos[i][j] = m_cos[i] * (j-22);
m_Isin[i][j] = m_sin[i] * (j-22);
}
}
}
Nah, the floats are not going to be the bottleneck. For a 64x64 sprite, you're drawing a lot of pixels, and doing a sine and cosine twice is not going to hurt performance a lot compared to that loop.

I mentioned poor performance on the PocketPC for the bilerped version, since it involves scaling FOUR pixels to plot ONE. That is an aweful lot of work. :) This can be done much faster, but that requires a palettized source image. Since surfaces in the Overloaded game foundation can't be palettized at the moment, I'll do that some other time. :)
god that loop is not in the drawing method, that's the Table precalculation loop :') only have to do this once at Initialization, fortunatly :P
I was talking about my bilerp code, not yours. :)
sorry :p
Uuuuh guys,

you are aware of the fact that there is a Surface::Polygon routine in the current library version? One that is subpixel and subtexel accurate and can be considered fast enough for all purposes. With that it is easy to do both sprite scaling and rotations.

It even does transparancy. Make sure the supplied texture surface has power of 2 dimensions though.

Cheers,

Jape
Really? Cool! I'll check it out. :)