PocketMatrix forums

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

How do I clip a bitmap to only display the part on-screen?

I have been having trouble with finding out how to clip an image displayed. For example: when an object I display with the Blt function has a small piece of it "hanging off the edge" it dosent display it at all.
Hi,

I'm just back from a party (at 04.30) so I am probably not the most appropriate person to answer your question...

Anyways... You can“t have pixels "hanging off the edge" of your surface. Use a simple routine to clip your source rectangle so that it doesn't copy pixels that are outside the destination surface.

Johan
Basically you just move the rectangle size around to make sure only the part you would see draws on the surface. Here is some simple clipping code, you can modify it how you like:
Code:

//Assume that RECT rcRect has been initialized
//allready to the appropriate sprite values

//X clipping---------------------------
if (x<0)
{
rcRect.left=rcRect.left-x;
x=0;
}
else if(x>(240-spritesizey))
{rcRect.right+=>(240-spritesizey)-x;}
//Y clipping---------------------------
if(y<0)
{
rcRect.top=rcRect.top-drawy;
y=0;
}
else if(y>(320-spritesizey))
{rcRect.bottom+=(320-spritesizey)-y;}
.. you might want to exchange "spritesizey" for "spritesizex" in the above example in the "X clipping" section if your sprite width is different from its height...
Ooops, I actually had the numbers in there before for my specific charicter I was drawing 50x40. I thought I fixed it for easier reading :? .......
Thanks for the advise and code DillRye and sbl. I'm making an asteroids-type scrolling game and just kind of hit a mental block. 8O
Here is the code that I used. I'm not sure if its better but it works for me! :mrgreen:
Code:

if(DestRect.left<0){
SrcRect.left-=DestRect.left;
DestRect.left=0;
}
if(DestRect.right>240){
SrcRect.right-=DestRect.right-240;
DestRect.right=240;
}
if(DestRect.top<0){
SrcRect.top-=DestRect.top;
DestRect.top=0;
}
if(DestRect.bottom>320){
SrcRect.bottom-=DestRect.bottom-320;
DestRect.bottom=320;
}
tedson · 21 Aug 2002, 22:09 UTC — What about rotation
I was using almost the exact code above to clip my sprites but the problem I am running in to now is I am rotating the sprites by setting a rotation angle and passing GDBLT_ROTATIONANGLE to Blt. The problem is the width and height of the sprite are now different. Anyone know a good way around this one?
thevicar · 28 Aug 2002, 23:54 UTC — this looks like a time for a FEATURE REQUEST
yes, it's easy enough to clip the source rect, but really the concept is of a rect on the Dest surface (that may even cover its entire area) which defines the area within which you want to blit TO, after you've performed a rotation or zoom or whatever (or not) on the pixels within the SOURCE rect that you want to take pixels FROM. so it would be nice to be able to specify both rects in the call. would it be possible to a flag in dwFlags such as GDCLIP_DEST or something, and a field in BltFX structures like pDestRect?
I believe just the normal Blt function has what your lookin for.......
Yep...


And the next version of GapiDraw will also support a variable pivot point on the target surface with automatic clipping (so if you want to rotate a sprite and place it in the corner you no longer need to Blt with rotate to a temp surface and then copy/clip the result to the correct location).

I will announce a betaprogram of 1.1 next month...

/Johan