Esempio n. 1
0
  /** Handles posted runnables, input, and rendering for each frame. */
  protected void frame() {
    if (!running) return;

    synchronized (runnables) {
      executedRunnables.clear();
      executedRunnables.addAll(runnables);
      runnables.clear();
    }
    if (executedRunnables.size > 0) {
      for (int i = 0; i < executedRunnables.size; i++) executedRunnables.get(i).run();
      if (!running) return;
      graphics.requestRendering();
    }

    input.update();

    long frameStartTime = System.nanoTime();
    int targetFPS =
        (graphics.isHidden() || graphics.isMinimized())
            ? hiddenFPS
            : //
            (graphics.isForeground() ? foregroundFPS : backgroundFPS);

    if (targetFPS == -1) { // Rendering is paused.
      if (!isPaused) listener.pause();
      isPaused = true;
    } else {
      if (isPaused) listener.resume();
      isPaused = false;
      if (graphics.shouldRender()) render(frameStartTime);
    }

    if (targetFPS != 0)
      sleep(
          targetFPS == -1
              ? 100
              : (int) (1000f / targetFPS - (System.nanoTime() - frameStartTime) / 1000000f));
  }
Esempio n. 2
0
 public void postRunnable(Runnable runnable) {
   synchronized (runnables) {
     runnables.add(runnable);
     graphics.requestRendering();
   }
 }