Apologies for my first post here being a question (and an uber n00b one at that) but this has been annoying me for some time and I don't know what I'm doing wrong.
I'm trying to get to grips with DirectDraw while learning anything more than basic C at the same time. I'm following an old book (Windows Game Programming For Dummies - Lamothe) so my techniques may be a bit dated but I want to get this sorted!
Anyway I've got everything set up and can plot pixels on screen, wow. I'm now trying to load in a bitmap and display it on screen. Earlier today I looked up the bitmap file structure and figured out C's I/O functions enough to be able to read in the file header, info header correctly (printing the size showed to be correct, as did the dimensions).
The following is the bitmap loading code:
input = fopen("test.bmp", "r"); //Opens the file
fread(&bitmapfileheader, sizeof(BITMAPFILEHEADER), 1, input); //Reads file header
fread(&bitmapinfoheader, sizeof(BITMAPINFOHEADER), 1, input); //Reads info header
//printf("The bitmap is %d bytes in size.\n", bmfh.bfSize); //These lines show the correct information
//printf("The bitmap is %d pixels high.\n", bmih.biHeight); //when in a console program.
fseek(input, bitmapfileheader.bfOffBits, SEEK_SET); //Find the start of the bitmap data
data = (USHORT *)malloc(bitmapinfoheader.biSizeImage); //Allocate the memory for data
fread(&data, bitmapinfoheader.biSizeImage, 1, input); //Read actual data
So as far as I'm aware I now have the bitmap data itself in the 'data' structure (which is declared USHORT *data; as a global).
The following is what actually displays (or should) the bitmap on the screen:
USHORT *video_buffer = NULL;
memset(&ddsd,0,sizeof(ddsd)); //Not entirely sure what these mean but they work, defining the surface maybe?
ddsd.dwSize = sizeof(ddsd);
lpddsprimary->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL); //Lock primary surface
// get video pointer
video_buffer = (USHORT *)ddsd.lpSurface;
int i;
for (i=0; i<32; i++)
{
memcpy(&video_buffer[i*ddsd.lPitch], // dest address
&data[i*32*2], // src address
32*2); // bytes to copy
}
lpddsprimary->Unlock(video_buffer); //Unlock the surface
The for loop is to display the bitmap on screen, copy each row at a time (the bitmap is 32x32x16, I'm not concerned about allowing for other sizes at the moment)
The problem is that every time I run this I get a variety of error messages:
-Various errors in memcpy.asm reading location 0x00000000
-Errors in the program seeming to crash out at the Lock() function (the cursor is here when it returns to VC++)
I can understand that I may have got the reading the bitmap info into the data structure part wrong but I would expect to at least see corruption on the screen rather than it crashing. I have got corruption on the screen (with no resemblance to my bitmap) sometimes.
Ok so what I'm asking is if someone with more experience than me could have a quick look and see if there's any glaring problems. It all compiles fine, but I'm completely new to a number of these functions (namely the file handling ones) so I can't be sure I've used them right. I also realise (from looking at other DD tutorials) that I seem to be using a rather outdated method (come to think of it, who uses DirectDraw now anyway?!) but I was getting on well with it so far! I'm mostly concerned with the reading in from the bitmap and the copying and allocating memory.
Thanks,
Ray