StatisticsGameState at the end of GameState queue

Currently to use StatisticsGameState you have to extend it. It would be cool to just add an instance of it at the end of the GameState queue instead.

The problem is that statistics text is set in update() method, which is called before render() so renderer statistics is empty. Also currently the statistical information lags one frame behind.



I've mushed StatisticsGameState with TextGameState together to make StatisicsGameState that works at the end of the state queue. There is really no new code, just recombination of what was there. Hope it's useful.



import com.jme.image.Texture;
import com.jme.scene.Node;
import com.jme.scene.Text;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.Timer;
import com.jmex.game.StandardGame;
import com.jmex.game.state.BasicGameState;

public class StatisticsGameState extends BasicGameState {
   private static final String FONT_LOCATION = "/com/jme/app/defaultfont.tga";
   
   private Text textObject;
   private Node textNode;

   private Timer timer;

   public StatisticsGameState() {
      super("StatisticsGameState");
      
      AlphaState as = DisplaySystem.getDisplaySystem().getRenderer().createAlphaState();
      as.setBlendEnabled(true);
      as.setSrcFunction(AlphaState.SB_SRC_ALPHA);
      as.setDstFunction(AlphaState.DB_ONE);
      as.setTestEnabled(true);
      as.setTestFunction(AlphaState.TF_GREATER);
      as.setEnabled(true);
      
      TextureState font = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
      font.setTexture(TextureManager.loadTexture(StandardGame.class.getResource(FONT_LOCATION),
                  Texture.MM_LINEAR, Texture.FM_LINEAR));
      font.setEnabled(true);
      
      textObject = new Text("Text", "");
      textObject.setTextureCombineMode(TextureState.REPLACE);
      textNode = new Node("TextNode");
      textNode.attachChild(textObject);
      textNode.setRenderState(font);
      textNode.setRenderState(as);
      textNode.updateGeometricState(0.0f, true);
      textNode.updateRenderState();
      
      timer = Timer.getTimer();
      DisplaySystem.getDisplaySystem().getRenderer().enableStatistics(true);
      textObject.print("Pending...");
   }
   
   public void update(float tpf) {
   }
   
   public void render(float tpf) {
      String text = "FPS: " + Math.round(timer.getFrameRate()) + " - " +
         DisplaySystem.getDisplaySystem().getRenderer().getStatistics();
      
      textObject.print(text);
      DisplaySystem.getDisplaySystem().getRenderer().draw(textNode);
   }
   
   public void cleanup() {
   }
}


…back when I wrote StatisticsGameState it worked stand-alone…I haven't delved into any jME code for a while (too busy with other projects ATM), but I'm curious what has changed?

The StandartGame.render() method erases previous statistics. My guess is the render method wasn't doing that when StatisticsGameState was first developed.