Skip to content

Instantly share code, notes, and snippets.

@thekeenant
Last active June 12, 2018 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thekeenant/05fe82efa368eac195da6e41e953fde5 to your computer and use it in GitHub Desktop.
Save thekeenant/05fe82efa368eac195da6e41e953fde5 to your computer and use it in GitHub Desktop.
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.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
public class InputLagGame extends ApplicationAdapter {
private boolean vsyncEnabled = false;
private boolean doSleep = false;
private Stage stage;
private Label misc;
private Label text;
private Label keyTyped;
private Label keyDown;
private Label keyUp;
@Override
public void create () {
float width = Gdx.graphics.getWidth();
float height = Gdx.graphics.getHeight();
stage = new Stage();
LabelStyle style1 = new LabelStyle();
style1.fontColor = Color.RED;
style1.font = new BitmapFont();
style1.font.getData().setScale(3);
LabelStyle style2 = new LabelStyle();
style2.fontColor = Color.WHITE;
style2.font = new BitmapFont();
style2.font.getData().setScale(3);
keyTyped = new Label("", style1);
keyTyped.setPosition(width / 2 - keyTyped.getWidth() / 2, height / 2 + 100);
keyDown = new Label("", style2);
keyDown.setPosition(width / 2 - keyDown.getWidth() / 2, height / 2);
keyUp = new Label("", style2);
keyUp.setPosition(width / 2 - keyDown.getWidth() / 2, height / 2 - 100);
text = new Label("", style2);
text.setPosition(0, 50);
misc = new Label("", style2);
misc.setPosition(0, 100);
stage.addActor(keyTyped);
stage.addActor(keyDown);
stage.addActor(keyUp);
stage.addActor(text);
stage.addActor(misc);
Gdx.input.setInputProcessor(new InputAdapter() {
@Override
public boolean keyTyped(char character) {
keyTyped.setText("typed: " + character + "");
return false;
}
@Override
public boolean keyDown(int keycode) {
keyDown.setText("down: " + Keys.toString(keycode));
return false;
}
@Override
public boolean keyUp(int keycode) {
keyUp.setText("up: " + Keys.toString(keycode));
String full = text.getText() + Keys.toString(keycode);
text.setText(full.substring(Math.max(0, full.length() - 20), full.length()));
return false;
}
});
}
@Override
public void render () {
if (doSleep) {
try {
Thread.sleep(1000 / 60);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT) && Gdx.input.isKeyJustPressed(Keys.V)) {
vsyncEnabled = !vsyncEnabled;
Gdx.graphics.setVSync(vsyncEnabled);
}
if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT) && Gdx.input.isKeyJustPressed(Keys.S)) {
doSleep = !doSleep;
}
misc.setText("Vsync: " + vsyncEnabled + ", Thread.sleep: " + doSleep);
stage.act();
stage.draw();
}
@Override
public void dispose () {
stage.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment