If anyone else was a bit confused by the examples in TANSTAAFL''s book, this should help you understand how to do this better (as those examples have to be general and should be taken as examples and not cut/paste code).
Here is my clip function:
/*------------------------------------------------------------------------------
CMap::Clip
Clipper to replace the directdraw clipper object - pretty simple eh?
PARAMETERS:
POINT* ptDest - pointer to the Dest blit POINT
RECT rcSrc - the RECT of the object to be clipped
RECT* rcClipSrc - pointer to the RECT to put the new clipped Src in
RETURN VALUE: void
------------------------------------------------------------------------------*/
void CMap::Clip(POINT* ptDest, RECT rcSrc, RECT* rcClipSrc)
{
RECT rcDif;
CopyRect(rcClipSrc, &rcSrc);
if((rcDif.left = ptDest->x - rcScreenClipper.left) < 0)
{
rcClipSrc->left = rcSrc.left - rcDif.left;
ptDest->x = ptDest->x - rcDif.left;
}
if((rcDif.top = ptDest->y - rcScreenClipper.top) < 0)
{
rcClipSrc->top = rcSrc.top - rcDif.top;
ptDest->y = ptDest->y - rcDif.top;
}
if((rcDif.right = rcScreenClipper.right - (ptDest->x + (rcSrc.right - rcSrc.left))) < 0)
rcClipSrc->right = rcSrc.right + rcDif.right;
if((rcDif.bottom = rcScreenClipper.bottom - (ptDest->y + (rcSrc.bottom - rcSrc.top))) < 0)
rcClipSrc->bottom = rcSrc.bottom + rcDif.bottom;
}
|
Let me explain... the parameters:
POINT* ptDest
is the original destination point for the object to be drawn. I used a POINT because the BltFast function takes x/y coords as it''s first 2 variables, unline Blt which takes a RECT. You can still use a RECT here instead and it would be easier to find the right/bottom clip params. This is a pointer because I want to change the destination point.
RECT rcSrc
is the original source RECT of the image to be copied from the surface. I give this so that the function will know what to clip from and won''t actualy change the source RECT. This is not a pointer because I don''t want to change the original source RECT.
RECT* rcClipSrc
should be an EMPTY RECT for isn''t needed for anything besides getting the clipped source RECT. This is filled with the RECT of the clipped object and should be used in the BltFast function.
Ok now I will explain the inside of the function and how it clips:
RECT rcDif
this is a RECT used to store the overlapping space that needs to be clipped.
CopyRect(rcClipSrc, &rcSrc)
this nice function copies the original source RECT into the blank rcClipSrc - which will only be changed if it has to.
((( rcScreenClipper is a RECT containing the space to clip to, mine is left:0 top:0 right:640 bottom: 480 )))
if((rcDif.left = ptDest->x - rcScreenClipper.left) < 0)
this assigns the difference of the left side of the object and the left side of the screen to rcDif and it will be < 0 if it is beyond the left screen boundary and needs to be clipped.
rcClipSrc->left = rcSrc.left - rcDif.left;
ptDest->x = ptDest->x - rcDif.left;
if it DOES need to be clipped, it first changes the new source RECT to fit then sets the left dest point to the new clip spot also.
Same thing for the top screen boundary.
The right and bottom are a little different... you can calculate these differen''t ways actualy, but this is how I did it:
if((rcDif.right = rcScreenClipper.right - (ptDest->x + (rcSrc.right - rcSrc.left))) < 0)
this might look a little wild, but it''s doing almost the same thing as above. the difference is since I am using a POINT and not a RECT I need to do (ptDest->x + (rcSrc.right - rcSrc.left)) in order to find the RIGHT side of the object.
Now in these you don''t need to change the ptDest if you are using a POINT this way, just change the rcClipSrc like:
rcClipSrc->bottom = rcSrc.bottom + rcDif.bottom
and that will work great.
Right and Bottom sides are done the same.
If you are clipping this way, your BltFast function would look like this:
g_pDDSBack->BltFast(ptDest.x, ptDest.y, lpDDSTileset, &rcClipSrc, DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
And that''s it, this works exactly like the DirectDraw clipper, except the BltFast function is faster.
Sorry if this is sloppy, but I did this on a 10 minute work break

Also I have a method for about the easiest mouse mapper ever, which doesn''t require any cursor image like the example in TANSTAAFL''s book, and is about 3 lines of code.
If anyone is interested in that I can post it

Let me know if you have any more questions!