Ejemplo n.º 1
0
  /**
   * Update and render the game
   *
   * @param delta The change in time since last update and render
   * @throws SlickException Indicates an internal fault to the game.
   */
  protected void updateAndRender(int delta) throws SlickException {
    storedDelta += delta;
    input.poll(width, height);

    SoundStore.get().poll(delta);
    if (storedDelta >= minimumLogicInterval) {
      try {
        if (maximumLogicInterval != 0) {
          long cycles = storedDelta / maximumLogicInterval;
          for (int i = 0; i < cycles; i++) {
            game.update(this, (int) maximumLogicInterval);
          }
          game.update(this, (int) (delta % maximumLogicInterval));
        } else {
          game.update(this, (int) storedDelta);
        }

        storedDelta = 0;
      } catch (Throwable e) {
        Log.error(e);
        throw new SlickException("Game.update() failure - check the game code.");
      }
    }

    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glLoadIdentity();

    graphics.resetFont();
    graphics.resetLineWidth();
    graphics.setAntiAlias(false);
    try {
      game.render(this, graphics);
    } catch (Throwable e) {
      Log.error(e);
      throw new SlickException("Game.render() failure - check the game code.");
    }
    graphics.resetTransform();

    if (showFPS) {
      defaultFont.drawString(10, 10, "FPS: " + recordedFPS);
    }

    if (targetFPS != -1) {
      Display.sync(targetFPS);
    }
  }
  /**
   * Update and render the game
   *
   * @param delta The change in time since last update and render
   * @throws SlickException Indicates an internal fault to the game.
   */
  protected void updateAndRender(int delta) throws SlickException {
    if (smoothDeltas) {
      if (getFPS() != 0) {
        delta = 1000 / getFPS();
      }
    }

    input.poll(width, height);

    Music.poll(delta);
    if (!paused) {
      storedDelta += delta;

      if (storedDelta >= minimumLogicInterval) {
        try {
          if (maximumLogicInterval != 0) {
            long cycles = storedDelta / maximumLogicInterval;
            for (int i = 0; i < cycles; i++) {
              game.update(this, (int) maximumLogicInterval);
            }

            int remainder = (int) (storedDelta % maximumLogicInterval);
            if (remainder > minimumLogicInterval) {
              game.update(this, (int) (remainder % maximumLogicInterval));
              storedDelta = 0;
            } else {
              storedDelta = remainder;
            }
          } else {
            game.update(this, (int) storedDelta);
            storedDelta = 0;
          }

        } catch (Throwable e) {
          //					Log.error(e);
          throw new SlickException("Game.update() failure.", e);
        }
      }
    } else {
      game.update(this, 0);
    }

    if (hasFocus() || getAlwaysRender()) {
      if (clearEachFrame) {
        GL.glClear(SGL.GL_COLOR_BUFFER_BIT | SGL.GL_DEPTH_BUFFER_BIT);
      }

      GL.glLoadIdentity();

      graphics.resetTransform();
      graphics.resetFont();
      graphics.resetLineWidth();
      graphics.setAntiAlias(false);
      try {
        game.render(this, graphics);
      } catch (Throwable e) {
        //				Log.error(e);
        throw new SlickException("Game.render() failure.", e);
      }
      graphics.resetTransform();

      if (showFPS) {
        defaultFont.drawString(10, 10, "FPS: " + recordedFPS);
      }

      GL.flush();
    }

    if (targetFPS != -1) {
      Display.sync(targetFPS);
    }
  }