• ### Announcements

#### Archived

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

# Can anyone spot the mistake in this code?

## Recommended Posts

theObserver    122
I created a c++ class for a directdraw project called Bitmap.But I am having trouble with this one meathod. Its purpose is so copy the bitmap onto an already created directdraw surface. 'Data' points to the actual bitmap data which is stored in the class. The thing is, it works fine when the Pitch == Bitmapwidth. When it doesnt, the bitmap is displayed wrong.
BOOL cBitmap::BitmapToSurface(LPDIRECTDRAWSURFACE7 lpddSurface,int ImageX, int ImageY)
{
DDSURFACEDESC2 ddsd;
UCHAR *Source;
UCHAR *Dest;

if (!Data || !lpddSurface)
return false;

memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);

if (FAILED(lpddSurface->Lock(NULL,&ddsd,DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,NULL)))
return false;

Source = Data + ImageX + (ImageY * BitmapWidth);
Dest = (UCHAR *) ddsd.lpSurface;

for (int Counter =0;Counter < BitmapHeight;Counter++){
memcpy(Dest,Source,BitmapWidth);
Dest += ddsd.lPitch;
Source += BitmapWidth;
}

lpddSurface->Unlock(NULL);
return true;
}


[edited by - theObserver on August 7, 2003 3:01:22 PM]

##### Share on other sites
Nik02    4348
You should jump the lPitch amount between each scanlines.
As ddraw docs tell you, the pitch isn''t necessarily the width*bitdepth/8 as you might assume. However, it is, in bytes, the memory width of a scanline (which could include some buffer space).

##### Share on other sites
theObserver    122
Doesnt the lpitch property from DDSURFACEDESC2 return the actual pitch of the surface (including any buffer)? My understanding was that once I locked the surface, I could use now valid lpitch from the DDSURFACEDESC2 to safely advance the scanline taking into account buffering etc.

##### Share on other sites
Neen10do    122
if you are trying to copy a bitmap onto another surface, why dont you use DirectDraw''s blt function or FastBlt. for one, it has to be faster than any method you are using. and two, it does it correctly!

##### Share on other sites
Nik02    4348
quote:
Original post by theObserver
Doesnt the lpitch property from DDSURFACEDESC2 return the actual pitch of the surface (including any buffer)? My understanding was that once I locked the surface, I could use now valid lpitch from the DDSURFACEDESC2 to safely advance the scanline taking into account buffering etc.

You''re right.