title: How to create update/draw loop appropriately with android opengl?
body: <p>I wanted to write some simple game in android using opengl es but immediately ran into trouble with the main game loop. As I read here: <a href="http://developer.android.com/resources/tutorials/opengl/opengl-es10.html">http://developer.android.com/resources/tutorials/opengl/opengl-es10.html</a> the app calls <code>public void onDrawFrame(GL10 gl)</code> every time it needs to redraw the surface. Or smth like that. My problem was - how to create an update loop that is independent of draw calls. I mean - it can't work like that, I can't update the game logic only when the device (app?) wants to redraw the surface (or am I wrong?).</p>
  
  <p>After some googling I came to a conclusion that I have to create another thread for update loop. But then I had another problem - with one thread taking care of drawing and another one taking care of updating the game logic I didn't know how to make them cooperate. First, they were two separate classes (at least in my implementation) so they couldn't use the same game variables and objects (sprites, timers, different variables, counters and so on... pretty much everything these two classes needed to do their jobs). Now I think I could somehow pack them both into one class. But - second, I needed to synchronize the two threads somehow.</p>
  
  <p>Finally, I came out with this general idea:</p>
  
  <ul>
  <li>3 classes:
  <ol>
  <li><code>public class MyRenderer implements GLSurfaceView.Renderer</code> with <code>onSurfaceCreated()</code> method taking care of drawing</li>
  <li><code>public class UpdateThread implements Runnable</code> with <code>run()</code> and <code>update()</code> methods. <code>run()</code> was calling <code>update()</code> method exactly 60 times a second (I wanted a fixed step loop)</li>
  <li><code>public class SpritesHolder</code> used as a container for all the game objects/variables/stuff (like sprites, timers, state variables and so on...) with all the fields public.</li>
  </ol></li>
  <li>So basically the <code>SpritesHolder</code> class was a box holding all the needed variables in one place, so <code>MyRenderer</code> and <code>UpdateThread</code> classes could access it and use it.</li>
  <li><p>As for synchronization - I just did smth like this:</p>
  
  <pre><code>public void update(float delta)
  {
      synchronized (spritesHolder)
      {
          // whole method code...
      }
  }
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>public void onDrawFrame(GL10 gl)
  {
      synchronized (spritesHolder)
      {
          // whole method code...
      }
  }
  </code></pre>
  
  <p>so that both threads didn't use the spritesHolder at the same time. So updates were done 60 times per second and drawing took place whenever app (device?) needed.</p></li>
  </ul>
  
  <p>A lot of talking, sorry, I've almost finished writing this post. ;) So anyway - this (described above) works and I even did write some game based on this 'template' but I think my ideas might be crazy and one can desing it all a whole lot better. I'd be very grateful for all comments and advices.</p>