Jump to content
  • Advertisement

Archived

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

silentrob66

Targa File and DirectDraw Problem

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

Hey guys, This problem is really starting to get to me. I'm cutting and pasting my function that loads a targa(*.tga) file to it's own 16bit surface. It works if I load 16bit color Targa files. However, when I load 24bit color files, only the last file loaded can be seen. The images loaded before it turn pure black. If anyone has any ideas as to what I'm doing wrong, please let me know. Oh, and by the way, I'm already aware that my code is horrible, hard to read, and ugly, so please don't remind me Thanks in advance!
  
int LoadTargaFile(char* filename,LPDIRECTDRAWSURFACE &surface)
{
	DDSURFACEDESC ddsd;
	int file = _open(filename,_O_RDONLY | _O_BINARY);
	BYTE* byte_buffer = NULL, *byte_surface = NULL;
	WORD* word_buffer = NULL, *word_surface = NULL;
	int image_length = 0, byte_count = 0;
	WORD i = 0, n = 0;
	WORD wTemp = 0;
	BYTE bTemp = 0, bRTemp = 0, bGTemp = 0, bBTemp = 0;

	TargaHeader header;
	memset(&header,0,sizeof(header));
	if(_read(file,&header,sizeof(header)) <= 0)
	{
		close(file);
		return FALSE;
	}

	lseek(file,header.bIDLength + header.wColorMapLength,SEEK_CUR);
	image_length = (header.wHeight * header.wWidth) * (header.bBitsPerPixel / 8);
	byte_buffer = (BYTE*)malloc(image_length);
	memset((void*)byte_buffer,0,image_length);
	word_buffer = (WORD*)byte_buffer;

	if(header.bImageType != 2)
	{
		if(byte_buffer)
			free(byte_buffer);
		close(file);
		return FALSE;
	}
	
	if(_read(file,byte_buffer,image_length) < image_length)
	{
		if(byte_buffer)
			free(byte_buffer);
		close(file);
		return FALSE;
	}

	memset(&ddsd,0,sizeof(ddsd));
	ddsd.dwSize = sizeof(ddsd);
	ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
	ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
	ddsd.dwHeight = header.wHeight;
	ddsd.dwWidth = header.wWidth;
	if(FAILED(g_pDD->CreateSurface(&ddsd,&surface,NULL)))
	{
		if(byte_buffer)
			free(byte_buffer);
		close(file);
		return FALSE;
	}

	surface->GetSurfaceDesc(&ddsd);
	if(FAILED(surface->Lock(NULL,&ddsd,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL)))
	{
		if(byte_buffer)
			free(byte_buffer);
		close(file);
		return FALSE;
	}
	memset(ddsd.lpSurface,0,image_length);
	byte_surface = (BYTE*)ddsd.lpSurface;
	word_surface = (WORD*)ddsd.lpSurface;

	switch(ddsd.ddpfPixelFormat.dwRGBBitCount)
	{
	case 15:
		// need to implement

		break;
	case 16:
		for(i = 0;i < header.wHeight;i++)
		{
			for(n = 0;n < header.wWidth;n++)
			{
				switch(header.bBitsPerPixel)
				{
				case 16:
					
					wTemp = word_buffer[(i * header.wWidth) + n];
					wTemp = ( ( (wTemp & _15BIT_REDMASK) << 1) | 
							( (wTemp & _15BIT_GREENMASK) << 1) |
							(wTemp & _15BIT_BLUEMASK) );
					word_surface[(((ddsd.dwHeight - 1) - i) * (ddsd.lPitch / 2)) + n] = wTemp;
					break;
				case 24:
					wTemp = (((byte_buffer[(((i * header.wWidth) + n) * 3) + 0] >> 3) & 0x1F) ) | 
							(((byte_buffer[(((i * header.wWidth) + n) * 3) + 1] >> 2) & 0x3F) << 5) | 
							(((byte_buffer[(((i * header.wWidth) + n) * 3) + 2] >> 3) & 0x1F) << 11);
					word_surface[(((ddsd.dwHeight - 1) - i) * (ddsd.lPitch / 2)) + n] = wTemp;
					break;
				default:
					if(byte_buffer)
						free(byte_buffer);
					close(file);
					surface->Unlock(ddsd.lpSurface);
					return FALSE;
				}
			}
		}
		break;
	case 24:
		// Need to implement

		break;
	case 32:
		// need to implement

		break;
	default:
		if(byte_buffer)
			free(byte_buffer);
		close(file);
		surface->Unlock(ddsd.lpSurface);
		return FALSE;
	}

	surface->Unlock(ddsd.lpSurface);
	if(byte_buffer)
		free(byte_buffer);
	close(file);

	return TRUE;
}
  
[edited by - silentrob66 on May 30, 2002 10:48:33 PM]

Share this post


Link to post
Share on other sites
Advertisement
I''m not expecting an answer, but I would like some opinions. Anyone have similar problems in the past? If so, what fixed it? Anyone have any ideas off the top of thier head? Thanks again, guys.

Share this post


Link to post
Share on other sites
I can''t see anything thats immediately obvious, but are you sure the problem lies in this piece of code?
Are you sure the other surfaces aren''t getting lost somewhere?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!