0
\$\begingroup\$

I want to poll only 4 keys i.e. Keys.LEFT, Keys.RIGHT, Keys.Up and Keys.Down in libGDX strictly. That means, I want to call render method, only when above 4 keys are pressed.

I followed below link and implemented InputProcessor (see code below), extending InputAdapter for keyDown only.

https://github.com/libgdx/libgdx/wiki/Event-handling

Gdx.input.setInputProcessor(new InputAdapter () {
            @Override
               public boolean keyDown (int keycode) {
                if(keycode==Keys.LEFT)
                {
                    System.out.println("left");
                    }
                if(keycode==Keys.RIGHT)
                {
                    System.out.println("right");
                }
                if(keycode==Keys.DOWN)
                {
                    System.out.println("DOWN");
                }
                if(keycode==Keys.UP)
                {
                    System.out.println("UP");
                }
                return true;
               }    
        });

But i am not getting the result as expected.

After running application on desktop, whenever I move mouse pointer over game screen or pressing other keys , its FPS is changing . That means render method is being called. Why?

Here is my full code for testing. See the output at bottom.

package com.GDXTests.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.FPSLogger;

public class FPSTest extends ApplicationAdapter {
    private Texture tex;
    private SpriteBatch batch;
    private OrthographicCamera camera;
    public FPSLogger fpslog;

    @Override
    public void create ()
    {
    // Loading Texture
         tex=new Texture(Gdx.files.external("desktop/bit0.jpg"));
         camera = new OrthographicCamera();
         camera.setToOrtho(false, 256, 256);
         batch = new SpriteBatch();
          //initializing logger
         fpslog=new FPSLogger();
          //disabling the continuous rendering
         Gdx.graphics.setContinuousRendering(false);
         Gdx.graphics.requestRendering();
    }
    @Override
    public void render () {

        //logging fps
        fpslog.log();
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
         // tell the camera to update its matrices.
          camera.update();
          // tell the SpriteBatch to render in the
          // coordinate system specified by the camera.
          batch.setProjectionMatrix(camera.combined);

        batch.begin();
        batch.draw(tex,2,3);
        batch.end();

        Gdx.input.setInputProcessor(new InputAdapter () {
            @Override
               public boolean keyDown (int keycode) {
                if(keycode==Keys.LEFT)
                {
                    System.out.println("left");
                }
                if(keycode==Keys.RIGHT)
                {
                    System.out.println("right");
                }
                if(keycode==Keys.DOWN)
                {
                    System.out.println("DOWN");
                }
                if(keycode==Keys.UP)
                {
                    System.out.println("UP");
                }
                return true;
               }        
        });
    }
     @Override
       public void dispose() {
          // dispose of all the native resources
         tex.dispose();   
         batch.dispose();
       }
       @Override
       public void resize(int width, int height) {
       }
       @Override
       public void pause() {
       }
       @Override
       public void resume() {
       }
}

Here is the output of logger:

right
FPSLogger: fps: 2
left
UP
FPSLogger: fps: 5
DOWN
left
FPSLogger: fps: 3
right
UP
DOWN
FPSLogger: fps: 6       // move mouse pointer over screen, not pressing any keys
FPSLogger: fps: 2       // move mouse pointer over screen, not pressing any keys
FPSLogger: fps: 61      // move mouse pointer over screen, not pressing any keys
FPSLogger: fps: 24      // move mouse pointer over screen, not pressing any keys
FPSLogger: fps: 52      // move mouse pointer over screen, not pressing any keys
FPSLogger: fps: 14      // move mouse pointer over screen, not pressing any keys
FPSLogger: fps: 21      // move mouse pointer over screen, not pressing any keys
UP
FPSLogger: fps: 15
DOWN
FPSLogger: fps: 2
left
right
FPSLogger: fps: 4
right
FPSLogger: fps: 3
right
FPSLogger: fps: 3

I want something like this

right
FPSLogger: fps: 1
left
FPSLogger: fps: 1
DOWN
FPSLogger: fps: 1
UP
FPSLogger: fps: 1

Thanks in Advance!

\$\endgroup\$
2
\$\begingroup\$

as written in their wiki:

If continuous rendering is set to false, the render() method will be called only when the following things happen.

An input event is triggered Gdx.graphics.requestRendering() is called Gdx.app.postRunnable() is called

in LibGDX every input event (mouse move, button pressed), the render function will be called

so, mouse movement is an Input Event, whether you do something with it or not.

same for the buttons pressed, no matter if you call 'requestRendering' in the event or not, the screen will render.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thanks for the answer. I also figured it out. Apparently, i was looking to find anyway through which i can define above 4 inputs as only Input event by disabling other form of input event at all. \$\endgroup\$ – kingAm Dec 23 '14 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.