Sign in to follow this  

BMFont binary format reading

This topic is 3621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 3621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this