Example #1
0
  /** Starts the game loop after the application internals have been initialized. */
  protected void start() {
    listener.create();
    listener.resize(graphics.getWidth(), graphics.getHeight());

    if (runOnEDT) {
      new Runnable() {
        public void run() {
          frame();
          if (running) EventQueue.invokeLater(this);
          else end();
        }
      }.run();
    } else {
      while (running) frame();
      end();
    }
  }
Example #2
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));
  }
Example #3
0
 public void postRunnable(Runnable runnable) {
   synchronized (runnables) {
     runnables.add(runnable);
     graphics.requestRendering();
   }
 }
Example #4
0
 void render(long time) {
   graphics.frameStart(time);
   listener.render();
   glfwSwapBuffers(graphics.window);
 }