This topic is now archived and is closed to further replies.


Does anyone use IDirectDrawClipper

Recommended Posts

Ive been trying to figure out how to use IDirectDrawClipper in delphi and havent found any articles in delphi and only one in C++. I was wondering if anyone uses it, is there a faster way? Any code in Delphi would be apprieciated(or even in C++).

Share this post

Link to post
Share on other sites
Clipping is generelly defined as "not drawing pixels or image elements that are out of bounds of viewport or the window"
So figur out what is to see on a scene and draw it, THIS is the fastest way to draw.

The DirectDrawClipper:
The DirctDraw Clipper works only if you are Using the Blt or BltFast Function from DXDraw that only works with Bitmaps(Surfaces) and NOT when you want to write directly in the Video Memory via Surface->Lock, to get a pointer to the Surface Memory.
When you also want to Copy directly from a Surface to the Backbuffer (Surface) via Software you must care that you are not write off in invalid Memory.

The windowed application:
But if you want to make your application is going to be windowed, or you want to render Bitmaps that could potentially go out of Bounds of the visible DirectDraw Surface you must create a single Clipper object that have the size of the visible Window.


So Clipping a pixel:
You Want to clip a pixel with coordinates (x,y), to a viewport located (x1,y1) to (x2,y2).
If (x,y) is within the rectangle defined by (x1,y1) to (x2,y2), render it; otherwise dont´.

// && is logical AND in C (I dont speak delphi)
if (x>=x1 && x<=x2 && y>=y1 && y<=y2)
{//Beginn Block
//Plot the Pixel
videoBuffer[x+y*Pitch] = color;
//Pitch is the real surface width in bytes
}//End Block

Clipping a Bitmap: (The Hard way)
The plan of attak is simple -- you just clip the virtual rectangle of the bitmap to the viewport and then draw only the portions of the Bitmap that are in the clipped bitmap.

So we want to define the Viewport to
#define ScreenBeginnX 0
#define ScreenBeginnY 0
#define ScreenWidth 640
#define ScreenHeight 480

making a Funktion like
void BltClp (int x,int y,int width,int height,
UCHAR* Bitmap,UCHAR* VideoBuffer)
{ // Beginn Block
// Bitmap is a Byte Array of Bitmap Data
// VideoBuffer is a Byte Array of the Surface or Backbuffer
// x,y is the Starting Point
// widht,height is the size of the Bitmap

// First we check is the Bitmap visible on Viewport
// || is logical OR
if ((x>=ScreenWidth) || (y<= ScreenHeight)) ||
((x + widht) <= 0) || ((y+height) <= 0)
return; // If not so return

// else Clipping Source Rectangle
// Pre Compute the bounding rect to make your life easy
int x1 = x;
int y1 = y;
int x2 = x1 + width - 1;
int y2 = y2 + height - 1;

// So we check the upper left corner
if (x1 < ScreenBeginnX)
x1 = 0;
if (y1 < ScreenBeginnY)
y1 = 0;
// Now the lower left corner
if (x2 >= ScreenWidth)
x2 = ScreenWidth-1;
if (y2 >= ScreenHeight)
y2 = ScreenHeight-1;

// So we know what portions of the Bitmap from (x1,y1)to(x2,y2)
// are Visible
// Compute offsets into bitmap on x,y axes, we need this to
// compute the new starting Point
int xoff = x1 - x;
int yoff = y1 - y;
// compute number of colums and rows to blitt
int destx = x2 - x1 + 1;
int desty = y2 - y1 + 1;
// compute the Startaddress in Videobuffer
// Pitch = Real Width of Surface in Bytes
VideoBuffer += (x1+y1*Pitch);
// Compute the new starting Address in bitmap to scan data from
Bitmap += (xoff + yoff*width);
// Now we know the first point of pixel in the Bitmap thats be
// blitted, and the videobuffer is pointing to the memory
// location on destination buffer to put it.
// So we want to copy the source to the destination

UCHAR Pixel; // A pixel in 8Bit Mode(CHAR) 16Bit is called USHORT

for (int indexy = 0; indexy < desty; indexy++)
for (int indexx = 0; indexx < destx;index++)
// Read Pixel from Source to Destination
// Test for Transparency and plot
if ((pixel = Bitmap[index_x]))
VideoBuffer[index] = pixel;
// Play with pointers
VideoBuffer += Pitch; // Bytes per Scan Line
Bitmap += width; // Bytes per Bitmap Row
}// End of indexx
}// End of indexy

So thats all, and sorry for the terrible English

Share this post

Link to post
Share on other sites
Well I got DDClipper to work but its mighty slow, and will try to blt with the lock function and use your approach to the clipping, thanks a bunch night hawk, oh and your english is pretty good to

A good game by any other name is still a good game, STOP the RPG mentality

Share this post

Link to post
Share on other sites