7
votes

I am trying to write some skeleton for a game on android, using OpenGL. I would like to know, where should I place my main game loop code?

So far, my best candidate is Renderer.onDrawFrame(...) method, which seems to be called per-frame, so the code looks like this:

void onDrawFrame(GL10 gl) 
{
     preLoopActions();

     m_gameScene->onUpdate();
     m_gameScene->onRender(gl);

     postLoopActions();
}

Is there any better approach? I dislike this one because 1) I have to mix updating and rendering in the place, where android expects me just to render, and 2) this method seems to be called from a separate "rendering thread", which increases game complexity.

2

2 Answers

2
votes

Separate the math and drawing logic. What you want is to create a thread that has a run loop in that loop you run the math() part and then call the render() part when you have that working there are some techniques to control the timing of the loop.

1
votes

Have you considered working with framework like AndEngine. It has something called as a GameActivity which makes life quite easy. The examples quite easy to pick up on.