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.
As a usage example:
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).
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).