0
\$\begingroup\$

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?

\$\endgroup\$
1

3 Answers 3

1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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! \$\endgroup\$ Apr 20, 2013 at 16:28
  • \$\begingroup\$ 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. \$\endgroup\$
    – House
    Apr 20, 2013 at 16:46
  • \$\begingroup\$ @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? \$\endgroup\$
    – async
    Mar 31, 2014 at 11:22
0
\$\begingroup\$

You can override the run method:

@Override
public void run() 
{
    while(gameRunning) 
    { 
        //logic updates
    }
}

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.

\$\endgroup\$
4
  • \$\begingroup\$ 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! \$\endgroup\$ Apr 20, 2013 at 21:06
  • \$\begingroup\$ See the question and answer I linked. Rendering is done on a separate thread. Good luck! \$\endgroup\$
    – House
    Apr 20, 2013 at 21:17
  • \$\begingroup\$ 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 :-/) \$\endgroup\$ Apr 20, 2013 at 22:07
  • \$\begingroup\$ @Byte56 Whose run method? \$\endgroup\$
    – async
    Mar 31, 2014 at 11:18
0
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ This is the identical result to this answer. Except you're pulling out the code into other functions. The same execution though. \$\endgroup\$
    – House
    Apr 21, 2013 at 5:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .