Skip to main content
gamedev.net
🔒 Locked

Loading Bitmaps...

Started by AinigmA Oct 7, 2001 at 12:41 AM 13 replies 750+ views
Original Post
AinigmA
AinigmA
Nuubie alert!!! I am trying to make a map editor, but for some reason i cant figure out how to load bitmaps into an array, and then display them to the screen. can someone help me how out please. O_o Thanks Edited by - AinigmA on October 7, 2001 1:42:44 AM
...
The Optimizer
The Optimizer
Well, you actually have 2 separate tasks here, Loading and drawing.

The first question is this: What format are your bitmaps in (.bmp, .tga, etc?) Do you have code working already to load ONE bitmap in and draw to the screen?

The second part is the management of multiple images. What language are you programming in?

-Matt P
Developer: AoE/AoK
-Mp(Developer on AoE/AoK/AoM, and a host of canceled projects)
TANSTAAFL
TANSTAAFL
you''re quickly putting me out of my job, matt.
Get off my lawn!
AinigmA
AinigmA
Im loading .bmp, and no i dont have working code for loading the bitmaps. language is C.
Unfortunatley, this is for school, so i have to use the very basics, and come up with everything on my own.
To be more specific, here is my problem. If i make a 32x32 picture in mspaint, how do i read it and store it into an array so that i can use it to put on the screen. or do i even need to store it in an array.

I have tried opening the file and storing it in an array by using getc(), and fread(). I was able to do it both ways, but when i try printing it to the screen it comes out wierd. I used putpixel() to try to display it to the screen. Im not exactly sure what i am doing wrong. I haven''t been able to find any information on reading and printing individual bitmaps, or how bitmaps are made.
...
AinigmA
AinigmA
oops, posted twice.
...
Null and Void
Null and Void
First of all, using C++ wouldn''t help him, his problem has absolutely nothing to do with the language. Secondly, NeHe''s tutorials are at http://nehe.gamedev.net. Thirdly, putpixel is not a standard function, neither C nor C++ have standard graphics functions.

Are you using the BGI (that''s where I remember putpixel from)? If not, what graphics mode are you using? Is it paletted or 16/24/32 bit? We''ll need some more information before we''re able to give examples.

[Resist Windows XP''s Invasive Production Activation Technology!]
AinigmA
AinigmA
Yeah, im using BGI. IBM8514.BGI graphics mode, LO. 256 color.
I feel so primitive... oh i am, hehe. Anything else?
...
The Optimizer
The Optimizer
quote:
Original post by TANSTAAFL
you''re quickly putting me out of my job, matt.


Hey! It was your suggestion! And I suppose that book on Isometric Game Engines I''m writing...... (Just Kidding!)

-Matt P
Developer: AoE/AoK
-Mp(Developer on AoE/AoK/AoM, and a host of canceled projects)
Null and Void
Null and Void
quote:
Original post by AinigmA
Yeah, im using BGI. IBM8514.BGI graphics mode, LO. 256 color.
I feel so primitive... oh i am, hehe. Anything else?


Well, I''ve never written code to load 8bit bitmaps, but you can find information on their format at wotsit.org. Then, you set each of the palette entries for the screen to what that entry is for the bitmap. Then you go through an set each pixel (beginning at offset (x,y) or whatever) to use the palette entry that it should be. Each image must have the same palette (or be less than 256 entries) if you want more than one.

Sorry, that''s kind of a generic description, but I really don''t remember much of the BGI (thankfully ).

[Resist Windows XP''s Invasive Production Activation Technology!]
The Optimizer
The Optimizer
Ok, Here is how you handle step #1 for BMP Files.

Get a file format reference, or look in the Win32 DSK Documents under "Bitmap Storage" You''ll see they document the contents of the .BMP as follows

BITMAPFILEHEADER (struct)
BITMAPINFOHEADER (struct)
RGBQUAD array {array of structs}
Image Data {array of bytes, 1 per pixel}

You''ll want to create a function that loads a bitmap to memory, something like: I''ve psuedo-coded some of the statements

BYTE *LoadBMPtoMemory(char *fileName)
{
if (!FileExists(fileName))
return(NULL); /* Error: File Not Found */

long memSize = FileLength(fileName); /* Size of file in Bytes */
FILE *dataStream = fopen(fileName, "rb"); /* Open file for binary input */

BYTE *DataBuffer = malloc(memSize); /* Allocate Stoage */

long BytesRead = fread(DataBuffer, memSize, 1, dataStream); /* Read .BMP file into memory *.

fclose(dataStream); /*close file */

return(DataBuffer); /* Don''t forget to free elsewhere */

}


Then to access it you can use these pointers:

BITMAPFILEHEADER *BmpHeader = (BITMAPFILEHEADER*) DataBuffer;

BYTE *BMPPixels = (BYTE*) DataBuffer + DataBuffer->bfOffBits;

BITMAPINFOHEADER *BMPInfo = (BITMAPINFOHEADER*) &DataBuffer[1];

long BMP_Width_In_Pixels = BMPInfo->biWidth;
long BMP_Height_In_Pixels = BMPInfo->biHeight;

and to access a pixel in the BMP at X,Y:

BYTE PixelValue = BMPPixels[ (BMP_Height_In_Pixels - Y -1) * BMP_Width_In_Pixels + X];


Hope That helps....

-Matt P
Developer: AoE/AoK


quote:
Original post by Anonymous Poster
Im loading .bmp, and no i dont have working code for loading the bitmaps. language is C.
Unfortunatley, this is for school, so i have to use the very basics, and come up with everything on my own.
To be more specific, here is my problem. If i make a 32x32 picture in mspaint, how do i read it and store it into an array so that i can use it to put on the screen. or do i even need to store it in an array.

I have tried opening the file and storing it in an array by using getc(), and fread(). I was able to do it both ways, but when i try printing it to the screen it comes out wierd. I used putpixel() to try to display it to the screen. Im not exactly sure what i am doing wrong. I haven''t been able to find any information on reading and printing individual bitmaps, or how bitmaps are made.


-Mp(Developer on AoE/AoK/AoM, and a host of canceled projects)
AinigmA
AinigmA
Thanks everyone, that helps greatly.
I''ll be back in the future to let everyone know how everything went.

/\ i n i g m /\
beginner game programmer...game programmer of the future...
...
Jedyte
Jedyte
If I remember correctly the Windows bitmap format is like this:

Offset Data
0 bitmap header
53 bitmap palette (256 entries)
xxx bitmap (padding at 4 bytes)

(Could be useful in the header:
18 width (2-bytes, most significant last)
22 height (2-bytes, most significant last))

A palette entry is of the form 0BGR. Please note that most colors are displayed RGB.
Hope this is helpful

Jedyte


And the price we paid was the price men have always paid for achieving paradise in this life -- we went soft, we lost our edge. - "Muad'Dib: Conversations" by the Princess Irulan

Topic Locked

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

Sign in to reply to this topic.