Bitmap loading in opengl in c++ WITHOUT any libraries

Started by
4 comments, last by morx 19 years, 7 months ago
helo everyone! i realy need your help: i want to make a class for my game engine that loads bitmaps from file e.g (bool Bitmap::Loadbitmap(const char* filename, GLuint &texture) without any library like the AUX or SDL. Can you please help me?
Advertisement
You'd have to parse the file format yourself.

This ought to help: Clicky!

Good luck.
This article should help: How to Load a Bitmap
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
My own source that works with 24-bit bitmaps:
http://www.nife.1go.dk/c++.php?id=1
And one for targa if you need it some day (24 and 32-bit):
http://www.nife.1go.dk/c++.php?id=2
Killers don't end up in jailThey end up on a high-score!
if you're going to use a bitmap then you may as well step up and use a .tga because they support an alpha channel.
Well, here's some code if it helps.. by the way, just replace the stream.get(), stream.read(), and so on with standard c library calls... for example.. stream.get() because fgetc(), read becomes fread(), etc.

Btw, here's some of the simple types
S8 - signed char (1 byte)
U8 - unsigned char (1 byte)
S16 - signed short (2 bytes)
U16 - unsigned short (2 bytes)
S32 - signed long (4 bytes)
U32 - unsigned long (4 bytes)

bool Bitmap::loadBmp( IStream &stream ) {	if (!stream.initialized())		return false;	S16 id;	U32 offset;	U32 headerSize;	U32 width, height;	U16 bpp;	U32 compression;	U32 dataSize;	stream.read(&id, sizeof(S16));	if (id != 'MB')		return false;	stream.seek(8, STREAM_CUR);	stream.read(&offset, sizeof(U32));	stream.read(&headerSize, sizeof(U32));	if (headerSize != 40)		return false;	stream.read(&width, sizeof(U32));	stream.read(&height, sizeof(U32));	stream.seek(2, STREAM_CUR);	stream.read(&bpp, sizeof(U16));	if (bpp != 24)	// currently only support 24-bit bitmaps		return false;	stream.read(&compression, sizeof(U32));	if (compression != 0)		// currently only support non-compressed bmp's		return false;	stream.read(&dataSize, sizeof(U32));	if (dataSize == 0)		dataSize = (width * bpp + 7) / 8 * abs(height);	stream.seek(-(S32)dataSize, STREAM_END);	mWidth = width;	mHeight = abs(height);		mData = new U8[mWidth * mHeight * 3];	if (!mData)		return false;	// read in data, flipping it as we read vertically, plus converting BGR -> RGB	U8 *p = mData + (mHeight - 1) * mWidth * 3;	U8 byte1, byte2, byte3;	// if mWidth is not a multiple of 4, then there is extra crap at the end of every line	S32 paddedBytes = (mWidth % 4) ? (4 - ((mWidth * 3) % 4)) : 0;	for(S32 y = 0; y < mHeight; y++)	{		for(S32 x = 0; x < mWidth; x++)		{			if (stream.eos())			{				destroy();				return false;			}			// test for last pixel to read			if( y == mHeight - 1 && x == mWidth - 1)			{				int z = z * 65;										}						byte1 = (U8)stream.get();			byte2 = (U8)stream.get();			byte3 = (U8)stream.get();			*p++ = byte3;	// swap BGR->RGB			*p++ = byte2;			*p++ = byte1;		}		// read the extra crap (align on 32-bit (4 byte) boundary)		for (S32 padCount=0; padCount<paddedBytes; padCount++)			stream.get();		// work bottom to top		p -= (mWidth * 3) * 2;	}	mFormat = BITMAP_FMT_24;	return true;}

This topic is closed to new replies.

Advertisement