Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. private static final int MAX_UPDATES_PER_DRAW = 10;
  2.  
  3. private long timeAccumulator = 0;
  4. private long lastTime = System.nanoTime();
  5.  
  6. @Override
  7. public void onDrawFrame(GL10 unused)
  8. {
  9. // Use this structure to set constant dt on a given frame
  10. // Limit updates to avoid spiral of death;
  11. long time = System.nanoTime();
  12. timeAccumulator += time - lastTime;
  13. lastTime = time;
  14.  
  15. int updateCount = 0;
  16. while (timeAccumulator >= dt && updateCount < MAX_UPDATES_PER_DRAW)
  17. {
  18. // Update
  19. game.update((double) dt / (double) NANOS_PER_SECOND); // Divide by 1 000 000 000 for seconds
  20. timeAccumulator -= dt;
  21. updateCount++;
  22.  
  23. // Account for any time lost
  24. time = System.nanoTime();
  25. timeAccumulator += time - lastTime;
  26. lastTime = time;
  27. }
  28.  
  29. // Calculate alpha to interpolate between states for smooth animation
  30. // to avoid temporal aliasing. Alpha ranges from 0.0 - 1.0
  31. double alpha = Maths.clamp((double) timeAccumulator / (double) dt, 0.0, 1.0);
  32.  
  33. // Draw game.
  34. game.draw(alpha);
  35.  
  36. // Check for OpenGL errors.
  37. int error = GLES20.glGetError();
  38. if (error != 0)
  39. throw new RuntimeException("OpenGL Error: " + error);
  40.  
  41. // Account for any time lost
  42. time = System.nanoTime();
  43. timeAccumulator += time - lastTime;
  44. lastTime = time;
  45.  
  46. // Delay to maintain fps for battery conservation
  47. long timeUntilUpdate = dt - timeAccumulator;
  48. if (timeUntilUpdate > 0)
  49. {
  50. try
  51. {
  52. Thread.sleep(timeUntilUpdate / NANOS_PER_MILI);
  53. }
  54. catch (InterruptedException e)
  55. {
  56. e.printStackTrace();
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement