dalerank / nanogui-sdl

Minimalistic port of NanoGUI claim works with SDL API w/o external dependencies.
Other
437 stars 77 forks source link

async texture load causes flickering during initial frames #55

Closed KarateSnoopy closed 2 years ago

KarateSnoopy commented 2 years ago

Might be something I'm doing wrong but when I load the simple example I see a quick single flicker, and later after I move my mouse cursor over the dialog I see flicker.

I debugged it down to draw() internally creates an AsyncTexturePtr if its not yet cached based on if mMouseFocus is true or not. I fixed it by adding a loadTex() API to widget and doing basically:

void Widget::loadTex(SDL_Renderer* renderer)
{
    for (auto child : mChildren)
        if (child->visible())
            child->loadTex(renderer);
}
void Window::loadTex(SDL_Renderer* renderer)
{
    for (int i = 0; i < 2; i++)
    {
        bool mMouseFocusTemp = (i==0) ? true : false;
        int id = (mMouseFocusTemp ? 0x1 : 0);
        auto atx = std::find_if(_txs.begin(), _txs.end(), [id](AsyncTexturePtr p) { return p->id == id; });
        if (atx == _txs.end())
        {
            AsyncTexturePtr newtx = std::make_shared<AsyncTexture>(id);
            newtx->load(this, 0, 0, mMouseFocusTemp);
            _txs.push_back(newtx);
        }
    }

    Widget::loadTex(renderer);
}

Then calling this at startup.
This avoids any runtime flickering.
No idea if I'm on the right track to solving this, but fyi in case any one hits this.

dalerank commented 2 years ago

Thanks,will check up it

akuskis commented 2 years ago

Now it should be fixed. When components are loaded, they use previous texture for the new state until new one is loaded.

PS: There is a not nice visual effect when components are loading first time (there are no textures ready). But this is out of this task (new issue should be created).

dalerank commented 2 years ago

thanks