Advertisement

Problems reading BMFont binary format

Started by September 27, 2010 11:17 AM
1 comment, last by Xycaleth 14 years, 2 months ago
So I've been trying to use BMFont (v1.12) for the past few days, and I'm having some problems reading the characters block. I'm using C++ to read the block, and I can retrieve the number of characters without problems - this number matches the number of characters I'd selected in the BMFont program. When I read in the characters block into a std::vector, I see that the first 10 or so characters are fine - the character ids are probably correct, and they each have x/y positions etc. However the characters after the first 10 or so are completely filled with 0s.

Here's my code to read in the blocks:
    InfoBlock info;    CommonBlock common;    std::string pageName;    std::vector<std::string> pageNames;    int numChars;    CharsBlock charsBlock;    std::vector<CharsBlock> chars;    while ( !ss.eof() )    {        BlockHeader bheader;        ss.read (reinterpret_cast<char *>(&bheader), sizeof (bheader));        switch ( bheader.type )        {            case BT_INFO:                ReadInfoBlock (bheader.size, info, ss);            break;            case BT_COMMON:                ss.read (reinterpret_cast<char *>(&common), bheader.size);            break;            case BT_PAGES:                pageName.resize (bheader.size);                for ( int i = 0; i < common.pages; i++ )                {                    ss.read (&pageName[0], bheader.size);                    pageNames.push_back (pageName);                }            break;            case BT_CHARS:                numChars = bheader.size / sizeof (CharsBlock);                chars.resize (numChars);                ss.read (reinterpret_cast<char *>(&chars[0]), bheader.size);            break;            default:                ss.seekg (bheader.size, std::ios::cur);            break;        }    }

Here's the struct I've created for CharsBlock:
struct CharsBlock{    unsigned int id;    unsigned short x;    unsigned short y;    unsigned short width;    unsigned short height;    short xoffset;    short yoffset;    short xadvance;    unsigned char page;    unsigned char channel;};

(The struct has 1-byte alignment)

Here's a screenshot of the contents of the chars variable.


And yes, I am using a debug build! :P

Does anyone have any ideas what could be causing the problem?

EDIT: I should add that outputting a plain text format .fnt file, I can see that all the characters are exported correctly.
I don't see any obvious errors in your code. Have you checked that the ss.read call actually reads the number of bytes it should?

You can also have a look at my own implementation of the font loader if you wish: acgfx_font.cpp (look for the CFontLoaderBinaryFormat class).

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
So I found the problem: I forgot to add the binary flag to the ifstream constructor. So it does do something! Thanks for the help though WitchLord, and great work on an excellent little utility :)

This topic is closed to new replies.

Advertisement