void mainLoop() {
    Array<LifecycleListener> lifecycleListeners = this.lifecycleListeners;

    listener.create();

    boolean wasActive = true;

    // unlike LwjglApplication, a headless application will eat up CPU in this while loop
    // it is up to the implementation to call Thread.sleep as necessary
    while (running) {
      executeRunnables();
      listener.render();

      // If one of the runnables set running to false, for example after an exit().
      if (!running) break;
    }

    synchronized (lifecycleListeners) {
      for (LifecycleListener listener : lifecycleListeners) {
        listener.pause();
        listener.dispose();
      }
    }
    listener.pause();
    listener.dispose();
  }
Example #2
0
  private void setupLoop() {
    // setup modules
    try {
      graphics = new GwtGraphics(root, config);
    } catch (Throwable e) {
      root.clear();
      root.add(new Label("Sorry, your browser doesn't seem to support WebGL"));
      return;
    }
    lastWidth = graphics.getWidth();
    lastHeight = graphics.getHeight();
    Gdx.app = this;
    Gdx.audio = new GwtAudio();
    Gdx.graphics = graphics;
    Gdx.gl20 = graphics.getGL20();
    Gdx.gl = graphics.getGLCommon();
    Gdx.files = new GwtFiles(preloader);
    this.input = new GwtInput(graphics.canvas);
    Gdx.input = this.input;
    this.net = new GwtNet();
    Gdx.net = this.net;

    // tell listener about app creation
    try {
      listener.create();
      listener.resize(graphics.getWidth(), graphics.getHeight());
    } catch (Throwable t) {
      error("GwtApplication", "exception: " + t.getMessage(), t);
      t.printStackTrace();
      throw new RuntimeException(t);
    }

    // setup rendering timer
    new Timer() {
      @Override
      public void run() {
        try {
          graphics.update();
          if (Gdx.graphics.getWidth() != lastWidth || Gdx.graphics.getHeight() != lastHeight) {
            GwtApplication.this.listener.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
            lastWidth = graphics.getWidth();
            lastHeight = graphics.getHeight();
            Gdx.gl.glViewport(0, 0, lastWidth, lastHeight);
          }
          for (int i = 0; i < runnables.size; i++) {
            runnables.get(i).run();
          }
          runnables.clear();
          listener.render();
          input.justTouched = false;
        } catch (Throwable t) {
          error("GwtApplication", "exception: " + t.getMessage(), t);
          throw new RuntimeException(t);
        }
      }
    }.scheduleRepeating((int) ((1f / config.fps) * 1000));
  }
Example #3
0
 /** Called when the game loop has exited. */
 protected void end() {
   synchronized (lifecycleListeners) {
     for (LifecycleListener listener : lifecycleListeners) {
       listener.pause();
       listener.dispose();
     }
   }
   listener.pause();
   listener.dispose();
   glfwTerminate();
   if (forceExit) System.exit(-1);
 }
Example #4
0
 private void onVisibilityChange(boolean visible) {
   if (visible) {
     for (LifecycleListener listener : lifecycleListeners) {
       listener.resume();
     }
     listener.resume();
   } else {
     for (LifecycleListener listener : lifecycleListeners) {
       listener.pause();
     }
     listener.pause();
   }
 }
Example #5
0
  void setupLoop() {
    // setup modules
    try {
      graphics = new GwtGraphics(root, config);
    } catch (Throwable e) {
      root.clear();
      root.add(getNoWebGLSupportWidget());
      return;
    }
    lastWidth = graphics.getWidth();
    lastHeight = graphics.getHeight();
    Gdx.app = this;
    Gdx.audio = new GwtAudio();
    Gdx.graphics = graphics;
    Gdx.gl20 = graphics.getGL20();
    Gdx.gl = Gdx.gl20;
    Gdx.files = new GwtFiles(preloader);
    this.input = new GwtInput(graphics.canvas);
    Gdx.input = this.input;
    this.net = new GwtNet();
    Gdx.net = this.net;
    this.clipboard = new GwtClipboard();
    updateLogLabelSize();

    // tell listener about app creation
    try {
      listener.create();
      listener.resize(graphics.getWidth(), graphics.getHeight());
    } catch (Throwable t) {
      error("GwtApplication", "exception: " + t.getMessage(), t);
      t.printStackTrace();
      throw new RuntimeException(t);
    }

    AnimationScheduler.get()
        .requestAnimationFrame(
            new AnimationCallback() {
              @Override
              public void execute(double timestamp) {
                try {
                  mainLoop();
                } catch (Throwable t) {
                  error("GwtApplication", "exception: " + t.getMessage(), t);
                  throw new RuntimeException(t);
                }
                AnimationScheduler.get().requestAnimationFrame(this, graphics.canvas);
              }
            },
            graphics.canvas);
  }
Example #6
0
  public Lwjgl3Application(ApplicationListener listener, Lwjgl3ApplicationConfiguration config) {
    initializeGlfw();
    this.config = Lwjgl3ApplicationConfiguration.copy(config);
    if (this.config.title == null) this.config.title = listener.getClass().getSimpleName();
    Gdx.app = this;
    if (!config.disableAudio) {
      try {
        this.audio =
            Gdx.audio =
                new OpenALAudio(
                    config.audioDeviceSimultaneousSources,
                    config.audioDeviceBufferCount,
                    config.audioDeviceBufferSize);
      } catch (Throwable t) {
        log("Lwjgl3Application", "Couldn't initialize audio, disabling audio", t);
        this.audio = Gdx.audio = new MockAudio();
      }
    } else {
      this.audio = Gdx.audio = new MockAudio();
    }
    this.files = Gdx.files = new Lwjgl3Files();
    this.net = Gdx.net = new Lwjgl3Net();
    this.clipboard = new Lwjgl3Clipboard();

    Lwjgl3Window window = createWindow(config, listener, 0);
    windows.add(window);
    try {
      loop();
    } catch (Throwable t) {
      t.printStackTrace();
    } finally {
      cleanup();
    }
  }
Example #7
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 #8
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 #9
0
 void mainLoop() {
   graphics.update();
   if (Gdx.graphics.getWidth() != lastWidth || Gdx.graphics.getHeight() != lastHeight) {
     GwtApplication.this.listener.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
     lastWidth = graphics.getWidth();
     lastHeight = graphics.getHeight();
     Gdx.gl.glViewport(0, 0, lastWidth, lastHeight);
   }
   runnablesHelper.addAll(runnables);
   runnables.clear();
   for (int i = 0; i < runnablesHelper.size; i++) {
     runnablesHelper.get(i).run();
   }
   runnablesHelper.clear();
   graphics.frameId++;
   listener.render();
   input.reset();
 }
Example #10
0
  void mainLoop() {
    try {
      graphics.setupDisplay();
    } catch (LWJGLException e) {
      throw new GdxRuntimeException(e);
    }

    listener.create();
    listener.resize(graphics.getWidth(), graphics.getHeight());
    graphics.resize = false;

    int lastWidth = graphics.getWidth();
    int lastHeight = graphics.getHeight();

    graphics.lastTime = System.nanoTime();
    while (running) {
      Display.processMessages();
      if (Display.isCloseRequested()) {
        exit();
      }

      boolean shouldRender = false;
      synchronized (runnables) {
        executedRunnables.clear();
        executedRunnables.addAll(runnables);
        runnables.clear();

        for (int i = 0; i < executedRunnables.size(); i++) {
          shouldRender = true;
          try {
            executedRunnables.get(i).run();
          } catch (Throwable t) {
            t.printStackTrace();
          }
        }
      }
      input.update();
      shouldRender |= graphics.shouldRender();

      if (graphics.canvas != null) {
        int width = graphics.canvas.getWidth();
        int height = graphics.canvas.getHeight();
        if (lastWidth != width || lastHeight != height) {
          lastWidth = width;
          lastHeight = height;
          Gdx.gl.glViewport(0, 0, lastWidth, lastHeight);
          listener.resize(lastWidth, lastHeight);
          shouldRender = true;
        }
      } else {
        graphics.config.x = Display.getX();
        graphics.config.y = Display.getY();
        if (graphics.resize
            || Display.wasResized()
            || Display.getWidth() != graphics.config.width
            || Display.getHeight() != graphics.config.height) {
          graphics.resize = false;
          Gdx.gl.glViewport(0, 0, Display.getWidth(), Display.getHeight());
          graphics.config.width = Display.getWidth();
          graphics.config.height = Display.getHeight();
          if (listener != null) listener.resize(Display.getWidth(), Display.getHeight());
          graphics.requestRendering();
        }
      }

      input.processEvents();
      audio.update();
      if (shouldRender) {
        graphics.updateTime();
        listener.render();
        Display.update();
        if (graphics.vsync && graphics.config.useCPUSynch) {
          Display.sync(60);
        }
      } else {
        // Effectively sleeps for a little while so we don't spend all available
        // cpu power in an essentially empty loop.
        Display.sync(60);
      }
    }

    listener.pause();
    listener.dispose();
    Display.destroy();
    audio.dispose();
    if (graphics.config.forceExit) System.exit(-1);
  }
Example #11
0
 public JglfwApplication(ApplicationListener listener) {
   this(listener, listener.getClass().getSimpleName(), 640, 480, false);
 }
Example #12
0
 void render(long time) {
   graphics.frameStart(time);
   listener.render();
   glfwSwapBuffers(graphics.window);
 }