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


My .bmp loader and directx class aren't working together

Recommended Posts

Hey. I am having some frustrating problems, and need help. I am working on a simple .bmp file loading class so that I can load 16 and 32 bit .bmp files into my direct draw surfaces. I am not sure what is going on, but after I load in a .bmp file, I try to lock my surface so that I can copy my bitmap onto it, but my surface is not locking. My "failed to lock surface" error message keeps comming up. This happens with any surface(offscreen, back, and primary). I am also not sure if my .bmp loading code is properly loading in bitmaps since I can''t display any bitmaps on my direct draw surfaces. I am not sure what is going on. Here is one of my I/O techniques for loading in a .bmp file: I used four different I/O techniques, but will only show one. I think this one should load properly. I also included the whole header file: Header File: #ifndef _KwamiBitMapBMP_H #define _KwamiBitMapBMP_H #include #include #include #include #include #include class KwamiBitMapBMP { private: struct KwamiBMP { BITMAPFILEHEADER bitmapfileheader; BITMAPINFOHEADER bitmapinfoheader; unsigned short *bitmaphold; }; HANDLE file; bool result; HFILE file_handle; OFSTRUCT data; unsigned int readresult; FILE *filepointer; int h; int r; public: KwamiBitMapBMP(); void LoadBMP_CreateFile(HWND handle,char *buffer); void LoadBMP_OpenFile(HWND handle,char *buffer); void LoadBMP_fopen(HWND handle,char *buffer); void LoadBMP_creat(HWND handle,char *buffer); void UnloadBMP(); unsigned short *bitmapholdtemp; KwamiBMP *bmap; unsigned long *bit; }; #endif Source File: This is the "Load BMP_OpenFile" code: void KwamiBitMapBMP::LoadBMP_OpenFile(HWND handle,char *buffer) { file_handle=OpenFile(buffer,&data,OF_READ); if(file_handle==HFILE_ERROR) MessageBox(handle,"The .bmp file path specified is invalid","KwamiBitMapBMP",MB_OK); else { readresult=_lread(file_handle,&bmap->bitmapfileheader,sizeof(BITMAPFILEHEADER)); if(readresult==HFILE_ERROR) MessageBox(handle,"Unable to load in the bitmapfileheader properly.","KwamiBitMapBMP",MB_OK); else { if(bmap->bitmapfileheader.bfType!=0x4D42) MessageBox(handle, "Requested file is not a .bmp file.","KwamiBitMapBMP",MB_OK); else { readresult=_lread(file_handle,&bmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER)); if(readresult==HFILE_ERROR) MessageBox(handle,"Unable to load in the bitmapinfoheader properly.","KwamiBitMapBMP",MB_OK); else { if(bmap->bitmapinfoheader.biBitCount==8) MessageBox(handle,"The KwamiEngine does not support 8 bit bitmaps","KwamiBitMapBMP",MB_OK); else { UnloadBMP(); bmap->bitmaphold=(unsigned short*)malloc(bmap->bitmapinfoheader.biSizeImage); readresult=_lread(file_handle,&bmap->bitmaphold,bmap->bitmapinfoheader.biSizeImage); if(readresult==HFILE_ERROR) MessageBox(handle,"Unable to load in the bitmap image size properly.","KwamiBitMapBMP",MB_OK); else { bitmapholdtemp=(unsigned short*)malloc(bmap->bitmapinfoheader.biSizeImage); bitmapholdtemp=bmap->bitmaphold; _lclose(file_handle); } } } } } } } Can someone please read this code and tell me if there are any errors in it. If there are no errors, I am not sure why my direct draw surfaces fail to lock after I use this code to load in a bitmap. can you please help me? thanks Edem Attiogbe

Share this post

Link to post
Share on other sites
Why are you trying to load the bitmaps yourself?
Let the GDI do it...sure, GDI is slow but it is fast enough for BMP, it does all of the conversions.

Here is a snippet from my engine:

// LoadBMP("filename")
// Loads a BMP file into the tempSurface
GFX_BMP::LoadBMP(char *filename)
if ( ( tempSurface = DDLoadBitmap( DD, filename, 0, 0) ) == NULL )
return false;
writeLog("\nBITMAP LOADED: %s", filename);
return(1); // Return SUCCESS

GFX_BMP is my class.

Be sure to include the ddutil.cpp and ddutil.h file in your project.


~ c o l ec o ~

Rock the cradle of love!
You stupid WANKER!



Share this post

Link to post
Share on other sites
I didn''t entirely follow what you were doing, but since no one
else has posted here I thought I''d chime in with few debugging
It looks like you haven''t totally tested out either your code that fills out a surface with a bitmap or your bmp file loader
in isolation (otherwise I presume you would have asked only
about the part that wasn''t working). Therefore, before you try
going any farther, why don''t you add some debug lines in your
bmp loader that output some header information (length, width,
color depth)as well as the first 100 bytes of the data stream to
a file. If you try loading a simple bmp with it (such as a red
and blue checkerboard) you should be able to confirm that the bmp file loader is operating properly by inspecting the header fields
and data stream by eye (that is, the data stream of a 16 bit
checkerboard image should have alternating words of one value
and then the other--DOS debug is your friend...)

In order to debug the function that fills out a surface from an
array in memory, you should make sure this actually works before
doing ANYTHING with file I/O.

Do something like:

short test_pic[10000]; //100x100x16 image
for(i=0;i<10000;i++) test_pic = 0xaaaa; //should be grayish
for(i=200;i<400;i++) test_pic[i] = 0xffff; //should be white

FillOutSurface(lplpDirectDrawSurface7 MySurface,(short*)test_pic,

Where FillOutSurface() is the function that takes the address of
a ddrawsurface pointer and fills it with the location of a ddraw
surface initialized with the information contained in the array
you pass it.

Before I stop, I should probably add something that might actually help: if the ->Lock() call you''re making in your
FillOutSurface method still fails, (and you are SURE that it
couldn''t possibly have been locked anywhere else in your program)
then you can always actually catch the HRESULT that the Lock
function returns and do a switch case on it to see why the heck
the call is actually failing (see the dd7 sdk entry on the Lock()
method for all the error codes it can return). It might be
really informative

Hope some part of that helped.

Share this post

Link to post
Share on other sites
Thanks Muse and Coleco. Muse, I might try the debugging round that you suggested. I was thinking of loading in a bitap through the LoadImage function, but I rather think of the code myself so that I know exactly what is going on.

Edem Attiogbe

Share this post

Link to post
Share on other sites
Also, another reason why I don''t want to use direct x to load my bitmaps is because I want to use my .bmp loading class as part of my engine, which will render primarily with OpenGL, and then Direct 3D.

Edem Attiogbe

Share this post

Link to post
Share on other sites