Using CopyRects in C#

Devoid

New member
Joined
Dec 19, 2002
Messages
1
Location
Sydney, Australia
Has anybody out there used the CopyRects in C# successfully?

Basically I have a bitmap that I have loaded onto a surface that contains about a dozen tiles, all 32 x 32 pixels. I then try to render the scene by looping through my map array (20 tiles by 15 tiles )which contains the numbers 1 through 12 to represent the tile at the location.

What I want to do, find the tile number (lets say 3) which will start (3 * 32 ) pixels into the tile bitmap for a length and height of 32 pixels. I want to copy that rectangle to the appropriate place on back buffer.

The C++ code that I am trying to convert looks like this:

RECT SrcTileRect;
POINT DstPoint;
SrcTileRect.left = TileNumber2SourceX(TileNumber, numTileCols, tileSize);
SrcTileRect.right = SrcTileRect.left + tileSize;
SrcTileRect.top = TileNumber2SourceY(TileNumber,
numTileCols, tileSize);
SrcTileRect.bottom = SrcTileRect.top + tileSize;
DstPoint.x = Column2X(DstCol, tileSize, numMapCols) + xOffset;
DstPoint.y = Row2Y(DstRow, tileSize) + yOffset;
HRESULT hResult = m_Direct3D.GetDevice()->
CopyRects(pTileSurface, &SrcTileRect,
1, pBackBuffer, &DstPoint);
return hResult;

My converted C# codes is as follows:

C#:
DxVBLibA.RECT srcTileRect = new DxVBLibA.RECT();
DxVBLibA.POINT dstPoint = new DxVBLibA.POINT();

srcTileRect.left = this.TileNumber2SourceX( tileNumber, numTileCols, tileSize );
srcTileRect.right = srcTileRect.left + tileSize;
srcTileRect.top = this.TileNumber2SourceY( tileNumber, numTileCols, tileSize );
srcTileRect.bottom = srcTileRect.top + tileSize;

dstPoint.x = this.Column2X( dstCol, tileSize, numMapCols ) + xOffset;
dstPoint.y = this.Row2Y( dstRow, tileSize ) + yOffset;

this.OurDirect3D.DeviceD3D.CopyRects( tileSurface, &srcTileRect,	1, backBuf, &dstPoint );

I have everything converted fine up until the copyrects line and am having problems with the &SrcTileRect and &DstPoint parameters. Error is cannot convert from DxVBLibA.RECT* to System.IntPtr.

Anybody have an example of something similiar? I have used the copyrects before when I wanted to copy the entire source to the desintation (the source tile rectangle and point are both passed as null).

Cheers,
Devoid
 
Last edited by a moderator:
You could try the StructureToPtr method of the Marshal class, if you have no control over the declare of the CopyRects method.
 
You can try the following. If you want to use StructureToPtr, youll have to allocate the memory for the IntPtr first.

Code:
GCHandle hSrc = GCHandle.Alloc(srcTileRect, GCHandleType.Pinned);

GCHandle hDest = GCHandle.Alloc(dstPoint, GCHandleType.Pinned);

this.OurDirect3D.DeviceD3D.CopyRects(tileSurface, hSrc.AddrOfPinnedObject(), 1, backBuf, hDest.AddrOfPinnedObject());

hSrc.Free();

hDest.Free();

-ner
 
Back
Top