Can anyone spot the mistake in this code?

Started by
3 comments, last by theObserver 20 years, 8 months ago
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]
Advertisement
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).

Niko Suni

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.
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!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
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.

Niko Suni

This topic is closed to new replies.

Advertisement