Print Timer on Screen

Hi everyone,



I am trying to print the Timer.getTimer().getTimeInSeconds() value on my screen of my game!



I tried to use the Font3D example for my needs! I added the Timer.getTimer().getTimeInSeconds() into createText!




Font3D font = new Font3D(new Font("Arial", Font.PLAIN, 24), 0.001f, true, true, true);
            Text3D text = font.createText("" + Timer.getTimer().getTimeInSeconds(), 50.0f, 0);
            text.setLocalScale(new Vector3f(5.0f, 5.0f, 0.01f));
            rootNode.attachChild(text);



After starting my game I figured that this is not the right way to do it!

Does anybody know a working way to do it?

Thanks for any help ;)
  1. dont do it with Text3D, you should only use that for static text, if you create a 3d text every update cycle you wont have any FPS  :wink:
  2. take a look at DebugGameState if you use StandardGame, or look at any of the tests in the jme 2.0 source, in those you can toggle a statistik view with many values (incl. FPS)
  3. if you still want to do it on your own:


   public void init(StandardGame game) {
      // Then our font Text object.
      // This is what will actually have the text at the bottom.
      fps = Text.createDefaultTextLabel("FPS label");
      fps.setCullHint(CullHint.Never);
      fps.setTextureCombineMode(TextureCombineMode.Replace);

      // Finally, a stand alone node (not attached to root on purpose)
      fpsNode = new Node("FPS node");
      fpsNode.setRenderState(fps.getRenderState(StateType.Blend));
      fpsNode.setRenderState(fps.getRenderState(StateType.Texture));
      fpsNode.attachChild(fps);
      fpsNode.setCullHint(CullHint.Never);
      fpsNode.updateGeometricState(0.0f, true);
      fpsNode.updateRenderState();
   } // init

   public void update(float tpf) {
      // print the FPS information
      StringBuffer updateBuffer = new StringBuffer();
      updateBuffer.append("FPS: ").append(Math.round(1 / tpf));
      /* Send the fps to our fps bar at the bottom. */
      fps.print(updateBuffer);
   } // update

   public void render(float tpf) {
      game.getDisplay().getRenderer().draw(fpsNode);
   } // render

I am using an InGameState  which extends PysicsGameState!

Which means you are using StandardGame. Create and add a StatisticsGameState.

okay thanks :wink:

One more thing:



I looked at StatisticsGameState and tried to integrate the function to my GameState!



So I have a initStatistics() in my initGame-function:


private void initStatistics(){
      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...");
   }




and then I changed my render-method:

 @Override
    public void render(float tpf) {
       if (Game.getInstance().isPaused()) {
          tpf = 0;
       }
       String text = "FPS: " + Math.round(timer.getFrameRate()) + " - " +
      DisplaySystem.getDisplaySystem().getRenderer().getStatistics();
   
       textObject.print(text);
       DisplaySystem.getDisplaySystem().getRenderer().draw(textNode);
        super.render(tpf);
    }




When I start my game no error appears! But I can't see anything!?!  Any ideas?

Did you turn off lighting for the text object? (Spatial.LightCombineMode property)

Added:


textNode.setRenderQueueMode(Renderer.QUEUE_INHERIT);
      textNode.setLightCombineMode(LightState.OFF);




It did not solve the problem ;-(

use Spatial.setCullHint(CullHint.Never) and ZBufferState.setFunction(TestFunction.Always)


also make sure to render the textNode in the ortho queue node.setRenderQueueMode(Renderer.QUEUE_ORTHO);

sorry for being unclear before: you should not integrate the functionality from StatisticsGameState into your GameState. somewhere in your code you should have something like that:


GameStateManager.getInstance().attachChild(myCustomGameState);



right below it, you can create your StatisticsGameState and add it to the GameStateManager, just like above. Else, you would just screw up the whole point of GameStates  ;)

okay great!



Working really good now- even as an own StatisticsGameState :wink: