Example #1
0
 //------------------------------------------------------------------------
 void TerrainBatch::loadRenderableObject()
 {
     if ( !mRender )
     {
         mRender = MG_NEW Rend(mTileRender, this, mMovableObject, mBoundingBox.getCenter());
     }
 }
/**
 * Process the text and generate the list of formatted renderables.
 */
void FmtTextDecor::Reformat()
{
    int lineHeight = TTF_FontLineSkip(**font);
    int spaceWidth = font->spaceLayoutWidth;
    if (text) s = **text;
    int x = 0, y = 0;
    int fmtColor = 7;

    sizeWidth = 0;
    sizeHeight = 0;

    rends.clear();
    rends.reserve(s.length());

    uint32_t ch = 0;
    for (auto iter = s.cbegin(), iend = s.cend(); iter != iend; ) {
        ch = utf8::next(iter, iend);

        // Only handle characters which we have included in the type case.
        if (ch > font->glyphs.size()) {
            SDL_Log("Unrenderable code point: %d", ch);
            continue;
        }

        switch (ch) {
        case '\n':  // Newlines.
            x = 0;
            y += lineHeight;
            break;

        case '^':  // Color codes.
            if (iter == iend) {
                return;
            }
            else {
                uint32_t idx = utf8::next(iter, iend);

                // Allow "^^" as an escape sequence for "^".
                if (idx == '^') {
                    const Ttf::Glyph &glyph = font->glyphs['^'];
                    if (glyph.avail) {
                        rends.emplace_back(Rend(&glyph, x, y, fmtColor));
                        x += glyph.layoutW;
                    }
                    break;
                }

                fmtColor = (idx + 16) & 31;
            }
            break;

        case '\r':  // Ignore CRs.
            break;

        case ' ':
            x += spaceWidth;
            break;

        default: {
            const Ttf::Glyph &glyph = font->glyphs[ch];
            if (!glyph.avail) continue;

            rends.emplace_back(Rend(&glyph, x, y, fmtColor));

            x += glyph.layoutW;
        }
        }

        if (x >= sizeWidth) {
            sizeWidth = x;
        }
    }

    // If the last char wasn't a newline, adjust the height otherwise we'll
    // cut off the bottom line of text.
    if (ch != '\n') {
        y += lineHeight;
    }
    sizeHeight = y;
}