std::shared_ptr<Resource> BillboardAnimationLoader::Load( const ResourceKey& key, const FileManager& fileManager) const { File file; fileManager.Open(key.GetFilePath(), file); auto animationJson = file.ReadAll(); auto deleter = [](json_value* p){ json_value_free(p); }; auto animationVal = std::unique_ptr<json_value, decltype(deleter)>( json_parse(animationJson.c_str(), animationJson.length() * sizeof(std::string::value_type)), deleter); assert(animationVal->type == json_object); auto textureVal = (*animationVal)["texture"]; auto framesXVal = (*animationVal)["framesX"]; auto framesYVal = (*animationVal)["framesY"]; auto framesVal = (*animationVal)["frames"]; assert(textureVal.type == json_string); std::string textureFilePath = MakeAbsolutePath( key.GetFilePath(), std::string(textureVal)); auto texture = g_resourceManager->Acquire<TextureResource>( ResourceKey(textureFilePath)); assert(framesXVal.type == json_integer); assert(framesYVal.type == json_integer); int numFramesX = json_int_t(framesXVal); int numFramesY = json_int_t(framesYVal); std::vector<Vec2f> texcoordsScales; std::vector<Vec2f> texcoordsOffsets; std::vector<int> times; assert(framesVal.type == json_array); const unsigned int numFrames = framesVal.u.array.length; for (size_t i = 0; i < numFrames; ++i) { assert(framesVal[i].type == json_object); auto frameXVal = framesVal[i]["x"]; auto frameYVal = framesVal[i]["y"]; auto timeVal = framesVal[i]["ms"]; assert(frameXVal.type == json_integer); assert(frameYVal.type == json_integer); assert(timeVal.type == json_integer); int frameX = json_int_t(frameXVal); int frameY = json_int_t(frameYVal); int time = json_int_t(timeVal); float frameWidth = 1.0f / numFramesX; float frameHeight = 1.0f / numFramesY; Vec2f texcoordsScale(frameWidth, frameHeight); Vec2f texcoordsOffset(frameX * frameWidth, (1 - frameY) * frameHeight); texcoordsScales.push_back(texcoordsScale); texcoordsOffsets.push_back(texcoordsOffset); times.push_back(time); } return std::make_shared<BillboardAnimationResource>( texcoordsScales, texcoordsOffsets, times, texture); }
std::shared_ptr<Resource> FontLoader::Load( const ResourceKey& key, const FileManager& fileManager) const { File file; fileManager.Open(key.GetFilePath(), file); auto fontJson = file.ReadAll(); auto deleter = [](json_value* p){ json_value_free(p); }; auto fontVal = std::unique_ptr<json_value, decltype(deleter)>( json_parse(fontJson.c_str(), fontJson.length() * sizeof(std::string::value_type)), deleter); assert(fontVal->type == json_object); auto textureVal = (*fontVal)["texture"]; auto dimXVal = (*fontVal)["dimX"]; auto dimYVal = (*fontVal)["dimY"]; auto charHVal = (*fontVal)["charH"]; auto charactersVal = (*fontVal)["characters"]; assert(textureVal.type == json_string); std::string textureFilePath = MakeAbsolutePath( key.GetFilePath(), std::string(textureVal)); auto texture = g_resourceManager->Acquire<TextureResource>( ResourceKey(textureFilePath)); assert(dimXVal.type == json_integer); assert(dimYVal.type == json_integer); assert(charHVal.type == json_integer); int dimX = json_int_t(dimXVal); int dimY = json_int_t(dimYVal); int charH = json_int_t(charHVal); float charTexW = 1.0f / dimX; float charTexH = 1.0f / dimY; float charScaleAspectRatio = 1.0f / (dimX * charH); float charScaleY = 1.0f / dimY; std::vector<Vec2f> texcoordsScales; std::vector<Vec2f> texcoordsOffsets; std::vector<float> aspectRatios; std::unordered_map<char, size_t> charToIdxLookup; assert(charactersVal.type == json_array); const unsigned int numCharacters = charactersVal.u.array.length; for (size_t i = 0; i < numCharacters; ++i) { assert(charactersVal[i].type == json_object); auto charVal = charactersVal[i]["c"]; auto xVal = charactersVal[i]["x"]; auto yVal = charactersVal[i]["y"]; auto charWVal = charactersVal[i]["w"]; assert(charVal.type == json_string); assert(xVal.type == json_integer); assert(yVal.type == json_integer); assert(charWVal.type == json_integer); char character = std::string(charVal).at(0); int charTexX = json_int_t(xVal); int charTexY = json_int_t(yVal); int charW = json_int_t(charWVal); Vec2f texcoordsScale(charW * charScaleAspectRatio, charScaleY); Vec2f texcoordsOffset( charTexX * charTexW, 1 - charTexY * charTexH - charTexH); texcoordsScales.push_back(texcoordsScale); texcoordsOffsets.push_back(texcoordsOffset); aspectRatios.push_back(charW / static_cast<float>(charH)); charToIdxLookup.insert(std::make_pair(character, i)); } return std::make_shared<FontResource>( texcoordsScales, texcoordsOffsets, aspectRatios, charToIdxLookup, texture); }