Skip to main content
GameDev.net gamedev.net
🔒 Locked

Some weird bitmap loading stuff

Started by Ilici May 17, 2003 at 11:27 AM 1 replies 250+ views
Original Post
Ilici
Ilici
ok now i''m really pissed off i made a nice little 8bit bmp loading function for my terrain renderer. i need 2^n + 1 sized images. ok first i tried with a 256 x 256 img and it works. next i tried it with a 257 x 257 img and weird stuff happes like: horizontal lines with 0 values on the heightfield. the function for getting the height works so i suspect that the data is not loaded corectly. ex: height[0] = 32; //for my bitmap height[257] = 0; //the pixel on the next row (it should be 35 - i checked with an editor) if ur confused heres the code for loading:
  
int CTerrain::LoadFromFile(char *fname)
{
	FILE *fi = NULL;
	BITMAPFILEHEADER head;
	BITMAPINFOHEADER info;

	fi = fopen(fname, "rb");

	if (!fi)
	{
		lprintf("Cannot open file %s\n", fname);
		return SYS_ERR;
	}

	fread(&head, sizeof(head), 1, fi);
	fread(&info, sizeof(info), 1, fi);

	if (info.biWidth != info.biHeight) 
	{
		lprintf("Map must be square size\n");
		return SYS_ERR;
	}

	if (info.biBitCount != 8)
	{
		lprintf("Only 8 bit bitmaps supported\n");
		return SYS_ERR;
	}

	MapSize = info.biWidth;

	HeightData = (unsigned char*) malloc(MapSize * MapSize);
	if (!HeightData)
	{
		lprintf("Cannot allocate %d memory\n", MapSize * MapSize);
		return SYS_ERR;
	}

	fseek(fi, head.bfOffBits, SEEK_SET);
	fread(HeightData, MapSize, MapSize, fi);

	fclose(fi);
	
	MapLogSize = log(MapSize - 1) / log(2);
	MaxTreeLevel = MapLogSize >> 1;

	CreateTree();
	return SYS_OK;
}
  
and for getting the value at some coordinates
  
inline unsigned char CTerrain::GetHeight(int x, int y)
{
	if (x < 0 || y < 0 || x >= MapSize || y >= MapSize) return 255;
	return HeightData[x * MapSize + y];
}
  
i''m really confused why fread doesnt load corectly. other weird stuff i tried to write the data to another bitmap like this fwrite(&head, sizeof(head), 1, fi) fwrite(&info, sizeof(info), 1, fi); fwrite(emptybuf, head.bfOffbits - sizeof(head) - sizeof(info), 1, fi); fwrite(HeightData, MapSize * MapSize, 1, fi); and the output is not a greyscale image (wtf not??? coz i used the exact same headers) the output is a 8bit palleted image . weird. and can some one explain 2 me what is the difference between fread(&head, sizeof(head), 1, fi) and fread(&head, 1, sizeof(head), fi) ? thnx in advance /*ilici*/
Ilici
Ilici
yep that was right, i figured out myself today by looking at the hex data in the bitmap, damn that alignement



/*ilici*/

Topic Locked

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

Sign in to reply to this topic.