bool SpriteSheet2D::EndLoadFromPListFile() { ResourceCache* cache = GetSubsystem<ResourceCache>(); texture_ = cache->GetResource<Texture2D>(loadTextureName_); if (!texture_) { LOGERROR("Could not load texture " + loadTextureName_); loadXMLFile_.Reset(); loadTextureName_.Clear(); return false; } const PListValueMap& root = loadPListFile_->GetRoot(); const PListValueMap& frames = root["frames"].GetValueMap(); for (PListValueMap::ConstIterator i = frames.Begin(); i != frames.End(); ++i) { String name = i->first_.Split('.')[0]; const PListValueMap& frameInfo = i->second_.GetValueMap(); if (frameInfo["rotated"].GetBool()) { LOGWARNING("Rotated sprite is not support now"); continue; } IntRect rectangle = frameInfo["frame"].GetIntRect(); Vector2 hotSpot(0.5f, 0.5f); IntVector2 offset(0, 0); IntRect sourceColorRect = frameInfo["sourceColorRect"].GetIntRect(); if (sourceColorRect.left_ != 0 && sourceColorRect.top_ != 0) { offset.x_ = -sourceColorRect.left_; offset.y_ = -sourceColorRect.top_; IntVector2 sourceSize = frameInfo["sourceSize"].GetIntVector2(); hotSpot.x_ = ((float)offset.x_ + sourceSize.x_ / 2) / rectangle.Width(); hotSpot.y_ = 1.0f - ((float)offset.y_ + sourceSize.y_/ 2) / rectangle.Height(); } DefineSprite(name, rectangle, hotSpot, offset); } loadXMLFile_.Reset(); loadTextureName_.Clear(); return true; }
bool SpriteSheet2D::EndLoadFromXMLFile() { ResourceCache* cache = GetSubsystem<ResourceCache>(); texture_ = cache->GetResource<Texture2D>(loadTextureName_); if (!texture_) { URHO3D_LOGERROR("Could not load texture " + loadTextureName_); loadXMLFile_.Reset(); loadTextureName_.Clear(); return false; } XMLElement rootElem = loadXMLFile_->GetRoot("TextureAtlas"); XMLElement subTextureElem = rootElem.GetChild("SubTexture"); while (subTextureElem) { String name = subTextureElem.GetAttribute("name"); int x = subTextureElem.GetInt("x"); int y = subTextureElem.GetInt("y"); int width = subTextureElem.GetInt("width"); int height = subTextureElem.GetInt("height"); IntRect rectangle(x, y, x + width, y + height); Vector2 hotSpot(0.5f, 0.5f); IntVector2 offset(0, 0); if (subTextureElem.HasAttribute("frameWidth") && subTextureElem.HasAttribute("frameHeight")) { offset.x_ = subTextureElem.GetInt("frameX"); offset.y_ = subTextureElem.GetInt("frameY"); int frameWidth = subTextureElem.GetInt("frameWidth"); int frameHeight = subTextureElem.GetInt("frameHeight"); hotSpot.x_ = ((float)offset.x_ + frameWidth / 2) / width; hotSpot.y_ = 1.0f - ((float)offset.y_ + frameHeight / 2) / height; } DefineSprite(name, rectangle, hotSpot, offset); subTextureElem = subTextureElem.GetNext("SubTexture"); } loadXMLFile_.Reset(); loadTextureName_.Clear(); return true; }
bool SpriteSheet2D::Load(Deserializer& source) { spriteMapping_.Clear(); SharedPtr<XMLFile> xmlFile(new XMLFile(context_)); if(!xmlFile->Load(source)) { LOGERROR("Could not load sprite sheet"); return false; } SetMemoryUse(source.GetSize()); XMLElement rootElem = xmlFile->GetRoot(); if (!rootElem) { LOGERROR("Invalid sprite sheet"); return false; } if (rootElem.GetName() == "spritesheet") { ResourceCache* cache = GetSubsystem<ResourceCache>(); String textureFileName = rootElem.GetAttribute("texture"); texture_ = cache->GetResource<Texture2D>(textureFileName, false); // If texture not found, try get in current directory if (!texture_) texture_ = cache->GetResource<Texture2D>(GetParentPath(GetName()) + textureFileName); if (!texture_) { LOGERROR("Cound not load texture"); return false; } XMLElement spriteElem = rootElem.GetChild("sprite"); while (spriteElem) { String name = spriteElem.GetAttribute("name"); IntRect rectangle = spriteElem.GetIntRect("rectangle"); Vector2 hotSpot(0.5f, 0.5f); if (spriteElem.HasAttribute("hotspot")) hotSpot = spriteElem.GetVector2("hotspot"); DefineSprite(name, rectangle, hotSpot); spriteElem = spriteElem.GetNext("sprite"); } } // Sparrow Starling texture atlas else if (rootElem.GetName() == "TextureAtlas") { String textureFileName = rootElem.GetAttribute("imagePath"); ResourceCache* cache = GetSubsystem<ResourceCache>(); texture_ = cache->GetResource<Texture2D>(textureFileName, false); // If texture not found, try get in current directory if (!texture_) texture_ = cache->GetResource<Texture2D>(GetParentPath(GetName()) + textureFileName); if (!texture_) { LOGERROR("Cound not load texture"); return false; } XMLElement subTextureElem = rootElem.GetChild("SubTexture"); while (subTextureElem) { String name = subTextureElem.GetAttribute("name"); int x = subTextureElem.GetInt("x"); int y = subTextureElem.GetInt("y"); int width = subTextureElem.GetInt("width"); int height = subTextureElem.GetInt("height"); IntRect rectangle(x, y, x + width, y + height); Vector2 hotSpot(0.5f, 0.5f); if (subTextureElem.HasAttribute("frameWidth") && subTextureElem.HasAttribute("frameHeight")) { int frameX = subTextureElem.GetInt("frameX"); int frameY = subTextureElem.GetInt("frameY"); int frameWidth = subTextureElem.GetInt("frameWidth"); int frameHeight = subTextureElem.GetInt("frameHeight"); hotSpot.x_ = ((float)frameX + frameWidth / 2) / width; hotSpot.y_ = 1.0f - ((float)frameY + frameHeight / 2) / height; } DefineSprite(name, rectangle, hotSpot); subTextureElem = subTextureElem.GetNext("SubTexture"); } } else { LOGERROR("Invalid sprite sheet file"); return false; } return true; }