Advertisement Jump to content


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


Loading Textures manually

This topic is 6545 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

I''m writting my own texture loader, to open a raw 24 bit image file, convert to 16 bit and set it to the texture interface. When I use d3dx it works fine but the following code doesn''t seem to work.
#include "Globals.h"
#include <stdio.h>

#define REDMASK (0xFF0000)
#define GREENMASK (0x00FF00)
#define BLUEMASK (0x0000FF)
#define RGB16BIT(r,g,b) ((b >> 3) | ((g >> 2) << 5) | ((r >> 3) << 11))

int LoadTexture(char* filename)
	FILE *hdimage;
	unsigned char* image = NULL;
	unsigned int tempImage;
	int i;
	unsigned char red, green, blue;
	USHORT color = 0;

	g_pTex->LockRect(0, &LockedRect, NULL, 0);
	if((hdimage = fopen(filename, "rb")) == NULL)
		return E_FAIL;

	if((image = (unsigned char*)malloc(131072)) == NULL)
		return E_FAIL;

	for(i = 0; i < 65536; i++)
		fread(&red, 1, 1, hdimage);
		fread(&green, 1, 1, hdimage);
		fread(&blue, 1, 1, hdimage);
		*(image+(i*2)) = color = RGB16BIT(red, green, blue);

	memcpy(LockedRect.pBits, image, sizeof(image));

	return S_OK;
Any help would be appreciated. Etran1

Share this post

Link to post
Share on other sites
What do you mean by ''doesn''t seem to work''?
Does it show anything at all or are the colors just garbled or something?

Your code also has some efficiency flaws.
Why do you create a memory buffer instead of just loading it directly into the surface? The ''color'' variable is also unnecessary.

Some things that may cause your problem:

- You ignore the pitch of the surface. There is a possibility of memory gaps between scanlines.
- You ignore the pixelformat of the surface.
- etc...

Share this post

Link to post
Share on other sites
I know it has some efficiency problems. Those I can rectify once I get the code working. What seems to happen is that there is one pixel that shows up in the upper left corner, and the rest is black. The color variable, I forgot to remove, it was there for some debugging code that I forgot to take out.

The pixel format for the texture is R5G6B5. I know how the pitch of a surface works in DDraw, but I''m not sure how it is applied here. Also, once the image is copied into the texture interface, do you need to do anything other than just

g_pD3DDevice->SetTexture(0, g_pTex); 

Here is my Initializing function for the texture interface:

int InitTexture(void)
if(FAILED(g_pD3DDevice->CreateTexture(256, 256, 3, 0, D3DFMT_R5G6B5, D3DPOOL_MANAGED, &g_pTex)))
return E_FAIL;

return S_OK;


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!