Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to place main game loop in android game

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.

like image 654
SadSido Avatar asked Oct 22 '10 09:10

SadSido


2 Answers

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.

like image 50
James Andino Avatar answered Sep 21 '22 19:09

James Andino


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.

like image 38
Sameer Segal Avatar answered Sep 22 '22 19:09

Sameer Segal