Exemplo n.º 1
0
  @Override
  public void run() {
    init();

    int fps = 0;
    long frameCounter = 0;

    long lastTime = Time.getTime();
    float unproccessedTime = 0;

    while (!Window.isCloseRequested()) {
      boolean render = false;

      long startTime = Time.getTime();
      long passed = startTime - lastTime;
      lastTime = startTime;

      unproccessedTime += passed / (float) Time.SECOND;
      frameCounter += passed;

      while (unproccessedTime > frameTime) {
        render = true;

        unproccessedTime -= frameTime;

        Time.setDelta((float) frameTime);

        game.fixedUpdate();
        renderingEngine.fixedUpdate();

        if (frameCounter >= Time.SECOND) {
          System.out.println(fps);
          fps = 0;
          frameCounter = 0;
        }

        update();
      }

      if (render) {
        renderingEngine.render(game.getRootObject());
        Window.render();
        fps++;
      } else {
        try {
          Thread.sleep(1);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
Exemplo n.º 2
0
  public void inputUpdate() {
    switch (type) {
      case Left:
        if (KeyBoardInput.keys[GLFW_KEY_W]) pos.setPosY(pos.getPosY() + speed * Time.getDelta());
        else if (KeyBoardInput.keys[GLFW_KEY_S])
          pos.setPosY(pos.getPosY() - speed * Time.getDelta());
        if (KeyBoardInput.keys[GLFW_KEY_D]) pos.setPosX(pos.getPosX() + speed * Time.getDelta());
        else if (KeyBoardInput.keys[GLFW_KEY_A])
          pos.setPosX(
              pos.getPosX()
                  - speed * Time.getDelta()); // Main.printXYCoords("Left:", pos.getPosX(),
        // pos.getPosY());

        break;
      case Right:
        if (KeyBoardInput.keys[GLFW_KEY_UP]) pos.setPosY(pos.getPosY() + speed * Time.getDelta());
        else if (KeyBoardInput.keys[GLFW_KEY_DOWN])
          pos.setPosY(pos.getPosY() - speed * Time.getDelta());
        if (KeyBoardInput.keys[GLFW_KEY_RIGHT])
          pos.setPosX(pos.getPosX() + speed * Time.getDelta());
        else if (KeyBoardInput.keys[GLFW_KEY_LEFT])
          pos.setPosX(pos.getPosX() - speed * Time.getDelta());
        break;
      default:
        break;
    }
  }