Android OpenGL gameloop outside onDrawFrame

I have a problem creating a gameloop for my first game. I read a lot about this, but still can not understand. It is based on OpenGL, so I used onDrawFrame as a game loop, and it works fine on my phone. The problem is that onDrawFrame is the update time, it depends on the hardware, so it works too fast on some devices. Therefore, I want to add a separate game cycle, which will be updated with a constant period of time on all smartphones. (and onDrawFrame will only care about the graphics, as it should be)

At the moment I have:

  • class myGameRenderer with all openGl files onDrawFrame
  • myGLSurfaceView supporting touch events
  • myGameActivity

onDrawFrame activates the function myGameUpdate, which controls the position change of all objects in the game depending on the information from myGLSurfaceView

I tried to create a new Runnable, but it does not seem to work, I can’t figure out how to run this runnable and where I should place it (I tried to put it in the myGameRenderer class, but it didn’t work, nothing moved:

private final Runnable mUpdateDisplay = new Runnable() { @Override public void run() { update(); }}; private void update() { //some update stuff blablabla //some update stuff blablabla mHandler.postDelayed(mUpdateDisplay,40); //to refresh at 25 fps } 

but I think I have no idea - I mean that I create this runnable. I tried putting it in onCreateSurface to run it, but no effect. So is this a general idea? And how to start a cycle? Where to place it? Or should I use any other way?

+4
source share
1 answer

Well, that was simple - I just missed r.run (); But, as always, there is something. Now it works the way I wanted - I mean, the framework is not hardware dependent, but everything is not as smooth as it was - and some of the objects in 3d flicker. It seems that some objects are visually drawn faster, and some later look ugly. So what am I doing wrong? Is there a better way?

0
source

All Articles