# Where to run logic update in openGL ES?

Using Canvas with Surfaceview I had a run() method where I would essentially do the following:

onDraw();
update();


Where onDraw method would do the rendering and update would update all my game physics / logic etc...

Having switched to openGL ES (2.0) and therefore now using a glSurfaceview, I have the following:

onDrawFrame(){

//Rendering code here

}


Which I don't call myself as it gets called automatically (my rendering is set to continuous).

So where / how do I run my logic updates?

The easiest solution is always nice:

onDrawFrame(){

//Rendering code here

update();

}


Or you could put a break point in your draw code and step out to see if there's an update loop above it you could override.

• hi @Byt56 - thanks for this, I didn't think of exactly the same thing, however I can see a problem at that is that I would want to limit my frames per second (probably to 30fps) but not my logic updates - this wouldn't be possible using the method you described as the logic is being called from the rendering method. Any other ideas? thanks! – BungleBonce Apr 20 '13 at 16:28
• It's the same execution as the first pair of functions you listed, is why I suggested it. I'll look around for other options, or someone with more knowledge of opengles will answer. – MichaelHouse Apr 20 '13 at 16:46
• @Byte56 Are you suggesting that we can safely consider one execution of onDrawFrame to be equivalent to one iteration in a standard while(running){ } game loop? – async Mar 31 '14 at 11:22

You can override the run method:

@Override
public void run()
{
while(gameRunning)
{
}
}


Since the rendering will be performed on a separate thread. You can run inside this while loop on the main thread.

You'll likely want to implement some limiting in there as well, you can find an example of that here.

• Thanks @Byte56, I will look into this, preferably, I would like to update the logic on the same thread as the rendering. (ie, rendering and logic on the same thread) as I have the main activity and the glSurfaceview in separate class files, so unsure best how to access variables of one from the other. Thanks I'll check it and report back! – BungleBonce Apr 20 '13 at 21:06
• See the question and answer I linked. Rendering is done on a separate thread. Good luck! – MichaelHouse Apr 20 '13 at 21:17
• Hi @Byte56, that question uses a very similar game loop to that which I used to use when using Canvas. But I'm not using canvas this time, as mentioned in my question. I'm now using openGL ES2.0 with a glSufaceview. I've looked but I can't see run() method to overide, that's the problem, unless I'm doing something very silly! (It wouldn't be the first time :-/) – BungleBonce Apr 20 '13 at 22:07
• @Byte56 Whose run method? – async Mar 31 '14 at 11:18

I've been reading up some more on this and it seems if you want a 2 threaded set up (that being 1 thread for UI and another for rendering and logic updates, then your best bet is the use the onDrawFrame method as something akin to Canvas's 'run()' method. Simply create your own Draw() and update() methods and call them from inside onDrawFrame, this way you can put your limiting / timing code inside onDrawFrame (the stuff that would normally go into run()). I can't see any other way around it, but if anyone has any other ideas for an effective 2 threaded OpenGL ES 2.0 setup, I'd appreciate hearing it!

• This is the identical result to this answer. Except you're pulling out the code into other functions. The same execution though. – MichaelHouse Apr 21 '13 at 5:01