4

I've got a problem with creating gameloop for my first game. I've read a lot about it but still can't figure it out. It's based on OpenGL so I've used onDrawFrame as a game loop and it works fine on my phone. Problem is that onDrawFrame is refresh time depends on hardware so it runs way too fast on some devices. So what I want is adding a separate game loop that will refresh itself at constant period of time on all smartphones. (and onDrawFrame will only take care of graphics as it should)

As for now I have:

  • myGameRenderer class with all openGl stuff an onDrawFrame
  • myGLSurfaceView that supports touch events
  • myGameActivity

onDrawFrame activates myGameUpdate function that controls changing positions of all objects in game depending on info from myGLSurfaceView

I've tried with creating new Runnable but it doesn't seem to work, I can't figure out how to start that runnable and where i should place it (I've tried to place it in myGameRenderer class, but it didn't seem to work, nothing was moving:

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 guess I don't get the idea of it - I mean I create this runnable. I've tried to place it in onCreateSurface to start it but no effect. So - is the generall idea ok? And how to start the loop? Where to place it? Or should I use any other way?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Lucass
  • 147
  • 3
  • 12
  • 1
    You should check out something on framerate independence. [This question](http://stackoverflow.com/q/17411/752320) is a good first step. – Geobits Feb 12 '13 at 19:40
  • Thx, I'll check out on this subject. Maybe it'll help – Lucass Feb 12 '13 at 20:14
  • @Geobits - thanks for the link, also [this](http://stackoverflow.com/a/17424/1929432) answer points to a really good article discussing different approaches to frame renders/game updates. – kerim Feb 13 '13 at 07:13

1 Answers1

0

Ok it was simple - I was just missing r.run(); But as allways there's something. Now it works as i wanted - I mean frames doesn't depend on hardware, but everything is not as smooth as it was - and part of objects in 3d are flickering. Seems like some objects visibly are drawn faster, some later and it looks ugly. So what am I doing wrong? Is there a better way?

Lucass
  • 147
  • 3
  • 12
  • 1
    See https://code.google.com/p/android-breakout/ for an example of an onDrawFrame()-based game loop. – fadden Feb 20 '13 at 02:07
  • @fadden Thx a lot man, I'll look into it :) I've just solved the problem with sollution so if someone has similar problem I also reccomend solution from here: http://stackoverflow.com/questions/5888284/android-putting-the-glsurfaceview-renderer-to-sleep-as-in-thread-sleep20 – Lucass Feb 25 '13 at 17:12