Switching between multiple Worlds

Started by K24A3, September 19, 2011, 04:09:06 PM

Previous topic - Next topic

K24A3

I'm trying to utilize two separate worlds, one world is for the menu, the other is for the game.

My design is this:

-start the app
-load the menu world
-when 'start' is pressed, load another world
-go back to the original world when done

I created a class for the menu world, within that menu world I load another world on the fly, and redirect the main GLSurfaceView.Renderer OnFrame call to the game world.

I've ran into all kinds of problems. It's crashing when loading the game world, it always crashed on world.draw() for some reason. If I remove world.draw, the skymap renders ok but nothing else. If I remove all the object3D creations, draw.world works fine.
I tried using the same World both both the menu and game, but still have problems.
The confusing thing is, if I load the game world first, it works ok.


But anyway, ignore all that... Time to start fresh!

1. My main questions is, how do you handle two separate worlds correctly? Logically?

2. Do you need to dispose the first world then reload the second?  Or can both worlds coexists side by side, redirecting the onFrame method to the current world?

3. Rather calling/loading a world from within a world (in terms of class files), would it be better to create a World Handler that disposes the current world before creating the new world, having each world in it's own class?


Any help is appreciated, thanks.

EgonOlsen

There's no problem with rendering different worlds into one frame buffer. No need to manage them somehow. You just pick the world that you need and render it. If that doesn't work, then its a bug and i would like to see the exception or a test case.

K24A3

Ok I'll persist further with the current setup and double check all the logic.

The two worlds are sharing the singular TextureManager and framebuffer, everything else is separate.

Thanks for the help, I'll post the exception if I get stuck.

K24A3

Ok I found the main problem. For those who run into the same problems, here's the story...

The GL renderer was calling onFrame while I was loading objects into the 2nd world, causing Object3D pointers to crash since they were null. Obviously the renderer is running on it's own thread.

What confused me is that the null objects were not generating an exception in the renderer thread (onFrame function). Instead it was just jumping/return out of the onFrame function half way through the function by itself, before it was reaching the world.draw() call, then crashing the app around 10 seconds later.

Anyway I started fresh and simply used the same World class for both worlds, using world.removeAllObjects() when switching worlds, using a boolean flag in onFrame to return straight away to avoid rendering while loading objects, and pre-loading all the textures at the start of the app rather than load them on the fly when needed. It's much less complicated that way in my opinion. The menu textures are small anyway.

K24A3

#4
I'm having headaches trying to load 3D objects during onDrawFrame(GL10 gl) from the renderer thread. I think there is a sync problem with the renderer thread and the touch input thread

If I do this:

onDrawFrame()
{
    if(bLoad3DObjects == true)
   {
      LoadWorldObjects();
                bLoad3DObjects = false; // all 3d objects are loaded, should be ready to draw
      return;
   }
   
   world.draw()...
   etc..
}

and..

onTouchEvent(MotionEvent event)
{
   if(ACTION_DOWN == true)
   {
      bLoad3DObjects = true;
   }
}


Next time it calls onDrawFrame, it crashes saying:


Thread [<9> GLThread 10] (Suspended (exception OutOfMemoryError))   
   GLSurfaceView$GLThread.run() line: 1184   




It doesn't crash when I do this:

onDrawFrame()
{
    if(bDontRender == true)
   {
      return;
   }
   
   world.draw()...
   etc..
}

and..

onTouchEvent(MotionEvent event)
{
   if(ACTION_DOWN == true)
   {
      bDontRender = true;
      LoadWorldObjects();
      bDontRender = false;
   }
}

But that introduces a crash when returning back to the menu. I'm guessing it's a threading problem. Should I be using Synhronize() somewhere?

Does onDrawFrame() expect something to be rendered?

Is it safe to exit from the start of onDrawFrame if I load or unload from a Touch event?

K24A3

Maybe I should use queueEvent()?

http://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer.html

Threading
The renderer will be called on a separate thread, so that rendering performance is decoupled from the UI thread. Clients typically need to communicate with the renderer from the UI thread, because that's where input events are received. Clients can communicate using any of the standard Java techniques for cross-thread communication, or they can use the queueEvent(Runnable) convenience method.

EgonOlsen

queueEvent is one options. Or you could do something like:


onDrawFrame()
{
   synchronize(this) {
    world.draw()...
    etc..
   }
}

and..

onTouchEvent(MotionEvent event)
{
   if(ACTION_DOWN == true)
   {
      synchronize(renderer) {
      LoadWorldObjects();
      }
   }
}


Never use simple boolean (or other) flags for synchronization, because you might still fall into a trap where both paths will be executed in parallel.

However, i don't think that this really solves the actual problem. Loading your objects in the render thread should actually work fine (it's not nice, because you are blocking the screen updates with that, but it should work...). If you are getting an OOM-error, maybe your problem is rather that your resources are a bit too large? The Wiki has some information about this: http://www.jpct.net/wiki/index.php/Reducing_memory_usage

Still, i don't see why you are dealing with two worlds in such a complicated manner. Just initialize both at startup and render them on demand once they have been fully loaded. As said: If this doesn't work (for any other reason than out of memory...), it's a bug. There should be no need to switch a world's content on the fly unless you are going to replace it anyway.

K24A3

I tried adding Synchronized() to those two areas and it helped with one problem, but it still crashes when exiting to the menu and it crashes at random when loading the app. It's got to be a threading/timing issue. The Resources are not large.

I'll redesign the system to use two Worlds as you recommended, and perhaps use RENDER_WHEN_DIRTY instead since I don't see the value of having a dedicated rendering thread if it complicates everything. Keeping the flow in my own loop seems cleaner and more logical to me.

Appreciate your help, thanks.


K24A3

I've got it all working now.

I ended up using two preloaded worlds, placing each world in it's own class file, and using a main class to handle both worlds. I ended up using queueEvent in the TouchEvent to handle the multiple threads.

Now I can swap between the two worlds 60 times a second via TouchEvent, causing the two worlds to appear blended together. It's solid as a rock now :)

Thanks again Egon.