Archive 17/01/2023.

Drawing 2d in screen coordintes

claudeHasler

Im trying to display a cross at the pixel where a mouseclick is performed, but I havent figured out how. Does Urho have a 2d primitives drawing system?

throwawayerino

You could get mouse position from UI subsystem and draw a borderimage

claudeHasler

Ill try that. What i’ve tried is to set a text “X” at the place where I clicked but nothing shows up.
This is subscribed to E_MOUSEBUTTONUP, and the handler gets called. It just doesnt draw the ui

void MyApp::HandleEvent(StringHash eventType, VariantMap& eventData)
{
    std::cout << "clicked" << std::endl;

    Input* input = GetSubsystem<Input>();
    UI* ui = GetSubsystem<UI>();
    if (input) {
        std::cout << input->GetMousePosition().x_ << " " << input->GetMousePosition().y_ << std::endl;
        if (ui) {
            auto root = ui->GetRoot();
            auto text = root->CreateChild<Text>();
            text->SetPosition(input->GetMousePosition().x_, input->GetMousePosition().y_);
            text->SetText("X");
            text->SetColor(Color(255, 0, 0));
        }
    }
}
Modanung

Does your UI have a (default) style assigned to it? Sample 2 shows you how.

claudeHasler

No im not setting any styles, is this necessary?

Modanung

Yes, I believe this to be the case.

throwawayerino

yes it is. UI is a bit weird right now, but trust me you’ll love it once you get used to it.

claudeHasler

Thanks to both of you. Drawing both sprites and text is now working.

When drawing a sprite, im currently assigning loading texture like so:

auto texture = GetSubsystem<ResourceCache>()->GetResource<Texture2D>("Textures/cross.png");
sprite->SetTexture(texture);

What is the lifetime of such a texture? Should I be loading the texture once for every Sprite I assign it to? Or only once and share it? If I share the texture, may I store it in a SharedPtr or should it be a raw pointer?

SirNate0

The resource cache stores a pointer to the loaded resource and returns it when you request the same resource. So they are shared between all of them, and as long as you know that the resource cache will keep the texture alive longer than you keep the pointer you can use a raw pointer. If you’re not certain, stick with the shared pointer.