Some DirectX help needed...

Status
Not open for further replies.

X_Breaker

Ars Tribunus Militum
2,299
I've been struggling with loading bitmaps and displaying them on a surface. I'm pretty sure I could debug it, but the only problem is I can't see the code while I'm debugging, because it's a fullscreen app. Any attempt to quit the program after it locks up locks the debugger up, so until I get two monitors, I'm stuck.

What's wrong here?

code:<HR>
Code:
// now... start to load in bitmaps to the sprite surface
BITMAP_FILE bitmap;<P>Load_Bitmap_File(&bitmap, "lupin.bmp");<P>// lock the primary surface
lpddsprimary->Lock(NULL,&ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);<P>// get video pointer to sprite surfce
USHORT *sprite_buffer = (USHORT *)ddsd.lpSurface;       <P>// process each line and copy it into the primary buffer
for (int index_y = 0; index_y < SCREEN_HEIGHT; index_y++)
{
     for (int index_x = 0; index_x < WHOLE_SCREEN_WIDTH; index_x++)
     {
          // this section scales images down to 16 bit
	  UCHAR blue  = (bitmap.buffer[index_y*WHOLE_SCREEN_WIDTH*3 + index_x*3 + 0]) >> 3,
	        green = (bitmap.buffer[index_y*WHOLE_SCREEN_WIDTH*3 + index_x*3 + 1]) >> 3,
                red   = (bitmap.buffer[index_y*WHOLE_SCREEN_WIDTH*3 + index_x*3 + 2]) >> 3;<P>	  // this builds a 16 bit color value in 565
	  USHORT pixel = _RGB16BIT565(red,green,blue);<P>	  // write the pixel
	  sprite_buffer[index_x + (index_y*ddsd.lPitch >> 1)] = pixel;
     } // end for index_x
} // end for index_y<P>lpddsprimary->Unlock(NULL);<P>Unload_Bitmap_File(&bitmap);

Known problems:

1. Ignore the variable WHOLE_SCREEN_WIDTH, it's a temporary fix until I write a function to take care of it...

2. Before, I tried to simply load the bitmap, and then unload it, without writing it to a surface. It crashed the program as well, until I commented out Unload_Bitmap_File. My first thoughts were that it wasn't loading the bitmap correctly in the first place, but wouldn't that cause it to crash as well?

3. Forgetting unloading the bitmap, the program is crashing when I try to write the bitmap to a surface (In this case, I'm simply writing to the primary surface, so I can see that it's being done. I've already set up a sprite surface that you can't see for when things are working).

Does anyone know where the problem is?

Edit (Oh yeah, I suppose I should include the function that loads the bitmap...)

code:<HR>
Code:
// Load_Bitmap_File - loads a bitmap into memory
int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename)
{
     int file_handle;<P>     UCHAR *temp_buffer = NULL;  // used to convert 24-bit images to 16 bit
     OFSTRUCT file_data;<P>     // open the file, as long as it exists
     if ((file_handle = OpenFile(filename, &file_data, OF_READ))==-1)
          return(0);<P>     // load the file header
     _lread(file_handle, &bitmap->bitmapfileheader, sizeof(BITMAPFILEHEADER));<P>     // is this a bitmap? If not, close the file
     if (bitmap->bitmapfileheader.bfType!=BITMAP_ID)
     {
          _lclose(file_handle);
               return(0);
     } // end if<P>     // if we get this far, it is indeed a bitmap. Read in the info header.
     _lread(file_handle, &bitmap->bitmapinfoheader, sizeof(BITMAPINFOHEADER));<P>     // skipping loading the color palette for now, will add later<P>     // read in the image data
     _lseek(file_handle, -(int)(bitmap->bitmapinfoheader.biSizeImage),SEEK_END);<P>     if (bitmap->buffer)
          free(bitmap->buffer);<P>     // allocate memory for the image
     if (!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))
     {
          _lclose(file_handle);
               return(0);
     } // end if<P>     // read data into the buffer
     _lread(file_handle, bitmap->buffer, bitmap->bitmapinfoheader.biSizeImage);<P>     // now, close the file
     _lclose(file_handle);<P>     // return success
     return(1);
} // end Load_Bitmap_File

[This message was edited by X_Breaker on November 25, 2001 at 04:49.]
 
Status
Not open for further replies.