Example #1
0
 private void switchState(GameState newState) {
   if (currentState != null) {
     currentState.dispose();
   }
   currentState = newState;
   LoggingContext.setGameState(newState);
   newState.init(this);
   for (StateChangeSubscriber subscriber : stateChangeSubscribers) {
     subscriber.onStateChange();
   }
   // drain input queues
   InputSystem inputSystem = rootContext.get(InputSystem.class);
   inputSystem.getMouseDevice().getInputQueue();
   inputSystem.getKeyboard().getInputQueue();
 }
Example #2
0
  private void cleanup() {
    logger.info("Shutting down Terasology...");
    changeStatus(StandardGameStatus.SHUTTING_DOWN);

    if (currentState != null) {
      currentState.dispose();
      currentState = null;
    }

    Iterator<EngineSubsystem> preshutdownIter = allSubsystems.descendingIterator();
    while (preshutdownIter.hasNext()) {
      EngineSubsystem subsystem = preshutdownIter.next();
      try {
        subsystem.preShutdown();
      } catch (RuntimeException e) {
        logger.error("Error preparing to shutdown {} subsystem", subsystem.getName(), e);
      }
    }

    Iterator<EngineSubsystem> shutdownIter = allSubsystems.descendingIterator();
    while (shutdownIter.hasNext()) {
      EngineSubsystem subsystem = shutdownIter.next();
      try {
        subsystem.shutdown();
      } catch (RuntimeException e) {
        logger.error("Error shutting down {} subsystem", subsystem.getName(), e);
      }
    }
  }
Example #3
0
  /**
   * The main loop runs until the EngineState is set back to INITIALIZED by shutdown() or until the
   * OS requests the application's window to be closed. Engine cleanup and disposal occur
   * afterwards.
   */
  private void mainLoop() {
    PerformanceMonitor.startActivity("Other");
    // MAIN GAME LOOP
    while (!shutdownRequested) {
      assetTypeManager.reloadChangedOnDisk();

      processPendingState();

      if (currentState == null) {
        shutdown();
        break;
      }

      Iterator<Float> updateCycles = timeSubsystem.getEngineTime().tick();

      for (EngineSubsystem subsystem : allSubsystems) {
        try (Activity ignored =
            PerformanceMonitor.startActivity(subsystem.getName() + " PreUpdate")) {
          subsystem.preUpdate(currentState, timeSubsystem.getEngineTime().getRealDelta());
        }
      }

      while (updateCycles.hasNext()) {
        float updateDelta = updateCycles.next(); // gameTime gets updated here!
        try (Activity ignored = PerformanceMonitor.startActivity("Main Update")) {
          currentState.update(updateDelta);
        }
      }

      // Waiting processes are set by modules via GameThread.a/synch() methods.
      GameThread.processWaitingProcesses();

      for (EngineSubsystem subsystem : getSubsystems()) {
        try (Activity ignored =
            PerformanceMonitor.startActivity(subsystem.getName() + " Subsystem postUpdate")) {
          subsystem.postUpdate(currentState, timeSubsystem.getEngineTime().getRealDelta());
        }
      }
      assetTypeManager.disposedUnusedAssets();

      PerformanceMonitor.rollCycle();
      PerformanceMonitor.startActivity("Other");
    }
    PerformanceMonitor.endActivity();
  }
Example #4
0
  private void cleanup() {
    logger.info("Shutting down Terasology...");

    try {
      Iterator<EngineSubsystem> iter = subsystems.descendingIterator();
      while (iter.hasNext()) {
        EngineSubsystem subsystem = iter.next();
        subsystem.shutdown(config);
      }

      config.save();
      if (currentState != null) {
        currentState.dispose();
        currentState = null;
      }
    } finally {
      // Even if a graceful shutdown of the subsystems fails,
      // the thread pool has to be shut down
      stopThreads();
    }
  }
Example #5
0
 @Override
 public boolean isHibernationAllowed() {
   return hibernationAllowed && currentState.isHibernationAllowed();
 }
Example #6
0
  /**
   * The main loop runs until the EngineState is set back to INITIALIZED by shutdown() or until the
   * OS requests the application's window to be closed. Engine cleanup and disposal occur
   * afterwards.
   */
  private void mainLoop() {
    NetworkSystem networkSystem = CoreRegistry.get(NetworkSystem.class);

    DisplayDevice display = CoreRegistry.get(DisplayDevice.class);

    PerformanceMonitor.startActivity("Other");
    // MAIN GAME LOOP
    while (engineState == EngineState.RUNNING && !display.isCloseRequested()) {

      long totalDelta;
      float updateDelta;
      float subsystemsDelta;

      // Only process rendering and updating once a second
      if (!display.isActive() && isHibernationAllowed()) {
        time.setPaused(true);
        Iterator<Float> updateCycles = time.tick();
        while (updateCycles.hasNext()) {
          updateCycles.next();
        }
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
          logger.warn("Display inactivity sleep interrupted", e);
        }

        display.processMessages();
        time.setPaused(false);
        continue;
      }

      processPendingState();

      if (currentState == null) {
        shutdown();
        break;
      }

      Iterator<Float> updateCycles = time.tick();

      try (Activity ignored = PerformanceMonitor.startActivity("Network Update")) {
        networkSystem.update();
      }

      totalDelta = 0;
      while (updateCycles.hasNext()) {
        updateDelta = updateCycles.next(); // gameTime gets updated here!
        totalDelta += time.getDeltaInMs();
        try (Activity ignored = PerformanceMonitor.startActivity("Main Update")) {
          currentState.update(updateDelta);
        }
      }

      subsystemsDelta = totalDelta / 1000f;

      for (EngineSubsystem subsystem : getSubsystems()) {
        try (Activity ignored =
            PerformanceMonitor.startActivity(subsystem.getClass().getSimpleName())) {
          subsystem.preUpdate(currentState, subsystemsDelta);
        }
      }

      // Waiting processes are set by modules via GameThread.a/synch() methods.
      GameThread.processWaitingProcesses();

      for (EngineSubsystem subsystem : getSubsystems()) {
        try (Activity ignored =
            PerformanceMonitor.startActivity(subsystem.getClass().getSimpleName())) {
          subsystem.postUpdate(currentState, subsystemsDelta);
        }
      }

      PerformanceMonitor.rollCycle();
      PerformanceMonitor.startActivity("Other");
    }
    PerformanceMonitor.endActivity();

    // This becomes important only if display.isCloseRequested() is true.
    // In all other circumstances the EngineState is already set to
    // INITIALIZED by the time the flow gets here.
    engineState = EngineState.INITIALIZED;
  }