package com.powersource.gdxtest;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Gdx2DPixmap;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class GdxTest implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
public float w = 0;
public float h = 0;
public float xpos;
public float ypos;
public float dirx = 0;
public float diry = 0;
public float diag = 0;
public float xprevpos = 0;
public float yprevpos = 0;
public float mouseSetX = 0;
public float mouseSetY = 0;
@Override
public void create() {
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h / w);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
sprite = new Sprite(region);
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
float xpos = -sprite.getWidth() / 2;
float ypos = -sprite.getHeight() / 2;
sprite.setPosition(xpos, ypos);
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
dirx += 1;
}
if (Gdx.input.isKeyPressed(Keys.LEFT)) {
dirx -= 1;
}
if (Gdx.input.isKeyPressed(Keys.UP)) {
diry += 1;
}
if (Gdx.input.isKeyPressed(Keys.DOWN)) {
diry -= 1;
}
if ((dirx == 1 || dirx == -1) && (diry == 1 || diry == -1)) {
diag = (float) Math.sqrt(0.5);
} else {
diag = 1;
}
xpos += diag * dirx * 1f / Gdx.graphics.getWidth();
ypos += diag * diry * 1f / Gdx.graphics.getHeight();
dirx = diry = 0;
sprite.setPosition(xpos, ypos);
if (Gdx.input.isButtonPressed(Buttons.LEFT)) {
mouseSetX = (Gdx.input.getX() - (w / 2f)) / w;
mouseSetY = ((h - Gdx.input.getY()) - (h / 2f)) / h;
p("((h - Gdx.input.getY()) - (h / 2f)) / h");
p("h: " + h);
p("getY: " + Gdx.input.getY());
p("width: " + Gdx.graphics.getWidth());
p("height: " + Gdx.graphics.getHeight());
p("mouseSetY * h: " + mouseSetY*h);
sprite.setPosition(mouseSetX, mouseSetY);
}
sprite.draw(batch);
batch.end();
}
public void p(String print) {
System.out.println(print);
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}