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?

3
  • 1
    You should check out something on framerate independence. This question is a good first step.
    – Geobits
    Feb 12, 2013 at 19:40
  • Thx, I'll check out on this subject. Maybe it'll help
    – Lucass
    Feb 12, 2013 at 20:14
  • @Geobits - thanks for the link, also this answer points to a really good article discussing different approaches to frame renders/game updates.
    – kerim
    Feb 13, 2013 at 7:13

1 Answer 1

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?

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.