Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void test() {
- final double GAME_HERTZ = 144;
- final double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;
- final int MAX_UPDATES_BEFORE_RENDER = 5;
- double lastUpdateTime = System.nanoTime();
- double lastRenderTime = System.nanoTime();
- final double TARGET_FPS = 144;
- final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;
- int lastSecondTime = (int) (lastUpdateTime / 1000000000);
- while (isRunning) {
- double now = System.nanoTime();
- int updateCount = 0;
- if (!paused) {
- while (now - lastUpdateTime > TIME_BETWEEN_UPDATES && updateCount < MAX_UPDATES_BEFORE_RENDER) {
- tick();
- lastUpdateTime += TIME_BETWEEN_UPDATES;
- updateCount++;
- }
- if (now - lastUpdateTime > TIME_BETWEEN_UPDATES) {
- lastUpdateTime = now - TIME_BETWEEN_UPDATES;
- }
- render();
- lastRenderTime = now;
- int thisSecond = (int) (lastUpdateTime / 1000000000);
- if (thisSecond > lastSecondTime) {
- System.out.println("FPS " + frameCount);
- fps = frameCount;
- frameCount = 0;
- lastSecondTime = thisSecond;
- }
- while (now - lastRenderTime < TARGET_TIME_BETWEEN_RENDERS
- && now - lastUpdateTime < TIME_BETWEEN_UPDATES) {
- Thread.yield();
- now = System.nanoTime();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment