Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
Example of a game that uses this structure: code.google.com/p/android-breakout . Some discussion about the one-vs-two thread approaches is in the supporting docs, and also here: stackoverflow.com/questions/14077403/… –  fadden Apr 24 '13 at 0:35
add comment

3 Answers 3

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.

share|improve this answer
    
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! –  user22241 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. –  Byte56 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? –  user16547 Mar 31 at 11:22
add comment

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.

share|improve this answer
    
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! –  user22241 Apr 20 '13 at 21:06
    
See the question and answer I linked. Rendering is done on a separate thread. Good luck! –  Byte56 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 :-/) –  user22241 Apr 20 '13 at 22:07
    
@Byte56 Whose run method? –  user16547 Mar 31 at 11:18
add comment
up vote 0 down vote accepted

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!

share|improve this answer
    
This is the identical result to this answer. Except you're pulling out the code into other functions. The same execution though. –  Byte56 Apr 21 '13 at 5:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.