void Font::drawText(Bitmap *dest, Point2i pos, const std::string &text) const { int initial = pos.x; for (size_t i=0; i<text.length(); i++) { char character = text[i]; if (character == '\r') continue; if (character == '\n') { pos.x = initial; pos.y += (int) (getMaxVerticalBearing()*4.0/3.0); continue; } const Font::Glyph &glyph = getGlyph(character); Point2i targetOffset = pos + Vector2i( glyph.horizontalBearing, getMaxVerticalBearing() - glyph.verticalBearing - 1 ); Point2i sourceOffset( (int) (glyph.tx.x * m_bitmap->getWidth()), (int) (glyph.tx.y * m_bitmap->getHeight())); dest->accumulate(m_bitmap.get(), sourceOffset, targetOffset, glyph.size); pos.x += glyph.horizontalAdvance; if (i+1 < text.length()) pos.x += getKerning(character, text[i+1]); } }
std::vector<SpriteFramePtr> SpriteFrame::loadSpriteFrames(const std::string& filename, bool mipmaps) { std::vector<SpriteFramePtr> frames; std::vector<uint8_t> data; if (!sharedEngine->getFileSystem()->loadFile(filename, data)) { return frames; } rapidjson::MemoryStream is(reinterpret_cast<char*>(data.data()), data.size()); rapidjson::Document document; document.ParseStream<0>(is); if (document.HasParseError()) { log("Failed to parse %s", filename.c_str()); return frames; } const rapidjson::Value& metaObject = document["meta"]; const rapidjson::Value& sizeObject = metaObject["size"]; Size2 textureSize(static_cast<float>(sizeObject["w"].GetInt()), static_cast<float>(sizeObject["h"].GetInt())); graphics::TexturePtr texture = sharedEngine->getCache()->getTexture(metaObject["image"].GetString(), false, mipmaps); const rapidjson::Value& framesArray = document["frames"]; frames.reserve(framesArray.Size()); for (rapidjson::SizeType index = 0; index < framesArray.Size(); ++index) { const rapidjson::Value& frameObject = framesArray[index]; const rapidjson::Value& rectangleObject = frameObject["frame"]; Rectangle rectangle(static_cast<float>(rectangleObject["x"].GetInt()), static_cast<float>(rectangleObject["y"].GetInt()), static_cast<float>(rectangleObject["w"].GetInt()), static_cast<float>(rectangleObject["h"].GetInt())); bool rotated = frameObject["rotated"].GetBool(); const rapidjson::Value& sourceSizeObject = frameObject["sourceSize"]; Size2 sourceSize(static_cast<float>(sourceSizeObject["w"].GetInt()), static_cast<float>(sourceSizeObject["h"].GetInt())); const rapidjson::Value& spriteSourceSizeObject = frameObject["spriteSourceSize"]; Vector2 sourceOffset(static_cast<float>(spriteSourceSizeObject["x"].GetInt()), static_cast<float>(spriteSourceSizeObject["y"].GetInt())); const rapidjson::Value& pivotObject = frameObject["pivot"]; Vector2 pivot(pivotObject["x"].GetFloat(), pivotObject["y"].GetFloat()); frames.push_back(std::make_shared<SpriteFrame>(rectangle, texture, rotated, sourceSize, sourceOffset, pivot)); } return frames; }