Sign in to follow this  

Sprite Loader SOLVED

This topic is 4203 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I made a function that loads a Part of a Surface and stores it in a new Surface. But it don't work. The new created Surface is always full of black and some white pixels. NOTE: I use 8BPP Mode
bool LoadSprite(LPDIRECTDRAWSURFACE7& rpDDSImage, LPDIRECTDRAWSURFACE7& rpDDSSprite,
				int nSpriteWidth, int nSpriteHeight, int nSpriteX, int nSpriteY)
{
	// Struktur vorbereiten
	DDSURFACEDESC2 ddsd;
	memset(&ddsd, 0, sizeof(ddsd));
	ddsd.dwWidth = sizeof(ddsd);

	// Image locken
	if(rpDDSImage->Lock(NULL, &ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL))
		return(false);

	int nImageWidth		= (int)ddsd.dwWidth;
	int nImageHeight	= (int)ddsd.dwHeight;
	int nImagePitch		= (int)ddsd.lPitch;

	int nStartX			= nSpriteX*nSpriteWidth;
	int nStartY			= nSpriteY*nSpriteHeight;

	BYTE* pbyImage		= (BYTE*)ddsd.lpSurface;

	// Bild ist groß genug?
	if(nSpriteWidth*nSpriteX > nImageWidth || nSpriteHeight*nSpriteY > nImageHeight)
	{
		rpDDSImage->Unlock(NULL);
		return(false);
	}

	// Speicher für das Sprite bereitstellen
	BYTE* pbyTemp = new BYTE[nSpriteWidth*nSpriteHeight];

	for(int nY=nStartY;nY<nSpriteHeight+nStartY; nY++)
	{
		for(int nX=nStartX;nX<nSpriteWidth+nStartX;nX++)
		{
			pbyTemp[nX + (nY * nSpriteWidth)] = pbyImage[nX + (nY * nImagePitch)];
		}
	}

	// Image freigeben
	if(rpDDSImage->Unlock(NULL))
		return(false);

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

	// Sprite Surface locken
	if(rpDDSSprite->Lock(NULL, &ddsd, DDLOCK_WAIT |DDLOCK_SURFACEMEMORYPTR, NULL))
		return(false);

	int	nSpritePitch	= (int)ddsd.lPitch;

	BYTE* pbySprite		= (BYTE*)ddsd.lpSurface;

	// Daten umkopieren
	for(int nY=0;nY<nSpriteHeight; nY++)
	{
		for(int nX=0;nX<nSpriteWidth;nX++)
		{
			pbySprite[nX + (nY * nSpritePitch)] = pbyTemp[nX + (nY * nSpriteWidth)];
		}
	}

	// Surface freigeben
	if(rpDDSSprite->Unlock(NULL))
		return(false);

	// Speicher freigeben
	delete pbyTemp;
	pbyTemp =NULL;

	return(true);
}[/code]





Please help, otherwise my project will never finish [tears] [Edited by - m4ster on January 14, 2007 12:42:07 PM]

Share this post


Link to post
Share on other sites
This doesn't realy answer your question - But why don't you lock both surfaces and just copy directly between them? You should be able to copy a row at a time (width_of_the_sprite bytes) too, which would save you the inner loop.

Share this post


Link to post
Share on other sites
I don't lock both surfaces the same time, because I don't like it. I tryed to make clean and easy code and in my opinion locking 2 surfaces the same time seems to be too complex. [lol]

Share this post


Link to post
Share on other sites
Hey,
I found the mistake by myself.

It's pretty easy and very very very stupid [lol].


// Struktur vorbereiten
DDSURFACEDESC2 ddsd;
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwWidth = sizeof(ddsd);// Has to be: ddsd.dwSize




[Edited by - m4ster on January 14, 2007 12:24:27 PM]

Share this post


Link to post
Share on other sites

This topic is 4203 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this