示例#1
0
Gdx2DPixmap::Gdx2DPixmap (files::FileHandle::ptr fhandle, int requestedFormat)
        :
        pixData(0)
        ,width(0)
        ,height(0)
        ,format(0)
{
    files::FileHandle::buffer_ptr buffer;    
    int readed = fhandle->readBytes(buffer);

    assert(readed);
    
    pixData = load((unsigned char*) buffer.get(), 0, readed, requestedFormat);

    width = pixData->width;
    height = pixData->height;
    format = pixData->format;

    if (pixData == NULL) {
        throw std::runtime_error("couldn't load pixmap");

    }
}
示例#2
0
BitmapFont::BitmapFontData::BitmapFontData ( files::FileHandle::ptr fontFile, bool flip )
    : fontFile ( fontFile ),
      flipped ( flip ),
      capHeight ( 1 ),
      scaleX ( 1 ),
      scaleY ( 1 ),
      ascent ( 0 ),
      descent ( 0 ),
      down ( 0 ),
      spaceWidth ( 0 ),
      xHeight ( 1 ) {
    glyphs = new Glyph**[PAGES];
    memset ( glyphs, 0, sizeof ( Glyph* ) * PAGES );

    std::vector <char> n_buffer;
    
    {
        files::FileHandle::buffer_ptr buffer;
        int readed = fontFile->readBytes ( buffer );
        n_buffer.resize ( readed + 1);
        memcpy ( &n_buffer[0], buffer.get(), readed );
        n_buffer.back() = 0;
    }

    char* line_r = NULL;

    try {
        char* line = strtok_r ( &n_buffer[0], "\n", &line_r );

        if ( ( line = strtok_r ( NULL, "\n", &line_r ) ) == NULL ) {
            throw std::runtime_error ( "Invalid font file: " + fontFile->toString() );
        }

        char* common = strtok ( line, " " );

        if ( sscanf ( strtok ( NULL, " " ), "lineHeight=%f", &lineHeight ) != 1 ) {
            throw std::runtime_error ( "Invalid font file: " + fontFile->toString() );
        }

        int baseLine = 0;
        if ( sscanf ( strtok ( NULL, " " ), "base=%d", &baseLine ) != 1 ) {
            throw std::runtime_error ( "Invalid font file: " + fontFile->toString() );
        }

        if ( ( line = strtok_r ( NULL, "\n", &line_r ) ) == NULL ) {
            throw std::runtime_error ( "Invalid font file: " + fontFile->toString() );
        }

        strtok ( line, " " );
        strtok ( NULL, " " );

        char file[1024];
        if ( sscanf ( strtok ( NULL, " " ), "file=%s", file ) < 0 ) {
            throw std::runtime_error ( "Invalid font file: " + fontFile->toString() );
        }

        //removing the quotes (") from start and end of the string
        char unquoted[1024];
        strncpy ( unquoted, file + 1, strlen ( file ) - 2 );
        unquoted[strlen ( file ) - 2] = 0;

        imagePath = unquoted;
        descent = 0;

        while ( true ) {
            line = strtok_r ( NULL, "\n", &line_r );

            if ( line == NULL ) {
                break;
            }

            if ( strstr ( line, "kernings " ) != NULL ) break;
            if ( strstr ( line, "char " ) == NULL ) continue;

            Glyph* glyph = new Glyph();

            int ch = 0;

            if ( sscanf ( line, "char id=%d x=%d y=%d width=%d height=%d xoffset=%d yoffset=%d xadvance=%d page=%*s chnl=%*s",
                          &ch, &glyph->srcX, &glyph->srcY, &glyph->width, &glyph->height, &glyph->xoffset, &glyph->yoffset, &glyph->xadvance ) != 8 ) {

                delete glyph;
                throw std::runtime_error ( "Invalid font file: " + fontFile->toString() );
            }

            if ( ch <= 0xffff ) {
                setGlyph ( ch, glyph );
            } else {
                delete glyph;
                continue;
            }

            if ( !flip ) {
                glyph->yoffset = - ( glyph->height + glyph->yoffset );
            }

            descent = std::min ( ( float ) ( baseLine + glyph->yoffset ), descent );
        }

        while ( true ) {
            line = strtok_r ( NULL, "\n", &line_r );
            if ( line == NULL ) {
                break;
            }

            if ( strstr ( line, "kerning " ) == NULL ) break;

            int first = 0, second = 0, amount = 0;

            if ( sscanf ( line, "kerning first=%d second=%d amount=%d", &first, &second, &amount ) != 3 ) {
                throw std::runtime_error ( "Invalid font file: " + fontFile->toString() );
            }

            if ( first < 0 || first > 0xffff || second < 0 || second > 0xffff )
                continue;

            Glyph* glyph = getGlyph ( ( char ) first );
            glyph->setKerning ( second, amount );
        }

        Glyph* spaceGlyph = getGlyph ( ' ' );
        if ( spaceGlyph == NULL ) {
            spaceGlyph = new Glyph();
            Glyph* xadvanceGlyph = getGlyph ( 'l' );
            if ( xadvanceGlyph == NULL ) xadvanceGlyph = getFirstGlyph();
            spaceGlyph->xadvance = xadvanceGlyph->xadvance;
            setGlyph ( ' ', spaceGlyph );
        }
        spaceWidth = spaceGlyph != NULL ? spaceGlyph->xadvance + spaceGlyph->width : 1;

        Glyph* xGlyph = NULL;
        for ( int i = 0; i < utils::array_size ( xChars ); i++ ) {
            xGlyph = getGlyph ( xChars[i] );
            if ( xGlyph != NULL ) break;
        }

        if ( xGlyph == NULL ) xGlyph = getFirstGlyph();
        xHeight = xGlyph->height;

        Glyph* capGlyph = NULL;
        for ( int i = 0; i < utils::array_size ( capChars ); i++ ) {
            capGlyph = getGlyph ( capChars[i] );
            if ( capGlyph != NULL ) break;
        }
        if ( xGlyph == NULL ) xGlyph = getFirstGlyph();
        capHeight = capGlyph->height;

        ascent = baseLine - capHeight;
        down = -lineHeight;
        if ( flip ) {
            ascent = -ascent;
            down = -down;
        }
    } catch ( std::exception e ) {
        gdx_cpp::Gdx::app->log ( "BitmapFont", "Constructor exception: %s", e.what() );
        throw std::runtime_error ( "Error loading font file: " + fontFile->name() );
    }
}