Sign in to follow this  
michaelvoit

updatesurface: weird results

Recommended Posts

hi, i try to create a font engine using freetype. freetype is returning a bitmap of each character i need to copy onto a texture to receive a big texture of all my characters i'm gonna use. so far so good. for this i changed my existing code, so i can copy one texture (from system memory) onto another one (which is is created using D3DPOOL_DEFAULT - as updatesurface requests it). the weird results now are, that if i create a texture in sysmem and then copy that texture onto the other one, and then render the new texture, i also get parts of my old texture (the original) onto the screen. i tried to only create a new texture without filling it: even here i get the old results from previous instances of my application. when i create a texture in managed pool though everything works fine (Except that i can't call updatesurface). please tell me if there is something wrong with my code or if i missed something...i can't seem to get this to work! i am using the following code:
void CTexture_Direct3D::copyTexture(const int& srcx, const int& srcy, 
									const int& srcwidth, const int& srcheight,
									const int& dstx, const int& dsty,
									const CTexture* Source)
{
	// in directx there are some restrictions for copying textures:
	// 1. the source texture must lie in system memory
	// 2. the destination texture must lie in video memory
	// 3. they both need to have the same format
	// 4. the rectangle that is about to be copied must fit in the destination texture
	// 5. no stretching allowed!

	if (!((CTexture*)Source)->getIsTextureInSysMem()) CError::throwError("Error when copying a texture onto another one: the source texture must lie in system memory.");
	if (m_SystemMem) CError::throwError("Error when copying a texture onto another one: the destination texture must lie in video memory.");
	if (((CTexture*)Source)->getTextureFormat() != m_TextureFormat) CError::throwError("Error when copying a texture onto another one: the destination format must be the same as the source format.");
	if ( (m_TextureWidth - dstx < srcwidth) || (m_TextureHeight - dsty < srcheight) ) CError::throwError("Error when copying a texture onto another one: the source area is too large in size for the destination texture. There is no stretching allowed!");

	HRESULT result;

	CTexture_Direct3D* SourceTexture = (CTexture_Direct3D*) Source;

	// get surface of source texture

	IDirect3DSurface9* SourceSurface = NULL;	
	result = (*SourceTexture->getDirect3DTexture())->GetSurfaceLevel(0, &SourceSurface);
	if (FAILED(result))
	{
		CError::throwError("Getting the surface level 0 of a texture failed.");
	}
	
	// get surface of this destination texture

	IDirect3DSurface9* DestSurface = NULL;
	result = m_Texture->GetSurfaceLevel(0, &DestSurface);
	if (FAILED(result))
	{
		CError::throwError("Getting the surface level 0 of a texture failed.");
	}

	// copy source surface to this texture

	RECT rect;
	rect.top = srcx;
	rect.left = srcy;
	rect.right = srcx + srcwidth;
	rect.bottom = srcy + srcheight;

	POINT point;
	point.x = dstx;
	point.y = dsty;

	LPDIRECT3DDEVICE9 Device = (((C3DRendering_Direct3D*)m_Renderer)->getDirect3DDevice());
	result = Device->UpdateSurface(SourceSurface, &rect, DestSurface, &point);

	if (FAILED(result))
	{
		CError::throwError("Copying a surface to another one failed.");
	}	

	DestSurface->Release();
	SourceSurface->Release();
}


this is how i create my textures:
void CTexture_Direct3D::createEmptyTexture(const int& width, const int& height, const TEXTUREFORMAT& format, const bool& SystemMem)
{
	HRESULT result;

	D3DFORMAT D3D9Format;
	enum TEXTUREFORMAT{R5G6B5, A8R8G8B8, R8G8B8};

	switch(format) 
	{
		case R5G6B5:	D3D9Format = D3DFMT_R5G6B5;
						break;
		case A8R8G8B8:	D3D9Format = D3DFMT_A8R8G8B8;
						break;
		case R8G8B8:	D3D9Format = D3DFMT_R8G8B8;
						break;
		default:		D3D9Format = D3DFMT_R8G8B8;
						break;
	}

	result = D3DXCreateTexture(	((C3DRendering_Direct3D*)m_Renderer)->getDirect3DDevice(), 
								width, height, 
								1,	// miplevels
								0,	// usage
								D3D9Format, 
								SystemMem ? D3DPOOL_SYSTEMMEM : D3DPOOL_DEFAULT, 
								&m_Texture);

	if (FAILED(result))
	{
		CError::throwError("Creating a new texture failed.");
	}

	m_TextureWidth = width;
	m_TextureHeight = height;
	m_TextureFormat = format;
	m_SystemMem = SystemMem;
}

Share this post


Link to post
Share on other sites
Im not sure I understood your problem. But it sounds like you have a bunch of bitmaps with letters. And you are blitting them to one large 'container' texture in the video memory pool.
But UpdateSurface is causing the destination to contain only one letter?
Maybe you can clarify. Another option is to use UpdateTexture instead, which is meant to copy textures, rather than surfaces (check your src/dest table in UpdateTexture to see if you are meeting the requirements).

Another option is to put everything in video memory and use SetRenderTarget and ID3DXSprite to render the source textures to your destination (this would be faster than UpdateSurface or UpdateTexture, as it is a dev-mem to dev-mem blit, rather than sysmem to dev-mem)

Share this post


Link to post
Share on other sites
hi, and thanks for your fast answer.

no, i am not even there where i want to copy bitmaps onto one big texture. at the moment i am sticking to get surface copying up and running at all.

here is a picture of the texture i want to copy

i want to copy that above picture onto an empty texture with the size 640x480.

this is the result

even if i run my program again later and do _not_ copy the particle-texture i get that werid result....something'S definitely wrong here :-(

Edited by Coder: Fixed links. Please consult GDNet Forums FAQ for post formatting guidelines

[Edited by - Coder on August 4, 2004 2:22:35 PM]

Share this post


Link to post
Share on other sites

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