PocketMatrix forums

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

DrawRotated() clipping...

It would be great if the DrawRotated() method included clipping. I tried the Polygon() and it didn't work for me (also, it doesn't clip anyways).

Is it planned at all? What if I change the source to include clipping?

Thanks.
It isn't planned,because the drawrotated thing is more or less an ad-hoc piece of code. If you add code, send it over, and we'll at it to the next release.
Ok, that method looks highly mind-encoded (I understand as it's ad-hoc), I saw a bunch of mind-specific optimizations, so I am unsure I'll have the energy to decode and modify it.

For now I will stay with the common Draw() to accelerate the prototype I'm working on, then I'll see if I'll be able to enhance DrawRotated(). DrawRotated() is nice because it is smooth. =)

Btw, the library is really good, I'm finding no problems to work with it in general (as I could expect from you and Jaap =).
Jaap Suter · 02 Sep 2002, 15:46 UTC — DrawPolygon
The new DrawPolygon does clipping. It's not released yet, but if you contact me directly (icq 19637432, or email j.suter@student.utwente.nl) I can send the updated version to you. Or you can await the next engine release.

Cheers,

Jape
hi guys,
i'm interested in the drawrotated() even w/o the clipping. i've tried it but i get a coloured morphing box instead of my rotated sprite. it's very suggestive, but it's not what i need... 8O
is there some constraints on the parameters to pass to it (like sprite size or such)?
thx
Polygon() works with power of 2 bitmaps (actually, I'm using 32x64 8bit). You have to set carefully parameters like vertex and uv coordinates to get it working, but it works and is really smooth.
wow, now all we need are vector transformations and matrices using fixed-point...
pib wrote:
hi guys,
i'm interested in the drawrotated() even w/o the clipping. i've tried it but i get a coloured morphing box instead of my rotated sprite. it's very suggestive, but it's not what i need... 8O
is there some constraints on the parameters to pass to it (like sprite size or such)?
thx


huhu looks like everybody has this marvelous "morphing" you're talking about...

i think that one reason could be the stranges 3024.0f in the code, witch gives much better results in size if you replace with 1024.0f (4 times)

but that's not all ... there's some other obscur reason why it doesn't do the stuff... may it come from the sub pixel of the device Jacco's using and wich is different from Ipaqs...

that code is too difficult to understand :') ... sorry

OUPS ! i also saw a 63 high in the code ... that looks like "hard" value ... hmmm ? :) let's try width m_width instead ...

Give me some Good data, i give it back as is :)

==================================================
(later...)
ok!!! i think i get it now, the 63 is not good
and also the ((vs >> 10) << 6) in the end, 6 is also a hard value... 1<<6 = 64. looks like Jacco was working with a 64x64 sprite :)

i finish cleaning/optimizing and post it later.
64? Lemmise... Yep. Uhm... Is that a problem? 64x64 is a fine size for a sprite, isn't it? ;)
Lewill, you are good! ever tried with Egyptian hieroglyphics? :lol:
huhu ... Egyptian ? :)

here is a working one
Code:

void SpriteImpl::DrawRotated( int a_X, int a_Y, int a_Rotation, Surface* a_Target ) const
{
if ( a_Target == NULL ) a_Target = GetApi()->GetSecondary();

unsigned short ToPlot;
unsigned char shift=0;
int dummy=m_Width;
while ( dummy>1)
{
shift++;
dummy=dummy>>1;
}
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;
ox[0] = ox[3] = -(m_Width>>1);
oy[0] = oy[1] = -(m_Height>>1);
ox[1] = ox[2] = (m_Width>>1);
u[0] = u[3] = v[0] = v[1] = 1<<10;
u[1] = u[2] = v[2] = v[3] = (m_Width-1) << 10;
oy[2] = oy[3] = (m_Height>>1);
float r = ((float)a_Rotation / 100) * 3.141592f / 180.0f;
for ( int i = 0; i < 4; i++ )
{
rx[i] = (a_X << 10) + (int)(ox[i] * 1024.0f * cos( r ) + oy[i] * 1024.0f * sin( r ));
ry[i] = (a_Y << 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 = a_Target->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]) / p, dv = (v2[i] - v1[i]) / p;
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 )
{
ToPlot = *(m_Pixels + (us >> 10) + ((m_CurrentFrame + (vs >> 10)) << shift));
if ( ToPlot != TRANSPARENT_COLOR )
*(dest + j) = ToPlot;
}
}
}
}


* transparency added
* use only power of 2 sprite dimension
* always no clipping :o) (transparency makes it already slower :/ )

good luck :')
!!! What happened to my beautifull obfuscated 2-liner? People don't recognize art when they see it nowadays...
... sorry about this :)

BTW if we want to make one using Polygon(), how could it be implemented in SpriteImpl ?!??? cause the Sprite do not Own the Surface and Polygon() do need one :)

==> i will try Egyptian
works great.
thx
Lewil:

When initializing the sprite, instead of:

Code:

m_Sprite.Reset( g_Factory->NewSprite( g_Factory->NewSurface( "graphics/sprite.tga" ), 1 ) );


I am doing this:

Code:

m_pSpriteSurface = g_Factory->NewSurface( "graphics/standing.tga" );
m_Sprite.Reset( g_Factory->NewSprite( m_pSpriteSurface,1 ) );


This snippet is encapsulated on my CPlayer class, the class keeps the pointer to the sprite surface, so that I can use it on Polygon().

Using Polygon(), we need to rotate the vertex array, of course. DrawRotated() is somewhat a high-level Polygon() imho.

Hope this helps.
yes, the begining of DrawRotated() is just the 4 corners rotation that we could pass as vertex array to Polygon(). the texture coordinates should be easy too for the Sprite() "overloads" m_Width and m_Heigth of the Surface().

the matter is when you have to pass the Surface being mapped. even if you declare as you do, the Sprite() only have a pointer to the pixels of the surface, not the full surface itself...



in fact it should be easy to make a Surface->Drawrotated() using polygon() but i don't see right now how to do so with a Sprite :)
Lewil · 05 Sep 2002, 14:40 UTC — DrawPolygon
Take that:

Jaap Suter wrote:
The new DrawPolygon does clipping. It's not released yet, but if you contact me directly (icq 19637432, or email j.suter@student.utwente.nl) I can send the updated version to you. Or you can await the next engine release.

Cheers,

Jape


Add that:

Code:

void DrawRS( Surface* a_Bmp, int a_X, int a_Y, int a_Rotation, float a_XScale = 1.0f, float a_YScale = 1.0f, Surface* a_Target = NULL );


Code:

void MyClass::DrawRS( Surface* a_Bmp, int a_X, int a_Y, int a_Rotation, float a_XScale, float a_YScale, Surface* a_Target)
{
Vector2 corners[4];
Vector2 textcoord[4];
if ( a_Target == NULL ) a_Target = g_Api->GetSecondary();
float ox[4], oy[4];
ox[0] = ox[3] = -(a_Bmp->GetWidth()>>1)*a_XScale;
ox[1] = ox[2] = (a_Bmp->GetWidth()>>1)*a_XScale;
oy[0] = oy[1] = -(a_Bmp->GetHeight()>>1)*a_YScale;
oy[2] = oy[3] = (a_Bmp->GetHeight()>>1)*a_YScale;
float r = ((float)a_Rotation ) * 3.141592f / 180.0f;
float cosr = (float)cos(r);
float sinr = (float)sin(r);
int FPx = a_X<<FP_Q, FPy = a_Y<<FP_Q;
for ( int i = 0; i < 4; i++ )
{
corners[i].m_X = FPx + FP_FROM_FLOAT( ox[i] * cosr + oy[i] * sinr );
corners[i].m_Y = FPy + FP_FROM_FLOAT( ox[i] * sinr - oy[i] * cosr );

}
textcoord[0].m_X = textcoord[3].m_X = textcoord[0].m_Y = textcoord[1].m_Y = 0;
textcoord[1].m_X = textcoord[2].m_X = FP_ONE;
textcoord[3].m_Y = textcoord[2].m_Y = FP_ONE;
a_Target->Polygon( corners, textcoord, 4, a_Bmp );
}


you're not far to get a Clipping/scaling/rotating/smooth/fast/transparency surface printing

... Jape please can u send me the new polygon() ? :)
wbill@wanadoo.fr

Thx !!