Skip to main content
GameDev.net gamedev.net
🔒 Locked

bmp loading problem

Started by darksteel Nov 4, 2004 at 12:47 PM 5 replies 950+ views
Original Post
darksteel
darksteel
I have been having some difficulty with a bmp loading fucntion i am trying to write using only c++. Here is the code for my function.


unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
	fstream File;
	
	BITMAPFILEHEADER	bitmapFileHeader;	
	int			imageIdx = 0;		
	unsigned char		tempRGB;			

	File.open(filename, ios::in|ios::binary);

	if(File.is_open())
	{
	File.read((char*)&bitmapFileHeader, sizeof(BITMAPFILEHEADER));

		if (bitmapFileHeader.bfType != BITMAP_ID)
		{
			File.close();
			return 0;
		}

		File.seekg(bitmapFileHeader.bfOffBits, ios::beg);
		
		bitmapImage = new paul [bitmapInfoHeader->biSizeImage];

	    if (!bitmapImage)
		{
			delete[] bitmapImage;
			File.close();
			return 0;	
		}

		File.read((char*)bitmapImage,bitmapInfoHeader->biSizeImage);
	if(bitmapImage==NULL)
		{
			File.close();
			return 0;
		}
		for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
		{
			tempRGB = bitmapImage[imageIdx];
			bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
			bitmapImage[imageIdx + 2] = tempRGB;
		}
	File.close();
	}
	else
	{
		return 0;
	}
	return bitmapImage;
}


The call is made like this bitmapData=LoadBitmapFile("pic.bmp",&bitmapInfoHeader); I know the problem is not with displaying te texture as I have got the texture to display properly using someone elses function. DS [Edited by - darksteel on November 5, 2004 8:10:05 AM]
Android_s
Android_s
To pinpoint any problem it would be good to know if you got any error messages whn compiling. If your code look exactly like that you should have gotten a few [smile]

Oh, and use the[ source] and [ /souce] tags to post code (without the whitespaces of course...)
----------------------------------------------------------------------------------------------------------------------"Ask not what humanity can do for you, ask what you can do for humanity." - By: Richard D. Colbert Jr.
owiley
owiley
hey man what your pproblem? do it compile or do it spit out a gazirion errors
Bring more Pain
darksteel
darksteel
It compiles and I get no errors it runs okay but the image isnt loaded. Thanks for the quick reply though.
CrystalClear
CrystalClear
Cant say what your problem is but this is a lot simpler

bool Texture::Load( char *name ){	HBITMAP hbmp;	BITMAP	bmp;	glGenTextures( 1, &texture[0] );	hbmp=(HBITMAP)LoadImage( NULL, name, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION );	if( hbmp!=NULL )	{		GetObject( hbmp, sizeof(bmp), &bmp );		glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );		glBindTexture( GL_TEXTURE_2D, texture[0] );		glTexImage2D( GL_TEXTURE_2D, 0, 3, bmp.bmWidth, bmp.bmHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, bmp.bmBits );		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );		DeleteObject( hbmp );		return true;	}	return false;}
owiley
owiley
do you get a message box from this
if (!bitmapImage)		{			MessageBox(/*your HWND*/,"error bitmap could not load!","eror!!!!!!",MB_OK);			delete[] bitmapImage;			File.close();			return 0;			}
Bring more Pain
darksteel
darksteel
Thanks for the help. I have solved the problem it was to do with the memory used to hold the image data but its working now thanks to everyone who replied Cheers

DS

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.