Advertisement

BMFont binary format reading

Started by January 05, 2008 05:02 PM
5 comments, last by ehmdjii 16 years, 10 months ago
hello, i have a problem when reading the binary format of bmfont. this is how i do it:

	ifstream f(filename);

	f.seekg(4);

	char blocktype;
	int blocksize;

	while (!f.eof()) {
		
		f.read(&blocktype, sizeof(blocktype));
		f.read((char*) &blocksize, sizeof(blocksize));
		
		switch (blocktype) {
			
			case 1:	  //info		
				f.read((char*) &infob, blocksize);
				break;

			case 2:	  //common
				f.read((char*) &commonb, blocksize);
				break;

			case 3:   //pages
				f.read((char*) &pagesb, blocksize);
				break;

			case 4:	  //chars
				f.read((char*) &charsb, blocksize);
				break;

			case 5:   //kerning pairs
				f.read((char*) &kerningpairsb, blocksize);
				break;

		}
	}

this works fine for the first block, however blocktype and blocksize for the second block are 0 and some high integer...
From the documentation:

"Each block starts with a one byte block type identifier, followed by a 4 byte integer that gives the size of the block, including the 4 byte size value, but not the block type identifier."

I suspect you missed this phrase in the documentation. When reading the block, you should only read the rest of the block, since you've already read the first 4 bytes in the block.

Simply add "blocksize -= 4;" after the "f.read((char*) &blocksize, sizeof(blocksize));" line, and you should be fine.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
thanks. i already thought so, that i am 4 bytes off. but why is the blocksize part of the struct then? because i have to comment it out now, since otherwise the read operation will start filling the struct at struct.blocksize which is off by 4 bytes again.

another question:
why does the charsBlock declare an array of size 1 of chars? shouldnt it declare a pointer of chars, since the size of that array should be the number characters later on?

thanks!

[Edited by - ehmdjii on January 6, 2008 7:59:37 AM]
I need to change the documentation to not describe the structure with C structures. It confuses too many people.

As the charsBlock is inlined in the structure it must be declared as an array, rather than a pointer. Since you don't know the size of the array before hand it is common to declare it with size 1. With C and C++ there's no boundscheck anyway so there is no problem accessing elements beyond this first element.


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

thanks a lot for your help.

however i have still problems reading the binary format, namely reading the chars-block. if i understand it correctly, then this is one large block with the size of sizeof(charsBlock::charInfo)*numberOfCharacters

reading the characters into the struct works fine until a certain character is reached. after that all further fstream.read() operations only read 0s. (all of the struct is 0). which also means there is no kerningBlock found. if i look at the text-version or the xml-version of the same font, it looks fine. there are no zeros and there is also the kerning-part.

which character it is, depends on the font itself. but it mostly reads between 30 and 60 characters after which it only reads 0s.

edit: ok i just found out, that eof has been reached on the fstream. could it be that the file ends too early?

edit: ok it turned out that i forgot to set the std::ios::binary flag on the fstream. sorry my bad.

[Edited by - ehmdjii on January 9, 2008 3:14:59 PM]
OK.

I'll have to update my acGraphics::CFont class to be able to load the font in binary format as well. That way people will have a reference implementation to look at, while they implement their own.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
yes that would be great! thanks a lot in advance!

This topic is closed to new replies.

Advertisement