Sprite2D* Sprite2D::LoadFromResourceRef(Object* object, const ResourceRef& value) { if (!object) return 0; ResourceCache* cache = object->GetSubsystem<ResourceCache>(); if (value.type_ == Sprite2D::GetTypeStatic()) return cache->GetResource<Sprite2D>(value.name_); if (value.type_ == SpriteSheet2D::GetTypeStatic()) { // value.name_ include sprite sheet name and sprite name. Vector<String> names = value.name_.Split('@'); if (names.Size() != 2) return 0; const String& spriteSheetName = names[0]; const String& spriteName = names[1]; SpriteSheet2D* spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetName); if (!spriteSheet) return 0; return spriteSheet->GetSprite(spriteName); } return 0; }
void Drawable2D::SetSpriteAttr(ResourceRef value) { // Delay applying material update materialUpdatePending_ = true; ResourceCache* cache = GetSubsystem<ResourceCache>(); if (value.type_ == Sprite2D::GetTypeStatic()) { SetSprite(cache->GetResource<Sprite2D>(value.name_)); return; } if (value.type_ == SpriteSheet2D::GetTypeStatic()) { // value.name_ include sprite speet name and sprite name. Vector<String> names = value.name_.Split('@'); if (names.Size() != 2) return; const String& spriteSheetName = names[0]; const String& spriteName = names[1]; SpriteSheet2D* spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetName); if (!spriteSheet) return; SetSprite(spriteSheet->GetSprite(spriteName)); } }
void CapitalShip::DelayedStart() { Graphics* graphics = GetSubsystem<Graphics>(); float halfHeight_ = graphics->GetHeight() * PIXEL_SIZE * 0.5f; ResourceCache* cache = GetSubsystem<ResourceCache>(); SpriteSheet2D* spaceSheet = cache->GetResource<SpriteSheet2D>("Sprites/spacegame_sheet.xml"); StaticSprite2D* sprite2D = node_->CreateComponent<StaticSprite2D>(); sprite2D->SetSprite(spaceSheet->GetSprite("spaceship_locust")); sprite2D->SetBlendMode(BLEND_ALPHA); node_->SetPosition2D(-4.0f, halfHeight_ - 1.0f); node_->Roll(180.0f); }
void Enemy::DelayedStart() { ResourceCache* cache = GetSubsystem<ResourceCache>(); SpriteSheet2D* spaceSheet = cache->GetResource<SpriteSheet2D>("Sprites/spacegame_sheet.xml"); StaticSprite2D* sprite2D = node_->CreateComponent<StaticSprite2D>(); sprite2D->SetSprite(spaceSheet->GetSprite(spriteName_)); sprite2D->SetBlendMode(BLEND_ALPHA); node_->SetPosition2D(spawnPosition_); node_->Roll(180.0f); node_->SetScale2D(0.65f, 0.65f); dir_ = (Random() > 0.5); }
void Player::Start() { input_ = GetSubsystem<Input>(); Graphics* graphics = GetSubsystem<Graphics>(); halfHeight_ = graphics->GetHeight() * PIXEL_SIZE * 0.5f; halfWidth_ = graphics->GetWidth() * PIXEL_SIZE * 0.5f; ResourceCache* cache = GetSubsystem<ResourceCache>(); SpriteSheet2D* spaceSheet = cache->GetResource<SpriteSheet2D>("Sprites/spacegame_sheet.xml"); StaticSprite2D* sprite2D = node_->CreateComponent<StaticSprite2D>(); sprite2D->SetSprite( spaceSheet->GetSprite("spaceship_mantis")); sprite2D->SetBlendMode(BLEND_ALPHA); node_->SetPosition2D(0, -halfHeight_ + 1.0f); }
bool Animation2D::Load(Deserializer& source) { frameEndTimes_.Clear(); frameSprites_.Clear(); SharedPtr<XMLFile> xmlFile(new XMLFile(context_)); if(!xmlFile->Load(source)) { LOGERROR("Could not load animation"); return false; } SetMemoryUse(source.GetSize()); XMLElement rootElem = xmlFile->GetRoot("Animation"); if (!rootElem) { LOGERROR("Invalid animation"); return false; } ResourceCache* cache = GetSubsystem<ResourceCache>(); XMLElement keyFrameElem = rootElem.GetChild("Frame"); if (!keyFrameElem) { LOGERROR("Could not found key frame"); return false; } float endTime = 0.0f; while (keyFrameElem) { endTime += keyFrameElem.GetFloat("duration"); frameEndTimes_.Push(endTime); SharedPtr<Sprite2D> sprite; Vector<String> names = keyFrameElem.GetAttribute("sprite").Split('@'); if (names.Size() == 1) sprite = cache->GetResource<Sprite2D>(names[0]); else if (names.Size() == 2) { SpriteSheet2D* spriteSheet = cache->GetResource<SpriteSheet2D>(names[0]); if (!spriteSheet) { LOGERROR("Could not get sprite speet"); return false; } sprite = spriteSheet->GetSprite(names[1]); } if (!sprite) { LOGERROR("Could not get sprite"); return false; } frameSprites_.Push(sprite); keyFrameElem = keyFrameElem.GetNext("Frame"); } return true; }
bool AnimationSet2D::LoadFolders(const XMLElement& rootElem) { ResourceCache* cache = GetSubsystem<ResourceCache>(); bool async = GetAsyncLoadState() == ASYNC_LOADING; String parentPath = GetParentPath(GetName()); String spriteSheetFilePath = parentPath + GetFileName(GetName()) + ".xml"; SpriteSheet2D* spriteSheet = 0; bool hasSpriteSheet = false; // When async loading, request the sprite sheet for background loading but do not actually get it if (!async) spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetFilePath, false); else { hasSpriteSheet = cache->Exists(spriteSheetFilePath); if (hasSpriteSheet) cache->BackgroundLoadResource<SpriteSheet2D>(spriteSheetFilePath, false, this); } for (XMLElement folderElem = rootElem.GetChild("folder"); folderElem; folderElem = folderElem.GetNext("folder")) { unsigned folderId = folderElem.GetUInt("id"); for (XMLElement fileElem = folderElem.GetChild("file"); fileElem; fileElem = fileElem.GetNext("file")) { unsigned fileId = fileElem.GetUInt("id"); String fileName = fileElem.GetAttribute("name"); // When async loading, request the sprites for background loading but do not actually get them if (!async) { SharedPtr<Sprite2D> sprite; if (spriteSheet) sprite = spriteSheet->GetSprite(GetFileName(fileName)); else sprite = (cache->GetResource<Sprite2D>(parentPath + fileName)); if (!sprite) { LOGERROR("Could not load sprite " + fileName); return false; } Vector2 hotSpot(0.0f, 1.0f); if (fileElem.HasAttribute("pivot_x")) hotSpot.x_ = fileElem.GetFloat("pivot_x"); if (fileElem.HasAttribute("pivot_y")) hotSpot.y_ = fileElem.GetFloat("pivot_y"); // If sprite is trimmed, recalculate hot spot const IntVector2& offset = sprite->GetOffset(); if (offset != IntVector2::ZERO) { int width = fileElem.GetInt("width"); int height = fileElem.GetInt("height"); float pivotX = width * hotSpot.x_; float pivotY = height * (1.0f - hotSpot.y_); const IntRect& rectangle = sprite->GetRectangle(); hotSpot.x_ = (offset.x_ + pivotX) / rectangle.Width(); hotSpot.y_ = 1.0f - (offset.y_ + pivotY) / rectangle.Height(); } sprite->SetHotSpot(hotSpot); sprites_[(folderId << 16) + fileId] = sprite; } else if (!hasSpriteSheet) cache->BackgroundLoadResource<Sprite2D>(parentPath + fileName, true, this); } } return true; }