Having only recently heard of the "malloc" function, supposedly to dynamically change the size of an array, I wanted to adjust my "LoadFile" function to using it, if it does what I think it does. I don't know how to set up the function parameters mainly. Here's the related code I have:
What I want to do is call the function like this:Code:// the global variable declarations (array sizes given is what should be allocated) ... unsigned char PrelakeHills04Data[700416]; // The prelake hills layer 4 background unsigned char *PrelakeHills04MainDataPointer; // malloc requires a pointer to the data unsigned long PrelakeHills04MemoryUsage; LPVOID PrelakeHills04DataPointer; // data pointer for use in AlphaBlend and DrawDibDraw functions BITMAPINFO PrelakeHills04MainInfo; BITMAPINFOHEADER PrelakeHills04Info; LPBITMAPINFOHEADER PrelakeHills04InfoPointer; HBITMAP PrelakeHills04BMPHandle; struct SceneryData PrelakeHills04; unsigned char PrelakeHills03Data[343040]; // The prelake hills layer 3 background unsigned char *PrelakeHills03MainDataPointer; unsigned long PrelakeHills03MemoryUsage; LPVOID PrelakeHills03DataPointer; BITMAPINFO PrelakeHills03MainInfo; BITMAPINFOHEADER PrelakeHills03Info; LPBITMAPINFOHEADER PrelakeHills03InfoPointer; HBITMAP PrelakeHills03BMPHandle; struct SceneryData PrelakeHills03; unsigned char PrelakeHills02Data[106496]; // The prelake hills layer 2 background unsigned char *PrelakeHills02MainDataPointer; unsigned long PrelakeHills02MemoryUsage; ... // the function's header (yes, lots of parameters) char LoadFile(const char FileName[64], char FileType, BITMAPINFOHEADER *BMPInfo, unsigned char *BMPData, char FogUsed, double ObjectScaling, char LoadType, unsigned char *BMPDataPointer, unsigned long *BMPMemoryUsage) { // local variables, setting directories, and setting the file handle (no problems here) ... // filling the structure works fine fseek(FileHandle, 14, SEEK_SET); // skip the basic file header data but read from the info header fread(&ValuesBase, 1, 4, FileHandle); // bytes 0E through 11 BMPInfo->biSize = (DWORD)ValuesBase[0]+(DWORD)ValuesBase[1]*256+(DWORD)ValuesBase[2]*65536+(DWORD)ValuesBase[3]*16777216; fread(&ValuesBase, 1, 4, FileHandle); // bytes 12 through 15 BMPInfo->biWidth = (long)ValuesBase[0]+(long)ValuesBase[1]*256+(long)ValuesBase[2]*65536+(long)ValuesBase[3]*16777216; fread(&ValuesBase, 1, 4, FileHandle); // bytes 16 through 19 BMPInfo->biHeight = (long)ValuesBase[0]+(long)ValuesBase[1]*256+(long)ValuesBase[2]*65536+(long)ValuesBase[3]*16777216; fread(&ValuesBase, 1, 2, FileHandle); // bytes 1A and 1B BMPInfo->biPlanes = (WORD)ValuesBase[0]+(WORD)ValuesBase[1]*256; fread(&ValuesBase, 1, 2, FileHandle); // bytes 1C and 1D BMPInfo->biBitCount = (WORD)ValuesBase[0]+(WORD)ValuesBase[1]*256; fread(&ValuesBase, 1, 4, FileHandle); // bytes 1E through 21 BMPInfo->biCompression = (DWORD)ValuesBase[0]+(DWORD)ValuesBase[1]*256+(DWORD)ValuesBase[2]*65536+(DWORD)ValuesBase[3]*16777216; fread(&ValuesBase, 1, 4, FileHandle); // bytes 22 through 25 BMPInfo->biSizeImage = (DWORD)ValuesBase[0]+(DWORD)ValuesBase[1]*256+(DWORD)ValuesBase[2]*65536+(DWORD)ValuesBase[3]*16777216; fread(&ValuesBase, 1, 4, FileHandle); // bytes 26 through 29 BMPInfo->biXPelsPerMeter = (long)ValuesBase[0]+(long)ValuesBase[1]*256+(long)ValuesBase[2]*65536+(long)ValuesBase[3]*16777216; fread(&ValuesBase, 1, 4, FileHandle); // bytes 2A through 2D BMPInfo->biYPelsPerMeter = (long)ValuesBase[0]+(long)ValuesBase[1]*256+(long)ValuesBase[2]*65536+(long)ValuesBase[3]*16777216; fread(&ValuesBase, 1, 4, FileHandle); // bytes 2E through 31 BMPInfo->biClrUsed = (DWORD)ValuesBase[0]+(DWORD)ValuesBase[1]*256+(DWORD)ValuesBase[2]*65536+(DWORD)ValuesBase[3]*16777216; fread(&ValuesBase, 1, 4, FileHandle); // bytes 32 through 35 BMPInfo->biClrImportant = (DWORD)ValuesBase[0]+(DWORD)ValuesBase[1]*256+(DWORD)ValuesBase[2]*65536+(DWORD)ValuesBase[3]*16777216; BMPMemoryUsage = BMPInfo->biWidth*BMPInfo->biHeight*3; // determine the amount of memory usage - 24-bit BMP's use 3 bytes per pixel BMPDataPointer = malloc(BMPMemoryUsage); // allocate the memory for the amount given in the previous statement fread(BMPData, 1, BMPMemoryUsage, FileHandle); // read the main image data // reads in BGR format ... // rest of function, which works without problems } ... // at end of main function free(PrelakeHills04MainDataPointer); // release the memory, to avoid memory leaks
I don't know how to set up the function so that it reads the amount of data allocated in the main data array, no more, no less.Code:LoadFile("PrelakeHills04.tga", 2, &PrelakeHills04Info, PrelakeHills04Data, 1, PrelakeHills04.Scaling, 0, PrelakeHills04MainDataPointer, PrelakeHills04MemoryUsage);



LinkBack URL
About LinkBacks



