Archived

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

DDS texture Loader

This topic is 5113 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, im trying to make my own DDS texture loader in C(not c++) i have been working on it for a little to long with many of the same errors poping up again and again. If you would all be so kind as to direct me as to what portion of my loader is wrong.
static void InitalizeTextures(D3DPointers *data)
{
	
	D3DLOCKED_RECT pLockedRect;
	//DDSURFACEDESC2 *ddDesc;

	FILE *in;
	VOID *ImgDat;
	VOID *bla;
	VOID *Header;
	DWORD *Size;
	int read = 0;
	
	in=fopen("img.dds","rb");
	
	Header = malloc(sizeof(DWORD)*10);
	bla = malloc(sizeof(DWORD)*22);
	Size = malloc(2);

	fread(Header,1,12,in);
	fread(Size,1,2,in);
	fread(bla,1,22,in);
	
	ImgDat = malloc(262144);
	//MessageBox(data->hWnd,read ,in,0);

	read = fread(ImgDat,1, 262144, in);

	data->m_pD3DDevice->lpVtbl->CreateTexture(data->m_pD3DDevice,512,512,0,NULL, D3DFMT_DXT5, D3DPOOL_MANAGED, &data->TextureArray[0], NULL);
	data->TextureArray[0]->lpVtbl->LockRect(data->TextureArray[0],0,&pLockedRect, NULL, NULL);
	pLockedRect.pBits = ImgDat;
	//pLockedRect.Pitch = Size;

	data->TextureArray[0]->lpVtbl->UnlockRect(data->TextureArray[0],0);
	free(ImgDat);
	free(bla);
	free(Header);
	fclose(in);
	
}

Share this post


Link to post
Share on other sites
First of all, I believe the DDS header is 128 bytes. (4 bytes of magic-number + 124 bytes for the DDSURFACEDESC2.) From what I can tell, you are only skipping 36 bytes (12+2+22) before you try to read your image data.

Secondly, you can''t just lock your surface and change the value of the pBits pointer. You have to actually copy your data. Something like memcpy(pBits, ImgDat, 262114) would work. Or you could read the data directly into your surface:

in = fopen(...);
fseek(in, 128, SEEK_CUR);
pTexture->LockRect(...);
fread(pLockedRect->pBits, pLockedRect->Size, 1, in);
pTexture->UnlockRect();
fclose(in);

xyzzy

Share this post


Link to post
Share on other sites
thanks so much your posting allowed me to go back to an earlyer version and find out that the reason i was haveing trouble with it was i thought the magic number was 2 bytes i know i saw that in the Dx docs but i guess not. well thanks alot for you help and may all you debugging be short.

Share this post


Link to post
Share on other sites