Non-continuous Rendering in Loading screen not working as it supposed to work - Multithreading LIBGDX

亡梦爱人 提交于 2019-12-08 15:16:31

If your loading happens faster than the frame time (e.g., if you're loading steps are faster than 60 steps per second) a user won't see all the animation steps. This sounds like a good thing to me!

If however, you want to make sure all the steps of the animation are shown, I think your background thread should just set a "target" animation frame, and the render thread should advance the actual frame by one until the target is hit. It could operate something like this:

  • Background thread sets target = 10 (and requests a render)
  • Render thread is at 0, sees target 10, draws 0, bumps count to 1, sets the request render flag.
  • Render thread is at 1, sees target of 10, draws 1, bumps count to 2, sets request render flag
  • Background thread sets target = 11 (and requests a render).
  • etc, etc.

This way the two can proceed at their own pace. If you want the loading to go quickly the render thread can just draw the target frame. If you want to show every frame, it can bump by 1.

One note: posting a runnable will implicitly request a render.

Secondly, it also looks like all of the work in your "background" thread is being done inside posted runnables (which is probably true, since most loading requires OpenGL context), but maybe its just because you simplified the code for posting your question. Anyway, if that is true, then it is just an awkward way of running everything on the render thread. You might be better off setting up a simple state machine, and having your render thread step through it to run your loading process directly:

if (! done) {
    switch(loadingStep) {
       case 0:
         // step 0 code
         break;
       case 1:
         // step 1 code 
         break;
       case 2:
         ...
       case 10:
         done = true;
         break;
    }

    loadingStep++;
}

This has the side benefit of running exactly one step per render.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!