/*
   * Update game state
   */
  @Override
  public void update() throws SDLException {
    if (frame >= KEYACCEPTFRAME) {
      // Up
      if (NullpoMinoSDL.keyPressedState[SDLKey.SDLK_UP]) {
        ResourceHolderSDL.soundManager.play("cursor");
        keynum--;
        if (keynum < 4) keynum = 15;
        frame = 0;
      }
      // Down
      else if (NullpoMinoSDL.keyPressedState[SDLKey.SDLK_DOWN]) {
        ResourceHolderSDL.soundManager.play("cursor");
        keynum++;
        if (keynum > 15) keynum = 4;
        frame = 0;
      }
      // Delete
      else if (NullpoMinoSDL.keyPressedState[SDLKey.SDLK_DELETE]) {
        ResourceHolderSDL.soundManager.play("change");
        buttonmap[keynum] = -1;
        frame = 0;
      }
      // Backspace
      else if (NullpoMinoSDL.keyPressedState[SDLKey.SDLK_BACKSPACE]) {
        NullpoMinoSDL.enterState(NullpoMinoSDL.STATE_CONFIG_JOYSTICK_MAIN);
        return;
      }
      // Enter/Return
      else if (NullpoMinoSDL.keyPressedState[SDLKey.SDLK_RETURN]) {
        ResourceHolderSDL.soundManager.play("decide");

        for (int i = 0; i < GameKeySDL.MAX_BUTTON; i++) {
          GameKeySDL.gamekey[player].buttonmap[i] = buttonmap[i];
        }
        GameKeySDL.gamekey[player].saveConfig(NullpoMinoSDL.propConfig);
        NullpoMinoSDL.saveConfig();

        NullpoMinoSDL.enterState(NullpoMinoSDL.STATE_CONFIG_JOYSTICK_MAIN);
        return;
      }
      // Joystick input
      else if (previousJoyPressedState != null) {
        int key =
            getPressedKeyNumber(previousJoyPressedState, NullpoMinoSDL.joyPressedState[joyNumber]);

        if (key != -1) {
          ResourceHolderSDL.soundManager.play("change");
          buttonmap[keynum] = key;
          frame = 0;
        }
      }
    }

    if (previousJoyPressedState != null) {
      for (int i = 0; i < NullpoMinoSDL.joyPressedState.length; i++) {
        previousJoyPressedState[i] = NullpoMinoSDL.joyPressedState[joyNumber][i];
      }
    }
    frame++;
  }
  /*
   * Update game state
   */
  @Override
  public void update() throws SDLException {
    // Update key input states
    for (int i = 0; i < 2; i++) {
      int joynum = NullpoMinoSDL.joyUseNumber[i];

      boolean ingame =
          (gameManager != null)
              && (gameManager.engine.length > i)
              && (gameManager.engine[i] != null)
              && (gameManager.engine[i].isInGame)
              && (!pause || enableframestep);

      if ((NullpoMinoSDL.joystickMax > 0)
          && (joynum >= 0)
          && (joynum < NullpoMinoSDL.joystickMax)) {
        GameKeySDL.gamekey[i].update(
            NullpoMinoSDL.keyPressedState,
            NullpoMinoSDL.joyPressedState[joynum],
            NullpoMinoSDL.joyAxisX[joynum],
            NullpoMinoSDL.joyAxisY[joynum],
            NullpoMinoSDL.joyHatState[joynum],
            ingame);
      } else {
        GameKeySDL.gamekey[i].update(NullpoMinoSDL.keyPressedState, ingame);
      }
    }

    // Title bar update
    if ((gameManager != null)
        && (gameManager.engine != null)
        && (gameManager.engine.length > 0)
        && (gameManager.engine[0] != null)) {
      boolean nowInGame = gameManager.engine[0].isInGame;
      if (prevInGameFlag != nowInGame) {
        prevInGameFlag = nowInGame;
        updateTitleBarCaption();
      }
    }

    // Pause
    if (GameKeySDL.gamekey[0].isPushKey(GameKeySDL.BUTTON_PAUSE)
        || GameKeySDL.gamekey[1].isPushKey(GameKeySDL.BUTTON_PAUSE)) {
      if (!pause) {
        if ((gameManager != null) && (gameManager.isGameActive()) && (pauseFrame <= 0)) {
          ResourceHolderSDL.soundManager.play("pause");
          pause = true;
          cursor = 0;
          if (!enableframestep) pauseFrame = 5;
          if (!enableframestep) ResourceHolderSDL.bgmPause();
        }
      } else {
        ResourceHolderSDL.soundManager.play("pause");
        pause = false;
        pauseFrame = 0;
        if (!enableframestep) ResourceHolderSDL.bgmResume();
      }
      updateTitleBarCaption();
    }
    // Pause menu
    else if (pause && !enableframestep && !pauseMessageHide) {
      // Cursor movement
      if (GameKeySDL.gamekey[0].isMenuRepeatKey(GameKeySDL.BUTTON_UP)) {
        cursor--;

        if (cursor < 0) {
          if (gameManager.replayMode && !gameManager.replayRerecord) cursor = 3;
          else cursor = 2;
        }

        ResourceHolderSDL.soundManager.play("cursor");
      }
      if (GameKeySDL.gamekey[0].isMenuRepeatKey(GameKeySDL.BUTTON_DOWN)) {
        cursor++;
        if (cursor > 3) cursor = 0;

        if ((!gameManager.replayMode || gameManager.replayRerecord) && (cursor > 2)) cursor = 0;

        ResourceHolderSDL.soundManager.play("cursor");
      }

      // Confirm
      if (GameKeySDL.gamekey[0].isPushKey(GameKeySDL.BUTTON_A)) {
        ResourceHolderSDL.soundManager.play("decide");
        if (cursor == 0) {
          // 再開
          pause = false;
          pauseFrame = 0;
          GameKeySDL.gamekey[0].clear();
          ResourceHolderSDL.bgmResume();
        } else if (cursor == 1) {
          // リトライ
          ResourceHolderSDL.bgmStop();
          pause = false;
          gameManager.reset();
        } else if (cursor == 2) {
          // 終了
          ResourceHolderSDL.bgmStop();
          NullpoMinoSDL.enterState(NullpoMinoSDL.STATE_TITLE);
          return;
        } else if (cursor == 3) {
          // Replay re-record
          gameManager.replayRerecord = true;
          ResourceHolderSDL.soundManager.play("tspin1");
          cursor = 0;
        }
        updateTitleBarCaption();
      }
      // Unpause by cancel key
      else if (GameKeySDL.gamekey[0].isPushKey(GameKeySDL.BUTTON_B) && (pauseFrame <= 0)) {
        ResourceHolderSDL.soundManager.play("pause");
        pause = false;
        pauseFrame = 5;
        GameKeySDL.gamekey[0].clear();
        ResourceHolderSDL.bgmResume();
        updateTitleBarCaption();
      }
    }
    if (pauseFrame > 0) pauseFrame--;

    // Hide pause menu
    pauseMessageHide = GameKeySDL.gamekey[0].isPressKey(GameKeySDL.BUTTON_C);

    if (gameManager.replayMode && !gameManager.replayRerecord && gameManager.engine[0].gameActive) {
      // Replay speed
      if (GameKeySDL.gamekey[0].isMenuRepeatKey(GameKeySDL.BUTTON_LEFT)) {
        if (fastforward > 0) {
          fastforward--;
        }
      }
      if (GameKeySDL.gamekey[0].isMenuRepeatKey(GameKeySDL.BUTTON_RIGHT)) {
        if (fastforward < 98) {
          fastforward++;
        }
      }

      // Replay re-record
      if (GameKeySDL.gamekey[0].isPushKey(GameKeySDL.BUTTON_D)) {
        gameManager.replayRerecord = true;
        ResourceHolderSDL.soundManager.play("tspin1");
        cursor = 0;
      }
      // Replay re-record
      if (GameKeySDL.gamekey[0].isPushKey(GameKeySDL.BUTTON_E)) {
        gameManager.replayShowInvisible = !gameManager.replayShowInvisible;
        ResourceHolderSDL.soundManager.play("tspin1");
        cursor = 0;
      }
    } else {
      fastforward = 0;
    }

    if (gameManager != null) {
      // BGM
      if (ResourceHolderSDL.bgmPlaying != gameManager.bgmStatus.bgm) {
        ResourceHolderSDL.bgmStart(gameManager.bgmStatus.bgm);
      }
      if (ResourceHolderSDL.bgmIsPlaying()) {
        int basevolume = NullpoMinoSDL.propConfig.getProperty("option.bgmvolume", 128);
        float basevolume2 = (float) basevolume / 128;
        int newvolume = (int) (128 * (gameManager.bgmStatus.volume * basevolume2));
        if (newvolume < 0) newvolume = 0;
        if (newvolume > 128) newvolume = 128;
        SDLMixer.volumeMusic(newvolume);
        if (newvolume <= 0) ResourceHolderSDL.bgmStop();
      }
    }

    // Execute game loops
    if (!pause
        || (GameKeySDL.gamekey[0].isPushKey(GameKeySDL.BUTTON_FRAMESTEP) && enableframestep)) {
      if (gameManager != null) {
        for (int i = 0; i < Math.min(gameManager.getPlayers(), 2); i++) {
          if (!gameManager.replayMode
              || gameManager.replayRerecord
              || !gameManager.engine[i].gameActive) {
            GameKeySDL.gamekey[i].inputStatusUpdate(gameManager.engine[i].ctrl);
          }
        }

        for (int i = 0; i <= fastforward; i++) gameManager.updateAll();
      }
    }

    if (gameManager != null) {
      // Retry button
      if (GameKeySDL.gamekey[0].isPushKey(GameKeySDL.BUTTON_RETRY)
          || GameKeySDL.gamekey[1].isPushKey(GameKeySDL.BUTTON_RETRY)) {
        ResourceHolderSDL.bgmStop();
        pause = false;
        gameManager.reset();
      }

      // Return to title
      if (gameManager.getQuitFlag()
          || GameKeySDL.gamekey[0].isPushKey(GameKeySDL.BUTTON_GIVEUP)
          || GameKeySDL.gamekey[1].isPushKey(GameKeySDL.BUTTON_GIVEUP)) {
        ResourceHolderSDL.bgmStop();
        NullpoMinoSDL.enterState(NullpoMinoSDL.STATE_TITLE);
        return;
      }
    }
  }