Sprite Loader SOLVED

Started by
2 comments, last by m4ster 17 years, 10 months ago
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);
}
Please help, otherwise my project will never finish [tears] [Edited by - m4ster on January 14, 2007 12:42:07 PM]
Advertisement
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.
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]
Hey,
I found the mistake by myself.

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

// Struktur vorbereitenDDSURFACEDESC2 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]

This topic is closed to new replies.

Advertisement