PocketMatrix forums

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

Clipping function

Maybe this won't be of any use to anyone, but it struck me as something others may want...

I had to write a clipping routine today. The one thing that maybe makes it different than others is that it allows for blitting partially on all sides. In other words, if you want to slide an image in from the side of the screen, or the top of the screen, and off the opposite side, you can do it with this.

Feel free to use and abuse, just lemme know what you fix or optimize so I can use it myself. And if it's of no use, oh well, no harm done.

Code:

// A function to do clipping. Pass it the surface you want to blit, the surface to
// blit to (usually this will be backbuffer), the x/y coordinates to blit to, and
// output parameters drawX/drawY that will recieve the modified x/y coordinates to
// blit to, and a RECT that will be the source rectangle to blit.
void CGame::gdeClipper(CGapiSurface& source_surface, CGapiSurface& destination_surface, int x, int y, int& drawX, int& drawY, RECT& srcRect) {
srcRect.left = 0;
srcRect.top = 0;
srcRect.right = source_surface.GetWidth();
srcRect.bottom = source_surface.GetHeight();
int srcSizeX = srcRect.right;
int srcSizeY = srcRect.bottom;
int destSizeX = destination_surface.GetWidth();
int destSizeY = destination_surface.GetHeight();
drawX = x;
drawY = y;
// Left edge X clipping
if (x < 0 && x + srcSizeX > 0) {
srcRect.left = srcSizeX - (srcSizeX + x);
drawX = 0;
}
// Right edge X clipping
if (x + srcSizeX > destSizeX) {
srcRect.right = srcSizeX - (x - (destSizeX - srcSizeX));
}
// Top edge Y clipping
if (y < 0 && y + srcSizeY > 0) {
srcRect.top = srcSizeY - (srcSizeY + y);
drawY = 0;
}
// Bottom edge Y clipping
if (y + srcSizeY > destSizeY) {
srcRect.bottom = srcSizeY - (y - (destSizeY - srcSizeY));
}
}

As a usage example:

Code:

// Assume that mg_surface is a 200x200 surface that you previously created and
// did some drawing on, now you want to blit that to the screen in ProcessNextFrame

gdeClipper(*mg_surface, backbuffer, x, y, drawX, drawY, srcRect);
backbuffer.BltFast(drawX, drawY, mg_surface, &srcRect;, 0, NULL);

// Now, if mg_surface is partially off the screen in any direction, only the
// visible portion will be blitted.

It occurs to me that any clipping algorithm should work this way, yet I was unable to find one in over an hour of searching, hence I wrote it myself (further hence, it's probably no optimal and maybe even broke under some condition I haven't tested).
Thanks fzammetti, I'm sure that I will be able to put this to use.
Thanks for the post! I'll make it sticky...
does your method work if you are doing a rotation blt?
That's a good question, and the answer is... wait for it... I'm not sure!

Thinking about it though, I think the rotating blit in GD does the rotation as it blits, so this function wouldn't help you, as it detrmines the clipping rectangle BEFORE the blit occurs. Since I don't know the details of the rotating blit function (i.e., if it rotates and extends beyond the surface, like if you rotate a square it's corners may extend beyond the surface it occupies), what happens? Does the rotating blit clip at the surface boundries (in which case my clipping function is just clipping a rectangular surface anyway so it will still work) or does it not blit anything as most of the other functions seem to do if you extend beyond the edges of a surface? Or will the surface be expanded to accomodate the rotated image, in which case my function wouldn't help either because it determines the clipping rectangle BEFORE the blit occurs.

So, I suspect it won't help, but I'm not sure because I don't know how the rotating blit currently works. A little experimentation is in order I think.
jhund1 · 25 Jun 2003, 03:56 UTC — Another way....
Another way I've thought about (haven't implemented it yet) for clipping was making your backbuffer slightly bigger than the physical screen size and then just blitting the portion (usually the screen size) you need. I'm not sure if this is a more efficient way of going about things, but it does work with all the Gapidraw functions.

Josh
Not a bad idea actually. It'd use more memory than you have to, but that might be acceptable. Not sure if there would be any performance boost, and you might still have to worry about clipping on your buffer surface if images can hang off the edges of it.

I suppose if you did something like a 480x640 buffer surface and blit a square centered on it to the screen, i.e., source rect(120, 160, 360, 480), if my math is right, then you could get away with simple bounds checking rather than clipping, assuming none of your moving images were larger than the border area.

I actually like the idea! Anyone see a reason it wouldn't be good, aside from some added memory used?
I've actually implemented your idea in my current game project. It was really tailor-made for it: I have a 200x200 game area, and I blit all the action to that, then blit that to the screen (the rest of the screen doesn't change as often). Using your approach works fantastically well, I don't have to bother with clipping at all now, as long as my images fit in the overlap area it works out fantastic. The only thing that's somewhat tricker is that you have to adjust your blits to hit the correct portion of the buffer. For instance, in my case I have a 400x400 buffer surface, and my game components are in the "visible" portion defined by the rect (100, 100, 300, 300). So, when I want to blit a sprite, I have to add 100 to the X&Y; to get it in that area. That's somewhat annoying, but it's simpler than clipping, and I suspect it actually is a little faster since it's less code to run through each blit. Thanks for the idea, I don't know if it was obvious to everyone but me, but I'm glad you posted it because I didn't think of it myself.
That was me who posted the above. Is it just me or is PM's cookies not persisting as they should? I seem to have to log in almost every time I come here now.
fzammetti: The only negative aspect with this approach is that you are not achieving optimum performance. Using clipping, the blit from the "larger" surface to back buffer could be omitted. If you use a Pocket PC, you will loose around 2.8 ms for a 200x200 blit.
THIS IS FZAMMETTI... I CAN'T SEEM TO LOG IN RIGHT NOW...

Good info sbl, thank you! For my current game project this performance hit won't matter, but it's certainly something to keep in mind. I may even go away from this approach since it simplifies my code slightly, and I like clean code :)
Hi,

sbl wrote:fzammetti: The only negative aspect with this approach is that you are not achieving optimum performance. Using clipping, the blit from the "larger" surface to back buffer could be omitted. If you use a Pocket PC, you will loose around 2.8 ms for a 200x200 blit.


How would you create a pixel scroll of a tile-based engine then? Could it be done only with clipping and not drawing an extra row of tiles on the sides?

--Eric
Eric,

I do all my pixel-perfect tile scrolling using clipping. Setting clipping rects 'should' always be faster than BLTing extra tiles and creating a viewport.
I have not released anything, but I have also created scrolling tile engines which dont require an extra viewport blit. If you would like some demonstration code, I would be happy to help.
I would greatly appreciate some pointers. I use an extra row of tiles in the direction the player is walking because this is the way I made it a long time back when I was larning to use GapiDraw, but I feel I should now get rid of the extra tiles and the extra full-screen blit.

I suppose I need to keep a "walking" offset for both X,Y from the current position, determine the walk direction and draw & clip the tiles accordingly?

Thanks for any further info.

--Eric
I get the offset by using the modulus function (It returns the remainder). I'm pretty new to C++, but I would assume it's in math.h. In BASIC it looks something like this:

OffsetX = ViewPointX MOD TileSizeX
OffsetY = ViewPointY MOD TileSizeY

Anybody else do this?
Heres one of my level drawing functions for a smooth scrolling engine.

Code:
void cDrDraw::DrawLevel()
{
RECT rcRect;
int drawx,drawy;
for(int x=0;x<7;x++)
{
for(int y=0;y<9;y++)
{
drawx=(x*40)-leveloffsetx;
drawy=(y*40)-leveloffsety;
rcRect.left = view[x][y] % 6 * 40;
rcRect.top = view[x][y] / 6 * 40;
rcRect.right = view[x][y] % 6 * 40+40;
rcRect.bottom = view[x][y] / 6 * 40+40;

//X clipping---------------------------
if (drawx<0)
{
rcRect.left-=drawx;
drawx=0;
}
else if(drawx>200)
{rcRect.right+=(200-drawx);}
//Y clipping---------------------------
if(drawy<0)
{
rcRect.top-=drawy;
drawy=0;
}
else if(drawy>280)
{rcRect.bottom+=(280-drawy);}



backbuffer.BltFast(drawx,drawy,&tiles,&rcRect,
GDBLTFAST_KEYSRC,NULL);

}
}

}

Basically there is an offset that it checks to see how much of the tiles need to be clipped in farthest rows. The numbers 200, and 280 are just the (screen size-my tile size). Oh, and View is just a matrix that stores some shorts where I store the viewable level layout. Also the 6 in the rect assignment lines is how many tiles per row I have on my source surface.

Its fairly simple code, I just run through each element in my two dimentional array, set the source rectangle to the tile that I want to blit, and then modify the source rectangle if need be and blit to the screen.

Hope this can be of some help