PocketMatrix forums

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

Clipping rotated/scaled blits (solution+code)

I had a quick search and didn't find any solution to this. Almost posted a whinging post that it looked impossible... then figured out how to do it - clip the dest rectangle, and very importantly, use and adjust the rotation center when top or left clipping is needed.

This is my current clipped blit function:

Code:

#define CLIP_XMIN 0
#define CLIP_XMAX 240
#define CLIP_YMIN 0
#define CLIP_YMAX 256 // (I've got a big control panel at 256<y<320)

void CTileMap::Blt(RECT *rDst,CGapiSurface *srcSurface,RECT *rSrc,int flags,GDBLTFX *pBltFX)
{
// TODO: Compute optimal dest rect here
// -------------------------------------------------------------------------------


// Position blit relative to the tilemap's scroll offsets
// -------------------------------------------------------------------------------
rDst->left-=scx; rDst->right-=scx;
rDst->top-=scy; rDst->bottom-=scy;

if (flags&(GDBLT_ROTATIONANGLE|GDBLT_ROTATIONSCALE))
{
// For rotated blits, we've got to clip the DESTINATION (and center)
// ----------------------------------------------------------------------------

flags|=GDBLT_ROTATIONCENTER; // Enable the rotation center
pBltFX->rotationCenter.x=(rDst->right-rDst->left)>>1;
pBltFX->rotationCenter.y=(rDst->bottom-rDst->top)>>1;

if (rDst->left<CLIP_XMIN) // Left clip
{
pBltFX->rotationCenter.x+=(rDst->left-CLIP_XMIN);
rDst->left=CLIP_XMIN;
}

if (rDst->top<CLIP_YMIN) // Top clip
{
pBltFX->rotationCenter.y+=(rDst->top-CLIP_YMIN);
rDst->top=CLIP_YMIN;
}

if (rDst->right >CLIP_XMAX) rDst->right=CLIP_XMAX; // Right clip
if (rDst->bottom>CLIP_YMAX) rDst->bottom=CLIP_YMAX; // Bottom clip


// Check if we've actually got something left to draw
// ----------------------------------------------------------------------------
if (rDst->left>=rDst->right || rDst->top>=rDst->bottom) return;
}
else
{
// TODO : Normal source rect clipping on non-rotated blits
// ----------------------------------------------------------------------------
}

// Blit it to the back buffer (pbb)
pbb->Blt(rDst,srcSurface,rSrc,flags,pBltFX);
}
[/code]
Doh! posted too soon...

This clips fine on the PC build... but on my iPaq 3870 it's almost as if the clipping code isn't there at all :( Rather broken...
Hmm... after a bit of debugging, it looks like RotationCenter doesn't behave as expected on the iPaq. But works fine on the PC build

Stepping through it, it seems to be clipping the bottom of the sprite when it goes off the left of the screen... My app is running in 'portrait' mode (GDDISPMODE_NORMAL), which I think means that my coordinate system will be 90degrees rotated from GapiDraw's internal blitting orientation.

Maybe GapiDraw isn't converting the rotation centre to it's own coord system correctly?

Can anyone confirm whether this is a GapiDraw bug, or me doing something silly? (I'm using 2.01b)
I have a 3870 lying around.. I'll test the code and report back...
Johan wrote:
I have a 3870 lying around.. I'll test the code and report back...


That'd be cool. :D

It's probably been suggested before, but how about adding a CGapiClipper class to GapiDraw?

CGapiClipper::SetClipWindow(RECT *clipWindow)
CGapiClipper::BltFast()
CGapiClipper::Blt()
It still might be added. But not in 2.02.
Just wanted to confirm the rotationcenter bug. It is fixed in the upcoming 2.02 release...