Hello, I’m trying to draw some 2d stuff but I’m missing something. In the code I create a node with sprite2d comp by using a section of another image, but it doesn’t show on the screen. As a base I’m using example 24. In create scene I add:
auto createNode = [&](String spriteName) ->Node* {
Node* node = scene_->CreateChild();
StaticSprite2D* spriteComp {node->CreateComponent<StaticSprite2D>()};
Sprite2D* sprite = cache->GetResource<Sprite2D>(spriteName);
spriteComp->SetSprite(sprite);
return node;
};
auto subSpriteShow = [&](){
int subImageRow = 0;
int subImageCol = 0;
//load the whole image
Image* wholeImage = cache->GetResource<Image>("GameData/2D/mona lisa.png");
int wholeImageSize = wholeImage->GetWidth();
//images are squared by design
int subImageSize = wholeImageSize / 4;
//left-top-right-bottom
IntRect subImageRect = IntRect(
subImageCol * subImageSize, subImageRow * subImageSize,
subImageCol * subImageSize + subImageSize, subImageRow * subImageSize + subImageSize);
Image* subImage = wholeImage->GetSubimage(subImageRect);
if (!subImage) {
URHO3D_LOGERROR("Not able to slice image. Aborting.");
return;
}
//testing output. it outputs a correct image
subImage->SavePNG("GameData/2D/mona slice.png");
//create Texture2D
Texture2D* subTex2D = new Texture2D(context_);
subTex2D->SetData(subImage);
subTex2D->SetSize(subImageSize, subImageSize, Graphics::GetRGBAFormat(), TextureUsage::TEXTURE_STATIC);
//create Sprite2D
Sprite2D* subSprite = new Sprite2D(context_);
subSprite->SetTexture(subTex2D);
subSprite->SetRectangle(subImageRect);
//this works ok, using the created image, and shows it on screen
//lisaNode = createNode("GameData/2D/mona slice.png");
//this doesn't show anything
//create node
lisaNode = scene_->CreateChild("lisaNode");
lisaNode->SetPosition(Vector3::ZERO);
//create component
spriteText2d = lisaNode->CreateComponent<StaticSprite2D>(LOCAL);
spriteText2d->SetSprite(subSprite);
spriteText2d->SetLayer(100);
};
subSpriteShow();
If I use the saved image, everything shows ok. There must be st I’m missing in the construction of sub sprite… thanks in advance.