Jump to content
  • Advertisement
Sign in to follow this  
Little Coding Fox

Bitmap loading in opengl in c++ WITHOUT any libraries

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

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?

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!